Mapscript, SVG and Annotations

Martin Weinelt mweinelt at PLANIGLOBE.COM
Wed Jul 11 04:46:25 EDT 2007


Thanks a lot,

your tips at least kept trying. What I did was translate your example 
to Perl mapscript. Some PHP methods are not available or a bit 
different in the SWIG versions. That looks like this:

my $oShp  = new mapscript::shapeObj($mapscript::MS_SHAPE_POINT);
my $oLine = new mapscript::lineObj;
my $oPt   = new mapscript::pointObj;
   $oPt->setXY(-11, 22); # DD
   $oLine->add($oPt);
   $oShp->add($oLine);
   $oShp->{text} = 'Hello World';
my $numlayers = $map->{numlayers};
my $oLayer;
for (my $h=0; $h<$numlayers; $h++) {
   $oLayer = $map->getLayer($h);
   $oLayer->{status} =  $mapscript::MS_OFF;
}

$oLayer = $map->getLayerByName('popplace');
$oLayer->{status} = $mapscript::MS_ON;
$oLayer->addFeature($oShp);
 # here the loop below  is going to be inserted
my $SVGimg = $map->draw();
my $svg_file = '/path/to/willi.svg';
$SVGimg->save($svg_file);

The result is exactly like yours: including the label. Ok.
But now I add the following:

for (my $foo=0; $foo < 3; $foo++) {
  $oShp  = new mapscript::shapeObj($mapscript::MS_SHAPE_POINT);
  $oLine = new mapscript::lineObj;
  $oPt   = new mapscript::pointObj;
  $oPt->setXY($foo,$foo);
  $oLine->add($oPt);
  $oShp->add($oLine);
  $oShp->{text} = 'Hello World  ' . "$foo";
  $oLayer->addFeature($oShp);
  $oShp=$oLine=$oPt=undef;  # don't know if this is needed
}

this is the result:

<?xml version="1.0" encoding="UTF-8"?>
<svg version="1.1" width="530" height="350" 
xmlns="http://www.w3.org/2000/svg" 
xmlns:xlink="http://www.w3.org/1999/xlink">

<!-- START LAYER popplace -->
<ellipse cx="249" cy="143" rx="3" ry="3" fill="#0000ff"  />
<ellipse cx="265" cy="175" rx="3" ry="3" fill="#0000ff"  />
<ellipse cx="266" cy="174" rx="3" ry="3" fill="#0000ff"  />
<ellipse cx="268" cy="172" rx="3" ry="3" fill="#0000ff"  />
<text x="249" y="142" font-family="Arial" font-size="12pt" 
fill="#ff0000" stroke="#ffffff" stroke-width="0.5" 
transform="rotate(-10.000000 249 142)">Hello World</text>
</svg>

What you see is that the labels from the loop are missing while the 
label from the first addFeature is there. Weird.

Going through the MAP-file reference I find the 'FORCE' property for 
labels. Setting 'FORCE' to 'true' did the trick. All labels, 
including those from the loop, do now appear in the SVG. I do not 
know if this is exactly what I want, but at least I got my labels.

Cheers,

Martin



On Dienstag 10 Juli 2007, Yewondwossen Assefa wrote:
> Hi there,
>
>   I did a quick test using php mapscript and an svg output. The
> poplace layer in the map file is a point layer and has a class with
> a label object (note that there is no labelitem set)
>
> LAYER
>   NAME popplace
>   TYPE POINT
>    STATUS ON
>    ...
> CLASS
>        style
>          SYMBOL 'circle'
>         SIZE 6
>         COLOR  0 0 255
>       END
>       LABEL
>        COLOR  255 0 0
> #      FONT fritqat-italic
>        FONT Arial
>        TYPE truetype
>        SIZE 12
>        ANGLE 10
>        POSITION AUTO
>        PARTIALS FALSE
>        OUTLINECOLOR 255 255 255
>      END
>   ...
>
>   script looks something like this
>
> $oMap = ms_newmapobj("c:/msapps/gmap-ms40/htdocs/gmap75.map");
>
> $oShp = ms_newShapeObj(MS_SHAPE_POINT);
>
> $oLine = ms_newLineObj();
>
> $oLine->addXY(-2299954.0642738,-612631);
> $oShp->add( $oLine );
>
> $oShp->set("text", "mytext");
>
> $numlayers = $oMap->numlayers;
> for ($i=0; $i<$numlayers; $i++)
> {
>    $oLayer = $oMap->getlayer($i);
>    $oLayer->set("status", MS_OFF);
> }
> $oLayer = $oMap->getlayerbyname('popplace');
> $oLayer->set("status", MS_ON);
> $oLayer->addfeature($oShp);
>
> $img = $oMap->draw();
> $url = $img->saveWebImage();
>
> header("Content-type: image/svg+xml");
> $url = $img->saveImage("");
>
>
>   It produces an svg point with the label my text :
>
>   ...
> <!-- START LAYER popplace -->
> <ellipse cx="39" cy="586" rx="3" ry="3" fill="#0000ff"  />
> <text x="38" y="581" font-family="Arial" font-size="12pt"
> fill="#ff0000" stroke="#ffffff" stroke-width="0.5"
> transform="rotate(-10.000000 38 581)">mytext</text>
> </svg>
>
>   Not sure what the issue would be in your case, the only time I
> did not have the text produced was when I set the labelitem object.
> Could that be the case ?
>
>
> Later
>
> kk+w - digitale kartografie GmbH wrote:
> > Hi,
> >
> > I am using addFeature to dynamically add site locations with a
> > label to a map. It's Perl mapscript.
> >
> > @sites is an array of hash references, $sitesLyr is from the map
> > file:
> >
> > ###############
> >  foreach my $site (@sites) {
> >    my $site_pt = $$site{'site_loc'};
> >       $site_pt =~s/.*\((.*)\)*./$1/;  # extract the points x and
> > y my $line     = new mapscript::lineObj();
> >    my $siteShp  = new
> > mapscript::shapeObj($mapscript::MS_SHAPE_POINT);
> >
> >    my $point = new mapscript::pointObj();
> >    my($x,$y) = split(/ /, $site_pt);
> >       $point->setXY($x,$y);
> >       $line->add($point);
> >       $siteShp->add($line);
> >       $siteShp->{text} = $$site{'site_name'};
> >       $siteShp->setBounds();
> >       $sitesLyr->addFeature($siteShp);
> >   }
> >
> >  my $img = $map->draw();
> >
> > #############
> >
> > This works perfectly for the usual PNG output map. Using the same
> > routine for a SVG 'image' results in the symbols/markers getting
> > plotted, but the labels are missing. If I switch to a static
> > shapefile for this layer, the labels do appear in the SVG.
> >
> > Is there anything special with SVG output and the 'addFeature'
> > method for layers that I am missing?
> >
> > Thanks,
> >
> > Martin



More information about the mapserver-users mailing list