Saturday, 21 November 2015

coordinate system - Using Fiona to write a new shapefile from scratch


I am new to Fiona, and trying to wrap my head around reading a csv file and writing out an ESRI shapefile. I have most of it down, but am missing two things; (1) defining the projection/spatial reference and (2) the attributes actually being populated with the values from the csv file.



So my questions are: 1. How do I define the spatial reference, either by defining an EPSG or a custom projection 2. Why is my data not writing as attributes?


Thanks in advance for any guidance.


import csv
from shapely.geometry import Point, mapping
from fiona import collection
from fiona.crs import from_epsg


schema = { 'geometry': 'Point',
'properties': {'dx': 'float:13.3',

'dy': 'float:13.3',
'dline': 'str',
'dtrace': 'int',
'ddepth': 'float:9.4',
'dtrash': 'float:9.4',
'sx': 'float:13.3',
'sy': 'float:13.3',
'sline': 'str',
'strace': 'int',
'sdepth': 'float:9.4',

'strash': 'float:9.4',
'bx': 'float:13.3',
'by': 'float:13.3',
'bline': 'str',
'btrace': 'int',
'bdepth': 'float:9.4',
'btrash': 'float:9.4',}}

with collection(
shapeout, "w", "ESRI Shapefile", schema) as output:

with open(finalout, 'rb') as f:
reader = csv.DictReader(f)
for row in reader:
point = Point(float(row['dx']), float(row['dy']))
output.write({
'properties': {'dx': 'float:13.3',
'dy': 'float:13.3',
'dline': 'str',
'dtrace': 'int',
'ddepth': 'float:9.4',

'dtrash': 'float:9.4',
'sx': 'float:13.3',
'sy': 'float:13.3',
'sline': 'str',
'strace': 'int',
'sdepth': 'float:9.4',
'strash': 'float:9.4',
'bx': 'float:13.3',
'by': 'float:13.3',
'bline': 'str',

'btrace': 'int',
'bdepth': 'float:9.4',
'btrash': 'float:9.4',},
'geometry': mapping(point)
})

With the code updated to the "newer" (aka, not outdated) process:


with fiona.open(shapeout, 'w', crs=from_epsg(3996), driver='ESRI Shapefile', 
schema=myschema) as output:
with open(finalout, 'rb') as f:

reader = csv.DictReader(f)
for row in reader:
#geometry
point = Point(float(row['dx']), float(row['dy']))
#attributes
prop = {'dx': float(row['dx']),
'dy': float(row['dy']),
'dline': str(row['dline']),
'dtrace': float(row['dtrace']),
'ddepth': float(row['ddepth']),

'dtrash': float(row['dtrash'])},
print{'geometry': mapping(point), 'properties':prop}

Gives me this:


{'geometry': {'type': 'Point', 'coordinates': (-245215.3333, -975226.3333)}, 'properties': ({'dtrace': 24387.0, 'ddepth': 1433.2458, 'dtrash': 1433.25, 'dx': -245215.3333, 'dy': -975226.3333, 'dline': '      lsl0904.201302'},)}
{'geometry': {'type': 'Point', 'coordinates': (-245215.0, -975251.0)}, 'properties': ({'dtrace': 24386.0, 'ddepth': 1432.7268, 'dtrash': 1432.73, 'dx': -245215.0, 'dy': -975251.0, 'dline': ' lsl0904.201302'},)}

But as soon as I change to:


output.write({'geometry': mapping(point), 'properties':prop})


I get the error message: AttributeError: 'tuple' object has no attribute 'keys'




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...