[mapguide-users] Convert lat lon to UTM

Jason Birch Jason.Birch at nanaimo.ca
Wed May 9 12:40:40 EDT 2007


Hi Jim,

I find EPSG codes easiest to work with, because they are numbered
consistantly.  For instance, UTM WGS84 North has a base of 32600, so for
UTM84-1N you add 32600 + 1 to get the EPSG code.

You can calculate the Zone fairly easily given a longitude value.  If
your longitudes are in the range -180 -> +180 the math is something like
(please double-check; off the top of my head):
  ( (lon + 180) div 6 ) + 1

You'll want to apply similar logic to tell whether you're in the north
or south.
  ( lat >= 0 ) ? 32600 : 32700

You could do the whole thing in one shot in PHP if you wanted

$epsg = ( ( ( $lat >= 0 ) ? 32600 : 32700 ) + ( floor( ( $lon + 180 ) /
6 ) + 1 ) );

Once you know the zone, you can use the coordinate system functions to
get back the correct WKT for that zone.

There are examples of flipping between EPSG and WKT under
/mapguide/mapagent/ in epsgcodetowkt.php.  Once you have the WKT, you
can do an MgCoordinateSystemFactory->Create($wkt) to get back an
MgCoordinateSystem.

Finally, you'd do a standard transform as shown in your example below to
get back the Northing and Easting.  In that example $yConv is the
Northing and $xConv is the Easting.

You'd have to implement something similar to get from the map's Albers
into LL in the first place, but it all follows the same pattern.

Jason


-----Original Message-----
From: Jim O'Leary
Subject: [mapguide-users] Convert lat lon to UTM

Is there a way to convert a lat lon to UTM and extract the zone,
northing, and easting? I have a form where the user enters a position
either by clicking on the map or by manually entering the coordinates.
The map is in BC Albers. They can also manually enter the UTM zone,
northing, and easting instead of the lat lon.

On the database side, it would be nice to convert what the user entered
so I can put both (lat,  lon) and (UTM zone, northing, easting) in the
database. 

I found some code in the Web API Reference under MgCoordinateSystem that
might be on the right track:

$wktProj = 'PROJCS["UTM Zone 18 (NAD 83)", GEOGCS ["NAD 83 (Continental
US)", DATUM ["NAD 83 (Continental US)", SPHEROID ["GRS 80", 6378137,
298.257222101]], PRIMEM [ "Greenwich", 0.000000 ], UNIT ["Decimal
Degree", 0.01745329251994330]], PROJECTION ["Transverse Mercator"],
PARAMETER ["Scale_Factor", 0.999600], PARAMETER ["Central_Meridian",
-75.000000], PARAMETER ["False_Easting", 500000.000000], UNIT ["Meter",
1.000000000000]]';
        $coordSysProj = $coordSysFactory->Create($wktProj);
        $xGeog = -71.061342;
        $yGeog = 42.355892;
        $coordinate = $geometryFactory->CreateCoordinateXY($xGeog,
$yGeog);
        $convertedCoordinate =
$coordSysProj->ConvertFromLonLat($coordinate);
        $xConv = $convertedCoordinate->GetX();
        $yConv = $convertedCoordinate->GetY();
        echo  "($xGeog, $yGeog) to ($xConv, $yConv)n";

However, this code assumes that you know what UTM zone you want. As
well, it does not show how to extract the northing and easting.


More information about the mapguide-users mailing list