Skip to content

Commit 9552bca

Browse files
authored
Merge pull request #219 from Esri/read-geopackage
Implement Read GeoPackage sample
2 parents 9562c23 + 9c40ed0 commit 9552bca

File tree

3 files changed

+230
-0
lines changed

3 files changed

+230
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<h1>Read GeoPackage</h1>
2+
3+
<p>Demonstrates how to read rasters and feature tables from geoPackages to show them as layers in a map.</p>
4+
5+
<p><img src="ReadGeoPackage.png" /></p>
6+
7+
<h2>How to use the sample</h2>
8+
9+
<p>The layers in the geoPackage, which have not been added to the map are shown in the bottom list. Click an item to
10+
show it as a layer in the map. Layers in the map are listed in the top list. Click layers from the top list to
11+
remove them from the map.</p>
12+
13+
<h2>How it works</h2>
14+
15+
<p>To read layers from a geoPackage and show them in a map:</p>
16+
17+
<ol>
18+
<li>Create a <code>GeoPackage</code> with the path to the local geoPackage file.</li>
19+
<li>Load the <code>GeoPackage</code> with <code>GeoPackage.loadAsync</code>.</li>
20+
<li>Create raster layers for each of these with <code>new RasterLayer(geoPackageRaster)</code>.</li>
21+
<li>Add each layer to the map as an operational layer with <code>map.getOperationalLayers().add(layer)</code>.</li>
22+
<li>When it's done loading, get the <code>GeoPackageFeatureTable</code>s inside with <code>geoPackage
23+
.getGeoPackageFeatureTables()</code>.</li>
24+
<li>For each feature table, create a feature layer with <code>new FeatureLayer(featureTable)</code>.</li>
25+
<li>You can also get the <code>GeoPackageRaster</code>s inside using <code>GeoPackage.getGeoPackageRasters()</code>.</li>
26+
</ol>
27+
28+
<h2>Features</h2>
29+
30+
<ul>
31+
<li>ArcGISMap</li>
32+
<li>Basemap</li>
33+
<li>FeatureLayer</li>
34+
<li>GeoPackage</li>
35+
<li>GeoPackageFeatureTable</li>
36+
<li>GeoPackageRaster</li>
37+
<li>Layer</li>
38+
<li>MapView</li>
39+
<li>RasterLayer</li>
40+
</ul>
334 KB
Loading
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/*
2+
* Copyright 2018 Esri.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.esri.samples.map.read_geopackage;
18+
19+
import java.io.File;
20+
21+
import javafx.application.Application;
22+
import javafx.geometry.Insets;
23+
import javafx.geometry.Pos;
24+
import javafx.scene.Scene;
25+
import javafx.scene.control.Label;
26+
import javafx.scene.control.ListCell;
27+
import javafx.scene.control.ListView;
28+
import javafx.scene.input.MouseButton;
29+
import javafx.scene.layout.StackPane;
30+
import javafx.scene.layout.VBox;
31+
import javafx.stage.Stage;
32+
import javafx.util.Callback;
33+
34+
import com.esri.arcgisruntime.data.GeoPackage;
35+
import com.esri.arcgisruntime.data.GeoPackageFeatureTable;
36+
import com.esri.arcgisruntime.layers.FeatureLayer;
37+
import com.esri.arcgisruntime.layers.Layer;
38+
import com.esri.arcgisruntime.layers.RasterLayer;
39+
import com.esri.arcgisruntime.mapping.ArcGISMap;
40+
import com.esri.arcgisruntime.mapping.Basemap;
41+
import com.esri.arcgisruntime.mapping.view.MapView;
42+
import com.esri.arcgisruntime.raster.GeoPackageRaster;
43+
44+
public class ReadGeoPackageSample extends Application {
45+
46+
private MapView mapView;
47+
48+
@Override
49+
public void start(Stage stage) throws Exception {
50+
51+
try {
52+
// create stack pane and application scene
53+
StackPane stackPane = new StackPane();
54+
Scene scene = new Scene(stackPane);
55+
scene.getStylesheets().add(getClass().getResource("/css/style.css").toExternalForm());
56+
57+
// set title, size, and add scene to stage
58+
stage.setTitle("Read GeoPackage Sample");
59+
stage.setWidth(800);
60+
stage.setHeight(700);
61+
stage.setScene(scene);
62+
stage.show();
63+
64+
// create a map and add it to the map view
65+
final ArcGISMap map = new ArcGISMap(Basemap.Type.STREETS, 39.7294, -104.8319, 11);
66+
mapView = new MapView();
67+
mapView.setMap(map);
68+
69+
// create two list views, one showing the layers in the map,
70+
// the other, showing the layers in the geoPackage not yet added to the map
71+
ListView<Layer> mapLayers = new ListView<>();
72+
ListView<Layer> geoPackageLayers = new ListView<>();
73+
74+
// create labels for the lists
75+
Label mapLayersLabel = new Label("Map layers");
76+
Label geoPackageLayersLabel = new Label("GeoPackage layers (not in the map)");
77+
mapLayersLabel.getStyleClass().add("panel-label");
78+
geoPackageLayersLabel.getStyleClass().add("panel-label");
79+
80+
// create a control panel
81+
VBox vBoxControl = new VBox(6);
82+
vBoxControl.setMaxSize(250, 260);
83+
vBoxControl.getStyleClass().add("panel-region");
84+
85+
// add labels and lists to the control panel
86+
vBoxControl.getChildren().addAll(mapLayersLabel, mapLayers, geoPackageLayersLabel, geoPackageLayers);
87+
88+
// create a cell factory to show the layer descriptions in the list view
89+
Callback<ListView<Layer>, ListCell<Layer>> cellFactory = list -> new ListCell<Layer>() {
90+
91+
@Override
92+
protected void updateItem(Layer layer, boolean bln) {
93+
94+
super.updateItem(layer, bln);
95+
if (layer != null) {
96+
if (layer instanceof FeatureLayer) {
97+
FeatureLayer featureLayer = (FeatureLayer) layer;
98+
GeoPackageFeatureTable featureTable = (GeoPackageFeatureTable) featureLayer.getFeatureTable();
99+
setText(featureTable.getDescription());
100+
} else if (layer instanceof RasterLayer) {
101+
RasterLayer rasterLayer = (RasterLayer) layer;
102+
GeoPackageRaster raster = (GeoPackageRaster) rasterLayer.getRaster();
103+
setText(raster.getDescription());
104+
}
105+
} else {
106+
setText(null);
107+
}
108+
}
109+
110+
};
111+
112+
mapLayers.setCellFactory(cellFactory);
113+
geoPackageLayers.setCellFactory(cellFactory);
114+
115+
// when you click on a layer in the geopackage layers list view, add it to the map
116+
geoPackageLayers.setOnMouseClicked(e -> {
117+
if (e.isStillSincePress() && e.getButton() == MouseButton.PRIMARY) {
118+
// get selected layer
119+
Layer layer = geoPackageLayers.getSelectionModel().getSelectedItem();
120+
if (layer != null) {
121+
// add it to the map and the top of the map layers list
122+
map.getOperationalLayers().add(layer);
123+
mapLayers.getItems().add(0, layer);
124+
// remove it from the geoPackage layers list
125+
geoPackageLayers.getItems().remove(layer);
126+
}
127+
}
128+
});
129+
130+
// when you click on a layer in the map layers list view, remove it from the map
131+
mapLayers.setOnMouseClicked(e -> {
132+
if (e.isStillSincePress() && e.getButton() == MouseButton.PRIMARY) {
133+
// get selected layer
134+
Layer layer = mapLayers.getSelectionModel().getSelectedItem();
135+
if (layer != null) {
136+
// remove it from the map and the map layers list
137+
map.getOperationalLayers().remove(layer);
138+
mapLayers.getItems().remove(layer);
139+
// add it back to the geoPackage layers list
140+
geoPackageLayers.getItems().add(layer);
141+
}
142+
}
143+
});
144+
145+
// read the raster and feature layers from the geoPackage and show them in the list view
146+
File geoPackageFile = new File("./samples-data/auroraCO/AuroraCO.gpkg");
147+
GeoPackage geoPackage = new GeoPackage(geoPackageFile.getAbsolutePath());
148+
geoPackage.loadAsync();
149+
geoPackage.addDoneLoadingListener(() -> {
150+
geoPackage.getGeoPackageRasters().forEach(r -> {
151+
RasterLayer rasterLayer = new RasterLayer(r);
152+
// make the raster layer semi-transparent so we can see layers below it
153+
rasterLayer.setOpacity(0.5f);
154+
geoPackageLayers.getItems().add(rasterLayer);
155+
});
156+
geoPackage.getGeoPackageFeatureTables().forEach(t -> geoPackageLayers.getItems().add(new FeatureLayer(t)));
157+
});
158+
159+
// add the map view and control box to stack pane
160+
stackPane.getChildren().addAll(mapView, vBoxControl);
161+
StackPane.setAlignment(vBoxControl, Pos.TOP_LEFT);
162+
StackPane.setMargin(vBoxControl, new Insets(10, 0, 0, 10));
163+
} catch (Exception e) {
164+
// on any error, display the stack trace
165+
e.printStackTrace();
166+
}
167+
}
168+
169+
/**
170+
* Stops and releases all resources used in application.
171+
*/
172+
@Override
173+
public void stop() {
174+
175+
if (mapView != null) {
176+
mapView.dispose();
177+
}
178+
}
179+
180+
/**
181+
* Opens and runs application.
182+
*
183+
* @param args arguments passed to this application
184+
*/
185+
public static void main(String[] args) {
186+
187+
Application.launch(args);
188+
}
189+
190+
}

0 commit comments

Comments
 (0)