public
Last active

Mersh.txt requirements file for pip

  • Download Gist
mersh.txt
1 2 3
Fiona==0.8
Pyproj==1.9.0
Shapely==1.2.14
with-pyproj-shapely.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
# Example of using Fiona, pyproj, and Shapely together. Install all of them like this:
#
# $ pip install -r https://gist.github.com/raw/1689767/34dedfd28546d41f8dbd7a08ccf2c8abf9ebdf6a/mersh.txt
 
import logging
import sys
 
from fiona import collection
from pyproj import Proj, transform
from shapely.geometry import mapping, shape
 
 
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
 
with collection("docs/data/test_uk.shp", "r") as input:
schema = input.schema.copy()
p_in = Proj(input.crs)
 
with collection(
"with-pyproj-shapely.shp", "w", "ESRI Shapefile",
schema=schema,
crs={'init': "epsg:27700", 'no_defs': True}
) as output:
p_out = Proj(output.crs)
for f in input:
try:
 
# Transform the feature geometries.
assert f['geometry']['type'] == "Polygon"
new_coords = []
for ring in f['geometry']['coordinates']:
x2, y2 = transform(p_in, p_out, *zip(*ring))
new_coords.append(zip(x2, y2))
f['geometry']['coordinates'] = new_coords
 
# Ensure that they are valid and "clean".
geom = shape(f['geometry'])
if not geom.is_valid:
clean = geom.buffer(0.0)
assert clean.is_valid
assert clean.geom_type == 'Polygon'
geom = clean
f['geometry'] = mapping(geom)
 
output.write(f)
except Exception, e:
# Writing uncleanable features to a different shapefile
# is another option.
logging.exception(
"Error transforming or cleaning feature %s:", f['id'])

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.