3

I am writing a fairly straight forward script of converting GeoJSON data into a shapefile using Shapely and Fiona. My script works but I cannot figure out how to define the CS in the script.

def GeoJSONToFC(shpname,USGSurl):
    import urllib2
    import json
    from shapely.geometry import Point, mapping
    from fiona import collection
    from fiona.crs import from_epsg
    crs = from_epsg(4326)
    schema = {'geometry': 'Point', 'properties': { 'Place': 'str', 'Magnitude': 'str' }}
    with collection(shpname, "w", crs, "ESRI Shapefile", schema) as output:
        url = USGSurl
        weburl = urllib2.urlopen(url)
        if weburl.getcode() == 200:
            data = json.loads(weburl.read())
        for i in data["features"]:
            mag, place = i["properties"]["mag"],i["properties"]["place"]
            x,y = float(i["geometry"]["coordinates"][0]),float(i["geometry"]["coordinates"][1])
            point = Point(x,y)
            output.write({'properties':{'Place': place, 'Magnitude': mag},
            'geometry': mapping(point)})

shpname = "C:\Users\Ralph\Desktop\quakes.shp"
url = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.geojson"
GeoJSONToFC(shpname,url)

the crs variable contains the 4326 CS or WGS 1984. the error occurs on line 9 when I insert the crs variable containing the coordinate system into the collection.

Traceback (most recent call last):
  File "C:\Users\Ralph\Desktop\GeoJSONToShapefile.py", line 23, in <module>
    GeoJSONToFC(shpname,url)
  File "C:\Users\Ralph\Desktop\GeoJSONToShapefile.py", line 9, in GeoJSONToFC
    with collection(shpname, "w", crs, "ESRI Shapefile", schema) as output:
  File "C:\Python27\ArcGIS10.1\lib\site-packages\fiona\__init__.py", line 180, in open
    this_schema = schema.copy()
AttributeError: 'str' object has no attribute 'copy'

the script runs fine when I remove the crs variable but the shapefile has no projection

I have seen this post Using Fiona to write a new shapefile from scratch where the answer to defining a CS is

with fiona.open(shapeout, 'w',crs=from_epsg(3857),driver='ESRI Shapefile', schema=yourschema) as output:

I have tried this method and it gives me the exact same error as above. I also checked out the doc pages on fiona to no avail http://toblerity.org/fiona/fiona.html#module-fiona.crs

  • "I have tried this method and it does not work." How does it not work? What is the error message (if any) of this alternative attempt? – Richard Law Oct 10 '16 at 22:35
  • @RichardLaw it gives me the same exact error as the one I have posted – ziggy Oct 10 '16 at 22:39
6

You are following a red herring, since the issue has nothing to do with a projection, but all to do with the positional arguments of collection (or type help(collection) from an interactive Python shell).

The positional order of arguments should be:

collection(shpname, "w", "ESRI Shapefile", schema, crs)

You can avoid these issues with keyword arguments like:

collection(shpname, "w", crs=crs, driver="ESRI Shapefile", schema=schema)

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.