PyEPSGLookup

About

PyEPSGLookup is a small Python utility complementary to Projection module derived from Thuban project. It provides user with functionality to query epsg file for projection parameters by its number or name.

Note: In order to use PyEPSGLookup you need to download and install Projection module for Python as well.

Currently, PyEPSGLookup supports two formats of epsg file:

  • epsg file in original format as used by PROJ.4 and derived from EPSG database
  • epsg.proj file in XML format defined and used by Thuban. epsg.proj is generated from the PROJ.4’s epsg file using small utility script create_epsg.py available from Thuban’s CVS repository.

The idea is very simple. Here is step-by-step list of how PyEPSGLookup is used:

  1. Create projection lookup object and attach epsg data file that will be queried.
  2. Call one of find functions of lookup classes: find_by_id(id) or find_by_name(name)
    with projection EPSG id or name passed to them.
  3. If projection is found, use it’s parameters when calling Projection class functions to project your coordinates.

I hope examples below are clear enought to explain the idea in details.

If you have any questions, comments or you’ve found a bug then please give me a message. Thanks!

License

This software is released under the terms of the MIT License.

Download

pyepsglookup-1.2.zip

pyepsglookup-1.2.tar.gz

Example how to query PROJ.4’s epsg file

This example shows how to use PyEPSGLookup to query for projection parameters from standard epsg file. If you need, you can change the epsg file path in line 24 accordingly.

import os
import sys
import epsglookup
import Projection

try:
    # Simple configuration:
    # True - find projection by ID
    # False - find projection by Name
    lookup_by_id = False;

    #
    # 1. Preparation
    #

    # Input coordinates
    input_coords = (-118.0, 33.0)

    # Expected output with coordinates projected to UTM Zone 11
    output_coords = (406582.22, 3651730.97, 0.00)

    # epsg file location
    epsg_file = os.path.join(os.getcwd(), "epsg")

    # Instantiate lookup object attached to the epsg file.
    proj_lookup = epsglookup.ProjectionLookup(epsg_file)

    #
    # 2. Find projection definiton by EPSG id or name.
    #

    if lookup_by_id is True:
        # by EPSG id
        epsg_id_utm = 32611 # WGS 84 / UTM zone 11N
        epsg_utm_def = proj_lookup.find_by_id(epsg_id_utm)
    else:
        # by EPSG name
        epsg_name_utm = "WGS 84 / UTM zone 11N"
        epsg_utm_def = proj_lookup.find_by_name(epsg_name_utm)

    if epsg_utm_def is None:
        print "Projection not found"
        sys.exit()

    # Check what we've found
    print "Found projection parameters:"
    print "ID: ", epsg_utm_def.id
    print "Name: ", epsg_utm_def.name
    print "Params: ", epsg_utm_def.params

    #
    # 3. Project coordinates
    #
    proj = Projection.Projection(epsg_utm_def.params)

    print "---Project to UTM Zone 11---"
    print "Location: ", input_coords[0], input_coords[1]
    print “Forward: “, proj.Forward(input_coords[0], input_coords[1])
    print “—Back to WGS84—”
    print “Location: “, output_coords[0], output_coords[1]
    print “Forward: “, proj.Inverse(output_coords[0], output_coords[1])

except epsglookup.EPSGLookupError, err:
    print “Message: “, err.message, “, Ln: “, err.line, “,Col: “, err.column


Example how to query Thuban’s epsg.proj file

This example shows how to use PyEPSGLookup with Thuban’s epsg.proj file in XML format. This file stores projections in described in following format:

In order to use XML epsg.proj file you need to use ProjectionLookupXML class instead of ProjectionLookup. So, just find and replace following lines from the first example of PROJ.4’s epsg usage:

# epsg file location
    epsg_file = os.path.join(os.getcwd(), "epsg")

    # Init lookup object attached to the epsg file.
    proj_lookup = epsglookup.ProjectionLookup(epsg_file)

with these lines (If you need, you can change the epsg.proj file path in line 2):

# epsg file location
    epsg_file = os.path.join(os.getcwd(), "epsg.proj")

    # Init lookup object attached to the epsg file.
    proj_lookup = epsglookup.ProjectionLookupXML(epsg_file)

Enjoy!