|
| 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.featurelayers.feature_layer_shapefile; |
| 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.ShapefileFeatureTable; |
| 28 | +import com.esri.arcgisruntime.layers.FeatureLayer; |
| 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 | + |
| 34 | +public class FeatureLayerShapefileSample extends Application { |
| 35 | + |
| 36 | + private MapView mapView; |
| 37 | + |
| 38 | + @Override |
| 39 | + public void start(Stage stage) throws Exception { |
| 40 | + |
| 41 | + try { |
| 42 | + // create stack pane and application scene |
| 43 | + StackPane stackPane = new StackPane(); |
| 44 | + Scene scene = new Scene(stackPane); |
| 45 | + |
| 46 | + // set title, size, and add scene to stage |
| 47 | + stage.setTitle("Feature Layer Shapefile Sample"); |
| 48 | + stage.setWidth(800); |
| 49 | + stage.setHeight(700); |
| 50 | + stage.setScene(scene); |
| 51 | + stage.show(); |
| 52 | + |
| 53 | + // create a map with a basemap |
| 54 | + ArcGISMap map = new ArcGISMap(Basemap.createStreetsVector()); |
| 55 | + |
| 56 | + // set the map to the map view |
| 57 | + mapView = new MapView(); |
| 58 | + mapView.setMap(map); |
| 59 | + |
| 60 | + // create a shapefile feature table from the local file |
| 61 | + File shapefile = new File("./samples-data/auroraCO/Public_Art.shp"); |
| 62 | + ShapefileFeatureTable shapefileFeatureTable = new ShapefileFeatureTable(shapefile.getAbsolutePath()); |
| 63 | + |
| 64 | + // use the shapefile feature table to create a feature layer |
| 65 | + FeatureLayer featureLayer = new FeatureLayer(shapefileFeatureTable); |
| 66 | + featureLayer.addDoneLoadingListener(() -> { |
| 67 | + if (featureLayer.getLoadStatus() == LoadStatus.LOADED) { |
| 68 | + // zoom to the area containing the layer's features |
| 69 | + mapView.setViewpointGeometryAsync(featureLayer.getFullExtent()); |
| 70 | + } else { |
| 71 | + Alert alert = new Alert(Alert.AlertType.ERROR, featureLayer.getLoadError().getMessage()); |
| 72 | + alert.show(); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + // add the feature layer to the map |
| 77 | + map.getOperationalLayers().add(featureLayer); |
| 78 | + |
| 79 | + // add the map view to the stack pane |
| 80 | + stackPane.getChildren().add(mapView); |
| 81 | + } catch (Exception e) { |
| 82 | + // on any error, display the stack trace. |
| 83 | + e.printStackTrace(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Stops and releases all resources used in application. |
| 89 | + */ |
| 90 | + @Override |
| 91 | + public void stop() { |
| 92 | + |
| 93 | + if (mapView != null) { |
| 94 | + mapView.dispose(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Opens and runs application. |
| 100 | + * |
| 101 | + * @param args arguments passed to this application |
| 102 | + */ |
| 103 | + public static void main(String[] args) { |
| 104 | + |
| 105 | + Application.launch(args); |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments