[mapserver-users] How to set Labels dynamically?

Worth Lutz wal3 at mindspring.com
Thu Dec 3 17:05:57 EST 2009


I think that you need to add a class with a label definition to your layer.

 

Here is how I add a set of virtual layers to my map object before drawing
it.  I'm sorry I do not have time this afternoon to distill it down to just
the information you are looking for.  Things to notice that I am using
update from string to update my layer with a class which has style and label
information.  I have found that the SYMBOL part of the updateFromText does
not work thus the

 

 "$style->set('symbolname',$virtualLayers[$i]->symbol);"

 

statement which would seem redundant but is necessary.  Let me know if you
cannot get the required information from this example and I'll help more.

 

 

function addVirtualLayers($ms, $virtualLayers) {

  if (isset($virtualLayers)) {

    $newLayer = array();

    for ($i=0; $i < sizeof($virtualLayers); $i++) {

      $newLayer[$i] = ms_newLayerObj($ms);

      $newLayer[$i]->set('type',$virtualLayers[$i]->layerType);

      #echo('layer type='.$virtualLayers[$i]->layerType);

      if($virtualLayers[$i]->layerType == MS_LAYER_POINT) {

        $string = 'LAYER NAME VirtualLayer'.$i.' CLASS NAME SELECTEDPTS
STYLE SYMBOL CROSS COLOR 255 0 200 SIZE 8 END LABEL TYPE truetype
MINFEATURESIZE 50 MINDISTANCE 0 POSITION CR FONT vera SIZE 7 COLOR 255 0 255
END END END';

      } else if ($virtualLayers[$i]->layerType == MS_LAYER_POLYGON) {

        $string = 'LAYER NAME VirtualLayer'.$i.' CLASS NAME SELECTEDPOLYGONS
STYLE OUTLINECOLOR 255 0 200 WIDTH 3 END LABEL TYPE truetype MINFEATURESIZE
50 MINDISTANCE 0 POSITION LC FONT vera SIZE 7 COLOR 255 0 255 END END END';

      } else if ($virtualLayers[$i]->layerType == MS_LAYER_LINE) {

        $string = 'LAYER NAME VirtualLayer'.$i.' CLASS NAME SELECTEDPOLYGONS
STYLE COLOR 255 0 0 WIDTH 3 END LABEL TYPE truetype MINFEATURESIZE 50
MINDISTANCE 0 POSITION LC FONT vera SIZE 7 COLOR 255 0 255 END END END';

      }

      $newLayer[$i]->updateFromString($string);

      #echo('layer type2 ='.$newLayer[$i]->type);

      $newLayer[$i]->set('status',MS_ON);

 

      $class = $newLayer[$i]->getClass(0);

#echo($virtualLayers[$i]->class);

      $class->updateFromString($virtualLayers[$i]->class);  //returns
MS_SUCCESS | MS_FAILURE

      

      if(isset($virtualLayers[$i]->symbol)) {

        $style = $class->getStyle($class->numstyles-1);

        $style->set('symbolname',$virtualLayers[$i]->symbol);

      }

 

      $shape = ms_shapeObjFromWkt($virtualLayers[$i]->shapeWkt);

#      $shape->set('text', $i);

        if(isset($virtualLayers[$i]->labelText)) {

          $shape->set('text', $virtualLayers[$i]->labelText);

        }

 

      $newLayer[$i]->addFeature($shape);

    }

  }

}

?>

 

 

 

  _____  

From: mapserver-users-bounces at lists.osgeo.org
[mailto:mapserver-users-bounces at lists.osgeo.org] On Behalf Of Helen Eskina
Sent: Thursday, December 03, 2009 11:13 AM
To: mapserver-users at lists.osgeo.org
Subject: [mapserver-users] How to set Labels dynamically?

 

Hello,

 

I couldn't find documentation how to set the text labels dynamically using
MapScript. 

My application should show points with labels on the map. Points and labels
are obtained dynamically from the database or some text file.

I add the points to the layer and draw layer as following:

 

$my_point = ms_newpointObj();

 

 $my_point->setXY(-75.895, 45.9227);

 $my_point->project($projInObj,$projOutObj);

 

 $line = ms_newLineObj();

$line->add($my_point);

 $shape = ms_newShapeObj(MS_SHAPE_POINT);

 $shape->add($line);

 $shape->set(text, 'new point');

 $daLayer->addFeature($shape);

 

$image=$map->draw();

$daLayer->draw($image);

 $image_url=$image->saveWebImage();

 

Points are displayed on the map, but no text. What am I doing wrong?

 

I will appreciate any help,

Helen

No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 9.0.709 / Virus Database: 270.14.91/2542 - Release Date: 12/03/09
02:32:00


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/mapserver-users/attachments/20091203/652f0401/attachment-0001.html


More information about the mapserver-users mailing list