Commit 32531454d9100e0c5e6924833e886bd37cea14ba

Authored by Edmar Moretti
1 parent 3e5b8dba

Adaptação dos códigos para geração de arquivos KML compactados (kmz) contendo os…

… dados dos layers no formato vetorial
Showing 1 changed file with 104 additions and 54 deletions   Show diff stats
pacotes/kmlmapserver/classes/layerserver.class.php
... ... @@ -40,7 +40,7 @@ This file is part of KMLMAPSERVER.
40 40  
41 41  
42 42 /** Fix a GE bug for filled polygons */
43   -define('TREAT_POLY_AS_LINE', true);
  43 +define('TREAT_POLY_AS_LINE', false);
44 44  
45 45 /** Enable cache */
46 46 define('ENABLE_CACHE', true);
... ... @@ -49,12 +49,13 @@ if (!extension_loaded('MapScript'))
49 49 {
50 50 dl( 'php_mapscript.' . PHP_SHLIB_SUFFIX );
51 51 }
52   -
53   -
  52 +if (!extension_loaded('php_mbstring'))
  53 +{
  54 + dl( 'php_mbstring.' . PHP_SHLIB_SUFFIX );
  55 +}
54 56 /**
55 57 * Main server class
56 58 */
57   -
58 59 class LayerServer {
59 60  
60 61 /** map file path */
... ... @@ -159,7 +160,6 @@ class LayerServer {
159 160 }
160 161 }
161 162 }
162   -
163 163 // If not layer are requested, send all as networklinks
164 164 if(!$this->typename){
165 165 $this->_networklink = true;
... ... @@ -167,7 +167,6 @@ class LayerServer {
167 167 } else {
168 168 $this->_networklink = false;
169 169 }
170   -
171 170 $this->_xml = new SimpleXMLElement('<kml xmlns="http://earth.google.com/kml/2.0"><Document ></Document></kml>');
172 171 // Prepare projection
173 172 $this->in_proj = ms_newProjectionObj($this->map_object->getProjection());
... ... @@ -178,8 +177,6 @@ class LayerServer {
178 177 $protocolo = explode("/",$_SERVER['SERVER_PROTOCOL']);
179 178 $protocolo = strtolower($protocolo[0]);
180 179 $this->endpoint = $protocolo . '://'.$_SERVER['SERVER_NAME'] . ($_SERVER['SERVER_PORT'] ? ':'.$_SERVER['SERVER_PORT'] : '') . $_SERVER['PHP_SELF'];
181   -
182   -
183 180 // Process request
184 181 if(!$this->has_error()) {
185 182 $this->process_request();
... ... @@ -242,7 +239,6 @@ class LayerServer {
242 239 }
243 240 return array($searchfield, $searchstring);
244 241 }
245   -
246 242 /**
247 243 * Process request
248 244 */
... ... @@ -259,7 +255,6 @@ class LayerServer {
259 255 }
260 256 }
261 257 }
262   -
263 258 /**
264 259 * Add a networklink
265 260 */
... ... @@ -279,11 +274,11 @@ class LayerServer {
279 274 * @return boolean false on error
280 275 */
281 276 function process_layer_request(&$layer_name){
282   -
  277 +//error_reporting(E_ALL);
283 278 $layer = @$this->map_object->getLayerByName($layer_name);
284 279  
285 280 if(!$layer){
286   - $this->set_error('Layer not found ' . $layer_name, $layer_name);
  281 + $this->set_error('Nenhum layer com esse nome foi encontrado no mapfile ' . $layer_name, $layer_name);
287 282 return false;
288 283 }
289 284  
... ... @@ -294,7 +289,7 @@ class LayerServer {
294 289 $description_template = $layer->getMetadata('DESCRIPTION_TEMPLATE');
295 290  
296 291 // Set on
297   - $layer->set( 'status', MS_ON );
  292 + $layer->set( 'status', MS_DEFAULT );
298 293  
299 294 // Set kml title from layer description (default to layer name)
300 295 $layer_desc = $this->get_layer_description($layer);
... ... @@ -345,11 +340,13 @@ class LayerServer {
345 340 // Get results
346 341 if(MS_SUCCESS == $layer->open()){
347 342 // Search which column to use to identify the feature
348   - $namecol = $layer->getMetadata('RESULT_FIELDS');
349   - if(!$namecol){
  343 + $namecol = $layer->getMetadata("itens");
  344 + if($namecol == ""){
350 345 $cols = array_values($layer->getItems());
351 346 $namecol = $cols[0];
352 347 }
  348 + else
  349 + {$namecol = explode(",",$namecol);$namecol = $namecol[0];}
353 350 // Add classes
354 351 $folder =& $this->_xml->Document->addChild('Folder');
355 352 $class_list = $this->parse_classes($layer, $folder, $namecol, $title_field, $description_template);
... ... @@ -359,6 +356,8 @@ class LayerServer {
359 356 $folder->addChild('name', $layer_desc);
360 357  
361 358 //print("$searchfield && $searchstring");
  359 + if(!isset($searchfield)){$searchfield = false;}
  360 + if(!isset($searchstring)){$searchstring = false;}
362 361 if($searchfield && $searchstring){
363 362 if(@$layer->queryByAttributes($searchfield, $searchstring, MS_MULTIPLE) == MS_SUCCESS){
364 363 $layer->open();
... ... @@ -376,9 +375,21 @@ class LayerServer {
376 375 return false;
377 376 }
378 377 } else { // Get all shapes
379   - $status = $layer->whichShapes($this->map_object->extent);
380   - while ($shape = $layer->nextShape()) {
381   - $this->process_shape($layer, $shape, $class_list, $folder,$namecol );
  378 + $layer->set("template","none.htm");
  379 + $layer->queryByrect($this->map_object->extent);
  380 + $layer->open();
  381 + $n = $layer->getNumResults();
  382 + for ($j=0; $j < $n; $j++)
  383 + {
  384 + // get next shape row
  385 + $result = $layer->getResult($j);
  386 + $shape = $layer->getShape($result->tileindex, $result->shapeindex);
  387 + $shape->classindex = $result->classindex;
  388 + $this->process_shape($layer, $shape, $class_list, $folder, $namecol);
  389 + }
  390 + if($n == 0) {
  391 + $this->set_error('QueryByRect returned no data', $layer_name);
  392 + return false;
382 393 }
383 394 }
384 395 $layer->close();
... ... @@ -394,15 +405,19 @@ class LayerServer {
394 405 * Process the shape
395 406 */
396 407 function process_shape(&$layer, &$shape, &$class_list, &$folder, &$namecol){
  408 + $itens = $layer->getmetadata("itens");
  409 + $itensdesc = $layer->getmetadata("itensdesc");
397 410 $shape->project($this->in_proj, $this->out_proj);
398 411 // Assign style
399 412 if($layer->classitem){
400 413 $style_id = $this->get_shape_class($layer->classitem, $shape->values, $class_list);
401 414 }
402   - if(!$style_id){
  415 + if(!isset($style_id)){
403 416 // Get first class
404 417 $class_keys = array_keys($class_list);
405   - $style_id = $class_keys[0];
  418 + $nome = $layer->getclass($shape->classindex);
  419 + $nome= $nome->name;
  420 + $style_id = preg_replace('/[^A-z0-9]/', '_', $layer->name . $nome);
406 421 }
407 422 // Add the feature
408 423 if(array_key_exists('folder', $class_list[$style_id])) {
... ... @@ -418,23 +433,21 @@ class LayerServer {
418 433 }
419 434 // Add style class
420 435 $style_url =& $this->add_style($layer, $feature_folder, $style_id, $class_list[$style_id], $namecol, $shape->values);
421   -
422 436 $wkt = $shape->toWkt();
423   - $placemark =& $this->add_feature($feature_folder, $wkt, $shape->values[$namecol], $shape->values, $description_template, $class_list[$style_id]);
  437 + $description_template = false;
  438 + $placemark =& $this->add_feature($feature_folder, $wkt, $shape->values[$namecol], $shape->values, $description_template, $class_list[$style_id],$itens,$itensdesc);
424 439  
425 440 $placemark->addChild('styleUrl', '#'. $style_url);
426 441  
427 442 }
428   -
429 443 /**
430 444 * Add the feature to the result set
431 445 * @return reference to placemark object
432 446 */
433   - function &add_feature(&$folder, &$wkt, $featurename, $attributes, $description_template, $style_data){
  447 + function &add_feature(&$folder, &$wkt, $featurename, $attributes, $description_template, $style_data,$itens,$itensdesc){
434 448 $pm = $folder->addChild('Placemark');
435   - //if($featurename == 'VERCELLI') {var_dump($wkt); die();}
436   - $pm->addChild('name', iconv($this->encoding, 'utf-8', $featurename));
437   - $pm->addChild('description', $this->get_feature_description($featurename, $attributes, $description_template));
  449 + $pm->addChild('name',mb_convert_encoding($featurename,"UTF-8",mb_detect_encoding($featurename,"UTF-8, ISO-8859-1")));
  450 + $pm->addChild('description', $this->get_feature_description($featurename, $attributes, $description_template,$itens,$itensdesc));
438 451 // Now parse the wkt
439 452 if(strpos($wkt, 'MULTILINESTRING') !== false){
440 453 $this->add_multilinestring($wkt, $pm, $featurename, $style_data['icon']);
... ... @@ -506,8 +519,16 @@ class LayerServer {
506 519 */
507 520 function add_point(&$wkt, &$element, $featurename){
508 521 $pt = $element->addChild('Point');
  522 + /*
509 523 preg_match('/(\d\.?\d+\s\d+\.?\d+)/', $wkt, $data);
  524 + //var_dump($data);exit;
510 525 $data = str_replace(' ', ',', $data[1]);
  526 + */
  527 + $data = str_replace("(","",$wkt);
  528 + $data = str_replace(")","",$data);
  529 + $data = str_replace("POINT","",$data);
  530 + $data = str_replace(" ",",",trim($data));
  531 + //echo $data;exit;
511 532 $pt->addChild('coordinates', $data);
512 533 }
513 534  
... ... @@ -548,20 +569,28 @@ class LayerServer {
548 569 /**
549 570 * Get the feature description
550 571 */
551   - function get_feature_description($featurename, $attributes, $description_template){
  572 + function get_feature_description($featurename, $attributes, $description_template,$itens,$itensdesc){
552 573 // Compute hyperlink
553   - if($description_template){
554   - $description = $description_template;
555   - foreach($attributes as $k => $val){
556   - $description = str_replace("%$k%", iconv($this->encoding, 'utf-8', $val), $description);
557   - }
558   - } else {
559   - $description = iconv($this->encoding, 'utf-8', $featurename);
  574 + //var_dump($attributes);exit;
  575 + $n = "";
  576 + if($itens == "")
  577 + {
  578 + foreach($attributes as $k => $val){
  579 + $n .= $k." - ".$attributes[$k]."\n";
  580 + }
560 581 }
561   - return htmlentities($description);
  582 + else
  583 + {
  584 + $itens = explode(",",$itens);
  585 + $itensdesc = explode(",",$itensdesc);
  586 + for($i=0;$i<count($itens);$i++)
  587 + {
  588 + $n .= $itensdesc[$i]." - ".$attributes[$itens[$i]]."\n";
  589 + }
  590 + }
  591 + $description = mb_convert_encoding($n,"UTF-8",mb_detect_encoding($n,"UTF-8,ISO-8859-1"));
  592 + return $description;
562 593 }
563   -
564   -
565 594 /**
566 595 * Parse classes
567 596 * @return array hash of 'style_id' => style_data)
... ... @@ -583,9 +612,9 @@ class LayerServer {
583 612 $style['outlinecolor'] = $_style->outlinecolor;
584 613 $style['width'] = $_style->size; // Lines
585 614 $style['backgroundcolor'] = $_style->backgroundcolor;
586   - $style['icon'] = $this->get_icon_url($layer, $class->name);
  615 + if($layer->type == MS_LAYER_POINT)
  616 + $style['icon'] = $this->get_icon_url($layer, $class->name,$i);
587 617 $style['icon_width'] = $_style->size; // Points
588   -
589 618 }
590 619 $style['expression'] = $class->getExpression();
591 620 // Set description_template if any
... ... @@ -597,10 +626,10 @@ class LayerServer {
597 626 }
598 627 // Create style element
599 628 $style_id = preg_replace('/[^A-z0-9]/', '_', $layer->name . $class->name);
600   - //$this->add_style($layer, $folder, $style_id, $style, $namecol, $title_field );
  629 + $this->add_style($layer, $folder, $style_id, $style, $namecol, $title_field );
601 630 // create folder if more than one class
602 631 if($numclasses > 1){
603   - $style['folder'] =& $class->name;
  632 + //$style['folder'] =& $class->name;
604 633 //$folder->addChild('Folder');
605 634 //$style['folder']->addChild('name', $class->name);
606 635 }
... ... @@ -717,6 +746,7 @@ class LayerServer {
717 746 }
718 747 */
719 748 // Label size and color
  749 + /*
720 750 if($style_data['label_size'] || $style_data['label_color']){
721 751 $ls =& $new_style->addChild('LabelStyle');
722 752 if($style_data['label_size'] != -1 && $style_data['label_size'] != 32){
... ... @@ -726,6 +756,7 @@ class LayerServer {
726 756 $ls->addChild('color', sprintf('FF%02X%02X%02X', $style_data['label_color']->blue, $style_data['label_color']->green, $style_data['label_color']->red));
727 757 }
728 758 }
  759 + */
729 760 }
730 761  
731 762 /**
... ... @@ -736,9 +767,9 @@ class LayerServer {
736 767 $this->add_style_line($new_style, $style_data);
737 768 $st =& $new_style->addChild('PolyStyle');
738 769 //die(print_r($backgroundcolor, true));
739   - if($style_data['backgroundcolor']->red != -1){
740   - $st->addChild('color', sprintf('FF%02X%02X%02X', $style_data['backgroundcolor']->blue, $style_data['backgroundcolor']->green, $style_data['backgroundcolor']->red));
741   - $st->addChild('fill', 0);
  770 + if($style_data['color']->red != -1){
  771 + $st->addChild('color', sprintf('FF%02X%02X%02X', $style_data['color']->blue, $style_data['color']->green, $style_data['color']->red));
  772 + $st->addChild('fill', 1);
742 773 } else {
743 774 $st->addChild('fill', 0);
744 775 }
... ... @@ -778,8 +809,18 @@ class LayerServer {
778 809 /**
779 810 * Get the url for a point icon
780 811 */
781   - function get_icon_url($layer, $classname){
782   - return $this->endpoint . '?service=icon&map=' . $this->map . '&typename=' . urlencode($layer->name) . '&classname=' . urlencode($classname);
  812 + function get_icon_url($layer, $classname, $classindex){
  813 + $classe = $layer->getclass($classindex);
  814 + $estilo = $classe->getstyle(0);
  815 + $url = "";
  816 + $imageObj = $classe->createLegendIcon($estilo->size, $estilo->size);
  817 + if($imageObj)
  818 + {
  819 + $url = $imageObj->saveWebImage();
  820 + $protocolo = explode("/",$_SERVER['SERVER_PROTOCOL']);
  821 + $url = strtolower($protocolo[0]."://".$_SERVER['HTTP_HOST']).$url;
  822 + }
  823 + return $url; //$this->endpoint . '?service=icon&map=' . $this->map . '&typename=' . urlencode($layer->name) . '&classname=' . urlencode($classname);
783 824 }
784 825  
785 826 /**
... ... @@ -857,8 +898,10 @@ class LayerServer {
857 898 else
858 899 {$this->map = "../../aplicmap/geral1.map";}
859 900 $this->map_object = ms_newMapObj($this->map);
  901 + if(!$this->_zipped)
860 902 $this->map_object->setmetadata('wms_onlineresource',$servidor.":80/i3geo/ogc.php?tema=".$temp."&width=1500&height=1500&");
861   - for ($i=0;$i < ($this->map_object->numlayers);$i++)
  903 + $n = $this->map_object->numlayers;
  904 + for ($i=0;$i < $n;$i++)
862 905 {
863 906 $l = $this->map_object->getlayer($i);
864 907 $l->set("status",MS_DELETE);
... ... @@ -868,8 +911,11 @@ class LayerServer {
868 911 {
869 912 $l = $maptemp->getlayer($i);
870 913 $l->set("status",MS_DEFAULT);
871   - $l->set("type",MS_LAYER_RASTER);
872   - $l->setmetadata('wms_onlineresource',"../../ogc.php?tema=".$temp."&width=500&height=500&");
  914 + if(!$this->_zipped)
  915 + {
  916 + $l->set("type",MS_LAYER_RASTER);
  917 + $l->setmetadata('wms_onlineresource',"../../ogc.php?tema=".$temp."&width=500&height=500&");
  918 + }
873 919 ms_newLayerObj($this->map_object, $l);
874 920 }
875 921 }
... ... @@ -880,11 +926,14 @@ class LayerServer {
880 926 {include_once("ms_configura.php");}
881 927 else
882 928 {include_once("../../ms_configura.php");}
  929 + if(!$this->_zipped)
883 930 $this->map_object->setmetadata('wms_onlineresource',$servidor.":80".$locmapserv."?map=".$temp."&width=1500&height=1500&");
884   - for ($i=0;$i < ($this->map_object->numlayers);$i++)
  931 + $n = $this->map_object->numlayers;
  932 + for ($i=0;$i < $n;$i++)
885 933 {
886 934 $l = $this->map_object->getlayer($i);
887 935 $l->set("status",MS_DEFAULT);
  936 + if(!$this->_zipped)
888 937 $l->set("type",MS_LAYER_RASTER);
889 938 //$l->setmetadata('wms_onlineresource',"../../ogc.php?tema=".$temp."&width=500&height=500&");
890 939 //ms_newLayerObj($this->map_object, $l);
... ... @@ -939,7 +988,8 @@ class LayerServer {
939 988 * Calculate cache file name
940 989 */
941 990 function get_cache_file_name(){
942   - return 'cache/'. md5($_SERVER['QUERY_STRING']) . ($this->_zipped ? '.kmz' : '.kml');
  991 + include("../../ms_configura.php");
  992 + return $dir_tmp.'/'. md5($_SERVER['QUERY_STRING']) . ($this->_zipped ? '.kmz' : '.kml');
943 993 }
944 994  
945 995 /**
... ... @@ -955,8 +1005,8 @@ class LayerServer {
955 1005 $data = $ziper->file();
956 1006 }
957 1007 // Create cache if needed
958   - if(ENABLE_CACHE && count($this->layers) == 1 && $this->layers[$this->typename]->getMetadata('KML_CACHE')) {
959   - error_log( 'creating cache ' . $this->get_cache_file_name() );
  1008 + if(ENABLE_CACHE && count($this->layers) == 1) {
  1009 + //error_log( 'creating cache ' . $this->get_cache_file_name() );
960 1010 file_put_contents($this->get_cache_file_name(), $data);
961 1011 }
962 1012 print $data;
... ...