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 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
- Create a
FeatureCollectionLayer
using a new feature collection,FeatureCollectionLayer(featureCollection)
- Add the feature collection layer to the map,
ArcGISMap.getOperationalLayers().add(featureCollectionLayer)
. - Create a
FeatureCollectionTable
for theGeometryType
sPoint
,Polyline
, andPolygon
,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 namedname
is added.
- Additionally, pass in a list of
- Assign a
SimpleRenderer
to each table to render anyFeature
s from that table using theSymbol
that was set. - Add the feature collection table to the feature collection,
FeatureCollection.getTables().add(featureCollectionTable)
. - 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)
. - Add new features to the table,
FeatureCollectionTable.addFeatureAsync(feature)
.
Relevant API
- ArcGISFeature
- FeatureCollection
- FeatureCollectionLayer
- FeatureCollectionTable
- Field
- SimpleRenderer
Tags
collection, feature, layers, table
Sample Code
/*
* 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.
*/
package com.esri.samples.feature_collection_layer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.FeatureCollection;
import com.esri.arcgisruntime.data.FeatureCollectionTable;
import com.esri.arcgisruntime.data.Field;
import com.esri.arcgisruntime.geometry.GeometryType;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.PolygonBuilder;
import com.esri.arcgisruntime.geometry.PolylineBuilder;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.layers.FeatureCollectionLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol.Style;
import com.esri.arcgisruntime.symbology.SimpleRenderer;
public class FeatureCollectionLayerSample extends Application {
private MapView mapView;
private static final SpatialReference WGS84 = SpatialReferences.getWgs84();
@Override
public void start(Stage stage) {
try {
// create stack pane and application scene
StackPane stackPane = new StackPane();
Scene scene = new Scene(stackPane);
// set title, size, and add scene to stage
stage.setTitle("Feature Collection Layer Sample");
stage.setWidth(800);
stage.setHeight(700);
stage.setScene(scene);
stage.show();
// authentication with an API key or named user is required to access basemaps and other location services
String yourAPIKey = System.getProperty("apiKey");
ArcGISRuntimeEnvironment.setApiKey(yourAPIKey);
// create a map with the oceans basemap style
ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_OCEANS);
// create a map view and set the map to it
mapView = new MapView();
mapView.setMap(map);
// set initial location for view
mapView.setViewpointCenterAsync(new Point(-79.497238, 8.849289, WGS84), 1000000);
// create feature collection and add to the map as a layer
FeatureCollection featureCollection = new FeatureCollection();
FeatureCollectionLayer featureCollectionLayer = new FeatureCollectionLayer(featureCollection);
map.getOperationalLayers().add(featureCollectionLayer);
// add point, line, and polygon geometry to feature collection
createPointTable(featureCollection);
createPolylineTable(featureCollection);
createPolygonTables(featureCollection);
// add the map view to stack pane
stackPane.getChildren().addAll(mapView);
} catch (Exception e) {
// on any error, display the stack trace.
e.printStackTrace();
}
}
/**
* Creates a Point Feature Collection Table with one Point and adds it to the Feature collection that was passed.
*
* @param featureCollection that the point Feature Collection Table will be added to
*/
private void createPointTable(FeatureCollection featureCollection) {
// defines the schema for the geometry's attribute
List<Field> pointFields = new ArrayList<>();
pointFields.add(Field.createString("Place", "Place Name", 50));
// a feature collection table that creates point geometry
FeatureCollectionTable pointsTable = new FeatureCollectionTable(pointFields, GeometryType.POINT, WGS84);
// set a default symbol for features in the collection table
SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(Style.TRIANGLE, 0xFFFF0000, 18);
SimpleRenderer renderer = new SimpleRenderer(markerSymbol);
pointsTable.setRenderer(renderer);
// add feature collection table to feature collection
featureCollection.getTables().add(pointsTable);
// create feature using the collection table by passing an attribute and geometry
Map<String, Object> attributes = new HashMap<>();
attributes.put(pointFields.get(0).getName(), "Current Location");
Point point = new Point(-79.497238, 8.849289, WGS84);
Feature addedFeature = pointsTable.createFeature(attributes, point);
// add feature to collection table
pointsTable.addFeatureAsync(addedFeature);
}
/**
* Creates a PolyLine Feature Collection Table with one PolyLine and adds it to the Feature collection that was passed.
*
* @param featureCollection that the polyline Feature Collection Table will be added to
*/
private void createPolylineTable(FeatureCollection featureCollection) {
// defines the schema for the geometry's attribute
List<Field> polylineFields = new ArrayList<>();
polylineFields.add(Field.createString("Boundary", "Boundary Name", 50));
// a feature collection table that creates polyline geometry
FeatureCollectionTable polylineTable = new FeatureCollectionTable(polylineFields, GeometryType.POLYLINE, WGS84);
// set a default symbol for features in the collection table
SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.DASH, 0xFF00FF00, 3);
SimpleRenderer renderer = new SimpleRenderer(lineSymbol);
polylineTable.setRenderer(renderer);
// add feature collection table to feature collection
featureCollection.getTables().add(polylineTable);
// create feature using the collection table by passing an attribute and geometry
Map<String, Object> attributes = new HashMap<>();
attributes.put(polylineFields.get(0).getName(), "AManAPlanACanalPanama");
PolylineBuilder builder = new PolylineBuilder(WGS84);
builder.addPoint(new Point(-79.497238, 8.849289, WGS84));
builder.addPoint(new Point(-80.035568, 9.432302, WGS84));
Feature addedFeature = polylineTable.createFeature(attributes, builder.toGeometry());
// add feature to collection table
polylineTable.addFeatureAsync(addedFeature);
}
/**
* Creates a Polygon Feature Collection Table with one Polygon and adds it to the Feature collection that was passed.
*
* @param featureCollection that the polygon Feature Collection Table will be added to
*/
private void createPolygonTables(FeatureCollection featureCollection) {
// defines the schema for the geometry's attribute
List<Field> polygonFields = new ArrayList<>();
polygonFields.add(Field.createString("Area", "Area Name", 50));
// a feature collection table that creates polygon geometry
FeatureCollectionTable polygonTable = new FeatureCollectionTable(polygonFields, GeometryType.POLYGON, WGS84);
// set a default symbol for features in the collection table
SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFF0000FF, 2);
SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.DIAGONAL_CROSS, 0xFF00FFFF, lineSymbol);
SimpleRenderer renderer = new SimpleRenderer(fillSymbol);
polygonTable.setRenderer(renderer);
// add feature collection table to feature collection
featureCollection.getTables().add(polygonTable);
// create feature using the collection table by passing an attribute and geometry
Map<String, Object> attributes = new HashMap<>();
attributes.put(polygonFields.get(0).getName(), "Restricted area");
PolygonBuilder builder = new PolygonBuilder(WGS84);
builder.addPoint(new Point(-79.497238, 8.849289, WGS84));
builder.addPoint(new Point(-79.337936, 8.638903, WGS84));
builder.addPoint(new Point(-79.11409, 8.895422, WGS84));
Feature addedFeature = polygonTable.createFeature(attributes, builder.toGeometry());
// add feature to collection table
polygonTable.addFeatureAsync(addedFeature);
}
/**
* Stops and releases all resources used in application.
*/
@Override
public void stop() {
if (mapView != null) {
mapView.dispose();
}
}
/**
* Opens and runs application.
*
* @param args arguments passed to this application
*/
public static void main(String[] args) {
Application.launch(args);
}
}