[Mapbender-commits] r8500 - trunk/mapbender/http/classes

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Mon Oct 8 07:26:15 PDT 2012


Author: verenadiewald
Date: 2012-10-08 07:26:15 -0700 (Mon, 08 Oct 2012)
New Revision: 8500

Modified:
   trunk/mapbender/http/classes/class_kml_parser_ows.php
Log:
try to get KML point icon img url (if exists) from KML

Modified: trunk/mapbender/http/classes/class_kml_parser_ows.php
===================================================================
--- trunk/mapbender/http/classes/class_kml_parser_ows.php	2012-10-08 12:59:49 UTC (rev 8499)
+++ trunk/mapbender/http/classes/class_kml_parser_ows.php	2012-10-08 14:26:15 UTC (rev 8500)
@@ -107,7 +107,68 @@
 		$doc = new DOMDocument("1.0");
 		$doc->preserveWhiteSpace = false;
 		$doc->loadXML($kml);
+		$xpath = new DOMXPath($doc);
+		$xpath->registerNamespace("kml","http://earth.google.com/kml/2.2");
 
+		$styles =array();
+		$styleNodes = $xpath->query("/kml:kml/kml:Document/kml:Style");
+		
+		//$e = new mb_exception("found this many styles:". $styleNodes->length);
+		foreach($styleNodes as $styleNode){
+		    $hrefNodes = $styleNode->getElementsByTagName("href");	
+		    if($hrefNodes->length > 0){
+			    $href = $hrefNodes->item(0)->nodeValue;
+			    $styles[$styleNode->getAttribute("id")] = array(
+			        "href" => $href
+			    );	
+		    }	        
+		}
+		
+		$styleMapNodes = $xpath->query("/kml:kml/kml:Document/kml:StyleMap");
+		//$e = new mb_exception($styleMapNodes->length);
+		foreach($styleMapNodes as $styleMapNode){
+		    
+		    foreach($styleMapNode->children as $child){
+		        $keyNodes = $child->findElementsByTagName("key");
+		        if($keyNodes->length > 0){
+		            if($keyNodes->item(0)->nodeValue == "normal"){
+		                
+		                
+    		            $styleNodes = $child->findElementsByTagName("Style");
+    		            if($styleNodes->length > 0){
+        		            $hrefNodes = $styleNodes->item(0)->getElementsByTagName("href");	
+        		            if($hrefNodes->length > 0){
+        			            $href = $hrefNodes->item(0)->nodeValue;
+        			            $styles[$styleMapNode->getAttribute("id")] = array(
+        			        	"href" => $href
+        			            );
+        			            continue;	
+        		            }
+    		            }
+    		            
+		                
+		                
+		                $styleUrlNodes = $child->findElementsByTagName("styleUrl");
+		                if($styleUrlNodes->length > 0){
+		                    $id = $styleUrlNodes->item(0)->nodeValue;
+		                	if(substr($id,0,1) == "#"){
+					            $id = substr($id,1);
+					            $styles[$styleMapNode->getAttribute("id")] = array(
+					                "href" => $styles[$id]["href"]
+					            ); 
+					        }else{
+					            $e = new mb_exception("External style references not supported in KML parser");
+					        }
+		                    
+		                }
+		            }
+		        }
+		    }
+		    $styles[$styleMapNode->getAttribute("id")] = array(
+				"href" => $href
+			);
+		}
+		
 		/*
 		 * Get geometry information only, store it in placemarkArray
 		 */
@@ -125,11 +186,12 @@
 				 * For a placemark, the geometryArray should only contain 1 geometry!
 				 */
 				for ($i=0; $i < count($geometryArray); $i++) {
-					$currentPlacemark = new KMLPlacemark($geometryArray[$i]);
-					
-					foreach ($metadataArray as $key => $value) {
+				    $currentPlacemark = new KMLPlacemark($geometryArray[$i]);
+				    
+				    foreach ($metadataArray as $key => $value) {
 						$currentPlacemark->setProperty($key, $value);
 					}
+					
 					$currentPlacemark->setProperty("Mapbender:kml", true);
 					$currentPlacemark->setProperty("Mapbender:name", "unknown");
 					$currentPlacemark->setProperty("Mapbender:id", $kmlId);
@@ -138,14 +200,46 @@
 					// add description and name:
 					$namesNode = $node->getElementsByTagName('name');
 					if($namesNode->length > 0){
-						$name = $namesNode->item(0)->nodeValue;
+						$name = trim($namesNode->item(0)->nodeValue);
 					}
 					$descriptionsNode = $node->getElementsByTagName('description');
 					if($descriptionsNode->length > 0){
-						$description = $descriptionsNode->item(0)->nodeValue;
+						$description = trim($descriptionsNode->item(0)->nodeValue);
 					}
 					$currentPlacemark->setProperty("name", $name);
 					$currentPlacemark->setProperty("description", $description);
+
+					//get style information for KML point objects
+					if(get_class($geometryArray[$i]) == "KMLPoint") {
+    					//Inline Styles take precedence over shared styles
+    					$styleNodes = $node->getElementsByTagName('Style');
+    					if($styleNodes->length > 0) {
+    				        $styleNode = $styleNodes->item(0);
+    				        $hrefNodes = $styleNode->getElementsByTagName("href");	
+            			    if($hrefNodes->length > 0){
+            				    $href = $hrefNodes->item(0)->nodeValue;
+            				    $currentPlacemark->setProperty("Mapbender:icon",$href);	
+            			    }	        
+    					}else{
+    					    $styleUrlNodes = $node->getElementsByTagName('styleUrl');
+    					    if($styleUrlNodes->length > 0) {
+    					        $styleUrlNode = $styleUrlNodes->item(0);
+    					        // cut off leading #
+    					        $id = $styleUrlNode->nodeValue;
+                                $e = new mb_notice("found style url reference, id is '$id'");
+    					        if(substr($id,0,1) == "#"){
+    					            $id = substr($id,1);
+    					        }else{
+    					            $e = new mb_exception("External style references not supported in KML parser");
+    					        }
+    					        $e = new mb_notice("Looking up style: $id");
+    				             $currentPlacemark->setProperty("Mapbender:icon",$styles[$id]["href"]);
+    					        
+    					    }
+    					}
+					}
+					
+					
 					array_push($this->placemarkArray, $currentPlacemark);
 				}
 				$id ++;		    



More information about the Mapbender_commits mailing list