Introduction to the Spatial DataFrame | ArcGIS for Developers
This article mentions that a Spatial DataFrame can be created from a local feature class using the ArcGIS API for python:
"If the ArcPy module is installed, meaning you have installed ArcGIS Pro and have installed the ArcGIS API for Python in that same environment, the SpatialDataFrame has methods to read a subset of the ArcGIS Desktop supported geographic formats, most notably:
feature classes
shapefiles,
ArcGIS Server Web Services and ArcGIS Online Hosted Feature Layers
OGC Services "
The article goes through an example where they construct a SDF using a local path to a shape file, but I'd like to build a spatial dataframe from a feature layer coming from a map in a ArcGIS pro project. Does anyone know how one could do this?
I've looked into the documentation for the API. The arcgis.features module has the SpatialDataFrame object and a constructor method called "from_layer", but I am not sure how to pass a FeatureLayer object to it. Is there a way to construct a FeatureLayer object from a local feature layer?
I was able to host the feature layer on AGOL and then create the spatial data frame from there, but I'd like to bypass having to host it.
Solved! Go to Solution.
Hi Brent Davis,
Try using the from_featureclass() method - this one supports featureclass-like entities including shapefiles, and feature layers from a map in an ArcGIS Pro project.
As an example - running this code within an ArcGIS Pro session should create a spatial dataframe from the first layer in the first map of the current project.
import arcgis,arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT")
project_map = aprx.listMaps()[0]
lyr = project_map.listLayers()[0]
sdf = arcgis.features.SpatialDataFrame.from_featureclass(lyr)
Hope that helps!
Cheers,
James
or you can use the most updated module called GeoAccessor and GeoSeriesAccessor to achieve this:
from arcgis.features import GeoAccessor, GeoSeriesAccessor
from arcgis.gis import GIS
import pandas as pd
import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT" or path to aprx)
project_map = aprx.listMaps("map name")[0]
lyr = project_map.listLayers("layer name")[0]
sdf = pd.DataFrame.spatial.from_featureclass(lyr)
for more information about GeoAccessor refer to the link below:
Hi Brent Davis,
Try using the from_featureclass() method - this one supports featureclass-like entities including shapefiles, and feature layers from a map in an ArcGIS Pro project.
As an example - running this code within an ArcGIS Pro session should create a spatial dataframe from the first layer in the first map of the current project.
import arcgis,arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT")
project_map = aprx.listMaps()[0]
lyr = project_map.listLayers()[0]
sdf = arcgis.features.SpatialDataFrame.from_featureclass(lyr)
Hope that helps!
Cheers,
James
Awesome! Thank you.
or you can use the most updated module called GeoAccessor and GeoSeriesAccessor to achieve this:
from arcgis.features import GeoAccessor, GeoSeriesAccessor
from arcgis.gis import GIS
import pandas as pd
import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT" or path to aprx)
project_map = aprx.listMaps("map name")[0]
lyr = project_map.listLayers("layer name")[0]
sdf = pd.DataFrame.spatial.from_featureclass(lyr)
for more information about GeoAccessor refer to the link below:
Thank you. Works beautifully.