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)
    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())

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

  • I don't think you can store a point object in the properties (attributes) of a polygon feature.
    – user2856
    Jan 28 '19 at 11:22
  • I drooped the column with Points objects (centroid) and I don't have the exception anymore. Then I dropped the column with Polygons objects (geometry) and I still have the exception. Seems like the only field where you are able to put geographical objects is under "geometry". So how can we have both geographical information exported in a single file?
    – oso_ted
    Jan 28 '19 at 12:58
  • Even if GeoPandas would support multiple geometry columns, many geospatial file formats (including GeoJSON) don't support storing multiple geometry columns. One option is to convert the points to a column of x and y coordinates, so you can store them as integers.
    – joris
    Jan 28 '19 at 14:30

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')

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.