|
| 1 | +/* |
| 2 | + * Copyright 2016 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 | +package com.esri.samples.featurelayers.feature_collection_layer; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import javafx.application.Application; |
| 24 | +import javafx.scene.Scene; |
| 25 | +import javafx.scene.layout.StackPane; |
| 26 | +import javafx.stage.Stage; |
| 27 | + |
| 28 | +import com.esri.arcgisruntime.data.Feature; |
| 29 | +import com.esri.arcgisruntime.data.FeatureCollection; |
| 30 | +import com.esri.arcgisruntime.data.FeatureCollectionTable; |
| 31 | +import com.esri.arcgisruntime.data.Field; |
| 32 | +import com.esri.arcgisruntime.geometry.GeometryType; |
| 33 | +import com.esri.arcgisruntime.geometry.Point; |
| 34 | +import com.esri.arcgisruntime.geometry.PolygonBuilder; |
| 35 | +import com.esri.arcgisruntime.geometry.PolylineBuilder; |
| 36 | +import com.esri.arcgisruntime.geometry.SpatialReference; |
| 37 | +import com.esri.arcgisruntime.geometry.SpatialReferences; |
| 38 | +import com.esri.arcgisruntime.layers.FeatureCollectionLayer; |
| 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.symbology.SimpleFillSymbol; |
| 43 | +import com.esri.arcgisruntime.symbology.SimpleLineSymbol; |
| 44 | +import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol; |
| 45 | +import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol.Style; |
| 46 | +import com.esri.arcgisruntime.symbology.SimpleRenderer; |
| 47 | + |
| 48 | +public class FeatureCollectionLayerSample extends Application { |
| 49 | + |
| 50 | + private MapView mapView; |
| 51 | + |
| 52 | + private static final SpatialReference WGS84 = SpatialReferences.getWgs84(); |
| 53 | + |
| 54 | + @Override |
| 55 | + public void start(Stage stage) throws Exception { |
| 56 | + |
| 57 | + try { |
| 58 | + // create stack pane and application scene |
| 59 | + StackPane stackPane = new StackPane(); |
| 60 | + Scene scene = new Scene(stackPane); |
| 61 | + |
| 62 | + // set title, size, and add scene to stage |
| 63 | + stage.setTitle("Feature Collection Layer Sample"); |
| 64 | + stage.setWidth(800); |
| 65 | + stage.setHeight(700); |
| 66 | + stage.setScene(scene); |
| 67 | + stage.show(); |
| 68 | + |
| 69 | + // create amp and set it to be displayed in this view |
| 70 | + ArcGISMap map = new ArcGISMap(Basemap.createOceans()); |
| 71 | + mapView = new MapView(); |
| 72 | + mapView.setMap(map); |
| 73 | + |
| 74 | + // set initial location for view |
| 75 | + mapView.setViewpointCenterAsync(new Point(-79.497238, 8.849289, WGS84), 1000000); |
| 76 | + |
| 77 | + // create feature collection and add to the map as a layer |
| 78 | + FeatureCollection featureCollection = new FeatureCollection(); |
| 79 | + FeatureCollectionLayer featureCollectionLayer = new FeatureCollectionLayer(featureCollection); |
| 80 | + map.getOperationalLayers().add(featureCollectionLayer); |
| 81 | + |
| 82 | + // add point, line, and polygon geometry to feature collection |
| 83 | + createPointTable(featureCollection); |
| 84 | + createPolylineTable(featureCollection); |
| 85 | + createPolygonTables(featureCollection); |
| 86 | + |
| 87 | + // add the map view to stack pane |
| 88 | + stackPane.getChildren().addAll(mapView); |
| 89 | + } catch (Exception e) { |
| 90 | + // on any error, display the stack trace. |
| 91 | + e.printStackTrace(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Creates a Point Feature Collection Table with one Point and adds it to the Feature collection that was passed. |
| 97 | + * |
| 98 | + * @param featureCollection that the point Feature Collection Table will be added to |
| 99 | + */ |
| 100 | + private void createPointTable(FeatureCollection featureCollection) { |
| 101 | + |
| 102 | + // defines the schema for the geometry's attribute |
| 103 | + List<Field> pointFields = new ArrayList<>(); |
| 104 | + pointFields.add(Field.createString("Place", "Place Name", 50)); |
| 105 | + |
| 106 | + // a feature collection table that creates point geometry |
| 107 | + FeatureCollectionTable pointsTable = new FeatureCollectionTable(pointFields, GeometryType.POINT, WGS84); |
| 108 | + |
| 109 | + // set a default symbol for features in the collection table |
| 110 | + SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(Style.TRIANGLE, 0xFFFF0000, 18); |
| 111 | + SimpleRenderer renderer = new SimpleRenderer(markerSymbol); |
| 112 | + pointsTable.setRenderer(renderer); |
| 113 | + |
| 114 | + // add feature collection table to feature collection |
| 115 | + featureCollection.getTables().add(pointsTable); |
| 116 | + |
| 117 | + // create feature using the collection table by passing an attribute and geometry |
| 118 | + Map<String, Object> attributes = new HashMap<>(); |
| 119 | + attributes.put(pointFields.get(0).getName(), "Current Location"); |
| 120 | + Point point = new Point(-79.497238, 8.849289, WGS84); |
| 121 | + Feature addedFeature = pointsTable.createFeature(attributes, point); |
| 122 | + |
| 123 | + // add feature to collection table |
| 124 | + pointsTable.addFeatureAsync(addedFeature); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Creates a PolyLine Feature Collection Table with one PolyLine and adds it to the Feature collection that was passed. |
| 129 | + * |
| 130 | + * @param featureCollection that the polyline Feature Collection Table will be added to |
| 131 | + */ |
| 132 | + private void createPolylineTable(FeatureCollection featureCollection) { |
| 133 | + |
| 134 | + // defines the schema for the geometry's attribute |
| 135 | + List<Field> polylineFields = new ArrayList<>(); |
| 136 | + polylineFields.add(Field.createString("Boundary", "Boundary Name", 50)); |
| 137 | + |
| 138 | + // a feature collection table that creates polyline geometry |
| 139 | + FeatureCollectionTable polylineTable = new FeatureCollectionTable(polylineFields, GeometryType.POLYLINE, WGS84); |
| 140 | + |
| 141 | + // set a default symbol for features in the collection table |
| 142 | + SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.DASH, 0xFF00FF00, 3); |
| 143 | + SimpleRenderer renderer = new SimpleRenderer(lineSymbol); |
| 144 | + polylineTable.setRenderer(renderer); |
| 145 | + |
| 146 | + // add feature collection table to feature collection |
| 147 | + featureCollection.getTables().add(polylineTable); |
| 148 | + |
| 149 | + // create feature using the collection table by passing an attribute and geometry |
| 150 | + Map<String, Object> attributes = new HashMap<>(); |
| 151 | + attributes.put(polylineFields.get(0).getName(), "AManAPlanACanalPanama"); |
| 152 | + PolylineBuilder builder = new PolylineBuilder(WGS84); |
| 153 | + builder.addPoint(new Point(-79.497238, 8.849289, WGS84)); |
| 154 | + builder.addPoint(new Point(-80.035568, 9.432302, WGS84)); |
| 155 | + Feature addedFeature = polylineTable.createFeature(attributes, builder.toGeometry()); |
| 156 | + |
| 157 | + // add feature to collection table |
| 158 | + polylineTable.addFeatureAsync(addedFeature); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Creates a Polygon Feature Collection Table with one Polygon and adds it to the Feature collection that was passed. |
| 163 | + * |
| 164 | + * @param featureCollection that the polygon Feature Collection Table will be added to |
| 165 | + */ |
| 166 | + private void createPolygonTables(FeatureCollection featureCollection) { |
| 167 | + |
| 168 | + // defines the schema for the geometry's attribute |
| 169 | + List<Field> polygonFields = new ArrayList<>(); |
| 170 | + polygonFields.add(Field.createString("Area", "Area Name", 50)); |
| 171 | + |
| 172 | + // a feature collection table that creates polygon geometry |
| 173 | + FeatureCollectionTable polygonTable = new FeatureCollectionTable(polygonFields, GeometryType.POLYGON, WGS84); |
| 174 | + |
| 175 | + // set a default symbol for features in the collection table |
| 176 | + SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFF0000FF, 2); |
| 177 | + SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.DIAGONAL_CROSS, 0xFF00FFFF, lineSymbol); |
| 178 | + SimpleRenderer renderer = new SimpleRenderer(fillSymbol); |
| 179 | + polygonTable.setRenderer(renderer); |
| 180 | + |
| 181 | + // add feature collection table to feature collection |
| 182 | + featureCollection.getTables().add(polygonTable); |
| 183 | + |
| 184 | + // create feature using the collection table by passing an attribute and geometry |
| 185 | + Map<String, Object> attributes = new HashMap<>(); |
| 186 | + attributes.put(polygonFields.get(0).getName(), "Restricted area"); |
| 187 | + PolygonBuilder builder = new PolygonBuilder(WGS84); |
| 188 | + builder.addPoint(new Point(-79.497238, 8.849289, WGS84)); |
| 189 | + builder.addPoint(new Point(-79.337936, 8.638903, WGS84)); |
| 190 | + builder.addPoint(new Point(-79.11409, 8.895422, WGS84)); |
| 191 | + Feature addedFeature = polygonTable.createFeature(attributes, builder.toGeometry()); |
| 192 | + |
| 193 | + // add feature to collection table |
| 194 | + polygonTable.addFeatureAsync(addedFeature); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Stops and releases all resources used in application. |
| 199 | + */ |
| 200 | + @Override |
| 201 | + public void stop() throws Exception { |
| 202 | + |
| 203 | + if (mapView != null) { |
| 204 | + mapView.dispose(); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Opens and runs application. |
| 210 | + * |
| 211 | + * @param args arguments passed to this application |
| 212 | + */ |
| 213 | + public static void main(String[] args) { |
| 214 | + |
| 215 | + Application.launch(args); |
| 216 | + } |
| 217 | +} |
0 commit comments