Toblerity / Fiona

Fiona reads and writes geographic data files
https://fiona.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.16k stars 203 forks source link

Support for list-like feature properties #435

Open egorf opened 7 years ago

egorf commented 7 years ago

Expected behavior and actual behavior.

Instead of writing a triplet property, I get the following exception:

Warning 6: dataset /Users/egor/Documents/reach-surveying/surveying/gdf.geojson does not support layer creation option ENCODING
Traceback (most recent call last):
  File "project.py", line 118, in <module>
    gdf.to_file("gdf.geojson", driver="GeoJSON")
  File "/Users/egor/Documents/reach-surveying/venv/lib/python2.7/site-packages/geopandas/geodataframe.py", line 343, in to_file
    to_file(self, filename, driver, schema, **kwargs)
  File "/Users/egor/Documents/reach-surveying/venv/lib/python2.7/site-packages/geopandas/io/file.py", line 63, in to_file
    c.write(feature)
  File "/Users/egor/Documents/reach-surveying/venv/lib/python2.7/site-packages/fiona/collection.py", line 339, in write
    self.writerecords([record])
  File "/Users/egor/Documents/reach-surveying/venv/lib/python2.7/site-packages/fiona/collection.py", line 333, in writerecords
    self.session.writerecs(records, self)
  File "fiona/ogrext.pyx", line 1019, in fiona.ogrext.WritingSession.writerecs (fiona/ogrext2.c:17895)
  File "fiona/ogrext.pyx", line 326, in fiona.ogrext.OGRFeatureBuilder.build (fiona/ogrext2.c:7195)
ValueError: Invalid field type <type 'tuple'>

I can see this data type is not supported, nor the lists are mentioned in FIELD_TYPES_MAP and FIELD_TYPES. Is there a potential way to work around this problem?

Steps to reproduce the problem.

    gs = geopandas.GeoSeries({
        "name": "name",
        "triplet_data": (0, 1, 2),
        "geometry": shapely.geometry.Point((0.0, 0.0, 0.0))
    })

    gdf = geopandas.GeoDataFrame()
    gdf = gdf.append(gs, ignore_index=True)
    gdf = gdf.append(gs, ignore_index=True)
    gdf = gdf.append(gs, ignore_index=True)
    gdf.to_file("gdf.geojson", driver="GeoJSON")

Operating system

Mac OS X 10.12.3

Fiona version and provenance

Fiona==1.7.5, installed as a pip dependency to geopandas

sgillies commented 7 years ago

@egorf you are right, there is currenty no support in Fiona for tuple or list properties. The work arounds are to explode these tuples into three separate properties or to encode them in a string using a microformat specific to your application.

The blocker for Fiona is that the GDAL/OGR libraries lack support for arbitrary Python collections. In the list at http://www.gdal.org/ogr__core_8h.html#a787194bea637faf12d61643124a7c9fc you'll see there is no support for dicts or heterogeneous lists or tuples, only array-like lists. Supporting arrays of a single element type would not be very hard, though we might find that they are not supported in certain data formats.

egorf commented 7 years ago

Ok, I see. Theoretically, there may be check, that all data inside the list is the right type, then create an OFTList, raise ValueError otherwise. I will look into this