0

I am trying to get an overlay (intersection) of two geodataframes of polygons, but it returns me a ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

It's weird as far as I did an intersection some lines before without issue.

clip = gpd.overlay(result_join, concave_hull, how='intersection')
# ^^This one works well
clip.crs = {'init': 'epsg:3857'}

parc = gpd.read_file(parcellaire)
parc = gpd.GeoDataFrame(parc).drop(['name'], axis=1)
parc = parc.set_geometry('geometry')
parc = parc.explode()
parc = parc.buffer(0)
parc.crs = {'init': 'epsg:4326'}
parc = parc.to_crs({'init': 'epsg:3857'})
parc = gpd.GeoDataFrame(parc).rename(columns={0:'geometry'}).set_geometry('geometry')

zonage = gpd.overlay(clip, parc, how='intersection') # This one crashes

Full traceback :

File "/Tim/Google Drive/Fruition Sciences/Dev/Geomatic/Scripts/Zonage/Zonage_standalone.py", line 278, in <module>
    zonage(shape)
  File "/Tim/Google Drive/Fruition Sciences/Dev/Geomatic/Scripts/Zonage/Zonage_standalone.py", line 225, in zonage
    zonage = gpd.overlay(clip, parc, how='intersection') # Intersection du zonage par le parcellaire
  File "/usr/local/lib/python2.7/dist-packages/geopandas/tools/overlay.py", line 175, in overlay
    prop2.drop(df2._geometry_column_name)])
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/reshape/concat.py", line 213, in concat
    return op.get_result()
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/reshape/concat.py", line 372, in get_result
    name = com._consensus_name_attr(self.objs)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/common.py", line 130, in _consensus_name_attr
    if obj.name != name:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

To reproduce it ---> files

Command: python .../Zonage_standalone.py .../shpdetest 10 .../block.shp

  • Can you provide some more information? (ideally a reproducible example, or the full traceback, info() on the dataframes, ...). I think this would also suitable to open an issue on the tracker, as it is either a bug or the error report should be better: github.com/geopandas/geopandas/issues – joris May 11 '18 at 9:33
  • Ok, I put all I can ^^. For information, I run it in a docker with python 2.7 – Tim C. May 11 '18 at 9:53
  • What version of pandas are you using? It runs fine for me with geopandas 0.3.0 and pandas 0.22.0 (but on Python 3) – joris May 11 '18 at 12:05
  • I have Pandas 0.22.0 and Geopandas 0.3.0 on Python 2.7 – Tim C. May 11 '18 at 13:56
  • Not sure yet why it comes up in python 2 and not 3, but the bug in pandas is this: github.com/pandas-dev/pandas/issues/21015. The "cause" in your code is the explode call, which results in a MultiIndex, and then geopandas.overlay fails on that due to the pandas bug. – joris May 12 '18 at 14:21
2

Effectively, your original script works in Python 3.6 but not in Python 2.7. I still don't understand why but if you change the processing of parc:

parc = gpd.read_file(parcellaire)

parc is already a GeoDataFrame with a geometry column, so why

#parc = gpd.GeoDataFrame(parc).drop(['name'], axis=1)
#parc = parc.set_geometry('geometry')
#parc = parc.explode()
#parc = parc.buffer(0)
#parc = gpd.GeoDataFrame(parc).rename(columns={0:'geometry'}).set_geometry('geometry')

Therefore the solution

parc = gpd.read_file(parcellaire)
parc = parc.drop('name', axis=1) # parc is already a GeoDataFrame with a geometry column
parc.crs = {'init': 'epsg:4326'}
parc = parc.to_crs({'init': 'epsg:3857'})
zonage = gpd.overlay(clip, parc, how='intersection')

works for me with Python 2.7 and 3.6 with GeoPandas 3.0 and Pandas 0.19, 0.21 or 0.22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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