[GIS] Exception raised when saving Geopandas DataFrame into a file

geopandaspythonsave

I am trying to apply the method to_file of a Geopandas DataFrame.

My DataFrame contains a column Polygon, a column Point, a string and float columns.

Here the output of the describe method:

<class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 1332 entries, 0 to 1331
Data columns (total 7 columns):
ID                    1332 non-null object
geometry              1332 non-null object
centroid              1332 non-null object
epsg4326_longitude    1332 non-null float64
epsg4326_latitude     1332 non-null float64
epsg3857_x            1332 non-null float64
epsg3857_y            1332 non-null float64
dtypes: float64(4), object(3)
memory usage: 72.9+ KB

The specific types are:

Polygon: shapely.geometry.polygon.Polygon
Point: shapely.geometry.point.Point
String: str
Floats: numpy.float64

When I try apply the method to_file i have the following exception:

geo_grid_df.to_file(filename='grid_50km.geojson', driver='GeoJSON')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-78-d1eb792e5885> in <module>()
----> 1 geo_grid_df.to_file(filename='grid_france_50km.geojson', driver='GeoJSON')

C:\ProgramData\Anaconda3\lib\site-packages\geopandas\geodataframe.py in to_file(self, filename, driver, schema, **kwargs)
    411         """
    412         from geopandas.io.file import to_file
--> 413         to_file(self, filename, driver, schema, **kwargs)
    414 
    415     def to_crs(self, crs=None, epsg=None, inplace=False):

C:\ProgramData\Anaconda3\lib\site-packages\geopandas\io\file.py in to_file(df, filename, driver, schema, **kwargs)
    109         with fiona.open(filename, 'w', driver=driver, crs=df.crs,
    110                         schema=schema, **kwargs) as colxn:
--> 111             colxn.writerecords(df.iterfeatures())
    112 
    113 

C:\ProgramData\Anaconda3\lib\site-packages\fiona\collection.py in writerecords(self, records)
    333         if self.mode not in ('a', 'w'):
    334             raise IOError("collection not open for writing")
--> 335         self.session.writerecs(records, self)
    336         self._len = self.session.get_length()
    337         self._bounds = self.session.get_extent()

fiona\ogrext.pyx in fiona.ogrext.WritingSession.writerecs()

fiona\ogrext.pyx in fiona.ogrext.OGRFeatureBuilder.build()

ValueError: Invalid field type <class 'shapely.geometry.point.Point'>

Any ideas?

pandas: 0.20.3 | geopandas: 0.4.0 | fiona: 1.7.10

Best Answer

Seems like a Geopandas DataFrame has only ONE column geometry [check it here]

So dumping a Geo DataFrame with two columns or more columns with geometry objects is not possible.

I was able to dump my GeoJSON without the Points objects column:

geo_grid_df.drop(labels='centroid', axis=1).to_file(filename='grid_50km.geojson', driver='GeoJSON')

And without the polygons in another one, after setting the points as my new geometry reference.

geo_grid_df.drop(labels='geometry', axis=1).set_geometry('centroid', crs={'init': 'epsg:4326'}).to_file(filename='grid_50km_centroids.geojson', driver='GeoJSON')
Related Question