|
39 | 39 | import com.esri.arcgisruntime.geometry.Envelope; |
40 | 40 | import com.esri.arcgisruntime.geometry.Point; |
41 | 41 | import com.esri.arcgisruntime.layers.ArcGISVectorTiledLayer; |
42 | | -import com.esri.arcgisruntime.layers.Layer; |
43 | 42 | import com.esri.arcgisruntime.loadable.LoadStatus; |
44 | 43 | import com.esri.arcgisruntime.mapping.ArcGISMap; |
45 | 44 | import com.esri.arcgisruntime.mapping.Basemap; |
|
49 | 48 | import com.esri.arcgisruntime.mapping.view.Graphic; |
50 | 49 | import com.esri.arcgisruntime.mapping.view.GraphicsOverlay; |
51 | 50 | import com.esri.arcgisruntime.mapping.view.MapView; |
52 | | -import com.esri.arcgisruntime.portal.PortalItem; |
53 | 51 | import com.esri.arcgisruntime.symbology.SimpleLineSymbol; |
54 | | -import com.esri.arcgisruntime.tasks.vectortilecache.ExportVectorTilesJob; |
55 | 52 | import com.esri.arcgisruntime.tasks.vectortilecache.ExportVectorTilesParameters; |
56 | 53 | import com.esri.arcgisruntime.tasks.vectortilecache.ExportVectorTilesResult; |
57 | 54 | import com.esri.arcgisruntime.tasks.vectortilecache.ExportVectorTilesTask; |
58 | 55 |
|
59 | 56 | public class ExportVectorTilesSample extends Application { |
60 | 57 |
|
61 | 58 | private MapView mapView; |
62 | | - private PortalItem portalItem; // keep loadable in scope to avoid garbage collection |
63 | 59 |
|
64 | 60 | @Override |
65 | 61 | public void start(Stage stage) { |
@@ -138,85 +134,85 @@ public void start(Stage stage) { |
138 | 134 | if (layer instanceof ArcGISVectorTiledLayer) { |
139 | 135 | ArcGISVectorTiledLayer vectorTiledLayer = (ArcGISVectorTiledLayer) layer; |
140 | 136 |
|
141 | | - // when the button is clicked, export the tiles to a temporary file |
142 | | - exportVectorTilesButton.setOnAction(e -> { |
143 | | - try { |
144 | | - // disable the button and show the progress bar |
145 | | - exportVectorTilesButton.setDisable(true); |
146 | | - progressBar.setVisible(true); |
147 | | - |
148 | | - // create temporary files for the .vtpk file and style item resources |
149 | | - File vtpkFile = File.createTempFile("tiles", ".vtpk"); |
150 | | - File resDir = Files.createTempDirectory("StyleItemResources").toFile(); |
151 | | - vtpkFile.deleteOnExit(); |
152 | | - resDir.deleteOnExit(); |
153 | | - |
154 | | - // create a new export vector tiles task |
155 | | - var exportVectorTilesTask = new ExportVectorTilesTask(vectorTiledLayer.getUri()); |
156 | | - |
157 | | - // create parameters for the export vector tiles job |
158 | | - double mapScale = mapView.getMapScale(); |
159 | | - // the max scale parameter is set to 10% of the map's scale to limit the |
160 | | - // number of tiles exported to within the vector tiled layer's max tile export limit |
161 | | - ListenableFuture<ExportVectorTilesParameters> exportVectorTilesParametersFuture = exportVectorTilesTask |
162 | | - .createDefaultExportVectorTilesParametersAsync(downloadArea.getGeometry(), mapScale * 0.1); |
163 | | - |
164 | | - exportVectorTilesParametersFuture.addDoneListener(() -> { |
165 | | - try { |
166 | | - var exportVectorTilesParameters = exportVectorTilesParametersFuture.get(); |
167 | | - |
168 | | - // create a job with the parameters |
169 | | - var exportVectorTilesJob = |
170 | | - exportVectorTilesTask.exportVectorTiles(exportVectorTilesParameters, vtpkFile.getAbsolutePath(), resDir.getAbsolutePath()); |
171 | | - |
172 | | - // start the job and wait for it to finish |
173 | | - exportVectorTilesJob.start(); |
174 | | - exportVectorTilesJob.addProgressChangedListener(() -> progressBar.setProgress(exportVectorTilesJob.getProgress() / 100.0)); |
175 | | - exportVectorTilesJob.addJobDoneListener(() -> { |
176 | | - |
177 | | - if (exportVectorTilesJob.getStatus() == Job.Status.SUCCEEDED) { |
178 | | - // show preview of exported tiles in alert |
179 | | - ExportVectorTilesResult tilesResult = exportVectorTilesJob.getResult(); |
180 | | - VectorTileCache tileCache = tilesResult.getVectorTileCache(); |
181 | | - ItemResourceCache resourceCache = tilesResult.getItemResourceCache(); |
182 | | - Alert preview = new Alert(Alert.AlertType.INFORMATION); |
183 | | - preview.initOwner(mapView.getScene().getWindow()); |
184 | | - preview.setTitle("Preview"); |
185 | | - preview.setHeaderText("Exported tiles to " + tileCache.getPath() + "\nExported resources to " + |
186 | | - resourceCache.getPath()); |
187 | | - MapView mapPreview = new MapView(); |
188 | | - mapPreview.setMinSize(400, 400); |
189 | | - ArcGISVectorTiledLayer vectorTiledLayerPreview = new ArcGISVectorTiledLayer(tileCache, resourceCache); |
190 | | - ArcGISMap previewMap = new ArcGISMap(new Basemap(vectorTiledLayerPreview)); |
191 | | - mapPreview.setMap(previewMap); |
192 | | - preview.getDialogPane().setContent(mapPreview); |
193 | | - preview.show(); |
194 | | - |
195 | | - } else { |
196 | | - Alert alert = new Alert(Alert.AlertType.ERROR, exportVectorTilesJob.getError().getAdditionalMessage()); |
197 | | - alert.show(); |
198 | | - } |
199 | | - |
200 | | - // reset the UI |
| 137 | + // when the button is clicked, export the tiles to a temporary file |
| 138 | + exportVectorTilesButton.setOnAction(e -> { |
| 139 | + try { |
| 140 | + // disable the button and show the progress bar |
| 141 | + exportVectorTilesButton.setDisable(true); |
| 142 | + progressBar.setVisible(true); |
| 143 | + |
| 144 | + // create temporary files for the .vtpk file and style item resources |
| 145 | + File vtpkFile = File.createTempFile("tiles", ".vtpk"); |
| 146 | + File resDir = Files.createTempDirectory("StyleItemResources").toFile(); |
| 147 | + vtpkFile.deleteOnExit(); |
| 148 | + resDir.deleteOnExit(); |
| 149 | + |
| 150 | + // create a new export vector tiles task |
| 151 | + var exportVectorTilesTask = new ExportVectorTilesTask(vectorTiledLayer.getUri()); |
| 152 | + |
| 153 | + // create parameters for the export vector tiles job |
| 154 | + double mapScale = mapView.getMapScale(); |
| 155 | + // the max scale parameter is set to 10% of the map's scale to limit the |
| 156 | + // number of tiles exported to within the vector tiled layer's max tile export limit |
| 157 | + ListenableFuture<ExportVectorTilesParameters> exportVectorTilesParametersFuture = exportVectorTilesTask |
| 158 | + .createDefaultExportVectorTilesParametersAsync(downloadArea.getGeometry(), mapScale * 0.1); |
| 159 | + |
| 160 | + exportVectorTilesParametersFuture.addDoneListener(() -> { |
| 161 | + try { |
| 162 | + var exportVectorTilesParameters = exportVectorTilesParametersFuture.get(); |
| 163 | + |
| 164 | + // create a job with the parameters |
| 165 | + var exportVectorTilesJob = |
| 166 | + exportVectorTilesTask.exportVectorTiles(exportVectorTilesParameters, vtpkFile.getAbsolutePath(), resDir.getAbsolutePath()); |
| 167 | + |
| 168 | + // start the job and wait for it to finish |
| 169 | + exportVectorTilesJob.start(); |
| 170 | + exportVectorTilesJob.addProgressChangedListener(() -> progressBar.setProgress(exportVectorTilesJob.getProgress() / 100.0)); |
| 171 | + exportVectorTilesJob.addJobDoneListener(() -> { |
| 172 | + |
| 173 | + if (exportVectorTilesJob.getStatus() == Job.Status.SUCCEEDED) { |
| 174 | + // show preview of exported tiles in alert |
| 175 | + ExportVectorTilesResult tilesResult = exportVectorTilesJob.getResult(); |
| 176 | + VectorTileCache tileCache = tilesResult.getVectorTileCache(); |
| 177 | + ItemResourceCache resourceCache = tilesResult.getItemResourceCache(); |
| 178 | + Alert preview = new Alert(Alert.AlertType.INFORMATION); |
| 179 | + preview.initOwner(mapView.getScene().getWindow()); |
| 180 | + preview.setTitle("Preview"); |
| 181 | + preview.setHeaderText("Exported tiles to " + tileCache.getPath() + "\nExported resources to " + |
| 182 | + resourceCache.getPath()); |
| 183 | + MapView previewMapView = new MapView(); |
| 184 | + previewMapView.setMinSize(400, 400); |
| 185 | + ArcGISVectorTiledLayer vectorTiledLayerPreview = new ArcGISVectorTiledLayer(tileCache, resourceCache); |
| 186 | + ArcGISMap previewMap = new ArcGISMap(new Basemap(vectorTiledLayerPreview)); |
| 187 | + previewMapView.setMap(previewMap); |
| 188 | + preview.getDialogPane().setContent(previewMapView); |
| 189 | + preview.show(); |
| 190 | + // dispose of the preview mapview's resources |
| 191 | + preview.setOnCloseRequest(event -> previewMapView.dispose()); |
| 192 | + |
| 193 | + } else { |
| 194 | + new Alert(Alert.AlertType.ERROR, exportVectorTilesJob.getError().getAdditionalMessage()).show(); |
| 195 | + } |
| 196 | + |
| 197 | + // reset the UI |
| 198 | + progressBar.setVisible(false); |
| 199 | + progressBar.setProgress(0); |
| 200 | + exportVectorTilesButton.setDisable(false); |
| 201 | + }); |
| 202 | + |
| 203 | + } catch (InterruptedException | ExecutionException ex) { |
| 204 | + new Alert(Alert.AlertType.ERROR, ex.getMessage()).show(); |
201 | 205 | progressBar.setVisible(false); |
202 | 206 | progressBar.setProgress(0); |
203 | | - exportVectorTilesButton.setDisable(false); |
204 | | - }); |
205 | | - |
206 | | - } catch (InterruptedException | ExecutionException ex) { |
207 | | - Alert alert = new Alert(Alert.AlertType.ERROR, ex.getMessage()); |
208 | | - alert.show(); |
209 | | - progressBar.setVisible(false); |
210 | | - progressBar.setProgress(0); |
211 | | - } |
212 | | - }); |
213 | | - |
214 | | - } catch (IOException ex) { |
215 | | - Alert alert = new Alert(Alert.AlertType.ERROR, "Failed to create temporary file"); |
216 | | - alert.show(); |
217 | | - } |
218 | | - }); |
219 | | - } |
| 207 | + } |
| 208 | + }); |
| 209 | + |
| 210 | + } catch (IOException ex) { |
| 211 | + Alert alert = new Alert(Alert.AlertType.ERROR, "Failed to create temporary file"); |
| 212 | + alert.show(); |
| 213 | + } |
| 214 | + }); |
| 215 | + } |
220 | 216 |
|
221 | 217 | } else { |
222 | 218 | new Alert(Alert.AlertType.ERROR, "Map could not be loaded").show(); |
|
0 commit comments