3

I've got a spatially enabled dataframe (SEDF) I'm trying to convert to a GeoJSON. Normally I do this as a 2 step process by creating a featureclass then converting that into a GeoJSON with SEDF.spatial.to_featureclass and arcpy.conversion.FeaturesToJSON; however, featureclasses can be cumbersome and it would be nice to skip this step altogether.

ESRI's SEDF website (https://developers.arcgis.com/python/guide/introduction-to-the-spatially-enabled-dataframe/) states that you can export an SEDF in many formats, including GeoJSON; however, I don't see an example of this process anywhere.

Does anyone know how this is done?

1 Answer 1

7

Neither the API documentation nor dir(pd.DataFrame.spatial) has any indication that there is a to_geojson() method. So there is no direct way to do this as far as I know.

However, instead of using to_featureclass(location) where you have to create an actual file, you could use to_featureset().

to_featureset() creates an arcgis.features.feature.FeatureSet instance which has a to_geojson property. The property's string value can be written to a file using Python's built-in features.

I am not sure if that is the best way to go but it will save you to write an unnecessary file and you can bypass arcpy...


Using Esri's example from here to get some data to have a working example for demonstration:

import pandas as pd
from arcgis.features import GeoAccessor, GeoSeriesAccessor
from arcgis import GIS

gis = GIS()
item = gis.content.get("85d0ca4ea1ca4b9abf0c51b9bd34de2e")
feature_layer = item.layers[0]

# create a Spatially Enabled DataFrame object
sdf = pd.DataFrame.spatial.from_layer(feature_layer)

And here is the code converting the ArcGIS Spatially Enabled Dataframe to a GeoJSON:

featureset = sdf.spatial.to_featureset()

with open(r"d:\temp\test.geojson", "w", encoding="utf-8") as file:
    file.write(featureset.to_geojson)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.