Pages

Friday 30 May 2008

How to look up the EPSG code for a CoordinateReferenceSystem

Today's question comes for the uDig developers list; this is a fairly classic problem - shapefile store their CoordinateReferneceSystem definition as a .prj file, containing "Well-Known-Text". How can you look up the "official" EPSG code for a CoordinateReferenceSystem? And why would you want to?

A CoordinateReferenceSystem generated from "raw" WKT:
  • Often does not have an EPSG code. These codes are useful to know when working with data from a range of sources. If we can figure out the code we can construct a WMS GetMap request (for a base map layer) that matches the shapefile.
  • Often lacks some of the meta-data used when creating a good MathTransform
Here is how to look up an "official" CoordinateReferenceSystem.
  1. Construct a CoordinateReferenceSystem based on what you have.
    String name = "ED50";
    String wkt =
    "GEOGCS[\"" + name + "\",\n" +
    " DATUM[\"European Datum 1950\",\n" +
    " SPHEROID[\"International 1924\", 6378388.0, 297.0]],\n" +
    "PRIMEM[\"Greenwich\", 0.0],\n" +
    "UNIT[\"degree\", 0.017453292519943295]]";
    CoordianteReferenceSystem example = CRS.parseWKT(wkt);
  2. Find the Identifier for your CoordinateReferenceSystem
    String code = CRS.lookupIdentifier( example, true ); // should be "EPSG:4230
  3. Look up the official definition for that Identifier
    CoordinateReferenceSystem crs = CRS.decode( code );
You will find the official CoordinateReferenceSystem is much better at generating MathTransforms.
CoordianteReferenceSystem wsg84 = CRS.decode( "EPSG:4326" );
MathTransform transform = CRS.findMathTransform(crs, wsg84, true);
Geometry targetGeometry = JTS.transform( sourceGeometry, transform );
If you do have any trouble - set the lenient flag to false, the transform will no longer be as accurate (as WKT by itself does not have enough information to account for datum shifts etc...).
CoordianteReferenceSystem wsg84 = CRS.decode( "EPSG:4326" );
MathTransform transform = CRS.findMathTransform(example, wsg84, false );
Geometry targetGeometry = JTS.transform( sourceGeometry, transform );
Documentation harmed in the making of this post:

4 comments: