image coordinates to geographic coordinates

Jan Hartmann j.l.h.hartmann at UVA.NL
Thu Oct 21 10:29:48 EDT 2004


temiz wrote:
>
> Despite all my  efforts, I can not correctly convert image coordinates
> to geographic coordinates (UTM).
>  what might  the javascript code be ?
>
> here is my code:
>    x_pct = (evt.clientX / width);
>     y_pct = 1 - (evt.clientY / height);
>     x_map = ext0 + ( (ext2 - ext0) * x_pct);
>     y_map = ext1+ ( (ext3 - ext1) *  y_pct);
>

This should work, provided that width and height are the current values
of the image's MAPSIZE and ext-ext3 the current values of MAPEXT. A
problem could be that you use global coordinates for the click: clientX
and clientY are measured from the upper left corner of the browser
window. If your image is somewhere else on the page, you need to
subtract the coordinates of its upper left corner from the global click
coordinates. For simple web pages (without nested HTML-elements) you can
do this simply by:

<img id=mapImgId>
<script>
var mapImg = document.getElementById("mapImgId")
var imgX = evt.clientX - mapImg.offsetLeft
var imgY = evt.clientY - mapImg.offsetTop
x_pct = imgX / width   // imgX / mapImg.offsetWidth would work too
...
</script>

Hope this helps,

Jan


Jan Hartmann
Department of Geography
University of Amsterdam



More information about the mapserver-users mailing list