Feature collection layer

View on GitHubSample viewer app

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

screenshot

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, Map::operationalLayers()::append(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::tables()::append(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

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

Tags

collection, feature, layers, table

Sample Code

Feature_Collection_Layer.cppFeature_Collection_Layer.cppFeature_Collection_Layer.hFeature_Collection_Layer.qml
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
// [WriteFile Name=Feature_Collection_Layer, Category=Layers]
// [Legal]
// 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.
// [Legal]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

// sample headers
#include "Feature_Collection_Layer.h"

// ArcGIS Maps SDK headers
#include "AttributeListModel.h"
#include "Basemap.h"
#include "Envelope.h"
#include "Feature.h"
#include "FeatureCollection.h"
#include "FeatureCollectionLayer.h"
#include "FeatureCollectionTable.h"
#include "FeatureCollectionTableListModel.h"
#include "Field.h"
#include "LayerListModel.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MapTypes.h"
#include "MapViewTypes.h"
#include "Point.h"
#include "PolygonBuilder.h"
#include "PolylineBuilder.h"
#include "SimpleFillSymbol.h"
#include "SimpleLineSymbol.h"
#include "SimpleMarkerSymbol.h"
#include "SimpleRenderer.h"
#include "SpatialReference.h"
#include "SymbolTypes.h"
#include "Viewpoint.h"

// Qt headers
#include <QFuture>

using namespace Esri::ArcGISRuntime;

Feature_Collection_Layer::Feature_Collection_Layer(QQuickItem* parent /* = nullptr */):
  QQuickItem(parent)
{
}

Feature_Collection_Layer::~Feature_Collection_Layer() = default;

void Feature_Collection_Layer::init()
{
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<Feature_Collection_Layer>("Esri.Samples", 1, 0, "Feature_Collection_LayerSample");
}

void Feature_Collection_Layer::componentComplete()
{
  QQuickItem::componentComplete();

  // find QML MapView component
  m_mapView = findChild<MapQuickView*>("mapView");
  m_mapView->setWrapAroundMode(WrapAroundMode::Disabled);

  // Create a map using the topographic basemap
  m_map = new Map(BasemapStyle::ArcGISOceans, this);
  m_map->setInitialViewpoint(Viewpoint(Envelope(-8917856.590171767, 903277.583136797, -8800611.655131537, 1100327.8941287803, SpatialReference(102100))));

  // Set map to map view
  m_mapView->setMap(m_map);

  // Create FeatureCollection and FeatureCollectionLayer
  m_featureCollection = new FeatureCollection(this);
  m_featureCollectionLayer = new FeatureCollectionLayer(m_featureCollection, this);

  // Add the Layer to the Map
  m_map->operationalLayers()->append(m_featureCollectionLayer);

  // Create the FeatureCollectionTables
  createPointTable();
  createPolylineTable();
  createPolygonTable();
}

void Feature_Collection_Layer::createPointTable()
{
  // Define the schema
  QList<Field> pointFields;
  Field placeField = Field::createText("Place", "Place Name", 50);
  pointFields.append(placeField);

  // Create the table
  FeatureCollectionTable* pointsTable =  new FeatureCollectionTable(pointFields, GeometryType::Point, SpatialReference(4326), this);

  // Define the renderer
  SimpleMarkerSymbol* sms = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Triangle, QColor("red"), 18.0, this);
  SimpleRenderer* renderer = new SimpleRenderer(sms, this);
  pointsTable->setRenderer(renderer);

  // Add the table to the collection
  m_featureCollection->tables()->append(pointsTable);

  // Add features to the table
  Point pt1(-79.497238, 8.849289, SpatialReference::wgs84());
  addPointToTable("Place", "Current location", pt1, pointsTable);
}

void Feature_Collection_Layer::createPolylineTable()
{
  // Define the schema
  QList<Field> lineFields;
  Field boundaryField = Field::createText("Boundary", "Boundary Name", 50);
  lineFields.append(boundaryField);

  // Create the table
  FeatureCollectionTable* linesTable =  new FeatureCollectionTable(lineFields, GeometryType::Polyline, SpatialReference(4326), this);

  // Define the renderer
  SimpleLineSymbol* sls = new SimpleLineSymbol(SimpleLineSymbolStyle::Dash, QColor("green"), 3.0, this);
  SimpleRenderer* renderer = new SimpleRenderer(sls, this);
  linesTable->setRenderer(renderer);

  // Add the table to the collection
  m_featureCollection->tables()->append(linesTable);

  // Add features to the table
  Point pt1(-79.497238, 8.849289, SpatialReference::wgs84());
  Point pt2(-80.035568, 9.432302, SpatialReference::wgs84());
  QList<Point> points { pt1, pt2 };
  addPolylineToTable("Boundary", "AManAPlanACanalPanama", points, linesTable);
}

void Feature_Collection_Layer::createPolygonTable()
{
  // Define the schema
  QList<Field> polyFields;
  Field areaField = Field::createText("Area", "Area Name", 50);
  polyFields.append(areaField);

  // Create the table
  FeatureCollectionTable* polysTable =  new FeatureCollectionTable(polyFields, GeometryType::Polygon, SpatialReference(4326), this);

  // Define the renderer
  SimpleLineSymbol* sls = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor("blue"), 2.0, this);
  SimpleFillSymbol* sfs = new SimpleFillSymbol(SimpleFillSymbolStyle::DiagonalCross, QColor("cyan"), sls, this);
  SimpleRenderer* renderer = new SimpleRenderer(sfs, this);
  polysTable->setRenderer(renderer);

  // Add the table to the collection
  m_featureCollection->tables()->append(polysTable);

  // Add features to the table
  const Point pt1(-79.497238, 8.849289, SpatialReference::wgs84());
  const Point pt2(-79.337936, 8.638903, SpatialReference::wgs84());
  const Point pt3(-79.11409, 8.895422, SpatialReference::wgs84());
  const QList<Point> points{ pt1, pt2, pt3 };
  addPolygonToTable("Area", "Restricted area", points, polysTable);
}

void Feature_Collection_Layer::addPointToTable(QString attrName, QString attrValue, Point point, FeatureCollectionTable* table)
{
  // Create the new feature
  Feature* feature = table->createFeature(this);
  feature->attributes()->replaceAttribute(attrName, attrValue);
  feature->setGeometry(point);

  // Add the feature to the table
  auto future = table->addFeatureAsync(feature);
  Q_UNUSED(future)
}

void Feature_Collection_Layer::addPolylineToTable(QString attrName, QString attrValue, QList<Point> points, FeatureCollectionTable* table)
{
  // Create the new feature
  Feature* feature = table->createFeature(this);
  feature->attributes()->replaceAttribute(attrName, attrValue);
  PolylineBuilder* builder = new PolylineBuilder(SpatialReference(4326), this);
  for (const Point& pt : points)
  {
    builder->addPoint(pt);
  }
  feature->setGeometry(builder->toGeometry());

  // Add the feature to the table
  auto future = table->addFeatureAsync(feature);
  Q_UNUSED(future)
}

void Feature_Collection_Layer::addPolygonToTable(QString attrName, QString attrValue, QList<Point> points, FeatureCollectionTable* table)
{
  // Create the new feature
  Feature* feature = table->createFeature(this);
  feature->attributes()->replaceAttribute(attrName, attrValue);
  PolygonBuilder* builder = new PolygonBuilder(SpatialReference(4326), this);
  for (const Point& pt : points)
  {
    builder->addPoint(pt);
  }
  feature->setGeometry(builder->toGeometry());

  // Add the feature to the table
  auto future = table->addFeatureAsync(feature);
  Q_UNUSED(future)
}

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