Hack 26. Work with Different Coordinate Systems
Your GPS gives you latitude and longitude, but the maps in your GIS system use UTM coordinates. Fortunately, you can reconcile the two.
Living on a round planet has its pros and cons. On the pro side, you can't, for example, sail or fly off the edge! This is a pretty big pro, all things considered. The con side, however, is that the task of mapping the planet is riddled with difficulties for would-be cartographers. For example, doing any kind of comparison between locations using latitude and longitude requires deploying heavy-duty spherical trigonometry [Hack #27] . It sure would be nice to be able to conveniently ignore the roundness of our lovely planet as needed, but, to do so, we have to somehow project its surface onto a plane in a way that preserves distance and bearingor else, why bother?
Enter the Universal Transverse Mercator, or UTM, coordinate system. UTM is a rectangular coordinate system that treats Earth as a single flat plane or, rather, as 60 flat planes called UTM zones. As you'll probably recall from looking at maps of the world, the Mercator projection preserves land shapes well near the equator but distorts them very badly at the Poles. The Transverse Mercator projection rotates the Mercator by 90 degrees, so that the distortion is pushed out to the sides instead. This makes the Transverse Mercator a popular choice for mapping regions that are longer north-to-south than east-to-west, such as Great Britain, for example.
The universal aspect of UTM lies in the way it divides most of the world into rectangles, or zones, oriented north-to-south to minimize distortion, each one 6 degrees of longitude wide by 8 degrees of latitude high. At the equator, each zone is about 414 miles wide by 552 miles high. Each of the north/south columns is numbered from 1 to 60, starting at the international date line, going east. The rows are given letters, starting with C for the row from 72 to 80 degrees South, up to row X for 72 to 84 degrees North. The letters I and O are not used to avoid confusion with 1 and 0. A couple of minor alterations are made to this grid system in the North Sea to keep bits of Norway in the same zone. A nice map of UTM grid zones is at http://www.dmap.co.uk/utmworld.htm.
Within each zone, UTM coordinates are expressed in meters as an easting offset from the base longitude, and a northing offset from the base latitude. To ensure that UTM coordinates are always positive, a false easting of 500,000 meters is added to all UTM coordinates, along with a false northing of 10,000,000 meters when coordinates lie south of the equator.
The main advantage of UTM is that it is generally easier to think in terms of meters than degrees, minutes, and seconds. Similarly, it is much easier to calculate an imperfect but reasonably accurate distance between two UTM coordinates by ignoring the curvature of the Earth and simply using the Pythagorean Theorem. Distances calculated this way should be multiplied by a scale factor of .9996 to average out the effects of distortion across the zone.
Like converting different representations of latitude and longitude [Hack #25] , the easiest way to "convert" Lat/Long to UTM is to simply have your GPS receiver display coordinates in your preferred format. On some Garmin units, you can select Menu
The next easiest conversion approach is to use an online coordinate converter, such as http://www.ngs.noaa.gov/TOOLS/utm.html.
3.6.1. Converting with the PROJ.4 Toolkit
The PROJ.4 toolkit provides a tool called proj for just this sort of task. PROJ.4 is in Debian APT and can also be obtained at http://proj.maptools.org/. proj accepts coordinate pairs, one per line, separated by whitespace, with longitude given first. Anything after the first two numbers is considered to be a label and is preserved in the output. Suppose we have some data in places.txt:
123d03'02.97"W 38d21'56.98"N Alice Rock 122d48'13.00"W 38d34'09.01"N Allan Ranch 122d23'57.01"W 38d16'27.01"N Arrowhead Mount 123d02'26.01"W 38d28'44.00"N Austin Gap
We can use proj to turn these coordinates into UTM as follows:
$ proj +proj=utm +zone=10 < places.txt 495559.96 4246406.86 Alice Rock 517108.31 4268986.90 Allan Ranch 552554.55 4236406.09 Arrowhead Mount 496462.37 4258951.69 Austin Gap
The only downside of using proj this way is that you have to know which longitudinal zone you're in. In this case, we knew we were in zone 10 by looking at the UTM grid zone map at http://www.dmap.co.uk/utmworld.htm. Although this example used coordinates in DMS format, you can also use degrees and decimal minutes, or just decimal degrees (see [Hack #25] for more details).
You can use the invproj program from PROJ.4, with the same command-line arguments and file format, to convert UTM coordinates back into Lat/Long.
3.6.2. Perl to the Rescue (Again)
The Geo::Coordinates::UTM module by Graham Crookham provides both a readable description of the UTM system, as well as a nice bit of Perl to convert UTM to and from lat/long. Geo::Coordinates::UTM is available from the CPAN. The following code performs the conversion:
#!/usr/bin/perl use Geo::Coordinates::UTM; my ($longitude, $latitude) = @ARGV; my $ellipsoid = 23; # WGS-84 ($zone,$easting,$northing)=latlon_to_utm($ellipsoid,$latitude,$longitude); print "easting: $easting northing: $northing zone: $zone "; ($latitude,$longitude)=utm_to_latlon($ellipsoid,$zone,$easting,$northing); print "latitude: $latitude longitude: $longitude ";
Running this we get:
$ perl utm_example.pl -122.829027 38.402528 easting: 514928.556644582 northing: 4250491.70355821 zone: 10S latitude: 38.4025280009781 longitude: -122.829026999993
For more on lat/long conversions, see [Hack #25]