[Mapserver-users] Create a legend using PHPMapscript

Van Ulden, Joost jvanulde at nrcan.gc.ca
Wed May 26 17:42:00 EDT 2004

You can use legend templates or code your own.  Here is a function that will
do it for you (excuse the formating!):

// draw the legend
	function drawLegend($layerArray) {
		GLOBAL $map;
		// get the list of layers
		$aLayerList = $map->getAllLayerNames();
		$aLayerList = array_reverse($aLayerList);
		$aSentLayers = explode(",",$layerArray);
		//loop through layers
		foreach($aLayerList As $sName)
			$currentLayer = $map->getLayerByName($sName);
			// check if layer is passed and if it was set the
status to on
			if ($currentLayer->status != 2)
				foreach($aSentLayers As $testName){
					if($sName == $testName)
					} else {
			$currentName = $sName;
			$currentTitle =
			$currentStatus = $currentLayer->status;
			$checked = "";
			// set the checked status of layer
			if ($currentStatus == 1)
				$checked = '<INPUT type="checkbox"
name="layer" value="'.$currentName.'"
onClick="mapForm.redraw.value=true;mapForm.submit()" CHECKED >';
			} elseif ($currentStatus == 0){
				$checked = '<INPUT type="checkbox"
name="layer" value="'.$currentName.'"
onClick="mapForm.redraw.value=true;mapForm.submit()" >';		
			// write out the title
			echo '<TR bgcolor="#CCCCCC"><TD
			if ($currentStatus >= 1)
				//get the number of classes for this layer
				$nClassCount = $currentLayer->numclasses;
				// loop through the classes
				for($j=0; $j<$nClassCount; $j++)
					$currentClass =
					$className = $currentClass->name;
					$classImage =
					$classImageUrl =
					// write out the html code
					echo '<TR bgcolor="#FFFFFF"><TD><IMG

Good luck!

Joost van Ulden 
Natural Resources Canada / Ressources naturelles Canada 
Geological Survey of Canada / Commission geologique du Canada 
101 - 605 Robson Street / 101 - 605 rue Robson 
Vancouver, B.C. / Vancouver (C.B.) 
V6B 5J3 
Office/Bureau:  604.666.7525 
fax/telecopieur: 604.666.1124 
jvanulde at nrcan.gc.ca 

-----Original Message-----
From: Frank Broniewski [mailto:Frank.Broniewski at mnha.etat.lu] 
Sent: Wednesday, May 26, 2004 6:25 AM
To: mapserver-users at lists.gis.umn.edu
Subject: [Mapserver-users] Create a legend using PHPMapscript

Dear List

I'm still a beginner with MapServer, so I have trouble understanding the
process of creating a legend using php-mapscript. Maybe a reader may send me
on the right path.

I've done so far this tutorial
And the tutorials found here
Which provide quite some background

But when it comes to creating the legend dynamically, I can't seem to find
any helpful source. And I must admit, that I'm not capable of understanding
the information provided here:

As far as I understand inherits the legend object from the mapobject. So I
thought it might be a good idea to create the legend in the same manner as
the scalebar, which I could figure out myself :-) To provide a little more
information on my work, here is the code I
  $legend = $map->legend();
  $legend_url = $legend->saveWebImage();
Am I missing vital parameters or something else? I'm currently displaying
one shapefile only in my map.

Thanks a lot in advance of your help and kind greetings from Luxembourg

Frank Broniewski
Musée National d'Histoire et d'Art
Section Préhistoire / Projet EPC
    Tél: +352 260 281-21
241, Rue de Luxembourg
L-8077 Bertrange

Mapserver-users mailing list
Mapserver-users at lists.gis.umn.edu

More information about the mapserver-users mailing list