[Mapserver-users] drawing lines with mapscript

Eric Bridger eric at gomoos.org
Tue Mar 30 12:01:03 EST 2004


--=-yKal72uLUmmo/itZBUVV
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Tue, 2004-03-30 at 09:56, Jeff Hoffmann wrote:
> Eric Bridger wrote:
> > On Mon, 2004-03-29 at 16:24, Jeff Hoffmann wrote:
> > 
> >>I'm trying to create lines programmatically & draw them using mapscript, 
> >>but I just can't seem to get any output.  It doesn't throw any errors, 
> >>but I don't see any output, either.   
> >>$layer = $map->getLayerByName('line_layer');
> >>$layer->{status} = 1;
> >>$class = $layer->getClass(0);
> >>$class->setText($layer,"LINE");
> >>my $shape = new mapscript::shapeObj($mapscript::MS_SHAPE_LINE);
> >>$shape->add($line);
> > 
> > 
> > Just a guess, but you could try $shape->setBounds() here.
> > 
> > 
> >>$shape->draw($map, $layer, $img);
> 
> That didn't change anything.  Does anybody at least know if this looks 
> like it should be working?  Are there any examples of code that does 
> this sort of thing?

This should work. I had never used $shape->draw() so I modified a test
script I had and it worked fine. I could not get $shape->{text} = "XX"
to work.  You haven't shown your $line creation. Perhaps your
coordinates are off.

I've attached my cgi script and map. Not sure if attachments will make
it to the list.





--=-yKal72uLUmmo/itZBUVV
Content-Disposition: attachment; filename=lines.cgi
Content-Transfer-Encoding: quoted-printable
Content-Type: text/x-perl; name=lines.cgi; charset=ISO-8859-15

#!/usr/bin/perl

use strict;
use mapscript;
use CGI ":cgi";

my $q =3D new CGI;
my $msg =3D '';

# A hash of points.
my %points =3D (
	10202 =3D>	{'longitude' =3D> -67.0173,
				'latitude'  =3D> 44.8911,
				},
	20103 =3D>	{'longitude' =3D>  -66.0146,
				'latitude' =3D> 45.2045,
				},
);

my $image_name =3D sprintf("%0.10d",rand(1000000000)) . ".png";
# see points.map
my $map =3D new mapscript::mapObj("lines.map");

if(!$map){
	warn "New mapObj() error: $mapscript::ms_error->{message}\n";
}

# Create a point object representing the mouse click on the map.
my ($x, $y) =3D get_click($q, $map);

my $click_pt =3D undef;
if($x !=3D 0 && $y !=3D 0){
	$click_pt =3D new mapscript::pointObj();
	$click_pt->{x} =3D $x;
	$click_pt->{y} =3D $y;
}

my $img =3D $map->draw();

if(!$img){
	warn "prepareImage() error: $mapscript::ms_error->{message}\n";
}

my $layerObj =3D undef;

$layerObj =3D $map->getLayerByName('lines');
my $point =3D new mapscript::pointObj();
my $point2 =3D new mapscript::pointObj();

$point2->{x} =3D $points{20103}{longitude};
$point2->{y} =3D $points{20103}{latitude};
$point->{x} =3D $points{10202}{longitude};
$point->{y} =3D $points{10202}{latitude};

my $line =3D new mapscript::lineObj();
$line->add($point);
$line->add($point2);

my $shp =3D new mapscript::shapeObj($mapscript::MS_SHAPE_LINE);
$shp->add($line);
# this doesn't work
$shp->{text} =3D "line";
my $ret =3D $shp->draw($map, $layerObj, $img);

# display the click point
if($click_pt){
	$layerObj =3D $map->getLayerByName('click');
	$click_pt->draw($map, $layerObj, $img, undef, "Click");
}


$map->drawLabelCache($img);

$img->saveImage($image_name, $mapscript::MS_PNG, $map->{transparent}, $map-=
>{interlace}, 0);

$img->free();

# Output the HTML form and map

print $q->header();
print $q->start_html(-title=3D>'MapServer - Dynamic Line', -bgcolor=3D>"#ff=
ffff");

print "<form name=3D\"pointmap\" action=3D\"lines.cgi\" method=3D\"GET\">\n=
";
print "<table border=3D\"1\" cellpadding=3D\"5\" cellspacing=3D\"2\">\n";
print "<tr>\n";
print "<td>\n";
print "<input border=3D\"2\" type=3D\"image\" name=3D\"img\" src=3D\"$image=
_name\">\n";
print "</td>\n";
print "</tr>\n";
print "</table>\n";
print "</form>\n";
print "$msg<br>\n";
print "<a href=3D\"lines.cgi\"> Start over </a><br>\n";
print "<p><br><br><br></p>\n";

print $q->end_html();

# translate mouse click x,y into map longitude, latitude based on map exten=
t. This is based on set_extent() in
# mapquakes.pl

sub get_click {
	my ($q, $map) =3D @_;
	my ($x, $y, $cx, $cy) =3D (0,0,0,0);
	my $minx =3D $map->{extent}->{minx};
	my $miny =3D $map->{extent}->{miny};
	my $maxx =3D $map->{extent}->{maxx};
	my $maxy =3D $map->{extent}->{maxy};

	if($q->param('img.x')) { # Make sure we got a click
		$x =3D $q->param('img.x');
		$y =3D $q->param('img.y');

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

		$x =3D $minx + $cx*$x; # change x,y from image to map coordinates
		$y =3D $maxy - $cy*$y;
	}=20

	return ($x, $y);
}


--=-yKal72uLUmmo/itZBUVV
Content-Disposition: attachment; filename=lines.map
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; name=lines.map; charset=ISO-8859-15

MAP
  STATUS ON
  EXTENT -71.5 39.5 -63.0 46.0
  SIZE 504 385
  IMAGETYPE PNG
  UNITS DD

SYMBOL
  TYPE ELLIPSE
  NAME "circle"
  POINTS 1 1 END
  FILLED TRUE
END

SYMBOL
  TYPE VECTOR
  NAME "plus"
  POINTS .5 0 .5 1 -99 -99 0 .5 1 .5 END
END=20

LAYER
  NAME "lines"
  TYPE LINE
  STATUS ON
  TEMPLATE "bogus.html"
  CLASS
	COLOR 255 0 0
	SIZE 5
  END
END

LAYER
  NAME "click"
  TYPE POINT
  STATUS ON
  CLASS
    NAME "click"
    SYMBOL "plus"
    SIZE  6
	COLOR 0 0 0
    LABEL
	  TYPE BITMAP
      SIZE TINY
      COLOR 0 0 0
      POSITION AUTO
      PARTIALS FALSE
	  BUFFER 1
    END
  END
END

END

--=-yKal72uLUmmo/itZBUVV--




More information about the mapserver-users mailing list