Skip to content

Instantly share code, notes, and snippets.

@davidlevy
Created July 1, 2013 07:32
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save davidlevy/5898989 to your computer and use it in GitHub Desktop.
Save davidlevy/5898989 to your computer and use it in GitHub Desktop.
Function to convert Geo coordinates To Pixels
<?php
function convertGeoToPixel($lat, $lon){
$mapWidth = 400;
$mapHeight = 260;
$mapLonLeft = -180;
$mapLonRight = 180;
$mapLonDelta = $mapLonRight - $mapLonLeft;
//http://stackoverflow.com/questions/2103924/mercator-longitude-and-latitude-calculations-to-x-and-y-on-a-cropped-map-of-the/10401734#10401734
//-169.4531,-44.8403,177.8906,77.9157
//-180,-56.1700,180,83.6769
$mapLatBottom = -56.1700;
$mapLatBottomDegree = $mapLatBottom * M_PI / 180;
// global $mapWidth, $mapHeight, $mapLonLeft, $mapLonDelta, $mapLatBottom, $mapLatBottomDegree;
$x = ($lon - $mapLonLeft) * ($mapWidth / $mapLonDelta);
$lat = $lat * M_PI / 180;
$worldMapWidth = (($mapWidth / $mapLonDelta) * 360) / (2 * M_PI);
$mapOffsetY = ($worldMapWidth / 2 * log((1 + sin($mapLatBottomDegree)) / (1 - sin($mapLatBottomDegree))));
$y = $mapHeight - (($worldMapWidth / 2 * log((1 + sin($lat)) / (1 - sin($lat)))) - $mapOffsetY);
return array($x, $y);
}
?>
@canovack
Copy link

canovack commented May 20, 2019

Do you have reverse function of this ?(Etc: pixel to geo coordinates)

@ozcan-durak
Copy link

I m also insterested in reverse function of this as well.

@davidlevy
Copy link
Author

No sorry I didn't need it at this time
If someone can work on it I'd be happy to include it

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