Feature collection layer

View on GitHubSample viewer app

Create a feature collection layer from a feature collection table, and add it to a map.

Image of feature collection layer

Use case

A Feature Collection allows easily importing external data (such as CSV files), as well as creating custom schema for data that is in non-standardized format. This data can then be used to populate a Feature Collection Table, and displayed in a Feature Collection Layer using the attributes and geometries provided in the external data source. For example, an electricity supplier could use this functionality to visualize existing location data of coverage areas (polygons), power stations (points), transmission lines (polylines), and others.

How to use the sample

When launched, this sample displays a FeatureCollectionLayer with a Point, Polyline and Polygon geometry. Pan and zoom to explore the scene.

How it works

  1. Create a FeatureCollectionLayer using a new feature collection, FeatureCollectionLayer(featureCollection)
  2. Add the feature collection layer to the map, ArcGISMap.getOperationalLayers().add(featureCollectionLayer).
  3. Create a FeatureCollectionTable for the GeometryTypes Point, Polyline, and Polygon, FeatureCollectionTable(fields, geometryType, spatialRefernce)
    • Additionally, pass in a list of Field objects to represent the table's schema. In this case a field of type String named name is added.
  4. Assign a SimpleRenderer to each table to render any Features from that table using the Symbol that was set.
  5. Add the feature collection table to the feature collection, FeatureCollection.getTables().add(featureCollectionTable).
  6. Use the createFeature method to create a feature from the feature collection table, passing an attribute and geometry for that feature, FeatureCollectionTable.createFeature(attributes, geometry).
  7. Add new features to the table, FeatureCollectionTable.addFeatureAsync(feature).

Relevant API

  • ArcGISFeature
  • FeatureCollection
  • FeatureCollectionLayer
  • FeatureCollectionTable
  • Field
  • SimpleRenderer

Tags

collection, feature, layers, table

Sample Code

FeatureCollectionLayerSample.java
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * Copyright 2016 Esri.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.esri.samples.feature_collection_layer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.FeatureCollection;
import com.esri.arcgisruntime.data.FeatureCollectionTable;
import com.esri.arcgisruntime.data.Field;
import com.esri.arcgisruntime.geometry.GeometryType;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.PolygonBuilder;
import com.esri.arcgisruntime.geometry.PolylineBuilder;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.layers.FeatureCollectionLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol.Style;
import com.esri.arcgisruntime.symbology.SimpleRenderer;

public class FeatureCollectionLayerSample extends Application {

  private MapView mapView;

  private static final SpatialReference WGS84 = SpatialReferences.getWgs84();

  @Override
  public void start(Stage stage) {

    try {
      // create stack pane and application scene
      StackPane stackPane = new StackPane();
      Scene scene = new Scene(stackPane);

      // set title, size, and add scene to stage
      stage.setTitle("Feature Collection Layer Sample");
      stage.setWidth(800);
      stage.setHeight(700);
      stage.setScene(scene);
      stage.show();

      // authentication with an API key or named user is required to access basemaps and other location services
      String yourAPIKey = System.getProperty("apiKey");
      ArcGISRuntimeEnvironment.setApiKey(yourAPIKey);

      // create a map with the oceans basemap style
      ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_OCEANS);

      // create a map view and set the map to it
      mapView = new MapView();
      mapView.setMap(map);

      // set initial location for view
      mapView.setViewpointCenterAsync(new Point(-79.497238, 8.849289, WGS84), 1000000);

      // create feature collection and add to the map as a layer
      FeatureCollection featureCollection = new FeatureCollection();
      FeatureCollectionLayer featureCollectionLayer = new FeatureCollectionLayer(featureCollection);
      map.getOperationalLayers().add(featureCollectionLayer);

      // add point, line, and polygon geometry to feature collection
      createPointTable(featureCollection);
      createPolylineTable(featureCollection);
      createPolygonTables(featureCollection);

      // add the map view to stack pane
      stackPane.getChildren().addAll(mapView);
    } catch (Exception e) {
      // on any error, display the stack trace.
      e.printStackTrace();
    }
  }

  /**
   * Creates a Point Feature Collection Table with one Point and adds it to the Feature collection that was passed.
   *
   * @param featureCollection that the point Feature Collection Table will be added to
   */
  private void createPointTable(FeatureCollection featureCollection) {

    // defines the schema for the geometry's attribute
    List<Field> pointFields = new ArrayList<>();
    pointFields.add(Field.createString("Place", "Place Name", 50));

    // a feature collection table that creates point geometry
    FeatureCollectionTable pointsTable = new FeatureCollectionTable(pointFields, GeometryType.POINT, WGS84);

    // set a default symbol for features in the collection table
    SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(Style.TRIANGLE, Color.RED, 18);
    SimpleRenderer renderer = new SimpleRenderer(markerSymbol);
    pointsTable.setRenderer(renderer);

    // add feature collection table to feature collection
    featureCollection.getTables().add(pointsTable);

    // create feature using the collection table by passing an attribute and geometry
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(pointFields.get(0).getName(), "Current Location");
    Point point = new Point(-79.497238, 8.849289, WGS84);
    Feature addedFeature = pointsTable.createFeature(attributes, point);

    // add feature to collection table
    pointsTable.addFeatureAsync(addedFeature);
  }

  /**
   * Creates a PolyLine Feature Collection Table with one PolyLine and adds it to the Feature collection that was passed.
   *
   * @param featureCollection that the polyline Feature Collection Table will be added to
   */
  private void createPolylineTable(FeatureCollection featureCollection) {

    // defines the schema for the geometry's attribute
    List<Field> polylineFields = new ArrayList<>();
    polylineFields.add(Field.createString("Boundary", "Boundary Name", 50));

    // a feature collection table that creates polyline geometry
    FeatureCollectionTable polylineTable = new FeatureCollectionTable(polylineFields, GeometryType.POLYLINE, WGS84);

    // set a default symbol for features in the collection table
    SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.DASH, Color.GREEN, 3);
    SimpleRenderer renderer = new SimpleRenderer(lineSymbol);
    polylineTable.setRenderer(renderer);

    // add feature collection table to feature collection
    featureCollection.getTables().add(polylineTable);

    // create feature using the collection table by passing an attribute and geometry
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(polylineFields.get(0).getName(), "AManAPlanACanalPanama");
    PolylineBuilder builder = new PolylineBuilder(WGS84);
    builder.addPoint(new Point(-79.497238, 8.849289, WGS84));
    builder.addPoint(new Point(-80.035568, 9.432302, WGS84));
    Feature addedFeature = polylineTable.createFeature(attributes, builder.toGeometry());

    // add feature to collection table
    polylineTable.addFeatureAsync(addedFeature);
  }

  /**
   * Creates a Polygon Feature Collection Table with one Polygon and adds it to the Feature collection that was passed.
   *
   * @param featureCollection that the polygon Feature Collection Table will be added to
   */
  private void createPolygonTables(FeatureCollection featureCollection) {

    // defines the schema for the geometry's attribute
    List<Field> polygonFields = new ArrayList<>();
    polygonFields.add(Field.createString("Area", "Area Name", 50));

    // a feature collection table that creates polygon geometry
    FeatureCollectionTable polygonTable = new FeatureCollectionTable(polygonFields, GeometryType.POLYGON, WGS84);

    // set a default symbol for features in the collection table
    SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.BLUE, 2);
    SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.DIAGONAL_CROSS, Color.CYAN, lineSymbol);
    SimpleRenderer renderer = new SimpleRenderer(fillSymbol);
    polygonTable.setRenderer(renderer);

    // add feature collection table to feature collection
    featureCollection.getTables().add(polygonTable);

    // create feature using the collection table by passing an attribute and geometry
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(polygonFields.get(0).getName(), "Restricted area");
    PolygonBuilder builder = new PolygonBuilder(WGS84);
    builder.addPoint(new Point(-79.497238, 8.849289, WGS84));
    builder.addPoint(new Point(-79.337936, 8.638903, WGS84));
    builder.addPoint(new Point(-79.11409, 8.895422, WGS84));
    Feature addedFeature = polygonTable.createFeature(attributes, builder.toGeometry());

    // add feature to collection table
    polygonTable.addFeatureAsync(addedFeature);
  }

  /**
   * Stops and releases all resources used in application.
   */
  @Override
  public void stop() {

    if (mapView != null) {
      mapView.dispose();
    }
  }

  /**
   * Opens and runs application.
   *
   * @param args arguments passed to this application
   */
  public static void main(String[] args) {

    Application.launch(args);
  }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.