Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
GIS with Python, Shapely, and Fiona Example 1 - CSV Files
import csv
from shapely.geometry import Point, mapping
from fiona import collection
schema = { 'geometry': 'Point', 'properties': { 'name': 'str' } }
with collection(
"some.shp", "w", "ESRI Shapefile", schema) as output:
with open('some.csv', 'rb') as f:
reader = csv.DictReader(f)
for row in reader:
point = Point(float(row['lon']), float(row['lat']))
output.write({
'properties': {
'name': row['name']
},
'geometry': mapping(point)
})
name lat lon
Chicago 41.88 -87.63
Kansas City 39.101 -94.584

This tutorial rules! Thanks Tom.

*got here from the blog post..

tmkasun commented Oct 7, 2014

Thanks for the code sample 👍 + nice tutorial

chai2010 commented Jan 5, 2015

mark

sariirc commented May 2, 2016

Why I only get 5 points, while my csv has 675?

this is the code:
schema = { 'geometry': 'Point', 'properties': { 'name': 'str' } }

with collection(
"restaurantes_bcn.shp", "w", "ESRI Shapefile", schema) as output:
with open('restaurantes_barcelona.csv', 'rb') as f:
reader = csv.DictReader(f, delimiter=',')
for row in reader:
point = Point(float(row['X']), float(row['Y']))
output.write({
'properties': {
'name': row['name']
},
'geometry': mapping(point)
})

with collection("restaurantes_bcn.shp", "r") as input:
schema = { 'geometry': 'Polygon', 'properties': { 'name': 'str' } }
with collection(
"buffer500.shp", "w", "ESRI Shapefile", schema) as output:
for point in input:
output.write({
'properties': {
'name': point['properties']['name']
},
'geometry': mapping(shape(point['geometry']).buffer(0.5))
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment