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.

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 be 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 an AGSFeatureCollectionLayer with an AGSPoint, AGSPolyline and AGSPolygon geometry. Pan and zoom to explore the scene.

How it works

  1. Create an AGSFeatureCollectionTable for each of the geometry types AGSPoint, AGSPolyline, and AGSPolygon using AGSFeatureCollectionTable.init(fields:geometryType:spatialReference:).

    1. Create the schema for each feature collection table by creating an array of AGSFields.
    2. Create an AGSFeatureCollectionTable with the fields created.
    3. Create an AGSSimpleRenderer from an AGSSimpleMarkerSymbol.
    4. Create a new point feature using AGSFeatureTable.createFeature().
    5. Add the feature to the AGSFeatureCollectionTable.
  2. Create an AGSFeatureCollection from the AGSFeatureCollectionTables.
  3. Create an AGSFeatureCollectionLayer using the AGSFeatureCollection.
  4. Add the feature collection layer to the map's, operationalLayers.

Relevant API

  • AGSFeature
  • AGSFeatureCollection
  • AGSFeatureCollectionLayer
  • AGSFeatureCollectionTable
  • AGSField
  • AGSSimpleRenderer

Tags

collection, feature, layers, table

Sample Code

FeatureCollectionLayerViewController.swift
                                                                                                                                              
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
//
// Copyright 2017 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.

import UIKit
import ArcGIS

class FeatureCollectionLayerViewController: UIViewController {
    @IBOutlet var mapView: AGSMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // add the source code button item to the right of navigation bar
        (self.navigationItem.rightBarButtonItem as! SourceCodeBarButtonItem).filenames = ["FeatureCollectionLayerViewController"]

        // initialize map with basemap
        let map = AGSMap(basemapStyle: .arcGISOceans)

        // assign map to the map view
        mapView.map = map

        // set viewpoint
        let point = AGSPoint(x: -79.497238, y: 8.849289, spatialReference: .wgs84())
        mapView.setViewpoint(AGSViewpoint(center: point, scale: 1.5e6))

        self.addFeatureCollectionLayer()
    }

    private func addFeatureCollectionLayer() {
        // feature collection table for point, polyline and polygon
        let pointsCollectionTable = self.pointsCollectionTable()
        let linesCollectionTable = self.linesCollectionTable()
        let polygonsCollectionTable = self.polygonsCollectionTable()

        // feature collection
        let featureCollection = AGSFeatureCollection(featureCollectionTables: [pointsCollectionTable, linesCollectionTable, polygonsCollectionTable])

        // feature collection layer
        let featureCollectionLayer = AGSFeatureCollectionLayer(featureCollection: featureCollection)

        // add layer to the map
        self.mapView.map?.operationalLayers.add(featureCollectionLayer)
    }

    private func pointsCollectionTable() -> AGSFeatureCollectionTable {
        // create schema for points feature collection table
        var fields = [AGSField]()
        let placeField = AGSField(fieldType: .text, name: "Place", alias: "Place name", length: 40, domain: nil, editable: true, allowNull: false)
        fields.append(placeField)

        // initialize feature collection table with the fields created
        // and geometry type as Point
        let pointsCollectionTable = AGSFeatureCollectionTable(fields: fields, geometryType: .point, spatialReference: .wgs84())

        // renderer
        let symbol = AGSSimpleMarkerSymbol(style: .triangle, color: .red, size: 18)
        pointsCollectionTable.renderer = AGSSimpleRenderer(symbol: symbol)

        // Create a new point feature, provide geometry and attribute values
        let pointFeature = pointsCollectionTable.createFeature()
        pointFeature.attributes["Place"] = "Current location"
        let point = AGSPoint(x: -79.497238, y: 8.849289, spatialReference: .wgs84())
        pointFeature.geometry = point

        // add feature to the feature collection table
        pointsCollectionTable.add(pointFeature, completion: nil)

        return pointsCollectionTable
    }

    private func linesCollectionTable() -> AGSFeatureCollectionTable {
        // create schema for points feature collection table
        var fields = [AGSField]()
        let boundaryField = AGSField(fieldType: .text, name: "Boundary", alias: "Boundary name", length: 40, domain: nil, editable: true, allowNull: false)
        fields.append(boundaryField)

        // initialize feature collection table with the fields created
        // and geometry type as Polyline
        let linesCollectionTable = AGSFeatureCollectionTable(fields: fields, geometryType: .polyline, spatialReference: .wgs84())

        // renderer
        let symbol = AGSSimpleLineSymbol(style: .dash, color: .green, width: 3)
        linesCollectionTable.renderer = AGSSimpleRenderer(symbol: symbol)

        // Create a new point feature, provide geometry and attribute values
        let lineFeature = linesCollectionTable.createFeature()
        lineFeature.attributes["Boundary"] = "AManAPlanACanalPanama"

        // geometry
        let point1 = AGSPoint(x: -79.497238, y: 8.849289, spatialReference: .wgs84())
        let point2 = AGSPoint(x: -80.035568, y: 9.432302, spatialReference: .wgs84())
        lineFeature.geometry = AGSPolyline(points: [point1, point2])

        // add feature to the feature collection table
        linesCollectionTable.add(lineFeature, completion: nil)

        return linesCollectionTable
    }

    private func polygonsCollectionTable() -> AGSFeatureCollectionTable {
        // create schema for points feature collection table
        var fields = [AGSField]()
        let areaField = AGSField(fieldType: .text, name: "Area", alias: "Area name", length: 40, domain: nil, editable: true, allowNull: false)
        fields.append(areaField)

        // initialize feature collection table with the fields created
        // and geometry type as Polygon
        let polygonsCollectionTable = AGSFeatureCollectionTable(fields: fields, geometryType: .polygon, spatialReference: .wgs84())

        // renderer
        let lineSymbol = AGSSimpleLineSymbol(style: .solid, color: .blue, width: 2)
        let fillSymbol = AGSSimpleFillSymbol(style: .diagonalCross, color: .cyan, outline: lineSymbol)
        polygonsCollectionTable.renderer = AGSSimpleRenderer(symbol: fillSymbol)

        // Create a new point feature, provide geometry and attribute values
        let polygonFeature = polygonsCollectionTable.createFeature()
        polygonFeature.attributes["Area"] = "Restricted area"

        // geometry
        let point1 = AGSPoint(x: -79.497238, y: 8.849289, spatialReference: .wgs84())
        let point2 = AGSPoint(x: -79.337936, y: 8.638903, spatialReference: .wgs84())
        let point3 = AGSPoint(x: -79.11409, y: 8.895422, spatialReference: .wgs84())
        polygonFeature.geometry = AGSPolygon(points: [point1, point2, point3])

        // add feature to the feature collection table
        polygonsCollectionTable.add(polygonFeature, completion: nil)

        return polygonsCollectionTable
    }
}

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