You will learn: how to use the ArcGIS API for Python to load a spatial data frame from a feature layer.
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.
Make sure you complete the Import Data tutorial before beginning this tutorial.
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.
Add the following code in a cell to import the ArcGIS API for Python.
from arcgis.gis import GIS from arcgis.features import SpatialDataFrame
Log into ArcGIS Online by making a GIS connection to ArcGIS Online using your developer account. Replace
password with your own credentials.
gis = GIS("https://www.arcgis.com", "username", "password")
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"') search_results
Retrieve the feature service item from the list of results. Then, get the layer from that service.
feature_service_item = search_results feature_layer = feature_service_item.layers feature_layer
Build the Pandas
sdf = SpatialDataFrame.from_layer(feature_layer) sdf.head()
Your notebook should now look something like this.
SpatialDataFrameto a shapefile
Try saving the
SpatialDataFrame to a file.