Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider using Fiona for reading Shapefiles #250

Closed
rsignell-usgs opened this issue Mar 19, 2013 · 9 comments
Closed

Consider using Fiona for reading Shapefiles #250

rsignell-usgs opened this issue Mar 19, 2013 · 9 comments

Comments

@rsignell-usgs
Copy link
Contributor

For reading shapefiles, I used to use pyshp, but switched to Fiona
http://toblerity.github.com/fiona/
based on recommendations from my opensource GIS buddies. It's pretty cool.

Fiona is also part of the Enthought Python Distribution, which is handy -- one less step for folks trying to build Cartopy.

Just a thought.

@pelson
Copy link
Member

pelson commented Mar 19, 2013

Thanks for your input @rsignell-usgs - I've looked at Fiona a couple of times and have to admit to finding it harder to get into than the interface described here.

In terms of performance Fiona is probably going to be better than PyShp (esp for spatial extractions), because under the hood it is using gdal where PyShp is pure python. On the flip side, PyShp is easier to install (because it doesn't depend on gdal) - though that benefit is diminished by your point that Fiona (and therefore gdal) is shipped with EPD.

Whilst I'm not sold on the use of Fiona in cartopy (at least as a hard dependency) at this sage, I'm keen to make interfacing with it easier, so any suggestions (or code) would be really valuable.

@sgillies - would you like to comment?

@pelson
Copy link
Member

pelson commented Mar 19, 2013

P.S. Are you doing anything with cartopy that is worth going in the gallery? If you're keen, it'd be great to get some new examples in there!

Cheers,

@mlaloux
Copy link

mlaloux commented Mar 19, 2013

I use also Fiona, now. For Mac OS X it is very easy to install with the Frameworks of KyngChaos (http://www.kyngchaos.com/software/frameworks), for Linux also and for Windows, Christoph Gohlke has compiled versions available of gdal/ogr (http://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal) and Fiona (http://www.lfd.uci.edu/~gohlke/pythonlibs/#fiona) for Python 2.6 and 2.7.
With Pyshp, I have big problems with shapefiles that are not encoded in Window 1252, with Fiona not.
Fiona is more particularly adapted to Shapely (direct conversion)

@pelson
Copy link
Member

pelson commented Mar 20, 2013

Thanks for voicing your opinion @mlaloux. If we put the installation issues aside, are there things that you would like to do with Fiona+Cartopy which are not currently possible because of a cartopy limitation? I'm keen to expose the necessary interfaces to allow users to use whatever tool they are most comfortable with.

Is there a particularly useful dataset that really wouldn't function well using the PyShp library which makes Fiona shine? (GSHHS highres springs to mind). Can that be demonstrated in combination with cartopy? I'm particularly interested in examples which can go into the cartopy gallery.

@mlaloux
Copy link

mlaloux commented Mar 20, 2013

Pyshp also has big problems if, unfortunately, one of the fields of the table is empty;

As a small example is worth a thousand words, the use of Fiona with my version of 110m-geography-regions-points.shp :

with fiona.collection('110m-geography-regions-points.shp', 'r') as layer:
    print "schema :", layer.schema
    print "number of elements :", len(layer)
schema : {'geometry': 'Point', 'properties': {u'Comment': 'str:254', u'Elevation': 'float', u'ScaleRank': 'float', u'NameAlt': 'str:254', u'FeatureCla': 'str:32', u'Name': 'str:254'}}
number of elements : 19 (in my version)
  • directly the GEOMETRY_FACTORIES of your script shapereader.py and the fields names and definitions

with fiona.collection('110m-geography-regions-points.shp', 'r') as layer:
    for element in layer:
        print element
{'geometry': {'type': 'Point', 'coordinates': (148.31402754129294, -36.405891208645684)}, 'id': '0', 'properties': {'Comment': u'Highest point in Australia', 'Elevation': 2228.0, 'ScaleRank': 2.0, 'NameAlt': None, 'FeatureCla': u'extreme', 'Name': u'Mt. Kosciuszko'}}

the result is one dictionary with the geometry and the attributes by element which is treated as all Python dictionaries

  • if you want the Shapely geometries of the elements (either a point, line or polygon)
from shapely.geometry import shape
with fiona.collection('110m-geography-regions-points.shp', 'r') as layer:
    for element in layer:
        print shape(element['geometry'])
POINT (148.3140275412929441 -36.4058912086456843)
POINT (79.5705233092471076 31.7884992538667035)
.....
  • if you want only the attributes of the elements

with fiona.collection('110m-geography-regions-points.shp', 'r') as layer:
    for element  in layer:
        print element ['properties']
{'Comment': u'Highest point in Australia', 'Elevation': 2228.0, 'ScaleRank': 2.0, 'NameAlt': None, 'FeatureCla': u'extreme', 'Name': u'Mt. Kosciuszko'}
{'Comment': None, 'Elevation': 6638.0, 'ScaleRank': 2.0, 'NameAlt': None, 'FeatureCla': u'mountain', 'Name': u'Kailash'}
....
  • if you want only one attribute

with fiona.collection('110m-geography-regions-points.shp', 'r') as layer:
    for element  in layer:
        print element ['properties']['Elevation']
2228.0
6638.0

....

The same thing as your script shapereader.py ,which is wonderful work with Pyshp, but easier with Fiona.

@shoyer
Copy link

shoyer commented Sep 19, 2014

geopandas (which uses fiona) has a very convenient interface that already plays very smoothly with the cartopy API. I simply replaced cartopy.io.shapereader.Reader('file.shp') with geopandas.read_file('file.shp')['geometry'] and my code ran an order of magnitude faster.

@rsignell-usgs
Copy link
Contributor Author

@shoyer. Nice! Thanks for the tip!

@pelson pelson added this to the 0.14 milestone Jan 28, 2016
@pelson pelson self-assigned this Jan 28, 2016
@QuLogic QuLogic modified the milestones: 0.15, 0.14 Mar 29, 2016
@pelson pelson modified the milestones: 0.16, 0.17 Nov 21, 2017
@dopplershift
Copy link
Contributor

Was this fixed by #1000?

@QuLogic
Copy link
Member

QuLogic commented Sep 4, 2018

I think so; I'm going to close.

@QuLogic QuLogic closed this as completed Sep 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants