Add feature collection layer from table

View on GitHub

Create a Feature Collection Layer from a Feature Collection Table, and add it to a map.

Image of add feature collection layer from table

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 ArcGISPoint, Polyline and Polygon geometry. Pan and zoom to explore the scene.

How it works

  1. Create a FeatureCollectionTable for the GeometryTypes ArcGISPoint, Polyline, and Polygon, using FeatureCollectionTable(fields, geometryType, spatialReference), passing in a list of Field objects to represent the table's schema, the GeometryType, and a SpatialReference.
  2. Assign a SimpleRenderer to each table to render any Features from that table using the Symbol that was set.
  3. Use the FeatureCollectionTable.createFeature(attributes, geometry) method to create a feature from the feature collection table, passing an attribute and geometry for that feature.
  4. Add new features to the table, FeatureCollectionTable.addFeature(feature).
  5. Add the feature collection table to the feature collection, FeatureCollection.tables.add(featureCollectionTable).
  6. Create a FeatureCollectionLayer using the feature collection, FeatureCollectionLayer.withFeatureCollection(featureCollection).
  7. Add the feature collection layer to the map, ArcGISMap.operationalLayers.add(featureCollectionLayer).

Relevant API

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

Tags

collection, feature, layers, table

Sample Code

add_feature_collection_layer_from_table.dart
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
//
// Copyright 2024 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
//
//   https://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.
//

import 'package:arcgis_maps/arcgis_maps.dart';
import 'package:flutter/material.dart';

import '../../utils/sample_state_support.dart';

class AddFeatureCollectionLayerFromTable extends StatefulWidget {
  const AddFeatureCollectionLayerFromTable({super.key});
  @override
  State<AddFeatureCollectionLayerFromTable> createState() =>
      _AddFeatureCollectionLayerFromTableState();
}

class _AddFeatureCollectionLayerFromTableState
    extends State<AddFeatureCollectionLayerFromTable> with SampleStateSupport {
  // Create a controller for the map view.
  final _mapViewController = ArcGISMapView.createController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // Add a map view to the widget tree and set a controller.
      body: ArcGISMapView(
        controllerProvider: () => _mapViewController,
        onMapViewReady: onMapViewReady,
      ),
    );
  }

  void onMapViewReady() {
    // Create a feature collection table for each of the geometry types Point, Polyline, and Polygon.
    final pointTable = createPointTable();
    final polylineTable = createPolylineTable();
    final polygonTable = createPolygonTable();

    // Create a new feature collection and add the feature collection tables.
    final featureCollection = FeatureCollection()
      ..tables.addAll([pointTable, polylineTable, polygonTable]);

    // Create a feature collection layer from the feature collection.
    final featureCollectionLayer =
        FeatureCollectionLayer.withFeatureCollection(featureCollection);

    // Create a map with a basemap style and initial viewpoint.
    final map = ArcGISMap.withBasemapStyle(BasemapStyle.arcGISOceans)
      ..initialViewpoint = Viewpoint.fromTargetExtent(
        Envelope.fromXY(
          xMin: -8917856.590171767,
          yMin: 903277.583136797,
          xMax: -8800611.655131537,
          yMax: 1100327.8941287803,
          spatialReference: SpatialReference(wkid: 102100),
        ),
      );

    // Add the feature collection layer to the map's operational layers.
    map.operationalLayers.add(featureCollectionLayer);

    // Set the map to the map view controller.
    _mapViewController.arcGISMap = map;
  }

  // Create a feature collection table using a point geometry.
  FeatureCollectionTable createPointTable() {
    // Create a feature collection table for the geometry type with a list of fields and a spatial reference.
    final pointTable = FeatureCollectionTable(
      fields: [Field.text(name: 'Place', alias: 'Place Name', length: 50)],
      geometryType: GeometryType.point,
      spatialReference: SpatialReference(wkid: 4326),
    );

    // Set a simple renderer to style features from the table.
    pointTable.renderer = SimpleRenderer(
      symbol: SimpleMarkerSymbol(
        style: SimpleMarkerSymbolStyle.triangle,
        color: Colors.red,
        size: 10,
      ),
    );

    // Create a point.
    final point = ArcGISPoint(
      x: -79.497238,
      y: 8.849289,
      spatialReference: SpatialReference.wgs84,
    );

    // Create a feature with the point and add it to the table.
    final feature = pointTable.createFeature(
      attributes: {'Place': 'Current location'},
      geometry: point,
    );
    pointTable.addFeature(feature);

    return pointTable;
  }

  // Create a feature collection table using a polyline geometry.
  FeatureCollectionTable createPolylineTable() {
    // Create a feature collection table for the geometry type with a list of fields and a spatial reference.
    final polylineTable = FeatureCollectionTable(
      fields: [
        Field.text(name: 'Boundary', alias: 'Boundary Name', length: 50),
      ],
      geometryType: GeometryType.polyline,
      spatialReference: SpatialReference(wkid: 4326),
    );

    // Set a simple renderer to style features from the table.
    polylineTable.renderer = SimpleRenderer(
      symbol: SimpleLineSymbol(
        style: SimpleLineSymbolStyle.dash,
        color: Colors.green,
        width: 3,
      ),
    );

    // Build a polyline.
    final polylineBuilder = PolylineBuilder(spatialReference: SpatialReference(wkid: 4326));
    polylineBuilder.addPoint(
      ArcGISPoint(
        x: -79.497238,
        y: 8.849289,
        spatialReference: SpatialReference.wgs84,
      ),
    );
    polylineBuilder.addPoint(
      ArcGISPoint(
        x: -80.035568,
        y: 9.432302,
        spatialReference: SpatialReference.wgs84,
      ),
    );
    final polyline = polylineBuilder.toGeometry();

    // Create a feature using the polyline and add it to the table.
    final feature = polylineTable.createFeature(
      attributes: {'Boundary': 'AManAPlanACanalPanama'},
      geometry: polyline,
    );
    polylineTable.addFeature(feature);

    return polylineTable;
  }

  // Create a feature collection table using a polygon geometry.
  FeatureCollectionTable createPolygonTable() {
    // Create a feature collection table for the geometry type with a list of fields and a spatial reference.
    final polygonTable = FeatureCollectionTable(
      fields: [Field.text(name: 'Area', alias: 'Area Name', length: 50)],
      geometryType: GeometryType.polygon,
      spatialReference: SpatialReference(wkid: 4326),
    );

    // Set a simple renderer to style features from the table.
    polygonTable.renderer = SimpleRenderer(
      symbol: SimpleFillSymbol(
        style: SimpleFillSymbolStyle.diagonalCross,
        color: Colors.cyan,
        outline: SimpleLineSymbol(
          style: SimpleLineSymbolStyle.solid,
          color: Colors.blue,
          width: 2,
        ),
      ),
    );

    // Build a polygon.
    final polygonBuilder = PolygonBuilder(spatialReference: SpatialReference(wkid: 4326));
    polygonBuilder.addPoint(
      ArcGISPoint(
        x: -79.497238,
        y: 8.849289,
        spatialReference: SpatialReference.wgs84,
      ),
    );
    polygonBuilder.addPoint(
      ArcGISPoint(
        x: -79.337936,
        y: 8.638903,
        spatialReference: SpatialReference.wgs84,
      ),
    );
    polygonBuilder.addPoint(
      ArcGISPoint(
        x: -79.11409,
        y: 8.895422,
        spatialReference: SpatialReference.wgs84,
      ),
    );
    final polygon = polygonBuilder.toGeometry();

    // Create a feature using the polygon and add it to the table.
    final feature = polygonTable.createFeature(
      attributes: {'Area': 'Restricted area'},
      geometry: polygon,
    );
    polygonTable.addFeature(feature);

    return polygonTable;
  }
}

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