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