Skip to content

Instantly share code, notes, and snippets.

@ptz0n
Created January 4, 2012 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptz0n/1560274 to your computer and use it in GitHub Desktop.
Save ptz0n/1560274 to your computer and use it in GitHub Desktop.
Convert geographical coordinates with Triona
<?php
/**
* Convert coordinates with projection
*
* @param string $easting X coordinate
* @param string $northing Y coordinate
* @param string $projection
*
* @return array
*/
function convert($easting, $northing, $projection) {
// Service endpoint
$endpoint = 'http://app.triona.se/proj/project.aspx';
// Valid projections
$projections = array(
'WGS84toRT90',
'RT90toWGS84',
'WGS84toUTM32',
'WGS84toUTM33',
'WGS84toUTM34',
'UTM32toWGS84',
'UTM33toWGS84',
'UTM34toWGS84',
'KKJtoWGS84',
'WGS84toKKJ'
);
$return = array();
if(in_array($projection, $projections)) {
$url = $endpoint.'?'.http_build_query(array(
'e' => $easting,
'n' => $northing,
'p' => $projection
));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($response = curl_exec($ch)) {
if(preg_match('/\((.+),(.+)\);$/', $response, $coordinates)) {
$return = array(
'x' => $coordinates[1],
'y' => $coordinates[2]
);
}
}
curl_close($ch);
}
else {
throw new Exception('Invalid projection "'.$projection.'".');
}
return $return;
}
/**
* Testing
*
*/
echo '<pre>';
print_r(convert('598024', '6642787', 'UTM32toWGS84'));
@ptz0n
Copy link
Author

ptz0n commented Jan 4, 2012

Outputs:

Array
(
    [x] => 10.7528310482097
    [y] => 59.9109098384598
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment