File: with-pyproj.py

package info (click to toggle)
fiona 1.10.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,660 kB
  • sloc: python: 12,616; makefile: 214; sh: 45
file content (40 lines) | stat: -rw-r--r-- 1,162 bytes parent folder | download | duplicates (2)
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
import logging
import sys

from pyproj import Proj, transform

import fiona
from fiona.crs import from_epsg

logging.basicConfig(stream=sys.stderr, level=logging.INFO)

with fiona.open('docs/data/test_uk.shp', 'r') as source:
    
    sink_schema = source.schema.copy()
    p_in = Proj(source.crs)

    with fiona.open(
            'with-pyproj.shp', 'w',
            crs=from_epsg(27700),
            driver=source.driver,
            schema=sink_schema,
            ) as sink:
        
        p_out = Proj(sink.crs)

        for f in source:
            
            try:
                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
                sink.write(f)
            
            except Exception as e:
                # Writing uncleanable features to a different shapefile
                # is another option.
                logging.exception("Error transforming feature %s:", f['id'])