You will learn: how to use the ArcGIS API for Python to load a spatial data frame from a feature layer.

The Pandas Dataframe is a common data structure for data scientists. This tutorial demonstrates how to load a spatial Dataframe from a feature layer. The resulting spatial data frame can be used for visualization, to perform analysis, or to integrate with third party libraries.

Before you begin

The ArcGIS API for Python tutorials use Jupyter Notebooks to execute Python code. If you are new to this environment, please see the guide to install the API and use notebooks locally.

Make sure you complete the Import Data tutorial before beginning this tutorial.


Create a new notebook

  1. Go to Esri Juptyter Notebooks and click New > Python 3 to create a new notebook.

    In each step below, type (or copy and paste) the commands into a new notebook cell and run the code by clicking run cell or pressing shift + Enter.

  1. Add the following code in a cell to import the ArcGIS API for Python.

    from arcgis.gis import GIS
    from arcgis.features import SpatialDataFrame
  2. Log into ArcGIS Online by making a GIS connection to ArcGIS Online using your developer account. Replace username and password with your own credentials.

    gis = GIS("https://www.arcgis.com", "username", "password")
  3. Search for the Trailsheads feature layer you created as a challenge in the Import Data tutorial.

    search_results = gis.content.search(query='title: "Trailheads*" AND type: "Feature Service"')
  4. Retrieve the feature service item from the list of results. Then, get the layer from that service.

    feature_service_item = search_results[0]
    feature_layer = feature_service_item.layers[0]

Search for an item and construct a Spatial Data Frame

  1. Build the Pandas SpatialDataFrame.

    sdf = SpatialDataFrame.from_layer(feature_layer)

Congratulations, you're done!

Your notebook should now look something like this.


Export a SpatialDataFrame to a shapefile

Try saving the SpatialDataFrame to a file.

sdf.to_featureclass(out_location='path/to/save/data', out_name='file.shp')