1

I successfully used to build a GeoDataFrame out of a shapely GeometryCollection (hereafter gc) as gdf = gpd.GeoDataFrame(gc) but I'm now facing this error with shapely 2.0.0:

gdf = gpd.GeoDataFrame(gc)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/site-packages/geopandas/geodataframe.py", line 135, in __init__
    super().__init__(data, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/pandas/core/frame.py", line 781, in __init__
    raise ValueError("DataFrame constructor not properly called!")
ValueError: DataFrame constructor not properly called!

Code to reproduce the error:

import geopandas as gpd
from shapely.geometry import LineString, Point, GeometryCollection

p = Point(51, -1)
l = LineString([(52, -1), (49, 2)])
gc = GeometryCollection([p, l])

gdf = gpd.GeoDataFrame(gc)

# Previously, with shapely 1.8.5:
gdf
>:
                          0
0             POINT (51 -1)
1  LINESTRING (52 -1, 49 2)

When I print gc with shapely 2.0.0, it gives:

gc
>:
  <GEOMETRYCOLLECTION (POINT (51 -1), LINESTRING (52 -1, 49 2))>

Whereas with shapely 1.8.5 it was:

gc
>:
  <shapely.geometry.collection.GeometryCollection object at 0x7fab2c6062c0>

Also:

gdf = gpd.GeoDataFrame({'geometry':gc})

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/site-packages/geopandas/geodataframe.py", line 135, in __init__
    super().__init__(data, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/pandas/core/frame.py", line 664, in __init__
    mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)
  File "/usr/local/lib/python3.10/site-packages/pandas/core/internals/construction.py", line 493, in dict_to_mgr
    return arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ, consolidate=copy)
  File "/usr/local/lib/python3.10/site-packages/pandas/core/internals/construction.py", line 118, in arrays_to_mgr
    index = _extract_index(arrays)
  File "/usr/local/lib/python3.10/site-packages/pandas/core/internals/construction.py", line 656, in _extract_index
    raise ValueError("If using all scalar values, you must pass an index")
ValueError: If using all scalar values, you must pass an index

Therefore, how to properly build a GeoDataFrame from a shapely GeometryCollection in GeoPandas with shapely 2.0.0?

Version info:

  • geopandas 0.12.2
  • pandas 1.5.3
  • shapely 2.0.0

2 Answers 2

0

A GeoDataFrame needs a geometry field (if not, it is a DataFrame). Therefore the correct method to create it is:

gdf = gpd.GeoDataFrame({'geometry':gc})
print(gdf)
                                       geometry
0                         POINT (51.00000 -1.00000)
1  LINESTRING (52.00000 -1.00000, 49.00000 2.00000)

Note that print gc with shapely 1.8.5 and 2.0.0 is equivalent

gc
<shapely.geometry.collection.GeometryCollection object at 0x11f0eb2e8>

but

print(gc)
GEOMETRYCOLLECTION (POINT (51 -1), LINESTRING (52 -1, 49 2))
# or
gc.wkt
'GEOMETRYCOLLECTION (POINT (51 -1), LINESTRING (52 -1, 49 2))'
1
  • Thanks for you answer. Unfortunately, gpd.GeoDataFrame({'geometry':gc}) raises another error. See the update in the main post. Feb 3 at 18:37
0

Building a GeoDataFrame out of a shapely GeometryCollection is done as follows with shapely >= 2.0.0:

gdf = gpd.GeoDataFrame(gc.geoms)
gdf
>:
                          0
0             POINT (51 -1)
1  LINESTRING (52 -1, 49 2)

See also: Access individual elements of a GeometryCollection with shapely >= 2.0.0

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.