[mapserver-users] calculating x,y from lat,lon
imap at chesapeake.net
imap at chesapeake.net
Mon Sep 17 14:40:14 PDT 2001
Walt,
Here is a piece of javascript code that works OK with projected
map coordinates, but I am not sure how it will work with unprojected
lat/lon values. Typically I call this before I submit to the
server. Your milage may vary. I have similar problems getting
accuracy going from x,y -> lat/lon -> projected map coordinates.
(mainly because no proj hooks from js.) If anyone has additional
insight (and/or code) to deal with this problem, please forward
to the list.
Regards,
Chris Stuber (mapsurfer)
function computeExtent() {
var mapext = new Array(4);
var imgext = document.mapserv.imgext.value.split(" ");
var imgbox = document.mapserv.imgbox.value.split(" ");
var imgxy = document.mapserv.imgxy.value.split(" ");
var cellsize, offsetx, offsety;
if((imgbox[0] != imgbox[2]) && (imgbox[1] != imgbox[3])) { // working from an
image box
cellsize = (imgext[2]-imgext[0])/(sx-1); // cells are square
mapext[0] = 1.0*imgext[0] + cellsize*imgbox[0];
mapext[2] = 1.0*imgext[0] + cellsize*imgbox[2];
mapext[1] = 1.0*imgext[3] - cellsize*imgbox[3];
mapext[3] = 1.0*imgext[3] - cellsize*imgbox[1];
} else { // working from an image x,z (imgxy)
var x, y, zoom=1;
if(document.mapserv.zoomdir[0].checked) // zooming in
zoom = document.mapserv.zoomsize.value;
if(document.mapserv.zoomdir[1].checked) // zooming out
zoom = 1/document.mapserv.zoomsize.value;
cellsize = (imgext[2]-imgext[0])/(sx-1); // cells are square
x = 1.0*imgext[0] + cellsize*imgxy[0];
y = 1.0*imgext[3] - cellsize*imgxy[1];
mapext[0] = x - .5*((imgext[2] - imgext[0])/zoom);
mapext[1] = y - .5*((imgext[3] - imgext[1])/zoom);
mapext[2] = x + .5*((imgext[2] - imgext[0])/zoom);
mapext[3] = y + .5*((imgext[3] - imgext[1])/zoom);
}
// adjust the map exent to optimally fit the image size
cellsize = Math.max((mapext[2] - mapext[0])/(sx-1), (mapext[3] -
mapext[1])/(sy-1));
offsetx = Math.round(Math.max(((sx-1)-(mapext[2]-mapext[0])/cellsize)/2,0));
offsety = Math.round(Math.max(((sy-1)-(mapext[3]-mapext[1])/cellsize)/2,0));
mapext[0] = mapext[0] - offsetx*cellsize;
mapext[1] = mapext[1] - offsety*cellsize;
mapext[2] = mapext[2] + offsetx*cellsize;
mapext[3] = mapext[3] + offsety*cellsize;
var temp = mapext[0]+" "+mapext[1]+" "+mapext[2]+" "+mapext[3];
document.mapserv.mapext.value = temp;
}
Walt Lin wrote:
>
> Hi all,
>
> I have mapserver generating a map image for me, and then I do my own calculation of x,y from lat,lon for certain points and overlay them. Sometimes they line up correctly, but most of the time, the x coordinate is very accurate but the y coordinate is off by a small margin. I would like to know the formula mapserver uses to calculate the pixel x,y from the lat,lon of a point, in the decimal degrees units. I have looked through the source but couldn't find it. The supposedly incorrect formula I have been using is:
>
> x = (point_lon - left_lon) / (right_lon - left_lon) * image_width
> y = (1 - (point_lat - lower_lat) / (upper_lat - lower_lat)) * image_height
>
> (the pixel origin being in the upper left corner). Also, it is not feasible to simply give mapserver the point and have it do the work. I need to do the calculation outside of mapserver. I am guessing that it has something to do with the curvature of the earth and whether it is projecting points with a normal to the earth's surface or a normal to the plane of the image?
>
> Thanks for any help,
> Walt
More information about the MapServer-users
mailing list