Let the following file be the result of a GeoDataFrame dump to a CSV file: gdf.to_csv('/path/to/gdf.csv')
$ cat '/path/to/gdf.csv'
myid,geometry
332,"MULTIPOLYGON Z (((0 0 0, 0 0 1, 0 1 1, 0 0 0)))"
220,"MULTIPOLYGON Z (((1 1 1, 1 1 2, 1 2 2, 1 1 1)))"
When I try to load it back, things surprisingly go wrong:
import geopandas as gpd
gdf = gpd.read_file('/path/to/gdf.csv')
Traceback (most recent call last):
File "/tmp/ipykernel_369762/4150368047.py", line 1, in <cell line: 1>
gpd.read_file('/path/to/gdf.csv')
File "/usr/local/lib/python3.10/dist-packages/geopandas/io/file.py", line 259, in _read_file
return _read_file_fiona(
File "/usr/local/lib/python3.10/dist-packages/geopandas/io/file.py", line 360, in _read_file_fiona
df = GeoDataFrame.from_features(
File "/usr/local/lib/python3.10/dist-packages/geopandas/geodataframe.py", line 643, in from_features
return cls(rows, columns=columns, crs=crs)
File "/usr/local/lib/python3.10/dist-packages/geopandas/geodataframe.py", line 159, in __init__
raise ValueError(
ValueError: GeoDataFrame does not support multiple columns using the geometry column name 'geometry'.
while if I sed -i 's/geometry/geometry2/g' /path/to/gdf.csv
and then open the same file again:
gpd.read_file('/path/to/gdf.csv')
>:
myid geometry2 geometry
0 332 MULTIPOLYGON Z (((0 0 0, 0 0 1, 0 1 ... None
1 220 MULTIPOLYGON Z (((1 1 1, 1 1 2, 1 2 ... None
So, for the moment, I have to load it this way:
import pandas as pd
import geopandas as gpd
gpd.GeoDataFrame(pd.read_csv('/path/to/gdf.csv'))
>: myid geometry2
0 332 MULTIPOLYGON Z (((0 0 0, 0 0 1, 0 1 ...
1 220 MULTIPOLYGON Z (((1 1 1, 1 1 2, 1 2 ...
Also, by carefully reading the doc of the read_file()
method, one can notice the ignore_geometry
option (which is a little counter intuitive imho):
gdf = gpd.read_file('/path/to/gdf.csv', ignore_geometry=True)
type(gdf)
>: pandas.core.frame.DataFrame
This forces me to do kind of a casting-back-to-GeoDataFrame operation:
gdf = gpd.GeoDataFrame(gpd.read_file('/path/to/gdf.csv', ignore_geometry=True))
type(gdf)
>: geopandas.geodataframe.GeoDataFrame
which sounds to be a bit of an "overstatement" to me, because I must tell it first to ignore the geometry, and then set it back as a geometry the step after...
I'm therefore wondering if there is a more natural/intuitive way of loading a CSV file dumped from a GeoDataFrame back into a GeoDataFrame?
My GeoPandas' version is '0.12.2'
.