Hello,
Do Spatially Enabled Dataframes have an equivalent to Geopandas .dissolve() method?
I've looked through the API documentation under both GeoAccessor and GeoSeriesAccessor but cannot find any classess or methods to dissolve/aggregate polygons for an SEDF.
NOTE - I tried doing the dissolve in geopandas and just exporting the dissolved result to an SEDF using the from_geodataframe method, but long story short, I've been having some problems with Geopandas and getting it to play nice with the arcgis python environment.
Based on what I have read, Esri is focused on updating a new set of objects called GeoAcessors. They are a lot like Geopandas and have the ability to apply both Shapely and Esri methods. Unfortunately .dissolve() is not one that is supported.
I think what you can get what you are looking for though by converting to a Feature collection or layer and then applying a .dissolve_boundries() https://developers.arcgis.com/python/api-reference/arcgis.features.manage_data.html#dissolve-boundar...
Thanks Gregory,
I tried running sedf.spatial.to_feature_collection() like so:
fcn = "feat_colln"
sedf.spatial.to_feature_collection(fcn)
But I got this error message:
KeyError Traceback (most recent call last)
<ipython-input-32-1c370e0f59c6> in <module>
4 import arcgis
5 fcn = "feat_colln"
----> 6 sedf.spatial.to_feature_collection(fcn)
7
8 # arcgis.features.manage_data.dissolve_boundaries(fcn)
~\AppData\Local\ESRI\conda\envs\arcgispro-nov042021\lib\site-packages\arcgis\features\geo\_accessor.py in to_feature_collection(self, name, drawing_info, extent, global_id_field)
2798 "spatialReference" : self.sr
2799 }
-> 2800 fs = self.__feature_set__
2801 fields = []
2802 for fld in fs['fields']:
~\AppData\Local\ESRI\conda\envs\arcgispro-nov042021\lib\site-packages\arcgis\features\geo\_accessor.py in __feature_set__(self)
2530 "globalIdFieldName" : "",
2531 "displayFieldName" : "",
-> 2532 "geometryType" : _geom_types[type(self._data[self.name][self._data[self.name].first_valid_index()])],
2533 "spatialReference" : sr,
2534 "fields" : [],
KeyError: <class 'pandas.core.series.Series'>
If it helps, here are the first few rows of the SEDF:
You can use Shapely's unary_union to dissolve geometries. For example:
from arcgis.features import FeatureLayer
from shapely.ops import unary_union
from arcgis.geometry import Geometry
# query some counties
counties_layer = FeatureLayer('https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Counties_Generalized/FeatureServer/0')
counties = counties_layer.query(
where="STATE_NAME IN ('California', 'Oregon', 'Washington')",
out_fields='NAME,STATE_NAME',
as_df=True
)
# function to dissolve polygons
def dissolve_polys(polys):
# convert to shapely polygons, using the buffer(0) trick to correct invalid polygons
shapely_polys = [s.as_shapely.buffer(0) for s in polys]
# union polygons
dissolved_poly = unary_union(shapely_polys)
# convert back to arcgis geometry
return Geometry(dissolved_poly.__geo_interface__, spatialReference=polys.iloc[0].spatialReference)
# dissolve county polygons by state
counties_dissolve = counties\
.groupby('STATE_NAME')\
.apply(lambda x: dissolve_polys(x.SHAPE))\
.reset_index(name='SHAPE')
NOTE: There can be issues converting between Shapely and Esri polygons, so check your output shapes. In the counties example above, several island polygons (such California's Channel Islands) don't get included in the dissolved polygon.