[mapguide-users] Convert lat lon to UTM

Paul Spencer pagameba at gmail.com
Wed May 9 19:05:26 EDT 2007


Here's a function I built to help me measuring features that were in  
latlon - the function returns a suitable projection in meters to make  
the measurements more accurate.  I tried a few combinations :)

/* return a UTM WKT appropriate for a given lat/lon */
function getUtmWkt($lon, $lat) {
     /** WGS 84 / Auto UTM **/
     $zone = floor( ($lon + 180.0) / 6.0 ) + 1;

     //WGS84 AUTO UTM
     $epsg42001 = "PROJCS[\"WGS 84 / Auto UTM\",GEOGCS[\"WGS 84 
\",DATUM[\"WGS_1984\",SPHEROID[\"WGS_1984\", 
6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Decimal_Degree 
\",0.0174532925199433]],PROJECTION[\"Transverse_Mercator\"],PARAMETER 
[\"central_meridian\",%.16f],PARAMETER[\"latitude_of_origin\", 
0],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\", 
500000],PARAMETER[\"false_northing\",%.16f],UNIT[\"Meter\",1]]";

     //WGS 84 AUTO TRANSVERSE MERCATOR
     $epsg42002 = "PROJCS[\"WGS 84 / Auto Tr. Mercator\",GEOGCS[\"WGS  
84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS_1984\", 
6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Decimal_Degree 
\",0.0174532925199433]],PROJECTION[\"Transverse_Mercator\"],PARAMETER 
[\"central_meridian\",%.16f],PARAMETER[\"latitude_of_origin\", 
0],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\", 
500000],PARAMETER[\"false_northing\",%.16f],UNIT[\"Meter\",1]]";

     //WGS 84 AUTO ORTHOGRAHPIC
     $epsg42003 = "PROJCS[\"WGS 84 / Auto Orthographic\",GEOGCS[\"WGS  
84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS_1984\", 
6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Decimal_Degree 
\",0.0174532925199433]],PROJECTION[\"Orthographic\"],PARAMETER 
[\"central_meridian\",%.16f],PARAMETER[\"latitude_of_origin\",%. 
16f],UNIT[\"Meter\",1]]";

     //$wkt = sprintf( $epsg42001, -183.0 + $zone * 6.0, ($lat >=  
0.0) ? 0.0 : 10000000.0 );
     //$wkt = sprintf( $epsg42002, $lon, ($lat >= 0.0) ? 0.0 :  
10000000.0 );
     $wkt = sprintf( $epsg42003, $lon, $lat);
     return $wkt;
}

On 9-May-07, at 12:40 PM, Jason Birch wrote:

> 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.
> _______________________________________________
> mapguide-users mailing list
> mapguide-users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/mapguide-users

+-----------------------------------------------------------------+
|Paul Spencer                          pspencer at dmsolutions.ca    |
+-----------------------------------------------------------------+
|Chief Technology Officer                                         |
|DM Solutions Group Inc                http://www.dmsolutions.ca/ |
+-----------------------------------------------------------------+







More information about the mapguide-users mailing list