Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'GeoSeries' object has no attribute '_geom'

Getting this strange error while merging two regions using shapely's unary_union.

Shapely version: 1.6.4.post2

Python 3.5

Data

Polygons (side by side)

I want to add Gujranwala 1 and Gujranwala 2 to make it a single polygon.

Code

from shapely.ops import unary_union
polygons = [dfff['geometry'][1:2], dfff['geometry'][2:3]]
boundary = unary_union(polygons)

Output

    ---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-41-ee1f09532724> in <module>()
      1 from shapely.ops import unary_union
      2 polygons = [dfff['geometry'][1:2], dfff['geometry'][2:3]]
----> 3 boundary = unary_union(polygons)

~/.local/lib/python3.5/site-packages/shapely/ops.py in unary_union(self, geoms)
    145         subs = (c_void_p * L)()
    146         for i, g in enumerate(geoms):
--> 147             subs[i] = g._geom
    148         collection = lgeos.GEOSGeom_createCollection(6, subs, L)
    149         return geom_factory(lgeos.methods['unary_union'](collection))

~/.local/lib/python3.5/site-packages/pandas/core/generic.py in __getattr__(self, name)
   4374             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   4375                 return self[name]
-> 4376             return object.__getattribute__(self, name)
   4377 
   4378     def __setattr__(self, name, value):

AttributeError: 'GeoSeries' object has no attribute '_geom'
like image 603
Ahsaan-566 Avatar asked Oct 31 '25 23:10

Ahsaan-566


1 Answers

Your attempt at making the unary union sort of splits the difference between two ways that do work. The way you've attempted to select the two polygons (dfff["geometry"][1:2] and dfff["geometry"][2:3]) actually returns a pair of GeoSeries (which contains some sequence of shapely geometries), so you're passing unary_union a list of GeoSeries, whereas the unary_union function within shapely is expecting a list of shapely geometries. You could do:

polygons = [dfff.iloc[1, "geometry"], dfff.iloc[2, "geometry"]]
boundary = unary_union(polygons)

That said, GeoSeries provide their own unary_union method that just calls shapely.ops.unary_union, but does so over GeoSeries objects. So the easier way to get the unary union would be:

boundary = dfff["geometry"][1:3].unary_union

This also extends much more easily to a longer list of polygons.

like image 142
jdmcbr Avatar answered Nov 02 '25 11:11

jdmcbr