Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open source PHP function for converting UTM coordinates to latitude and longitude?

I'm making a PHP application involving Google Maps. Maps only accepts lat&lng pairs, and the data I want to display comes only with UTM style coordinates. Is there an open-source PHP function to convert from one to the other?

Something like this would be great:

$UTM_ZONE = '32';
$UTMX = '60329834,34';
$UTMY = '67382984,9';

$latlng = convert($UTM_ZONE, $UTMX, $UTMY);

// $latlng = now looks like
// array('lat' => '59.4472917501', 'lng' => '5.3928572425')
like image 935
Hubro Avatar asked Feb 08 '12 23:02

Hubro


2 Answers

source link

<?php
function LatLonPointUTMtoLL($f, $f1, $j = 32) {
    
    $d = 0.99960000000000004;
    $d1 = 6378137;
    $d2 = 0.0066943799999999998;
    
    $d4 = (1 - sqrt(1 - $d2)) / (1 + sqrt(1 - $d2));
    $d15 = $f1 - 500000;
    $d16 = $f;
    $d11 = (($j - 1) * 6 - 180) + 3;
    $d3 = $d2 / (1 - $d2);
    $d10 = $d16 / $d;
    $d12 = $d10 / ($d1 * (1 - $d2 / 4 - (3 * $d2 * $d2) / 64 - (5 * pow($d2, 3)) / 256));
    $d14 = $d12 + ((3 * $d4) / 2 - (27 * pow($d4, 3)) / 32) * sin(2 * $d12) + ((21 * $d4 * $d4) / 16 - (55 * pow($d4, 4)) / 32) * sin(4 * $d12) + ((151 * pow($d4, 3)) / 96) * sin(6 * $d12);
    $d5 = $d1 / sqrt(1 - $d2 * sin($d14) * sin($d14));
    $d6 = tan($d14) * tan($d14);
    $d7 = $d3 * cos($d14) * cos($d14);
    $d8 = ($d1 * (1 - $d2)) / pow(1 - $d2 * sin($d14) * sin($d14) , 1.5);
    $d9 = $d15 / ($d5 * $d);
    $d17 = $d14 - (($d5 * tan($d14)) / $d8) * ((($d9 * $d9) / 2 - (((5 + 3 * $d6 + 10 * $d7) - 4 * $d7 * $d7 - 9 * $d3) * pow($d9, 4)) / 24) + (((61 + 90 * $d6 + 298 * $d7 + 45 * $d6 * $d6) - 252 * $d3 - 3 * $d7 * $d7) * pow($d9, 6)) / 720);
    $d17 = rad2deg($d17);
    $d18 = (($d9 - ((1 + 2 * $d6 + $d7) * pow($d9, 3)) / 6) + (((((5 - 2 * $d7) + 28 * $d6) - 3 * $d7 * $d7) + 8 * $d3 + 24 * $d6 * $d6) * pow($d9, 5)) / 120) / cos($d14);
    $d18 = $d11 + rad2deg($d18);
    return array(
        'lat' => $d17,
        'lng' => $d18
    );
}
?>
like image 152
Morteza Sepehri Niya Avatar answered Oct 28 '22 22:10

Morteza Sepehri Niya


I know it's late to answer this question but since I couldn't use any of the above codes, I wrote my own version which is actually very easy to use. This is the address: https://github.com/maroofi/coordinates To convert UTM to LatLong:

utm2ll(729286.9550018794,4021544.8279992654,40,true); 

output:

{"success":true,"attr":{"lat":36.311665575271,"lon":59.553858137274}} 

To convert LatLong to UTM:

ll2utm(36.311665575277935,59.55385813725379);

output:

{"success":true,"attr":{"x":729286.95500188,"y":4021544.8279993,"zone":40,"aboveEquator":true}} 

Hope it helps.

like image 32
Sourena Avatar answered Oct 28 '22 20:10

Sourena