Working with Feature Layers and Features

The feature layer is the primary concept for working with features in a GIS.

Users create, import, export, analyze, edit, and visualize features, i.e. “entities in space” as feature layers.

Feature layers can be added to and visualized using maps. They act as inputs to and outputs from feature analysis tools.

Feature layers are created by publishing feature data to a GIS, and are exposed as a broader resource (Item) in the GIS. Feature layer instances can be obtained through the layers attribute on feature layer collection Items in the GIS. A feature layer collection is a collection of feature layers and tables, with the associated relationships among the entities. A feature layer collection is backed by a feature service in a web GIS. See What is a feature service? for more details.

Accessing Feature Layers

Feature layer collection items are available as content in the GIS. You can search the GIS for feature layer collection items, or get them using their item id.

Feature layers are available through the layers attribute on feature layer collection Items in the GIS.

Searching the GIS for feature layers

You can search the GIS for feature layer collections by specifying the item type as 'Feature Layer Collection' or 'Feature Layer'.

Note: A feature layer collection can be considered a type of feature layer such as a group feature layer. Hence, you can specify the item type as 'Feature Layer' and still get back feature layer collection items as results.

The examples below will clarify this further:

# Establish a connection to your GIS.
from arcgis.gis import GIS
from IPython.display import display
gis = GIS() # anonymous connection to www.arcgis.com
# Search for 'USA major cities' feature layer collection
search_results = gis.content.search('title: USA Major Cities',
                                    'Feature Layer')

# Access the first Item that's returned
major_cities_item = search_results[0]

major_cities_item
USA Major Cities
This layer presents the locations of cities within the United States with populations of approximately 10,000 or greater, all state capitals, and the national capital.Feature Layer Collection by esri_dm
Last Modified: May 19, 2020
3 comments, 35821202 views

Note that the major_cities_item is a 'Feature Layer Collection' item. Since this item is a Feature Layer Collection, accessing the layers property will give us a list of FeatureLayer objects.

major_cities_layers = major_cities_item.layers
major_cities_layers
[<FeatureLayer url:"https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Major_Cities/FeatureServer/0">]

Accessing feature layers using item id

Feature layer collection items are available as content in the GIS. You can get them using their item id, and query their layers property to get to the feature layers:

freeways = gis.content.get('91c6a5f6410b4991ab0db1d7c26daacb')
freeways
USA Freeway System
This layer presents rural and urban interstate highways.Feature Layer Collection by esri_dm
Last Modified: January 07, 2020
2 comments, 4321723 views

Since freeways is a Feature Layer Collection item, accessing the layers property will give us a list of FeatureLayer objects. This item has two layers:

freeways.layers 
[<FeatureLayer url:"https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Freeway_System/FeatureServer/1">,
 <FeatureLayer url:"https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Freeway_System/FeatureServer/2">]

The code below cycles through the layers and prints their names:

for lyr in freeways.layers:
    print(lyr.properties.name)
USA Freeway System (over 1:500k)
USA Freeway System (below 1:500k)

Accessing feature layers and tables from feature services

A feature service serves a collection of feature layers and tables, with the associated relationships among the entities. It is represented by arcgis.features.FeatureLayerCollection in the ArcGIS Python API.

Instances of FeatureLayerCollection can be constructed using a feature service url, as shown below:

from arcgis.features import FeatureLayerCollection
fs_url = 'https://services2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/rest/services/SF_311_Incidents/FeatureServer'
sanfran = FeatureLayerCollection(fs_url)

The collection of layers and tables in a FeatureLayerCollection can be accessed using the layers and tables properties respectively:

sanfran.layers
[<FeatureLayer url:"https://services2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/rest/services/SF_311_Incidents/FeatureServer/0">]

Tables represent entity classes with uniform properties. In addition to working with “entities with location” as features, the GIS can also work with non-spatial entities as rows in tables. Working with tables is similar to working with feature layers, except that the rows (Features) in a table do not have a geometry, and tables ignore any geometry related operation.

The sanfran feature layer collection also has a table that can be obtained using its tables property:

sanfran.tables
[<Table url:"https://services2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/rest/services/SF_311_Incidents/FeatureServer/1">]

Accessing feature layers from a feature layer url

Instances of FeatureLayers can also be constructed using a url to the REST endpoint of a feature layer:

from arcgis.features import FeatureLayer
lyr_url = 'https://services2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/rest/services/SF_311_Incidents/FeatureServer/0'

layer = FeatureLayer(lyr_url)
layer
<FeatureLayer url:"https://services2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/rest/services/SF_311_Incidents/FeatureServer/0">

Properties of FeatureLayer

In this section, let us take a closer look at the properties of a FeatureLayer object. We will use the major_cities_layers object created earlier

feature_layer = major_cities_item.layers[0]
feature_layer
<FeatureLayer url:"https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Major_Cities/FeatureServer/0">

The properties field on a FeatureLayer object provides a dictionary representation of all its properties. However you can access individual properties as fields as well:

feature_layer.properties.extent
{
  "xmin": -17608123.3895845,
  "ymin": 2237818.89912024,
  "xmax": -7656889.54512499,
  "ymax": 9568526.08684907,
  "spatialReference": {
    "wkid": 102100,
    "latestWkid": 3857
  }
}

The capabilities property is useful to know what kinds of edits and operations be performed on the feature layer

feature_layer.properties.capabilities
'Query'

You can access the rendering information from the drawingInfo property

feature_layer.properties.drawingInfo.renderer.type
'classBreaks'

Querying feature layers

Querying is a powerful operation that can be performed on a FeatureLayer object. Let's take a closer look here. To write meaningful queries, we need to know the names of fields present in the layer. This can be determined by calling the fields property:

for f in feature_layer.properties.fields:
    print(f['name'])
FID
NAME
CLASS
ST
STFIPS
PLACEFIPS
CAPITAL
POP_CLASS
POPULATION
POP2010
WHITE
BLACK
AMERI_ES
ASIAN
HAWN_PI
HISPANIC
OTHER
MULT_RACE
MALES
FEMALES
AGE_UNDER5
AGE_5_9
AGE_10_14
AGE_15_19
AGE_20_24
AGE_25_34
AGE_35_44
AGE_45_54
AGE_55_64
AGE_65_74
AGE_75_84
AGE_85_UP
MED_AGE
MED_AGE_M
MED_AGE_F
HOUSEHOLDS
AVE_HH_SZ
HSEHLD_1_M
HSEHLD_1_F
MARHH_CHD
MARHH_NO_C
MHH_CHILD
FHH_CHILD
FAMILIES
AVE_FAM_SZ
HSE_UNITS
VACANT
OWNER_OCC
RENTER_OCC

The query method has a number of parameters that allow you to refine and transform the results. Since the processing is performed on the server, this operation is not restricted by the capacity of the client computer.

For instance, let us select all the cities whose population in the year 2010 was greater than 1 million. Instead of returning all the fields, let us get only population related fields

query_result1 = feature_layer.query(where='POP2010>1000000', 
                                    out_fields='WHITE,BLACK,MULT_RACE,HISPANIC')
len(query_result1.features)
9
query_result1.fields
[{'name': 'FID',
  'type': 'esriFieldTypeOID',
  'alias': 'FID',
  'sqlType': 'sqlTypeInteger',
  'domain': None,
  'defaultValue': None},
 {'name': 'WHITE',
  'type': 'esriFieldTypeInteger',
  'alias': 'WHITE',
  'sqlType': 'sqlTypeInteger',
  'domain': None,
  'defaultValue': None},
 {'name': 'BLACK',
  'type': 'esriFieldTypeInteger',
  'alias': 'BLACK',
  'sqlType': 'sqlTypeInteger',
  'domain': None,
  'defaultValue': None},
 {'name': 'MULT_RACE',
  'type': 'esriFieldTypeInteger',
  'alias': 'MULT_RACE',
  'sqlType': 'sqlTypeInteger',
  'domain': None,
  'defaultValue': None},
 {'name': 'HISPANIC',
  'type': 'esriFieldTypeInteger',
  'alias': 'HISPANIC',
  'sqlType': 'sqlTypeInteger',
  'domain': None,
  'defaultValue': None}]

If we are only interested in the count, we could save bandwidth by setting the return_count_only to True

feature_layer.query(where='POP2010>1000000', return_count_only=True)
9

Querying features using a different spatial reference

query_result1.spatial_reference
{'wkid': 102100, 'latestWkid': 3857}

By default, the query results are in the same spatial reference as the source layer. However you can use the out_sr parameter to reproject the result into a desired spatial reference. The projection happens on the server and on all the resulting features.

In the example above, we obtained data in wkid:3857, a well known id for 'Web Mercator' projection. We can observe how the coordinates look like below:

query_result1.features[0].geometry
{'x': -9756835.705284344,
 'y': 5124572.044517264,
 'spatialReference': {'wkid': 102100, 'latestWkid': 3857}}

The coordinates are in projected coordinate system as expected. If we wish to have this data in latitude and longitude instead, we could do so by changing the out_sr to wkid:4326

FeatureSet properties

As seen previously, a FeatureSet is returned by a query() operation. The FeatureSet object packs a bunch of useful properties that help us discern useful information about the features under access

One of the important properties is the spatial_reference as you saw earlier. Below, we are using the same query_result1 FeatureSet from earlier query operation.

query_result1.spatial_reference
{'wkid': 102100, 'latestWkid': 3857}

One of the most powerful operation on a FeatureSet is accessing the features not as Feature objects, but as pandas dataframe objects. The sdf property, returns a dataframe object:

query2 = feature_layer.query(where="POP2010 > 1000000")
query2.sdf
FIDNAMECLASSSTSTFIPSPLACEFIPSCAPITALPOP_CLASSPOPULATIONPOP2010...MARHH_NO_CMHH_CHILDFHH_CHILDFAMILIESAVE_FAM_SZHSE_UNITSVACANTOWNER_OCCRENTER_OCCSHAPE
063ChicagocityIL1717140001027811162695598...168435283321165555767933.41194337148777469562575998{"x": -9756835.705284344, "y": 5124572.0445172...
1415Los AngelescityCA0606440001039864423792621...245056469081189398073263.53141399595827503863814305{"x": -13165820.99345245, "y": 4035892.5853389...
2550San DiegocityCA0606660001013978561307402...10513412062338652852213.2851603332941233158249934{"x": -13040584.546974456, "y": 3858240.841215...
3910PhoenixcityAZ040455000State1016013811445632...10204723353544523307623.4259014975343296742218064{"x": -12476005.927345268, "y": 3954668.990210...
42174New YorkcityNY3636510001086915998175133...5664847706134475518502213.3233710622612789628922146892{"x": -8238770.183515309, "y": 4969744.1655956...
52987PhiladelphiacityPA4242600001015877611526006...9531018486869463403543.267017170435324536275200{"x": -8366882.6254769275, "y": 4858876.345453...
63592DallascityTX4848190001013236511197816...7690214512502422655383.4251663958582201880256177{"x": -10775254.59703981, "y": 3865959.6760583...
73641HoustoncityTX4848350001023332852099451...14547826039850144815703.38892646110003355236427407{"x": -10616262.315905476, "y": 3472578.411494...
83726San AntoniocityTX4848650001014424721327407...10031416067569013180433.3452424644604271070208572{"x": -10964134.924024679, "y": 3429663.909911...

9 rows × 50 columns

Accessing the features as a dataframe makes if easier to analyze the data statistically.

Accessing Features from query results

query_geographic = feature_layer.query(where='POP2010 > 1000000', out_sr='4326')
query_geographic.features[0].geometry
{'x': -87.64714638699998,
 'y': 41.75648806100002,
 'spatialReference': {'wkid': 4326, 'latestWkid': 4326}}

We can execute the query() method on the first FeatureLayer object and get a FeatureSet. Let us query and access the first 10 features in this layer

major_cities_l1 = major_cities_item.layers[0]
major_cities_l1_fset = major_cities_l1.query(where= 'FID < 11')
type(major_cities_l1_fset)
arcgis.features.feature.FeatureSet

Now, accessing the features property of the above FeatureSet object will provide us the individual point Features.

major_cities_l1_features = major_cities_l1_fset.features
len(major_cities_l1_features)
10

Accessing Feature geometry and attributes

As mentioned earlier, the Feature object is a fine grained representation of spatial information. Two important properties of a Feature object are its geometry and attributes:

Let us display the geometry and attributes of the first feature

major_cities_l1_features[0].geometry
{'x': -12462673.723706165,
 'y': 5384674.994080178,
 'spatialReference': {'wkid': 102100, 'latestWkid': 3857}}
major_cities_l1_features[0].attributes
{'FID': 1,
 'NAME': 'Ammon',
 'CLASS': 'city',
 'ST': 'ID',
 'STFIPS': '16',
 'PLACEFIPS': '1601990',
 'CAPITAL': ' ',
 'POP_CLASS': 6,
 'POPULATION': 15181,
 'POP2010': 13816,
 'WHITE': 13002,
 'BLACK': 73,
 'AMERI_ES': 67,
 'ASIAN': 113,
 'HAWN_PI': 9,
 'HISPANIC': 884,
 'OTHER': 307,
 'MULT_RACE': 245,
 'MALES': 6750,
 'FEMALES': 7066,
 'AGE_UNDER5': 1468,
 'AGE_5_9': 1503,
 'AGE_10_14': 1313,
 'AGE_15_19': 1058,
 'AGE_20_24': 734,
 'AGE_25_34': 2031,
 'AGE_35_44': 1767,
 'AGE_45_54': 1446,
 'AGE_55_64': 1136,
 'AGE_65_74': 665,
 'AGE_75_84': 486,
 'AGE_85_UP': 209,
 'MED_AGE': 29.6,
 'MED_AGE_M': 28,
 'MED_AGE_F': 30.8,
 'HOUSEHOLDS': 4476,
 'AVE_HH_SZ': 3.05,
 'HSEHLD_1_M': 457,
 'HSEHLD_1_F': 648,
 'MARHH_CHD': 1618,
 'MARHH_NO_C': 1131,
 'MHH_CHILD': 106,
 'FHH_CHILD': 335,
 'FAMILIES': 3352,
 'AVE_FAM_SZ': 3.61,
 'HSE_UNITS': 4747,
 'VACANT': 271,
 'OWNER_OCC': 3205,
 'RENTER_OCC': 1271}

Using feature collections

Similar to feature layers, feature collections can also be used to store features. With a feature collection, a service is not created to serve out feature data. Instead it is stored as json data with the item. Feature collections can be added to maps as layers, passed as input to feature analysis tools and queried for feature data. See the featureCollection for additional details.

Feature collections are shared in the GIS as items. Feature Collection Items can be searched by specifying 'Feature Collection' as the item_type.

Let us search for feature collection items published by Esri Media as an example:

Searching for Feature Collections

search_fc = gis.content.search("owner:EsriMedia", item_type='Feature Collection', max_items=20)
em_fc_item = search_fc[1]
em_fc_item
Crime Potential (Esri)
2014 Crime Potential (Esri)Feature Collection by EsriMedia
Last Modified: July 24, 2015
0 comments, 233 views

Accessing the layers property on a feature collection item returns a list of FeatureCollection objects

em_fc_item.layers
[<FeatureCollection>]
crime_pot_fc = em_fc_item.layers[0]

Querying the Feature Collection

You can call the query() method on a FeatureCollection object to get a FeatureSet.

crime_fset = crime_pot_fc.query()

Accessing features from a Feature Collection

Once you have a FeatureSet object, you can access the features property to get a list of Feature objects as seen earlier

crime_features = crime_fset.features
crime_features[0].geometry
{'rings': [[[-13162769, 4027325],
   [-13162572, 4027320],
   [-13162548, 4027353],
   [-13162307, 4027204],
   [-13162293, 4027223],
   [-13162309, 4027351],
   [-13162179, 4027349],
   [-13162165, 4027265],
   [-13162142, 4027265],
   [-13162097, 4027023],
   [-13162120, 4027016],
   [-13162117, 4026993],
   [-13162001, 4026344],
   [-13161974, 4026340],
   [-13161800, 4025409],
   [-13161828, 4025409],
   [-13161809, 4025235],
   [-13161662, 4024432],
   [-13161632, 4024432],
   [-13161536, 4024043],
   [-13161424, 4023822],
   [-13161405, 4023733],
   [-13161505, 4023717],
   [-13161459, 4023479],
   [-13161486, 4023475],
   [-13161482, 4023448],
   [-13161625, 4023452],
   [-13161625, 4023340],
   [-13161696, 4023422],
   [-13161785, 4023452],
   [-13162794, 4023452],
   [-13162836, 4023413],
   [-13162883, 4023452],
   [-13165214, 4023450],
   [-13165223, 4027359],
   [-13163604, 4027386],
   [-13163280, 4027371],
   [-13163256, 4027336],
   [-13162769, 4027325]]],
 'spatialReference': {'wkid': 102100, 'latestWkid': 3857}}

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