Click -> map coords

Aaron Craig mercutio at POBOX.COM
Tue Dec 21 02:25:54 PST 2004


I'm still fairly new to mapserver, and am experiencing some strange
behavior which probably has a straightforward explanation.

I'm using Perl to extend mapserver's functionality through the mapscript
API.

I have stolen a function for converting mouse click coordinates to
mapfile coordinates (I can't remember who on this list directed me to
the script a lifted the code from, but thanks!):

sub PixelsToMapCoords
    {
    my($map, $nPixelsX, $nPixelsY) = @_;
    return undef
        if(!$map || !$nPixelsX || !$nPixelsY)

    my $minx = $map->{extent}->{minx};
    my $miny = $map->{extent}->{miny};
    my $maxx = $map->{extent}->{maxx};
    my $maxy = $map->{extent}->{maxy};

    my $cx = ($maxx - $minx) / ($map->{width} - 1); # calculate cellsize
in x and y
    my $cy = ($maxy - $miny) / ($map->{height} - 1);

    my $x = $minx + $cx * $nPixelsX; # change x,y from image to map
coordinates
    my $y = $maxy - $cy * $nPixelsY;

    return { 'x' => $x, 'y' => $y };
    }

However, this function is giving the wrong result -- and I'm not sure
whether the error is in the code or in my mapfile.

A click near the center of the map gives coordinates that are close to
correct.  The farther towards the edge of the map the click happens, the
bigger the error, with the point returned occuring ever farther away
from the point clicked.  The error is always towards the center of the map:
If my map is like this:

|                          |
|                          |
|                          |


Then a click here
|                          |
|           x             |
|                          |
Returns a coordinate more or less correct

while a click here:
|                          |
|                       x |
|                          |

Returns a point more or less here
|                          |
|                  X  x |
|                          |

While a click here
|                          |
|x                        |
|                          |

Results in coordinates somewhere around here:
|                          |
|x      X               |
|                          |



I have SIZE set to 800 600 in my mapfile, however the map doesn't take
up all of the visible space, creating two white panels on either side of
the image.  I believe that this is the cause of the problem, as reducing
the width of the output image results in a smaller margin of error.

As I don't want to try to fix the error by trial and error by playing
with the width of the mapfile, I'd like to know if there is some way to
a) fix the calculation in my function so that it always returns a
correct value, or b) fix the width of the map so that the function's
existing calculation will work.

Thanks

--
Aaron Craig
mercutio at pobox.com

===========================================================
Get Firefox!
http://www.spreadfirefox.com/?q=affiliates&id=0&t=1
===========================================================



More information about the MapServer-users mailing list