NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/Chameleon/trunk/Chameleon/PrintProduction/production_draw_legend.php

Revision 13 (checked in by jcleary, 17 years ago)

Latest Chameleon code checkout from previous repository

Line 
1 <?php
2 /**
3  * CWC application
4  *
5  * @project     CWC
6  * @revision    $Id: production_draw_legend.php,v 1.4 2004/12/03 01:38:32 pspencer Exp $
7  * @purpose     This is a utility page that contains the code necessary to draw
8  *              a legend for print production
9  * @author      William A. Bronsema, C.E.T. (bronsema@dmsolutions.ca)
10  * @copyright
11  * <b>Copyright (c) 2001, DM Solutions Group Inc.</b>
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  */
30
31 /*****************************************************************************
32  * $Log: production_draw_legend.php,v $
33  * Revision 1.4  2004/12/03 01:38:32  pspencer
34  * bug MT 694: removed static references to GIF_SAMPLE, replaced with dynamic image generator.
35  *
36  * Revision 1.3  2004/11/16 15:23:40  pspencer
37  * bug MT 377: renamed PrintWidget to PrintProduction
38  *
39  * Revision 1.2  2004/06/05 13:38:26  pspencer
40  * bug 440: removed session variables related to gd module, free type.
41  *
42  * Revision 1.1  2004/04/23 16:52:05  pspencer
43  * moved production_draw_legend.php to PrintProduction/production_draw_legend.php
44  *
45  * Revision 1.17  2004/03/30 20:25:23  jlacroix
46  * Don't always use the same layer name in legend.
47  *
48  * Revision 1.16  2004/03/30 18:25:01  jlacroix
49  * Fix undefined variables notice (maptools bug 326)
50  *
51  * Revision 1.15  2003/10/27 20:46:19  sfournier
52  * Overwrite main branch with 1.1 stuff
53  *
54  * Revision 1.12.2.2  2003/10/15 21:04:57  pspencer
55  * fixes to work with Chameleon 1.1
56  *
57  * Revision 1.12.2.1  2003/08/05 15:08:33  zak
58  * removed tests requiring wms layers and metadata in map
59  * (bug 2161)
60  *
61  * Revision 1.12  2003/04/25 19:42:31  sacha
62  * Initialize a var
63  *
64  * Revision 1.11  2003/04/23 17:29:41  pspencer
65  * use GD version from session.
66  *
67  * Revision 1.10  2003/04/17 19:10:51  sacha
68  * Change the preview to reflect the exact map size from the main page.
69  *
70  * Revision 1.9  2003/03/27 15:40:50  sacha
71  * Use wms style when drawing legend icon
72  *
73  * Revision 1.8  2003/02/24 19:32:34  sacha
74  * Don't display layer temp_points in legend. Also fixed a few potential bugs in add point
75  *
76  * Revision 1.7  2003/01/30 13:49:58  sacha
77  * Changed the session management
78  *
79  * Revision 1.6  2003/01/14 19:49:29  sacha
80  * Fix a parsing font file bug
81  *
82  * Revision 1.5  2003/01/09 18:32:09  bronsema
83  * Added ability to delete some temp image files that get created for the legend
84  *
85  * Revision 1.4  2003/01/09 18:19:44  bronsema
86  * Updated to draw icon properly after bug was fixed in mapscript of not
87  * being able to use an image file as a symbol directly
88  *
89  * Revision 1.3  2003/01/08 16:43:12  bronsema
90  * Updated code to remove the dot that was appearing on labels with no
91  * symbol
92  *
93  * Revision 1.2  2003/01/07 20:25:08  bronsema
94  * Updated function header comments to the doxygen style
95  *
96  * Revision 1.1  2003/01/07 18:33:45  bronsema
97  * Added legend
98  *
99  *****************************************************************************/
100 $gdModule = "php_gd2";
101
102 // ensure that the gd module is loaded
103 if (PHP_OS == "WINNT" || PHP_OS == "WIN32")
104 {
105     if (!extension_loaded("gd")) dl($gdModule.".dll");
106 }
107 else
108 {
109     if (!extension_loaded("gd")) dl($gdModule.".so");
110 }
111
112 /**
113  * Postcondition:  This function builds a list of layers to show in the legend.
114  *
115  * @param oMap object - Map file object.
116  * @return array - List of layer indexes to draw in the legend or empty array.
117  **/
118 function getLegendList( $o_map )
119 {
120     // initialize arrays
121     $an_legend_layers = array();
122     $asz_group_names = array();
123
124     // get array of all group names
125     $asz_tmp_names = $o_map->getAllGroupNames();
126
127     // create the array with group names as the key
128     if (is_array($asz_tmp_names))
129     {
130         // loop to build array
131         foreach($asz_tmp_names as $name)
132         {
133             // create array item and set to false
134             $asz_group_names[$name] = 0;
135         }
136     }
137
138     // get all layers in map
139     $asz_all_layers = $o_map->getAllLayerNames();
140
141     // check if any layers
142     if (!is_array($asz_all_layers)) return array();
143
144     // loop and build the array of layer names to include in the legend
145     foreach ($asz_all_layers as $sz_layer)
146     {
147         // only add the the array if not in a group or 1st in group
148         $o_layer = $o_map->getLayerByName($sz_layer);
149
150         // check to see if in a group
151         $sz_group = $o_layer->group;
152
153         // add if necessary
154         if ($sz_group != "")
155         {
156             // check to see if this group has been added
157             if ($asz_group_names[$sz_group] == 0)
158             {
159                 // group has not been added so add
160                  if  ($o_layer->status == MS_ON)
161                         array_push($an_legend_layers,$o_layer->index);
162
163                  // set flag
164                  $asz_group_names[$sz_group] = -1;
165             }
166         }
167         else
168         {
169             // just add it
170             if ($o_layer->status == MS_ON)
171                         array_push($an_legend_layers,$o_layer->index);
172         }
173     }
174
175     // reverse the array to show drawing order
176     $an_reversed = array_reverse($an_legend_layers);
177
178     // return the array
179     return $an_reversed;
180
181 // end get_legend_list function
182 }
183
184 /**
185  * Postcondition:  This function creates a legend image object according to the
186  *                 legend settings in map file and passed parameters.
187  *
188  * @param o_map object - The map object to read.
189  * @param an_layers array - Layer indexes to draw in the legend.
190  * @param n_font_size integer - Font size to draw the legend at.
191  * @param sz_fontfile string - Path and filename of the fonts.txt file.
192  * @param sz_font_name string - Name of the font to use as listed in the
193  *        fonts.txt file.
194  * @param d_scale_factor double - Optional value to scale legend by.
195  *
196  * @return object - Handle for the image object.
197 */
198 function getLegendImage($o_map, $an_layers, $n_font_size, $sz_font, $sz_font_name,
199                         $d_scale_factor = 1)
200 {
201     // create default image on error
202     $o_default_image = $o_map->prepareImage();
203
204     // check if valid array
205     if (!is_array($an_layers)) return $o_default_image;
206
207     // get the map settings
208     $n_keyspace_x = $d_scale_factor * ($o_map->legend->keyspacingx);
209     $n_keyspace_y = $d_scale_factor * ($o_map->legend->keyspacingy);
210     $n_keysize_x = $d_scale_factor * ($o_map->legend->keysizex);
211     $n_keysize_y = $d_scale_factor * ($o_map->legend->keysizey);
212
213     // apply scale factor to supplied font size
214     $n_font_size = $d_scale_factor * ($n_font_size);
215
216     // get the font file to use
217     //$sz_font = $sz_font_file; //get_map_font($sz_font_name, $sz_fontfile);
218     //echo $sz_font." ".$sz_font_name."<BR>";
219     // initialize the variables
220     $n_max_width = 0;
221     $n_max_height = 0;
222     $n_num_leg_lines = 0;
223
224     // loop through each element of the array and process settings
225     for ($i=0;$i<count($an_layers);$i++)
226     {
227         // create layer object
228         $o_layer = $o_map->getlayer($an_layers[$i]);
229
230         // check for valid layer returned
231         if (!is_object($o_layer)) return $o_default_image;
232
233         // if not set the check for group name
234         $sz_layer_title = "";
235         // check group name
236         $sz_layer_title = $o_layer->group;
237
238         // if not set then default to the layer name
239         if ($sz_layer_title == "")
240         {
241             // set to layer name
242             $sz_layer_title = $o_layer->name;
243         }
244
245         $szCurrentStyle = $o_layer->getmetadata("wms_style");
246         $sz_layer_icon = "";
247
248
249         if ($szCurrentStyle != "")
250         {
251             // In mapserver 4.1 the href of the legendurl is in
252             // wms_style_$szCurrentStyle_legendurl_href
253             // in 4.0 it will be the fourth element (space separated) of the 
254             // wms_style_$szCurrentStyle_legendurl
255             $sz_layer_icon = $o_layer->getmetadata("wms_style_".$szCurrentStyle."_legendurl_href");
256
257             if($sz_layer_icon == "")
258             {
259                 $aStyle = explode(" ",
260              $o_layer->getmetadata("wms_style_".$szCurrentStyle."_legendurl"));
261
262                 if(is_array($aStyle) && isset($aStyle[3]) && $aStyle[3] != "")
263                     $sz_layer_icon = $aStyle[3];
264             }
265         }
266
267         // check if blank icon should be displayed
268         if ($sz_layer_icon == "" || $o_layer->numclasses > 0 )
269         {
270             //create a blank icon ...
271             $sz_layer_icon = $_SESSION['gszTmpPath']."tmp_icon_".$i.".gif";
272             clearstatcache();
273             if (!file_exists($sz_layer_icon))
274             {
275                 $oImg = imagecreate( $n_keysize_x, $n_keysize_y );
276                 $nWhite = imagecolorallocate($oImg, 255, 255, 255);
277                 imagecolortransparent($oImg, $nWhite);
278                 imagefill( $oImg, 0, 0, $nWhite );
279                 imagepng( $oImg, $sz_layer_icon );
280                 imagedestroy( $oImg );
281             }
282         }
283         else
284         {
285             // Read image to file
286             if (!($fpIn = fopen($sz_layer_icon, "rb")))
287             {
288                 trigger_error("ERROR: Could not access create icon $sz_layer_icon");
289             }
290     
291             if (!($fpOut = fopen($_SESSION['gszTmpPath']."tmp_icon_".$i.".gif" ,
292                                                                             "wb")))
293             {
294                 trigger_error("ERROR: Could not open icon");
295                 //return FALSE;
296             }
297     
298             while($fpIn && !feof($fpIn))
299             {
300                 $line = fread($fpIn, 4096);
301                 fwrite($fpOut, $line);
302             }
303     
304             fclose($fpIn);
305             fclose($fpOut);
306         }
307
308         // add the title to the title array
309         $asz_legend_items[$i][0]["label"] = $sz_layer_title;
310         $asz_legend_items[$i][0]["icon"] = $_SESSION['gszTmpPath'].
311                                                         "tmp_icon_".$i.".gif";
312         $asz_legend_items[$i][1] = array();
313         $asz_legend_items[$i][2] = array();
314
315         // increment the counter for the layer and then add the num classes
316         $n_num_leg_lines += $o_layer->numclasses + 1;
317
318         //echo "n_font_size: $n_font_size<br>";
319         //echo "sz_font: $sz_font<br>";
320         //echo "sz_layer_title: $sz_layer_title<br>";
321
322         // process the label widths to determine the max
323         $an_text_bbox = imagettfbbox ($n_font_size, 0, $sz_font,
324                                       $sz_layer_title);
325
326         //echo "an_text_bbox: ";
327         //foreach( $a as $k => $v ) echo "[$k] = $v ";
328         //echo "<br>";
329         // get width and height of text in pixels
330         $n_text_width = abs($an_text_bbox[2]-$an_text_bbox[0]);
331         $n_text_height = abs($an_text_bbox[5]-$an_text_bbox[1]);
332
333         $n_text_width = max( $n_text_width, strlen( $sz_layer_title ) * $n_font_size / 3 );
334
335         // check if this text string is the widest or highest yet
336         $n_max_width = max($n_text_width,$n_max_width);
337         $n_max_height = max($n_text_height,$n_max_height);
338
339         // Loop through the number of classes and check their titles too
340         for ($j=0;$j<$o_layer->numclasses;$j++)
341         {
342             // get the class object
343             $o_class = $o_layer->getclass($j);
344
345             // add the class name to the array
346             $asz_legend_items[$i][1][$j] = $o_class->name;
347
348             // get the image object
349             $asz_legend_items[$i][2][$j] =
350                                     $o_class->createLegendIcon($n_keysize_x,
351                                                                $n_keysize_y);
352
353             // process the label widths to determine the max
354             $an_text_bbox = imagettfbbox ($n_font_size, 0, $sz_font,
355                                           $o_class->name);
356
357             // get width and height of text in pixels
358             $n_text_width = abs($an_text_bbox[2]-$an_text_bbox[0]);
359             $n_text_height = abs($an_text_bbox[5]-$an_text_bbox[1]);
360
361             // add extra check to fix any discrepancies caused by the
362             // imagettfbbox() function
363             $n_text_width = max( $n_text_width, strlen( $sz_layer_title ) *
364                                  $n_font_size / 3 );
365
366             // check if this text string is the widest or highest yet
367             $n_max_width = max($n_text_width,$n_max_width);
368             $n_max_height = max($n_text_height,$n_max_height);
369         }
370     }
371
372     // take the maximum height to be the larger of the icon and text
373     $n_max_height = max($n_max_height,$n_keysize_y);
374
375     // calculate the overall width of the legend
376     $n_legend_width = ($n_keyspace_x * 12) + $n_max_width + $n_keysize_x +10;
377
378     // calculate the overall height
379     $n_legend_height = $n_num_leg_lines * ($n_max_height + $n_keyspace_y);
380
381     // record the cuurent map colour and size
382     $tmp_map_colour_r = $o_map->imagecolor->red;
383     $tmp_map_colour_g = $o_map->imagecolor->green;
384     $tmp_map_colour_b = $o_map->imagecolor->blue;
385     $tmp_map_width = $o_map->width+0; // leave the +0 to force a copy of the var
386     $tmp_map_height = $o_map->height+0;
387
388     // set the map colour to white
389     $o_map->imagecolor->setRGB(255,255,255);
390
391     // set the height and width
392     $o_map->set("width",$n_legend_width);
393     $o_map->set("height",$n_legend_height);
394
395     // create canvas
396     $o_img_canvas = $o_map->prepareImage();
397
398     // return the values to previous
399     $o_map->imagecolor->setRGB($tmp_map_colour_r,
400                                $tmp_map_colour_g,
401                                $tmp_map_colour_b);
402     $o_map->set("width",$tmp_map_width);
403     $o_map->set("height",$tmp_map_height);
404
405     // create annotation layer
406     $o_anno_layer = get_temp_anno_layer($o_map, $n_font_size, $sz_font_name);
407
408     // create temp point layer to draw the icons
409     $o_point_layer = get_temp_point_layer($o_map);
410
411     // create class object for icon
412     $o_point_class = $o_point_layer->getclass(0);
413
414     // create style object
415     $o_point_style = ms_newStyleObj( $o_point_class );
416
417     // initialize x & y poition variables
418     $n_pix_x = $n_keyspace_x;
419     $n_pix_y = $n_keyspace_y;
420
421     // loop to draw each legend record and paste onto canvas image
422     for ($i=0;$i<count($asz_legend_items);$i++)
423     {
424
425         // show the image
426         print_icon($o_map, $o_point_layer, $o_point_style, $o_img_canvas,
427                    $asz_legend_items[$i][0]["icon"], $n_pix_x,
428                    $n_pix_y, $d_scale_factor);
429
430         // delete the temp icon
431         if (file_exists($asz_legend_items[$i][0]["icon"]))
432                         unlink($asz_legend_items[$i][0]["icon"]);
433
434         // display layer title as point annotation
435         print_label($o_map, $o_anno_layer, $o_img_canvas,
436                     $asz_legend_items[$i][0]["label"], $n_pix_x + (22 * $d_scale_factor),
437                     $n_pix_y);
438
439         // init counter
440         $j=0;
441
442         // loop through classes
443         foreach($asz_legend_items[$i][1] as $class)
444         {
445             // set the y position
446             $n_pix_y += $n_max_height + 2;
447
448             // print the icon
449             $o_img_canvas->pasteImage( $asz_legend_items[$i][2][$j], -1,
450                                        ($n_pix_x * 3) + 14 , $n_pix_y );
451
452             // print the label
453             print_label($o_map, $o_anno_layer, $o_img_canvas,
454                         $class, ($n_pix_x * 4) + 14 + $n_keysize_x, $n_pix_y);
455
456             // increment the counter
457             $j++;
458         }
459
460         // update the y position variable
461         $n_pix_y += $n_max_height + $n_keyspace_y;
462
463     }
464
465     // delete temp layers
466     del_temp_anno_layer($o_map);
467     del_temp_point_layer($o_map);
468
469     // return the image object
470     return $o_img_canvas;
471
472 // end get_legend_image function
473 }
474
475 /**
476  * Function to print labels on given image using map object and temp annotation
477  * layer.
478  *
479  * @param o_map object - The map object to use.
480  * @param o_anno_layer object - Annotation layer object to use.
481  * @param o_image object - Image to paste the label onto.
482  * @param sz_label string - Label to print.
483  * @param n_x_pos integer - x poisiton of label.
484  * @param n_y_pos integer - y poisiton of label.
485  * @return void.
486  **/
487 function print_label($o_map, $o_anno_layer, $o_image, $sz_label,
488                      $n_x_pos, $n_y_pos)
489 {
490     // create new point object
491     $o_point = ms_newPointObj();
492
493     //position
494     $o_point->setXY($n_x_pos,$n_y_pos);
495
496     // draw the point onto the map
497     $o_point->draw($o_map, $o_anno_layer, $o_image, 0,$sz_label);
498
499 // end print_label function
500 }
501
502 /**
503  * Function to print icon on given image using map object and temp point
504  * layer
505  *
506  * @param o_map object - The map object to use.
507  * @param o_point_layer object - Annotation layer object to use.
508  * @param o_point_style object - Style object to use for styling the class.
509  * @param o_image object - Image to paste the label onto.
510  * @param sz_icon string - Path and filename of the icon to paste.
511  * @param n_x_pos integer - x poisiton of label.
512  * @param n_y_pos integer - y poisiton of label.
513  * @param d_scale_factor double - Scale factor to apply to size.
514  * @return void.
515  **/
516 function print_icon($o_map, $o_point_layer, $o_point_style, $o_image, $sz_icon,
517                      $n_x_pos, $n_y_pos, $d_scale_factor)
518 {
519     // create style object
520     $o_point_style->color->setRGB( 0,0,0 );
521     $o_point_style->set("maxsize"      , 30    );
522     $o_point_style->set("size"         , 15    );
523     $o_point_style->set("symbolname"   , $sz_icon );
524
525     // create new point object
526     $o_point = ms_newPointObj();
527
528     //position
529     $o_point->setXY($n_x_pos + (10 * $d_scale_factor),
530                     $n_y_pos + (4 * $d_scale_factor));
531
532     // draw the point onto the map
533     $nStatus = $o_point->draw($o_map, $o_point_layer, $o_image, 0,"");
534
535     // free the resources
536     //$o_point->free;
537
538 // end print_icon function
539 }
540
541 /**
542  * Function to create temp annotation layer and class and returns layer object.
543  *
544  * @param o_map object - The map object to read.
545  * @param n_font_size integer - Font size to draw the legend at.
546  * @param sz_msfont string - Name of the font to use as listed in the fonts.txt
547  *                           file.
548  * @return object - Handle for the layer object.
549  **/
550 function get_temp_anno_layer($o_map, $n_font_size, $sz_msfont)
551 {
552     // set flag
553     $b_layer_exists = false;
554
555     // loop through each layer
556     for ($i=0;$i<$o_map->numlayers;$i++)
557     {
558         // create layer object
559         $o_anno_layer = $o_map->getlayer($i);
560
561         // check name
562         if ($o_anno_layer->name == "DMSG_annotation_layer")
563         {
564             // flag as found
565             $b_layer_exists = true;
566             break;
567         }
568
569     }
570
571     // create layer if not found
572     if (!$b_layer_exists) $o_anno_layer = ms_newLayerObj($o_map);
573
574     // set the properties of the layer
575     $o_anno_layer->set("name", "DMSG_annotation_layer");
576     $o_anno_layer->set("type", MS_LAYER_ANNOTATION);
577     $o_anno_layer->set("status", 1);
578     $o_anno_layer->set("transform", MS_FALSE);
579     $o_anno_layer->set("labelitem", "annotation");
580     $o_anno_layer->set("labelcache", MS_OFF);
581
582     // set flag
583     $b_class_exists = false;
584
585     // loop through the classes and check if anno class exists
586     for ($i=0;$i<$o_anno_layer->numclasses;$i++)
587     {
588         // create class object
589         $o_anno_class = $o_anno_layer->getclass($i);
590
591         // check the name
592         if ($o_anno_class->name == "DMSG_anno_class")
593         {
594             // flag as found and exit loop
595             $b_class_exists = true;
596             break;
597         }
598     }
599
600     // create class object if necessary
601     if (!$b_class_exists) $o_anno_class = ms_newClassObj($o_anno_layer);
602
603     // create style object
604     $o_style = ms_newStyleObj( $o_anno_class );
605
606     // set the class colour
607     $o_style->color->setRGB( -1,-1,-1 );
608
609     // set class properties
610     //$o_anno_class->set("color", $n_class_colour    );
611     $o_anno_class->set("name" , "DMSG_anno_class"  );
612
613     // set the label properties
614     $o_anno_class->label->color->setRGB( 0,0,0 );
615     //$o_anno_class->label->set("color"        , $n_class_colour    );
616     //$o_anno_class->label->set("size"         , $n_font_size       );
617     $o_anno_class->label->set("size"         , 8       );
618     $o_anno_class->label->set("font"         , $sz_msfont         );
619     $o_anno_class->label->set("type"         , MS_TRUETYPE        );
620     $o_anno_class->label->set("position"     , MS_LR              );
621     $o_anno_class->label->set("partials"     , MS_FALSE           );
622
623     // return the layer object
624     return $o_anno_layer;
625
626 // end get_temp_anno_layer function
627 }
628
629 /**
630  * Function to create temp point layer and class and returns layer object
631  *
632  * @param o_map object - The map object to read.
633  * @return object - Handle for the layer object.
634  **/
635 function get_temp_point_layer($o_map)
636 {
637     // set flag
638     $b_layer_exists = false;
639
640     // loop through each layer
641     for ($i=0;$i<$o_map->numlayers;$i++)
642     {
643         // create layer object
644         $o_point_layer = $o_map->getlayer($i);
645
646         // check name
647         if ($o_point_layer->name == "DMSG_point_layer")
648         {
649             // flag as found
650             $b_layer_exists = true;
651             break;
652         }
653
654     }
655
656     // create layer if not found
657     if (!$b_layer_exists) $o_point_layer = ms_newLayerObj($o_map);
658
659     // set the properties of the layer
660     $o_point_layer->set("name", "DMSG_point_layer");
661     $o_point_layer->set("type", MS_LAYER_POINT);
662     $o_point_layer->set("status", 1);
663     $o_point_layer->set("transform", MS_FALSE);
664     $o_point_layer->set("labelitem", "point");
665     $o_point_layer->set("labelcache", MS_OFF);
666
667     // set flag
668     $b_class_exists = false;
669
670     // loop through the classes and check if point class exists
671     for ($i=0;$i<$o_point_layer->numclasses;$i++)
672     {
673         // create class object
674         $o_point_class = $o_point_layer->getclass($i);
675
676         // check the name
677         if ($o_point_class->name == "DMSG_point_class")
678         {
679             // flag as found and exit loop
680             $b_class_exists = true;
681             break;
682         }
683     }
684
685     // create class object if necessary
686     if (!$b_class_exists) $o_point_class = ms_newClassObj($o_point_layer);
687
688     // create style object
689     //$o_style = ms_newStyleObj( $o_point_class );
690
691     // set the class colour
692     //$o_style->color->setRGB( 0,0,0 );
693
694     // set class properties
695     $o_point_class->set("name" , "DMSG_point_class"  );
696
697     // return the layer object
698     return $o_point_layer;
699
700 // end get_temp_point_layer function
701 }
702
703 /**
704  * Function to set the status if the temp annotation layer to MS_DELETE.
705  *
706  * @param o_map object - The map object to read.
707  * @return void.
708  **/
709 function del_temp_anno_layer($o_map)
710 {
711     //  create the layer object
712     $o_layer = $o_map->getlayerbyname("DMSG_annotation_layer");
713
714     // set the status
715     $o_layer->set("status",MS_DELETE);
716
717 // end del_temp_anno_layer function
718 }
719
720 /**
721  * Function to set the status if the temp point layer to MS_DELETE.
722  *
723  * @param o_map object - The map object to read.
724  * @return void.
725  **/
726 function del_temp_point_layer($o_map)
727 {
728     //  create the layer object
729     $o_layer = $o_map->getlayerbyname("DMSG_point_layer");
730
731     // set the status
732     $o_layer->set("status",MS_DELETE);
733
734 // end del_temp_point_layer function
735 }
736
737 /**
738  * Function to get the path and filename of the font as it corresponds to
739  * the name of the mapfile font given.
740  *
741  * @param sz_font_name string - Name of the map file font to check.
742  * @param sz_fontfile string - Path to the fonts.txt file.
743  * @return mixed - Path and file name to the font file to use or false.
744  **/
745 function get_map_font( $sz_font_name, $sz_fontfile )
746 {
747     // get the font to use
748     $asz_font_list = ReadStringList( $sz_fontfile );
749
750     // check if results
751     if (!is_array($asz_font_list)) return false;
752
753     // loop through the array and read results
754     foreach($asz_font_list as $sz_list)
755     {
756         $szFont = preg_replace("/\s+/", "|", trim($sz_list));
757         $asz_fonts = explode("|", $szFont);
758
759     if (is_array($asz_fonts) && count($asz_fonts)==2 &&
760         stristr($sz_font_name, $asz_fonts[0]))
761     {
762             // return font
763             return dirname( $sz_fontfile )."/".trim($asz_fonts[1]);
764     }
765     }
766     return "";
767 // end get_map_font function
768 }
769
770 /**
771  * Reads the given textfile to an array.
772  *
773  * @param szfilename string - The textfile to read.
774  * @return mixed - Array or false if unsuccessful.
775  **/
776 function ReadStringList($szfilename)
777 {
778     // define array variable
779     $list = array();
780
781     // attempt to open the file
782     if (!($fp = fopen($szfilename, "r")))
783     {
784         // error occurred
785         trigger_error("ReadStringList(): Could not open $szfilename");
786
787         // return failure
788         return FALSE;
789     }
790
791     // file is open so parse each entry
792     while($fp && !feof($fp))
793     {
794         // get next line
795         $line = fgets($fp, 4096);
796
797         // trim() strips off whitespaces and \r and \n
798         $line = trim($line);
799         if (strlen($line) > 0)
800             array_push($list, $line);
801     }
802
803     // close the file
804     fclose($fp);
805
806     // return the array
807     return $list;
808 }
809
810 /**
811  * Write the given array to the specified textfile.
812  *
813  * @param szfilename string - The textfile to write to.
814  * @param aszlist array - the array write.
815  * @return boolean - Flag to indicate success.
816  **/
817 function WriteStringList($szfilename, $aszlist)
818 {
819     // open the file
820     if (!($fp = fopen($szfilename, "w")))
821     {
822         // open failed so give error
823         trigger_error("WriteStringList(): Could not open $szfilename");
824
825         // return failure
826         return FALSE;
827     }
828
829     // loop through the array an dwrite to file
830     foreach ($aszlist as $line)
831     {
832         // write entry to file
833         fputs($fp, $line."\n");
834     }
835
836     // close the file
837     fclose($fp);
838
839     // return success
840     return TRUE;
841 }
842
843 /**
844  * Draw the legend for the map onto an image at a given position
845  * it also draws a neatline around the legend.
846  *
847  * @param oMap object - The map to get the legend.
848  * @param oImage object - The image to draw the legend onto.
849  * @param oLegendImage object - The legend image to draw.
850  * @param nX integer - The left edge of the legend.
851  * @param nY integer - The top edge of the legend.
852  * @return mixed - The image object passed in.
853  **/
854 function draw_legend( $oMap, $oImage, $oLegendImage , $nX, $nY)
855 {
856     // get the legend object
857     $oLegend = $oMap->legend;
858
859      // set the outline colour, used again for the neatline.
860     //$nColor = $oMap->addColor( 0, 0, 0 );
861
862     // set status and transparency
863     $oLegend->set("status",MS_ON); //was MS_EMBED, now MS_ON
864     $oLegend->set("transparent",MS_FALSE);
865
866     //get the size of the map and legend for validation
867     $nMapWidth = $oImage->width;
868     $nMapHeight = $oImage->height;
869     $nLegendWidth = $oLegendImage->width;
870     $nLegendHeight = $oLegendImage->height;
871
872     //legend must fit in map size
873     // if ($nLegendWidth > $nMapWidth || $nLegendHeight > $nMapHeight)
874     //    return false;
875
876     //top & left validation
877     if ($nX < 0) $nX = 0;
878     if ($nY < 0) $nY = 0;
879
880     //bottom & right validation
881     if ( ($nX + $nLegendWidth) > $nMapWidth )
882         $nX = $nMapWidth - $nLegendWidth;
883     if ( ($nY + $nLegendHeight) > $nMapHeight )
884         $nY = $nMapHeight - $nLegendHeight;
885
886     //paste the legend onto the image at the desired location with
887     //no transparent colour.
888     $oImage->pasteImage( $oLegendImage, -1, $nX, $nY );
889
890     //draw a neatline around the legend
891     $oLayer = ms_newLayerObj($oMap);
892     $oLayer->set("name", "temp_layer");
893     $oLayer->set("type", MS_LAYER_LINE);
894     $oLayer->set("status", MS_OFF);
895     $oLayer->set("transform", MS_FALSE);
896     $oClass = ms_newClassObj($oLayer);
897
898     // set the class colour
899     //$oClass->set("color", $nColor);
900     // create style object
901     $o_style = ms_newStyleObj( $oClass );
902
903     // set the class colour
904     $o_style->color->setRGB( 0,0,0 );
905
906     // create new rectangle object
907     $oRect = ms_newRectObj();
908
909     // set to dimensions of the legend
910     $oRect->setExtent($nX, $nY,
911                       $nX + $nLegendWidth - 1,
912                       $nY + $nLegendHeight-1);
913
914     // draw the rectangle onto the map
915     $oRect->draw($oMap, $oLayer, $oImage, 0, "");
916
917     // delete temp layer
918     $oLayer->set("status", MS_DELETE);
919
920 // end draw_legend function
921 }
922
923 ?>
924
Note: See TracBrowser for help on using the browser.