[a little bit OT] calculating X,Y from latitude and longitude

Luca Bertoncello lucabert at lucabert.de
Tue Aug 19 02:01:01 PDT 2025


Am 19.08.2025 10:29, schrieb Kobben, Barend (UT-ITC):

     You can’t unless you know which X & Y you want/mean. It is NOT
     correct to say that y is latitude and x is longitude!! XY means you
     use a cartesian coordinate system (the map = flat rectangle) instead
     of lat & lon which are angles from the center of a spheroid or
     ellipsoid (the globe = a 3D object).

     We call the transformation from 3D globe to flat map a projection.
     There are endless ways of projecting. Choose one of those 
projections
     and do the math and then you go from lat-lons to y-es and x-es...


Can you suggest me a way?

     For a nice overview of all this see e.g.
     https://kartoweb.itc.nl/geometrics


I think, I don't have the needed mathematical knowledge to understand 
that... :(

I try to explain again what I need...
I created an image, putting more tiles together. This is a PNG.
Then I try to add some markers (airports) at known coordinates. And I 
need to convert this coordinates in X,Y related to my image.
I searched many procedures in Internet, but they always position the 
markers at the wrong place.
I tried now a very simple procedure that assumes Earth is flat, but it 
does not work correctly, too...

This is my code (PHP):

function latlngToScreenXY($lat, $lng, $minLat, $minLng, $maxLat, 
$maxLng, $mapWidth, $mapHeight)
{
   $lngDelta = $maxLng - $minLng;
   $relLng = $lng - $minLng;
   $percLng = $relLng / $lngDelta;
   $x = $mapWidth * $percLng;

   $latDelta = $maxLat - $minLat;
   $relLat = $lat - $minLat;
   $percLat = $relLat / $latDelta;
   $y = $mapWidth * $percLat;

   return array('X' => $x, 'Y' => $mapHeight - $y);
}

You can see the result as attachment.
In the Map I drawed two circles: the first one at the south-west corner 
of the map. This is correct.
The second one should be in the middle of the map ( ($maxLat - $minLat) 
/ 2 + $minLat, ($maxLng - $minLng) / 2 + $minLng ), and you see, this is 
NOT correct.
Finally I tried to add the markers for the airports of Kamenz and 
Bautzen. And they are on the wrong position (both are souther than the 
correct position, Kamenz is wester and Bautzen is easter, too).

Can you please send me at least a pseudocode to understand you to 
convert LatLng to Y,X of an image?

Thanks
Luca Bertoncello
(lucabert at lucabert.de)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: map.jpg
Type: image/jpeg
Size: 333650 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20250819/474956e7/attachment.jpg>


More information about the postgis-users mailing list