Can I dissolve polygons in spatially enabled dataframe?

11-10-2021 09:30 AM
Occasional Contributor


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.

0 Kudos
3 Replies
New Contributor

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()

Occasional Contributor

Thanks Gregory,

I tried running sedf.spatial.to_feature_collection() like so:

fcn = "feat_colln"


 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)
      8 # arcgis.features.manage_data.dissolve_boundaries(fcn)

~\AppData\Local\ESRI\conda\envs\arcgispro-nov042021\lib\site-packages\arcgis\features\geo\ in to_feature_collection(self, name, drawing_info, extent, global_id_field)
   2798                 "spatialReference" :
   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\ in __feature_set__(self)
   2530             "globalIdFieldName" : "",
   2531             "displayFieldName" : "",
-> 2532             "geometryType" : _geom_types[type(self._data[][self._data[].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:



0 Kudos
New Contributor III

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('')
counties = counties_layer.query(
    where="STATE_NAME IN ('California', 'Oregon', 'Washington')",

# 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\
    .apply(lambda x: dissolve_polys(x.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.

0 Kudos