[mapserver-users] Calculating pixel coordinates from latlong

Ken Patterson kap@qwest.net
Mon, 12 Aug 2002 23:26:06 -0700


This is a multi-part message in MIME format.

------=_NextPart_000_0052_01C24257.A1F9B600
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hello,

We hope to liven up our backgrounds using Mapserver output from either =
the perl interface or using shp2img.  At this time we are trying to =
figure out how to map a world coordinates (lat/long) to pixel =
coordinates (from Perl).  I can create a background image with Perl =
Mapserver and save it to a png file then to be loaded back in w/ =
GD::newFromPng().  What Information do I need in order to translate from =
World (lat/long) to a pixel coordinate?  Is this something that can be =
done w/in the Perl Mapserver interface?  Has anyone gone through this =
and can provide some direction?

>From searching through the Mapserver archives it seems that the "true" =
Extent needs to be found and that a projection needs to take place.  =
I've seen some references to a Perl interface to Proj.4 but could not =
retrieve the files. =20

Any help of ideas would be greatly appreciated!

Thanks,
    Ken

$map_path =3D <Path>/my.map";
$amap =3D new mapObj( "$map_path );

# ---------------------------- Set the image w/h=20
$amap->{width} =3D 200;
$amap->{height} =3D 100;

# ------------------------------ Create my extent (Arizona)=20
$myExtent =3D new rectObj();
$myExtent->{maxx} =3D -111.35;
$myExtent->{minx} =3D -112.97;
$myExtent->{maxy} =3D 33.62;
$myExtent->{miny} =3D 33.19;
$amap->{extent} =3D $myExtent;

# ----------------------------- Draw the maps
$image =3D $amap->draw();                                =20
                                                                         =
  =20
# ----------------------------- Save the bitmap=20
$image_url =3D $image->saveImage( "/tmp/test.png", $mapscript::MS_PNG, =
1, 1, 95 );

# ----------------------------- Get the image file to a GD image
my $backGroundImage =3D GD::Image->newFromPng( "/tmp/test.png" );

# ---------------------------- Want try draw some stuff on the image
my ( $vx, $vy ) =3D TranslateToViewport( ... )
# Draw my stuff at/around pt ( $vx, $vy )

If I'm understanding it correctly Mapserver is using a projection and =
the extent is altered as well. =20

------=_NextPart_000_0052_01C24257.A1F9B600
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2600.0" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hello,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>We hope to liven up =
our&nbsp;backgrounds using=20
Mapserver output from either the perl interface or =
using&nbsp;shp2img.&nbsp; At=20
this time&nbsp;we&nbsp;are trying to figure out how to map a world =
coordinates=20
(lat/long) to pixel coordinates (from Perl).&nbsp; I can create a =
background=20
image with&nbsp;Perl Mapserver and save it to a png file then to be =
loaded back=20
in w/ GD::newFromPng().&nbsp;&nbsp;What Information do I need in order =
to=20
translate from&nbsp;World (lat/long) to a pixel coordinate?&nbsp; Is =
this=20
something that can be done w/in the Perl Mapserver interface?&nbsp; Has =
anyone=20
gone through this and can provide some direction?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>From searching through the Mapserver =
archives it=20
seems that the "true" Extent needs to be found and that a projection =
needs to=20
take place.&nbsp; I've seen some references to a Perl interface to =
Proj.4 but=20
could not retrieve the files.&nbsp; </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Any help of ideas would be greatly=20
appreciated!</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Thanks,</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; Ken</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>$map_path =3D =
&lt;Path&gt;/my.map";<BR>$amap =3D new=20
mapObj( "$map_path );</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2># ---------------------------- Set the =
image w/h=20
</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>$amap-&gt;{width} =3D =
200;<BR>$amap-&gt;{height} =3D=20
100;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2># ------------------------------ Create =
my extent=20
(Arizona)&nbsp;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>$myExtent =3D new =
rectObj();<BR>$myExtent-&gt;{maxx}=20
=3D -111.35;<BR>$myExtent-&gt;{minx} =3D =
-112.97;<BR>$myExtent-&gt;{maxy} =3D=20
33.62;<BR>$myExtent-&gt;{miny} =3D 33.19;<BR>$amap-&gt;{extent} =3D=20
$myExtent;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2># ----------------------------- Draw =
the=20
maps<BR>$image =3D=20
$amap-&gt;draw();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;=20
<BR># ----------------------------- Save the bitmap <BR>$image_url =3D=20
$image-&gt;saveImage( "/tmp/test.png", $mapscript::MS_PNG, 1, 1, 95=20
);<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2># ----------------------------- Get the =
image file=20
to a GD image</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>my $backGroundImage =3D =
GD::Image-&gt;newFromPng(=20
"/tmp/test.png" );</DIV></FONT>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2># ---------------------------- Want try =
draw some=20
stuff on the image</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>my ( $vx, $vy ) =3D =
TranslateToViewport( ...=20
)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2># Draw my stuff at/around pt ( $vx, $vy =

)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>If I'm understanding it correctly =
Mapserver is=20
using a projection and the extent is altered as well.&nbsp;=20
</FONT></DIV></BODY></HTML>

------=_NextPart_000_0052_01C24257.A1F9B600--