|
| 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.raster.raster_layer_geopackage; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | + |
| 21 | +import javafx.application.Application; |
| 22 | +import javafx.scene.Scene; |
| 23 | +import javafx.scene.control.Alert; |
| 24 | +import javafx.scene.layout.StackPane; |
| 25 | +import javafx.stage.Stage; |
| 26 | + |
| 27 | +import com.esri.arcgisruntime.data.GeoPackage; |
| 28 | +import com.esri.arcgisruntime.layers.RasterLayer; |
| 29 | +import com.esri.arcgisruntime.loadable.LoadStatus; |
| 30 | +import com.esri.arcgisruntime.mapping.ArcGISMap; |
| 31 | +import com.esri.arcgisruntime.mapping.Basemap; |
| 32 | +import com.esri.arcgisruntime.mapping.view.MapView; |
| 33 | +import com.esri.arcgisruntime.raster.GeoPackageRaster; |
| 34 | + |
| 35 | +public class RasterLayerGeoPackageSample extends Application { |
| 36 | + |
| 37 | + private MapView mapView; |
| 38 | + |
| 39 | + @Override |
| 40 | + public void start(Stage stage) throws Exception { |
| 41 | + |
| 42 | + try { |
| 43 | + // create stack pane and application scene |
| 44 | + StackPane stackPane = new StackPane(); |
| 45 | + Scene scene = new Scene(stackPane); |
| 46 | + |
| 47 | + // set title, size, and add scene to stage |
| 48 | + stage.setTitle("Raster Layer GeoPackage"); |
| 49 | + stage.setWidth(800); |
| 50 | + stage.setHeight(700); |
| 51 | + stage.setScene(scene); |
| 52 | + stage.show(); |
| 53 | + |
| 54 | + // create a map with light gray canvas vector basemap |
| 55 | + ArcGISMap map = new ArcGISMap(Basemap.createLightGrayCanvasVector()); |
| 56 | + mapView = new MapView(); |
| 57 | + mapView.setMap(map); |
| 58 | + |
| 59 | + // create a geopackage from a local gpkg file |
| 60 | + GeoPackage geoPackage = new GeoPackage(new File("./samples-data/auroraCO/AuroraCO.gpkg").getAbsolutePath()); |
| 61 | + |
| 62 | + // load the geopackage |
| 63 | + geoPackage.loadAsync(); |
| 64 | + geoPackage.addDoneLoadingListener(() -> { |
| 65 | + if (geoPackage.getLoadStatus() == LoadStatus.LOADED && geoPackage.getGeoPackageRasters().size() > 0) { |
| 66 | + // get the geopackage raster |
| 67 | + GeoPackageRaster raster = geoPackage.getGeoPackageRasters().get(0); |
| 68 | + |
| 69 | + // create a raster layer and add it to the map |
| 70 | + RasterLayer rasterLayer = new RasterLayer(raster); |
| 71 | + map.getOperationalLayers().add(rasterLayer); |
| 72 | + |
| 73 | + // set viewpoint on the raster layer |
| 74 | + rasterLayer.addDoneLoadingListener(() -> { |
| 75 | + if (rasterLayer.getLoadStatus() == LoadStatus.LOADED) { |
| 76 | + mapView.setViewpointGeometryAsync(rasterLayer.getFullExtent(), 150); |
| 77 | + } else { |
| 78 | + Alert alert = new Alert(Alert.AlertType.ERROR, "Failed to load raster layer"); |
| 79 | + alert.show(); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + } else { |
| 84 | + Alert alert = new Alert(Alert.AlertType.ERROR, "Failed to load geopackage"); |
| 85 | + alert.show(); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + // add the map view to stack pane |
| 90 | + stackPane.getChildren().addAll(mapView); |
| 91 | + } catch (Exception e) { |
| 92 | + // on any error, display the stack trace. |
| 93 | + e.printStackTrace(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Stops and releases all resources used in application. |
| 99 | + */ |
| 100 | + @Override |
| 101 | + public void stop() { |
| 102 | + |
| 103 | + if (mapView != null) { |
| 104 | + mapView.dispose(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Opens and runs application. |
| 110 | + * |
| 111 | + * @param args arguments passed to this application |
| 112 | + */ |
| 113 | + public static void main(String[] args) { |
| 114 | + |
| 115 | + Application.launch(args); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments