Layer to Spatially Enabled DataFrame
As I continue to dive deeper and deeper into the intersection of Geography and Artifical Intelligence - what we are calling GeoAI, I find myself doing a lot of data work. This means I liberally blend the two Esri Python libraries, ArcPy and the Python API.
Many times I use ArcPy Layers to quickly select the data I need to be working with using either SelectLayerByLocation or SelectLayerByAttribute. This enables me to quickly reduce the data size for subsequent analysis without having to save a new dataset to disk, a common step when using GeoProcessing functions. From there I need the data in a Spatially Enabled DataFrame to continue my analysis work.
True, I definitely can use the spatial.from_featureclass
method. However, in a recent project, I was doing this a lot. To make my life easier, and clean up the code a little, I just created this little monkeypatch making it really easy to get from a Layer object instance diretly to a Spatially Enabled DataFrame.
From there, using the method is as simple as creating a Layer object instance, and outputting a Spatially Enabled DataFrame.
feature_class_path = r'./some/path/to/data.gdb/name'
# create an ArcPy feature layer
lyr = arcpy.management.MakeFeatureLayer(feature_class_path)[0]
# do something to select some data, and then output the remaining
# data to a Spatially Enabled DataFrame
sdf = lyr.sdf
Hopefully you find this useful, and it enables you to streamline a data pipeline a little as it did for me!