|
| 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.symbology.symbolize_shapefile; |
| 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.Alert; |
| 26 | +import javafx.scene.control.ToggleButton; |
| 27 | +import javafx.scene.layout.StackPane; |
| 28 | +import javafx.stage.Stage; |
| 29 | + |
| 30 | +import com.esri.arcgisruntime.data.ShapefileFeatureTable; |
| 31 | +import com.esri.arcgisruntime.layers.FeatureLayer; |
| 32 | +import com.esri.arcgisruntime.loadable.LoadStatus; |
| 33 | +import com.esri.arcgisruntime.mapping.ArcGISMap; |
| 34 | +import com.esri.arcgisruntime.mapping.Basemap; |
| 35 | +import com.esri.arcgisruntime.mapping.view.MapView; |
| 36 | +import com.esri.arcgisruntime.symbology.SimpleFillSymbol; |
| 37 | +import com.esri.arcgisruntime.symbology.SimpleLineSymbol; |
| 38 | +import com.esri.arcgisruntime.symbology.SimpleRenderer; |
| 39 | + |
| 40 | +public class SymbolizeShapefileSample extends Application { |
| 41 | + |
| 42 | + private MapView mapView; |
| 43 | + |
| 44 | + @Override |
| 45 | + public void start(Stage stage) throws Exception { |
| 46 | + |
| 47 | + try { |
| 48 | + // create stack pane and application scene |
| 49 | + StackPane stackPane = new StackPane(); |
| 50 | + Scene scene = new Scene(stackPane); |
| 51 | + |
| 52 | + // set title, size, and add scene to stage |
| 53 | + stage.setTitle("Symbolize Shapefile Sample"); |
| 54 | + stage.setWidth(800); |
| 55 | + stage.setHeight(700); |
| 56 | + stage.setScene(scene); |
| 57 | + stage.show(); |
| 58 | + |
| 59 | + // create a map with a basemap |
| 60 | + ArcGISMap map = new ArcGISMap(Basemap.createTopographic()); |
| 61 | + |
| 62 | + // set the map to the map view |
| 63 | + mapView = new MapView(); |
| 64 | + mapView.setMap(map); |
| 65 | + |
| 66 | + // create a shapefile feature table from the local data |
| 67 | + File shapefile = new File("./samples-data/auroraCO/Subdivisions.shp"); |
| 68 | + ShapefileFeatureTable shapefileFeatureTable = new ShapefileFeatureTable(shapefile.getAbsolutePath()); |
| 69 | + |
| 70 | + // use the shapefile feature table to create a feature layer |
| 71 | + FeatureLayer featureLayer = new FeatureLayer(shapefileFeatureTable); |
| 72 | + featureLayer.addDoneLoadingListener(() -> { |
| 73 | + if (featureLayer.getLoadStatus() == LoadStatus.LOADED) { |
| 74 | + // zoom to the feature layer's extent |
| 75 | + mapView.setViewpointGeometryAsync(featureLayer.getFullExtent()); |
| 76 | + } else { |
| 77 | + Alert alert = new Alert(Alert.AlertType.ERROR, featureLayer.getLoadError().getMessage()); |
| 78 | + alert.show(); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + // add the feature layer to the map |
| 83 | + map.getOperationalLayers().add(featureLayer); |
| 84 | + |
| 85 | + // create the symbols and renderer |
| 86 | + SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFFFF0000, 1.0f); |
| 87 | + SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, 0xFFFFFF00, lineSymbol); |
| 88 | + SimpleRenderer renderer = new SimpleRenderer(fillSymbol); |
| 89 | + |
| 90 | + // create a toggle button to switch between renderers |
| 91 | + ToggleButton symbolizeButton = new ToggleButton("Toggle Symbology"); |
| 92 | + symbolizeButton.setOnAction(e -> { |
| 93 | + if (symbolizeButton.isSelected()) { |
| 94 | + featureLayer.setRenderer(renderer); |
| 95 | + } else { |
| 96 | + // switch back to the default renderer |
| 97 | + featureLayer.resetRenderer(); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + // add the map view and toggle button to the stack pane |
| 102 | + stackPane.getChildren().addAll(mapView, symbolizeButton); |
| 103 | + StackPane.setAlignment(symbolizeButton, Pos.TOP_LEFT); |
| 104 | + StackPane.setMargin(symbolizeButton, new Insets(10, 0, 0, 10)); |
| 105 | + } catch (Exception e) { |
| 106 | + // on any error, display the stack trace. |
| 107 | + e.printStackTrace(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Stops and releases all resources used in application. |
| 113 | + */ |
| 114 | + @Override |
| 115 | + public void stop() throws Exception { |
| 116 | + |
| 117 | + if (mapView != null) { |
| 118 | + mapView.dispose(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Opens and runs application. |
| 124 | + * |
| 125 | + * @param args arguments passed to this application |
| 126 | + */ |
| 127 | + public static void main(String[] args) { |
| 128 | + |
| 129 | + Application.launch(args); |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments