[mapserver-users] please help with lat/on to xy, part 2
Jan Mantkowski
jan.mantkowski at gmx.de
Tue Jul 23 23:44:01 PDT 2002
Hi,
Please may I ask for your help with a formulae if found at
http://www.npm.ac.uk/rsdas/doc/description.html
----------------------------- snip ----------------------------------------
How to find a latitude/longitude point within an image
The images we produce are in Mercator projection and there are a set of
fairly simple formulae for converting latitude/longitude (lat/lon) positions
to x/y pixel co-ordinates.
rows - number of rows in image
cols - number of columns in image
minlon/maxlon - minimum/maximum longitude of image (in decimal degrees)
minlat/maxlat - minimum/maximum latitude of image (in decimal degrees)
Note that these are the coordinates of the CENTRE of the corner pixels.
DEGTORAD - conversion from degrees to radians (PI/180.0)
ln - natural log
Calculate X position.
FractX = (lon - minlon) / (maxlon - minlon)
x = (cols - 1) * FractX
Calculate Y position.
Ymin = ln (tan (DEGTORAD * (45.0 + (minlat / 2.0))))
Ymax = ln (tan (DEGTORAD * (45.0 + (maxlat / 2.0))))
Yint = ln (tan (DEGTORAD * (45.0 + (lat / 2.0))))
FractY = (Yint - YMin) / (YMax - YMin)
y = (rows - 1) * (1.0 - FractY)
----------------------------------- snap ------------------------------
I took your formulae to make this php formulae:
$Ymin = log (tan (deg2rad (45 + ($minlat / 2))));
$Ymax = log (tan (deg2rad (45 + ($maxlat / 2))));
$Yint = log (tan (deg2rad (45 + ($lat / 2))));
$y = ($width - 1)*(1 -(($Yint - $YMin)/($YMax - $YMin)));
$x = round(($height - 1) * (($lon - $minlon) / ($maxlon - $minlon)));
I use this basic data:
$height="411";
$width="449";
$maxlat="64.13";
$minlat="34.05";
$minlon="-21.95";
$maxlon="30.46";
Unfortunately I always get an error "division by zero".
($YMax - $YMin) seems to be zero under all circumstances.
Please can you help me to correct this?
I am not sure if it is the right way to take the width and height of the
image for the cols and rows the formulae.
You can see my corresponding map at my site:
http://www.jan-mantkowski.de/map/index.php?
It is a unprojected mercator map, from
http://stellwagen.er.usgs.gov/mapit//
and bases on the airports of Reykjavik (Island), Kiev and Rabat.
So I know that at least the coordinates and projection of the map itslef are
right.
Thank you very much in advance,
Jan
Luton, England
e-mail: jan.mantkowski at gmx.de
More information about the MapServer-users
mailing list