Feature collection layer

View on GitHub
Sample 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 feature collection layer 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 with new FeatureCollectionTable(fields, geometryType, spatialReference), specifying GeometryType.POINT, GeometryType.POLYLINE, or GeometryType.POLYGON for geometry type.

    • Additionally, pass in a list of Field objects to represent the table's schema.
  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. Create a feature from the feature collection table passing an attribute and geometry for that feature with featureCollectionTable.createFeature(attributes, geometry).
  7. Add new features to the table, featureCollectionTable.addFeatureAsync(feature).

Relevant API

  • Feature
  • FeatureCollection
  • FeatureCollectionLayer
  • FeatureCollectionTable
  • Field
  • SimpleRenderer

Tags

collection, feature, layers, table

Sample Code

MainActivity.java
                                                                                                                                                                                                        
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
/* Copyright 2018 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.arcgisruntime.sample.featurecollectionlayer;

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

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
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.SpatialReferences;
import com.esri.arcgisruntime.layers.FeatureCollectionLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
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.SimpleRenderer;

public class MainActivity extends AppCompatActivity {

  private MapView mMapView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // authentication with an API key or named user is required to access basemaps and other
    // location services
    ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY);

    // inflate MapView from layout
    mMapView = findViewById(R.id.mapView);

    // initialize map with basemap
    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_OCEANS);

    // assign map to the map view
    mMapView.setMap(map);
    mMapView.setViewpoint(new Viewpoint( 8.5844, -79.6597, 2000000));

    // 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);
  }

  /**
   * 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, SpatialReferences.getWgs84());

    // set a default symbol for features in the collection table
    SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.TRIANGLE, 0xFFFF0000, 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, SpatialReferences.getWgs84());
    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, SpatialReferences.getWgs84());

    // set a default symbol for features in the collection table
    SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.DASH, 0xFF00FF00, 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(SpatialReferences.getWgs84());
    builder.addPoint(new Point(-79.497238, 8.849289));
    builder.addPoint(new Point(-80.035568, 9.432302));
    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, SpatialReferences.getWgs84());

    // set a default symbol for features in the collection table
    SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFF0000FF, 2);
    SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.DIAGONAL_CROSS, 0xFF00FFFF, 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(SpatialReferences.getWgs84());
    builder.addPoint(new Point(-79.497238, 8.849289));
    builder.addPoint(new Point(-79.337936, 8.638903));
    builder.addPoint(new Point(-79.11409, 8.895422));
    Feature addedFeature = polygonTable.createFeature(attributes, builder.toGeometry());

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

  @Override
  protected void onPause() {
    mMapView.pause();
    super.onPause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mMapView.resume();
  }

  @Override
  protected void onDestroy() {
    mMapView.dispose();
    super.onDestroy();
  }
}

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