Fiona 1.0.1

2013-08-24T03:54:37Z in the lab, python, programming

I tried to say no to every extraneous feature on the road to 1.0. I also overlooked at least one important feature: control over the order of record fields when writing data files.

The fields of a Collection (the object you get when you call fiona.open()) are expressed as a mapping. But Python's standard mapping implementation, the dict, is not predictably sorted:

Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions.

and so in Fiona 1.0 there was a very probable mismatch between the output of the ogrinfo program and Collection.schema['properties']. Where ogrinfo reports ['CAT', 'FIPS_CNTRY', 'CNTRY_NAME', 'AREA', 'POP_CNTRY']

$ ogrinfo docs/data/test_uk.shp test_uk -so
INFO: Open of `docs/data/test_uk.shp'
      using driver `ESRI Shapefile' successful.

Layer name: test_uk
Geometry: 3D Polygon
Feature Count: 48
Extent: (-8.621389, 49.911659) - (1.749444, 60.844444)
Layer SRS WKT:
GEOGCS["GCS_WGS_1984",
    DATUM["WGS_1984",
        SPHEROID["WGS_84",6378137,298.257223563]],
    PRIMEM["Greenwich",0],
    UNIT["Degree",0.017453292519943295]]
CAT: Real (16.0)
FIPS_CNTRY: String (80.0)
CNTRY_NAME: String (80.0)
AREA: Real (15.2)
POP_CNTRY: Real (15.2)

the schema properties may be ordered differently by Fiona 1.0.

>>> list({
... 'CAT': 'float:16',
... 'FIPS_CNTRY': 'str',
... 'CNTRY_NAME': 'str',
... 'AREA': 'float:15.2',
... 'POP_CNTRY': 'float:15.2'}.keys())
['POP_CNTRY', 'CNTRY_NAME', 'CAT', 'AREA', 'FIPS_CNTRY']

I've changed this in 1.0.1 so that the schema properties are now an ordered dict with items ordered exactly as read by OGR.

>>> import pprint
>>> with fiona.open('docs/data/test_uk.shp') as c:
...     pprint.pprint(c.schema['properties'])
...
{'CAT': 'float:16',
 'FIPS_CNTRY': 'str',
 'CNTRY_NAME': 'str',
 'AREA': 'float:15.2',
 'POP_CNTRY': 'float:15.2'}

To write a file with fields in a certain order, you must provide them as an ordered dict

from collections import OrderedDict

schema_props = OrderedDict([('bar', 'int'), ('foo', 'str')])

c = fiona.open(
    '/tmp/file.shp',
    'w',
    schema={'properties': schema_props, ...},
    ... )

or a list (which gets converted to an ordered dict internally).

c = fiona.open(
    '/tmp/file.shp',
    'w',
    schema={'properties': [('bar', 'int'), ('foo', 'str')], ...},
    ... )

Heads up: creating an OrderedDict from a dict exposes you to the same uncertaintities as using a dict: the ordering is determined by the order of the dict's items, not by the order they appear in the Python expression.

>>> list(OrderedDict({'foo': 'str', 'bar': 'int'}).keys())
['bar', 'foo']

Comments

1Re: Fiona 1.0.1

Alban, 2013-08-26T07:17:04Z

Summer is over and I try now to use Fiona 1.0 in Python 2.7 with gdal 1.9 on Windows 32bits (binaries are from http://www.lfd.uci.edu/~gohlke/pythonlibs/#fiona). I've got a error "ImportError: DLL load failed" when python import Fiona. It seem that ogrext.pyd try to load systematically gdal110.dll (same issue on gis stackexchange : http://gis.stackexchange.com/questions/67372/fiona-importerror-dll-load-failed).

Is this a problem in Fiona or in the windows binaries from www.lfd.uci.edu/~gohlke?

2Re: Fiona 1.0.1

Sean, 2013-08-26T18:12:52Z

Gohlke's Fiona binary requires his GDAL binary. It won't work with any other.

Comments are closed after 13 days.

about archive feed find

Some rights reserved by Sean Gillies.