[Proj] From proj4 string to EPSG code

Glynn Clements glynn at gclements.plus.com
Tue Aug 12 07:04:59 EDT 2008


Maarten Plieger wrote:

> Is there a simple way to convert from a proj4 string to an EPSG code?
> My proj4 string has the same format as the strings in the epsg 
> definition file.

If the format is identical in every regard, you could just "grep" for
the string then extract the code from the output.

But it would be more robust to parse each line and check that the
key/value pairs match. Even then, there's the possibility that some
floating-point value (e.g. the ellipsoid radius) won't match exactly
e.g. due to trailing zeroes or some negligible difference in value.

Other than the last issue, the following python script will do the
job:

#!/usr/bin/env python
import sys

myparms = sorted(sys.argv[1:])
# use this instead if you want one argument rather than many:
# myparms = sorted(sys.argv[1].split())

f = file("/usr/share/proj/epsg")
for line in f:
    if line.startswith("#"):
        continue
    l = line.split()
    code = l[0].strip("<>")
    parms = l[1:-1]
    if sorted(parms) == myparms:
        print code
        break
f.close()

Example usage:

	$ ./epsg.py +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.999601 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs
	27700

If you need to handle the precision issues, things get more
complicated.

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the Proj mailing list