Create a feature collection layer from a feature collection table and add it to a map.
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
- Create an
AGSFeatureCollectionTablefor each of the geometry typesAGSPoint,AGSPolyline, andAGSPolygonusingAGSFeatureCollectionTable.init(fields:geometryType:spatialReference:).- Create the schema for each feature collection table by creating an array of
AGSFields. - Create an
AGSFeatureCollectionTablewith the fields created. - Create an
AGSSimpleRendererfrom anAGSSimpleMarkerSymbol. - Create a new point feature using
AGSFeatureTable.createFeature(). - Add the feature to the
AGSFeatureCollectionTable.
- Create the schema for each feature collection table by creating an array of
- Create an
AGSFeatureCollectionfrom theAGSFeatureCollectionTables. - Create an
AGSFeatureCollectionLayerusing theAGSFeatureCollection. - 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
//
// 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
}
}