Click -> map coords
Eric Bridger
eric at GOMOOS.ORG
Tue Dec 21 09:02:15 PST 2004
On Tue, 2004-12-21 at 05:25, Aaron Craig wrote:
> 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.
>
The above function will only work when your map's SIZE matches your
map's EXTENTS. That is the aspect ratio's must match. E.g.
w/h = maxx - minx / maxy - miny
or
width/height = max longitude - min longitude/ max latitude - min
latitude. (I think I have this right)
The easiest thing to do is just adjust your map SIZE. Should get rid of
your white areas as well.
More information about the MapServer-users
mailing list