You get a bonus - 1 coin for daily activity. Now you have 1 coin

How to convert coordinates from spherical to Cartesian on a flat map

Practice




how to convert coordinates from spherical to Cartesian on a flat map
how to get the coordinates of a point on a world map

The Earth is round, but monitors are flat. In order to be able to view the Earth on a monitor, there are methods for projecting the globe onto a plane.
One of these methods is the Gerardus Mercator projection, which consists in stretching the globe near the poles into a cylinder, and then cutting the resulting cylinder along the 180th meridian, which results in a flat image.

Since the map turns out to be very stretched near the poles, small pieces at the top and bottom of the map are cut off (leaving a latitude range of roughly -85° to 85°). The result is a map like this:

How to convert coordinates from spherical to Cartesian on a flat map

On Wikipedia you can download a high-resolution Mercator projection map, which is what we will be drawing on.

Now how do we display a point with a given coordinate on this map?
Longitude is not distorted in the Mercator projection. This means that to find point X on the map, we simply need to multiply the longitude by the map's X scale and add the resulting number to the point marking the middle of the map.

Latitude is more complicated. The scale increases from the equator toward the poles. The farther from the equator, the greater the distortion.
The scale increases according to this formula:

How to convert coordinates from spherical to Cartesian on a flat map

Here φ is the latitude (in radians), y is the distortion coefficient.
Knowing this, we multiply the latitude by the Y scale and by the distortion coefficient.

Briefly, the differences from the Mercator projection:

The projection is not distorted along latitude, accordingly latitude is calculated the same way as longitude.
There are several advantages to using this projection: first, the latitude range increases somewhat; second, points are calculated somewhat faster, since the math operations are primitive.
However, there is one minor downside — the map is slightly "compressed" vertically.






if(@$_POST['map_x'])

{
$width = 222;
$height = 111;
$values = getlocationcoords_inv($_POST['map_x'],$_POST['map_y'], $width,$height);
}



function getlocationcoords_inv($x,$y, $width,$height)
{
$lat = ((($y / $height) * 180) - 90) * -1;
$lon =(($x / $width) * 360) - 180;
return array($lat,$lon);

}

?>


Longitude:

Latitude:




?>



if you need the opposite, from spherical to Cartesian, in javascript


var MAP_WIDTH = 500;
var MAP_HEIGHT = 200;

function ra_de(value) {
var pi = Math.PI;
var ra_de = value*(180/pi);
return ra_de;
}



function de_ra(value) {
var pi = Math.PI;
var de_ra = $value*(pi/180);
return de_ra;
}




func tion conv ert($latitude, $longitude){


/* for Mercator
var x = (MAP_WIDTH / 2 + ($longitude * 85 / 15));
var y = (MAP_HEIGHT / 2 - ra_de( Math.log(Math.tan((Math.PI / 4) + de_ra($latitude) / 2))) * 85 / 15);
*/

/* for equirectangular*/
var x =-MAP_WIDTH * (-$longitude -180) / 360-13;
var y = MAP_HEIGHT *( 90-$latitude) / 180-38;

return {x:(x),y:(y)};
}

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Running server side scripts using PHP as an example (LAMP)"

Terms: Running server side scripts using PHP as an example (LAMP)