coordinate systems transformations

Gregor Mosheh stigmata_blackangel at YAHOO.COM
Thu Mar 24 16:39:31 PST 2005


> Does map server provides methods for transformation
> among the various coordinate systems that are used
> in the application (i.e. degrees,
> georeference extents, pixels, etc)?

MapServer itself does not. But there are ways to do it
that aren't terribly complicated. The following recipe
is how I made a map image clickable, while keeping
track of the projected coords.

// handle_map_click() figures out the map coords of
where you clicked
// and sets the variables coord_x and coord_y
// the [minx], et al are MapServer template markups
and will be replaced with
//    the correct numbers automagicallly
// the image size is hardcoded, but there is probably
a MapServer markup
//    that would have filled those in, too
// The variable 'image' is the HTML/DOM image object,
so be sure to name your image
function handle_map_click(image) {
   // some things are hardcoded
   image_width  = 200;
   image_height = 200;

   // the current display map's boundaries
   var display_lower_x = [minx];
   var display_lower_y = [miny];
   var display_upper_x = [maxx];
   var display_upper_y = [maxy];

   // convert the mouse click into pixelcoords
   clickcoords = GetClickCoordinates();
   imagecoords = GetObjectCoordinates(image);
   click_x = clickcoords.x - imagecoords.x;
   click_y = clickcoords.y - imagecoords.y;
   click_y = image_height - click_y; // for GIS, the
origin is lower-left, not upper-left

   // figure out how many meters are represented by 1
pixel
   var x_perpixel = (display_upper_x -
display_lower_x) / image_width ;
   var y_perpixel = (display_upper_y -
display_lower_y) / image_height;

   // now convert pixels into meters
   coord_x = display_lower_x + (click_x * x_perpixel)
   coord_y = display_lower_y + (click_y * y_perpixel)

   // we don't want to actually return anything, cuz
we're probably being called
   // by a click on an image, and returning false
keeps the form from being submitted
   return false;
}


These other two functions I got from the 'net
someplace. Sadly, I don't recall where so I am unable
to give credit. They figure out mouse clicks and image
positions.

// a function to fetch the location of the mouse-click
relative to the document's origin
function GetClickCoordinates() {
   if (window.Event) {
      mouseX = e.pageX;
      mouseY = e.pageY;
   }
   else {
      mouseX = window.event.clientX +
document.body.scrollLeft;
      mouseY = window.event.clientY +
document.body.scrollTop;
   }
  return {x:mouseX, y:mouseY}
}


// a function to return the location of an object
relative to the document's origin
function GetObjectCoordinates(obj) {
   return { x:GetObjectX(obj) - 2, y:GetObjectY(obj) -
2 };
}
function GetObjectX(obj) {
   var curleft = 0;
   if (obj.offsetParent) {
      while (obj.offsetParent) {
         curleft += obj.offsetLeft
         obj = obj.offsetParent;
      }
   }
   else if (obj.x) {
      curleft += obj.x;
   }
   return curleft-2;
}
function GetObjectY(obj) {
   var curtop = 0;
   if (obj.offsetParent) {
      while (obj.offsetParent) {
         curtop += obj.offsetTop
         obj = obj.offsetParent;
      }
   }
   else if (obj.y) {
      curtop += obj.y;
   }
   return curtop-2;
}



To make the image active and clickable, I use a form
whose input "button" is the image:

<form>
<input type="image" src="[img]" onClick="return
handle_map_click(this);">
</form>



__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com



More information about the MapServer-users mailing list