Skip to content

Writing np.int64 or np.int32 scalars raises an error #365

@fmaussion

Description

@fmaussion

Hi, this is on python3 with fiona 1.7.0.post2. Attempting to write properties as numpy scalars can fail for certain types. The following example reproduces the problem:

import fiona
import numpy as np

feat = {'geometry': {'coordinates': (((21.0, 22.0), (22.0, 23.0), 
                                      (24.0, 20.0), (21.0, 22.0)),),
        'type': 'Polygon'},
        'id': '0',
        'properties': {'PROP': np.int64(12)},  # this line is the problem
        'type': 'Feature'}
schema = {'geometry': 'Polygon', 'properties':{'PROP':'str'}}

with fiona.open('test.shp', 'w', driver="ESRI Shapefile", schema=schema) as c:
    c.write(feat)

will raise:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-84-e58056597894> in <module>()
     11 
     12 with fiona.open('test2.shp', 'w', driver="ESRI Shapefile", schema=schema) as c:
---> 13     c.write(feat)

/home/mowglie/.pyvirtualenvs/py3/lib/python3.4/site-packages/fiona/collection.py in write(self, record)
    331     def write(self, record):
    332         """Stages a record for writing to disk."""
--> 333         self.writerecords([record])
    334 
    335     def validate_record(self, record):

/home/mowglie/.pyvirtualenvs/py3/lib/python3.4/site-packages/fiona/collection.py in writerecords(self, records)
    325         if self.mode not in ('a', 'w'):
    326             raise IOError("collection not open for writing")
--> 327         self.session.writerecs(records, self)
    328         self._len = self.session.get_length()
    329         self._bounds = self.session.get_extent()

fiona/ogrext.pyx in fiona.ogrext.WritingSession.writerecs (fiona/ogrext1.c:17022)()

fiona/ogrext.pyx in fiona.ogrext.OGRFeatureBuilder.build (fiona/ogrext1.c:6615)()

ValueError: Invalid field type <class 'numpy.int64'>

Note that np.int or np.float would work. (see geopandas/geopandas#348 and pandas-dev/pandas#13468 for more background).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions