Debian Codegraph @

examples/with-pyproj.py

raw
1 import logging
2 import sys
3
4 from pyproj import Proj, transform
5
6 import fiona
7 from fiona.crs import from_epsg
8
9 logging.basicConfig(stream=sys.stderr, level=logging.INFO)
10
11 with fiona.open('docs/data/test_uk.shp', 'r') as source:
12
13 sink_schema = source.schema.copy()
14 p_in = Proj(source.crs)
15
16 with fiona.open(
17 'with-pyproj.shp', 'w',
18 crs=from_epsg(27700),
19 driver=source.driver,
20 schema=sink_schema,
21 ) as sink:
22
23 p_out = Proj(sink.crs)
24
25 for f in source:
26
27 try:
28 assert f['geometry']['type'] == "Polygon"
29 new_coords = []
30 for ring in f['geometry']['coordinates']:
31 x2, y2 = transform(p_in, p_out, *zip(*ring))
32 new_coords.append(zip(x2, y2))
33 f['geometry']['coordinates'] = new_coords
34 sink.write(f)
35
36 except Exception as e:
37 # Writing uncleanable features to a different shapefile
38 # is another option.
39 logging.exception("Error transforming feature %s:", f['id'])
40