Skip to content
View on GitHubSample viewer app

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 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.operationalLayers.add(featureCollectionLayer).
  3. Create a FeatureCollectionTable for the GeometryTypes Point, Polyline, and Polygon, FeatureCollectionTable(fields, geometryType, spatialReference)
  4. 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.
  5. Assign a SimpleRenderer to each table to render any Features from that table using the Symbol that was set.
  6. Add all the feature collection tables to the feature collection, FeatureCollection(featureCollectionTables = listOf(points, lines, polygons)).
  7. 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).
  8. Add new features to the table, FeatureCollectionTable.addFeature(feature).

Relevant API

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

Tags

collection, feature, layers, table

Sample Code

AddFeatureCollectionLayerFromTableViewModel.ktAddFeatureCollectionLayerFromTableViewModel.ktMainActivity.ktAddFeatureCollectionLayerFromTableScreen.kt
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
/* Copyright 2025 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.arcgismaps.sample.addfeaturecollectionlayerfromtable.components

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.arcgismaps.Color
import com.arcgismaps.data.FeatureCollection
import com.arcgismaps.data.FeatureCollectionTable
import com.arcgismaps.data.Field
import com.arcgismaps.data.FieldType
import com.arcgismaps.geometry.GeometryType
import com.arcgismaps.geometry.Polygon
import com.arcgismaps.geometry.Point
import com.arcgismaps.geometry.Polyline
import com.arcgismaps.geometry.SpatialReference
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.Viewpoint
import com.arcgismaps.mapping.layers.FeatureCollectionLayer
import com.arcgismaps.mapping.symbology.SimpleFillSymbol
import com.arcgismaps.mapping.symbology.SimpleFillSymbolStyle
import com.arcgismaps.mapping.symbology.SimpleLineSymbol
import com.arcgismaps.mapping.symbology.SimpleLineSymbolStyle
import com.arcgismaps.mapping.symbology.SimpleMarkerSymbol
import com.arcgismaps.mapping.symbology.SimpleMarkerSymbolStyle
import com.arcgismaps.mapping.symbology.SimpleRenderer
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import kotlinx.coroutines.launch

/**
 * ViewModel which creates three in-memory FeatureCollectionTables (point, polyline, polygon),
 * adds features to them, creates a FeatureCollection and displays it on the map as a
 * FeatureCollectionLayer.
 */
class AddFeatureCollectionLayerFromTableViewModel(app: Application) : AndroidViewModel(app) {

    // ArcGIS map exposed for Compose screen.
    val arcGISMap = ArcGISMap(BasemapStyle.ArcGISOceans).apply {
        initialViewpoint = Viewpoint(latitude = 8.849289, longitude = -79.497238, scale = 1e6)
    }

    // Message dialog view model for error handling
    val messageDialogVM = MessageDialogViewModel()

    init {
        // Load the map and then build the feature collection layer
        viewModelScope.launch {
            createAndAddFeatureCollectionLayer()
            arcGISMap.load()
                .onFailure { messageDialogVM.showMessageDialog(it) }
        }
    }

    private suspend fun createAndAddFeatureCollectionLayer() {

        // Create each feature collection table
        val pointsTable = createPointsCollectionTable()
        val linesTable = createLinesCollectionTable()
        val polygonsTable = createPolygonsCollectionTable()

        // Create a FeatureCollection from the tables
        val featureCollection = FeatureCollection(featureCollectionTables = listOf(pointsTable, linesTable, polygonsTable))

        // Create a layer from the feature collection and add it to the map
        val featureCollectionLayer = FeatureCollectionLayer(featureCollection = featureCollection)
        arcGISMap.operationalLayers.add(featureCollectionLayer)
    }

    /**
     * Create an in-memory points [FeatureCollectionTable], define a simple schema and renderer,
     * add a single point feature and return the table.
     */
    private suspend fun createPointsCollectionTable(): FeatureCollectionTable {

        // Define a simple text field for a place name
        val placeField = Field(
            fieldType = FieldType.Text,
            name = "Place",
            alias = "Place name",
            length = 40,
            isEditable = true,
            isNullable = false
        )

        // Create the feature collection table for points using WGS84 spatial reference
        val pointsCollectionTable = FeatureCollectionTable(
            fields = listOf(placeField),
            geometryType = GeometryType.Point,
            spatialReference = SpatialReference.wgs84()
        )

        // Set a simple renderer using a triangle marker symbol
        pointsCollectionTable.renderer = SimpleRenderer(
            symbol = SimpleMarkerSymbol(
                style = SimpleMarkerSymbolStyle.Triangle,
                color = Color.red,
                size = 18f
            )
        )

        // Create a point geometry (x = longitude, y = latitude) and a feature with attributes
        val pointGeometry = Point(x = -79.497238, y = 8.849289, spatialReference = SpatialReference.wgs84())
        val placeAttributes = mapOf("Place" to "Current location")

        // Create and add the feature to the table
        val pointFeature = pointsCollectionTable.createFeature(placeAttributes, pointGeometry)
        pointsCollectionTable.addFeature(pointFeature).onFailure { messageDialogVM.showMessageDialog(it) }

        return pointsCollectionTable
    }

    /**
     * Create an in-memory polyline FeatureCollectionTable with a dash renderer and a single line
     * feature between two points.
     */
    private suspend fun createLinesCollectionTable(): FeatureCollectionTable {
        val boundaryField = Field(
            fieldType = FieldType.Text,
            name = "Boundary",
            alias = "Boundary name",
            length = 40,
            isEditable = true,
            isNullable = false
        )

        val linesCollectionTable = FeatureCollectionTable(
            fields = listOf(boundaryField),
            geometryType = GeometryType.Polyline,
            spatialReference = SpatialReference.wgs84()
        )

        linesCollectionTable.renderer = SimpleRenderer(
            symbol = SimpleLineSymbol(
                style = SimpleLineSymbolStyle.Dash,
                color = Color.green,
                width = 3f
            )
        )

        val lineGeometry = Polyline(
            points = listOf(
                Point(x = -79.497238, y = 8.849289, spatialReference = SpatialReference.wgs84()),
                Point(x = -80.035568, y = 9.432302, spatialReference = SpatialReference.wgs84())
            )
        )

        val lineAttributes = mapOf("Boundary" to "AManAPlanACanalPanama")
        val lineFeature = linesCollectionTable.createFeature(lineAttributes, lineGeometry)
        linesCollectionTable.addFeature(lineFeature).onFailure { messageDialogVM.showMessageDialog(it) }

        return linesCollectionTable
    }

    /**
     * Create an in-memory polygon and add it to the FeatureCollectionTable as a single polygon feature.
     * A simple fill renderer is added to the FeatureCollectionTable to render the polygon.
     */
    private suspend fun createPolygonsCollectionTable(): FeatureCollectionTable {
        val areaField = Field(
            fieldType = FieldType.Text,
            name = "Area",
            alias = "Area name",
            length = 40,
            isEditable = true,
            isNullable = false
        )

        val polygonsCollectionTable = FeatureCollectionTable(
            fields = listOf(areaField),
            geometryType = GeometryType.Polygon,
            spatialReference = SpatialReference.wgs84()
        )

        // Create a simple fill symbol using a blue outline and cyan diagonal cross fill
        val outlineSymbol = SimpleLineSymbol(
            style = SimpleLineSymbolStyle.Solid,
            color = Color.blue,
            width = 2f
        )
        val fillSymbol = SimpleFillSymbol(
            style = SimpleFillSymbolStyle.DiagonalCross,
            color = Color.cyan,
            outline = outlineSymbol
        )
        polygonsCollectionTable.renderer = SimpleRenderer(symbol = fillSymbol)

        val polygonGeometry = Polygon(points = listOf(
            Point(x = -79.497238, y = 8.849289, spatialReference = SpatialReference.wgs84()),
            Point(x = -79.337936, y = 8.638903, spatialReference = SpatialReference.wgs84()),
            Point(x = -79.11409, y = 8.895422, spatialReference = SpatialReference.wgs84())
        ))

        val polygonAttributes = mapOf("Area" to "Restricted area")
        val polygonFeature = polygonsCollectionTable.createFeature(polygonAttributes, polygonGeometry)
        polygonsCollectionTable.addFeature(polygonFeature).onFailure { messageDialogVM.showMessageDialog(it) }

        return polygonsCollectionTable
    }
}

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