# Coordinate reference systems for Fiona

It's important to me that Fiona work with pyproj. This is how it does.

import logging import sys from pyproj import Proj, transform from fiona import collection logging.basicConfig(stream=sys.stderr, level=logging.INFO) with collection("docs/data/test_uk.shp", "r") as input: schema = input.schema.copy() # This is the input reference system. p_in = Proj(input.crs) with collection( "with-pyproj.shp", "w", "ESRI Shapefile", schema=schema, # New for 0.6: we can write coordinate reference systems. crs={'init': "epsg:27700"} ) as output: # This is the output reference system. p_out = Proj(output.crs) for f in input: try: assert f['geometry']['type'] == "Polygon" # Transform the coordinates of every ring. new_coords = [] for ring in f['geometry']['coordinates']: x, y = transform(p_in, p_out, *zip(*ring)) new_coords.append(zip(x, y)) f['geometry']['coordinates'] = new_coords output.write(f) except Exception, e: # Writing untransformable features to a different shapefile # is another option. logging.exception("Error transforming feature %s:", f['id'])

As I've said before, Python's `zip` is the essential coordinate wrangling
function.

**Update** (2012-01-05): after https://github.com/Toblerity/Fiona/commit/8b355678bab52f41b737383e2701f4137c2002b4 a `crs` takes the form of a mapping instead of a string.

## Comments

### Re: Coordinate reference systems for Fiona

Author: Even Rouault

Sean,

Why not

f['geometry']['coordinates'] = transform(p_in, p_out, f['geometry']['coordinates'])

or something close instead of iterating over each ring/subgeometry part ?

OGR SWIG bindings have a Geometry.Transform() ;-)

### Re: Coordinate reference systems for Fiona

Author: Sean

The reason why is that pyproj's transform function is very simple and limited. It operates only on coordinate sequences (Python buffers, to be precise). It doesn't have a concept of geometry or parts or rings. But because of its simple design, you can combine it with Numpy, matplotlib, and the universe of not-GIS Python software.

Geometry.Transform() embodies a number of patterns that I'm deliberately avoiding in my work on Fiona such as excessive object orientation, mutability, and capitalized method names ;) And while it may be easier in the short run, I'm in pursuit of simpler.