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

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Thu Sep 18 08:40:33 PDT 2014


Author: armin11
Date: 2014-09-18 08:40:32 -0700 (Thu, 18 Sep 2014)
New Revision: 9076

Modified:
   trunk/mapbender/http/classes/class_iso19139.php
Log:
allow multipolygons (gml3) as metadata extents

Modified: trunk/mapbender/http/classes/class_iso19139.php
===================================================================
--- trunk/mapbender/http/classes/class_iso19139.php	2014-09-15 12:13:17 UTC (rev 9075)
+++ trunk/mapbender/http/classes/class_iso19139.php	2014-09-18 15:40:32 UTC (rev 9076)
@@ -27,7 +27,7 @@
 	var $abstract;
 	var $metadata;
 	var $wgs84Bbox = array(); //minx, miny, maxx, maxy in EPSG:4326
-	var $polygonalExtentExterior; //Prototype: Two dimensional array of coordinates $polygonalExtentExterior[i]['x'],  $polygonalExtentExterior[i]['y']
+	var $polygonalExtentExterior; //Prototype: Array of polygon exterior rings which are two dimensional arrays of coordinates $polygonalExtentExterior[j][i]['x'],  $polygonalExtentExterior[j][i]['y'] - j is the index of the polygon, i of the coordinate or pos
 	//var $polygonalExtentExterior = boolean; //TODO maybe implemented somewhen, now only exterior (GML3) is supported, maybe we can use the GML classes themself?
 	var $datasetId;
 	var $datasetIdCodeSpace;
@@ -299,31 +299,18 @@
 			$this->wgs84Bbox = array($minx,$miny,$maxx,$maxy); 
 			//more info: https://geo-ide.noaa.gov/wiki/index.php?title=ISO_Extents
 			//look for GML3 polygon as exterior ring in two alternative encodings (see: http://www.galdosinc.com/archives/191 - all coords are interpreted as given in EPSG:4326 for the moment!!!):
-			if ($iso19139Xml->xpath('//gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_BoundingPolygon/gmd:polygon/gml:Polygon/gml:exterior/gml:LinearRing/gml:posList')) {
-				//read posList
-				$exteriorRingPoints = $iso19139Xml->xpath('//gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_BoundingPolygon/gmd:polygon/gml:Polygon/gml:exterior/gml:LinearRing/gml:posList');
-				if (count($exteriorRingPoints) > 0) {
-					//poslist is only space separated
-					$exteriorRingPointsArray = explode(' ',$exteriorRingPoints[0]);
-					for ($i = 0; $i <= count($exteriorRingPointsArray)/2-1; $i++) {
-						$this->polygonalExtentExterior[$i]['x'] = $exteriorRingPointsArray[2*$i];
-						$this->polygonalExtentExterior[$i]['y'] = $exteriorRingPointsArray[(2*$i)+1];
-					}
+			//allow multipolygons - multisurface objects
+			//test if single polygon or multipolygon is given
+			if ($iso19139Xml->xpath('//gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_BoundingPolygon/gmd:polygon/gml:MultiSurface')) {
+				//count surfaceMembers
+				$numberOfSurfaces = count($iso19139Xml->xpath('//gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_BoundingPolygon/gmd:polygon/gml:MultiSurface/gml:surfaceMember'));
+				$e = new mb_exception("class_iso19139.php: found multisurface element");
+				for ($k = 0; $k < $numberOfSurfaces; $k++) {
+					$this->polygonalExtentExterior[] = $this->parsePolygon($iso19139Xml, '//gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_BoundingPolygon/gmd:polygon/gml:MultiSurface/gml:surfaceMember/');
 				}
-			} else {
-				//try to read coordinates
-				$exteriorRingPoints = $iso19139Xml->xpath('//gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_BoundingPolygon/gmd:polygon/gml:Polygon/gml:exterior/gml:LinearRing/gml:coordinates');
-				if (count($exteriorRingPoints) > 0) {
-					//two coordinates of one point are comma separated
-					//problematic= ", " or " ," have to be deleted before
-					$exteriorRingPoints[0] = str_replace(', ',',',str_replace(' ,',',',$exteriorRingPoints[0]));
-					$exteriorRingPointsArray = explode(' ',$exteriorRingPoints[0]);
-					for ($i = 0; $i <= count($exteriorRingPointsArray)-1;$i++) {
-						$coords = explode(",",$exteriorRingPointsArray[$i]);
-						$this->polygonalExtentExterior[$i]['x'] = $coords[0];
-						$this->polygonalExtentExterior[$i]['y'] = $coords[1];
-					}
-				}
+			} else { 
+				$this->polygonalExtentExterior[0] = $this->parsePolygon($iso19139Xml, '//gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_BoundingPolygon/gmd:polygon/');
+
 			}
 			$this->hierachyLevel = $iso19139Xml->xpath('//gmd:MD_Metadata/gmd:hierarchyLevel/gmd:MD_ScopeCode');
 			$this->hierachyLevel = $this->hierachyLevel[0];
@@ -375,6 +362,39 @@
 		}
 	}
 	
+	public function parsePolygon($iso19139Xml, $pathToPolygon) {
+		$polygonalExtentExterior = array();		
+		// //gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_BoundingPolygon/gmd:polygon/
+		// or //gmd:MD_Metadata/gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_BoundingPolygon/gmd:polygon/gml:MultiSurface/surfaceMember[1]/
+		if ($iso19139Xml->xpath($pathToPolygon.'gml:Polygon/gml:exterior/gml:LinearRing/gml:posList')) {
+		//read posList
+			$exteriorRingPoints = $iso19139Xml->xpath($pathToPolygon.'gml:Polygon/gml:exterior/gml:LinearRing/gml:posList');
+			if (count($exteriorRingPoints) > 0) {
+				//poslist is only space separated
+				$exteriorRingPointsArray = explode(' ',$exteriorRingPoints[0]);
+				for ($i = 0; $i <= count($exteriorRingPointsArray)/2-1; $i++) {
+					$polygonalExtentExterior[$i]['x'] = $exteriorRingPointsArray[2*$i];
+					$polygonalExtentExterior[$i]['y'] = $exteriorRingPointsArray[(2*$i)+1];
+				}
+			}
+		} else {
+			//try to read coordinates
+			$exteriorRingPoints = $iso19139Xml->xpath($pathToPolygon.'gml:Polygon/gml:exterior/gml:LinearRing/gml:coordinates');
+			if (count($exteriorRingPoints) > 0) {
+				//two coordinates of one point are comma separated
+				//problematic= ", " or " ," have to be deleted before
+				$exteriorRingPoints[0] = str_replace(', ',',',str_replace(' ,',',',$exteriorRingPoints[0]));
+				$exteriorRingPointsArray = explode(' ',$exteriorRingPoints[0]);
+				for ($i = 0; $i <= count($exteriorRingPointsArray)-1;$i++) {
+					$coords = explode(",",$exteriorRingPointsArray[$i]);
+					$polygonalExtentExterior[$i]['x'] = $coords[0];
+					$polygonalExtentExterior[$i]['y'] = $coords[1];
+				}
+			}
+		}
+		return $polygonalExtentExterior; 
+	}
+
 	public function createFromUrl($url){
 		$this->href = $url;
 		if ($this->isLinkAlreadyInDB() == false) {
@@ -836,7 +856,37 @@
 				//store them in object
 				//$bbox = str_replace(' ',',',str_replace(')','',str_replace('BOX(','',$row['bbox2d'])));
 				//$e = new mb_exception("class_iso19139.php: got bbox for metadata: ".$bbox);
-				$this->polygonalExtentExterior = $this->parseExteriorPolygon($row['bounding_polygon']);
+
+				//check if multisurface or simple polygon is given
+				$gml3FromPostgis = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>".$row['bounding_polygon'];
+				libxml_use_internal_errors(true);
+				try {
+					$gml3 = simplexml_load_string($gml3FromPostgis);
+					if ($gml3 === false) {
+						foreach(libxml_get_errors() as $error) {
+        						$err = new mb_exception("class_Iso19139:".$error->message);
+    						}
+						throw new Exception("class_Iso19139:".'Cannot parse Metadata XML!');
+						return false;
+					}
+				}
+				catch (Exception $e) {
+    					$err = new mb_exception("class_Iso19139:".$e->getMessage());
+					return false;
+				}			
+				//if parsing was successful
+				if ($gml3 !== false) {
+					//check count of polygons
+					if ($gml3->xpath('//gml:MultiSurface')) {
+						//count surfaceMembers
+						$numberOfSurfaces = count($gml3->xpath('//gml:MultiSurface/gml:surfaceMember'));
+						for ($k = 0; $k < $numberOfSurfaces; $k++) {
+							$this->polygonalExtentExterior[] = $this->parsePolygon($gml3, '//gml:MultiSurface/gml:surfaceMember/');
+						}
+					} else { 
+						$this->polygonalExtentExterior[0] = $this->parsePolygon($gml3, '/');
+					}
+				}
 			}
 			//fill keywords and categories later cause they are stored in relations!
 			/*$this->keywords = array();
@@ -1166,30 +1216,43 @@
 		$postGisBbox = "";
 		if (count($bboxArray) != 4 || $bboxArray[0] == '') {
 			//create dummy bbox
-			//$e = new mb_exception("class_iso19139.php: bbox: ".$postGisBbox);
 			$bboxArray = array(-180,-90,180,90);
 		}
 		//"SRID=4326;POLYGON((-140 -80,-140 80,170 80,170 -80,-140 -80))"
 		$postGisBbox = "SRID=4326;POLYGON((".$bboxArray[0]." ".$bboxArray[1].",".$bboxArray[0]." ".$bboxArray[3].",".$bboxArray[2]." ".$bboxArray[3].",".$bboxArray[2]." ".$bboxArray[1].",".$bboxArray[0]." ".$bboxArray[1]."))";
-		//$e = new mb_exception("class_iso19139.php: bbox: ".$postGisBbox);
 		return $postGisBbox;
 	}
 
-	private function createWktPolygonFromPointArray($pointArray) { //-180 -90 180 90 
+	public function createWktPolygonFromPointArray($pointArray) { //-180 -90 180 90 
+		if ($pointArray == null) {
+			return null;
+		}
 		$postGisPolygon = "";
-		//"SRID=4326;POLYGON((-140 -80,-140 80,170 80,170 -80,-140 -80))"
-		$postGisPolygon = "SRID=4326;POLYGON((";
-		if (count($pointArray) == 0) {
-			return null;
- 		} else {
-			foreach ($pointArray as $point) {
-				$postGisPolygon .= trim($point['x'])." ".trim($point['y']).",";
+		if (count($pointArray) > 1) { //multisurface
+			$postGisPolygon = "SRID=4326;MULTIPOLYGON(((";
+			foreach ($pointArray as $polygon) {
+					foreach($polygon as $point) {
+						$postGisPolygon .= trim($point['x'])." ".trim($point['y']).",";
+					}
+					$postGisPolygon = rtrim($postGisPolygon,',')."),(";	
 			}
-			$postGisPolygon = rtrim($postGisPolygon,',');
-			$postGisPolygon .= "))";
-			$e = new mb_notice("class_iso19139.php: polygon: ".$postGisPolygon);
-			return $postGisPolygon;
+			$postGisPolygon = rtrim($postGisPolygon,'(,')."))";
+		} else {
+			//"SRID=4326;POLYGON((-140 -80,-140 80,170 80,170 -80,-140 -80))"
+			$postGisPolygon = "SRID=4326;POLYGON((";
+			if (count($pointArray) == 0) {
+				return null;
+ 			} else {
+				//only use first polygon entry
+				$pointArray = $pointArray[0];
+				foreach ($pointArray as $point) {
+					$postGisPolygon .= trim($point['x'])." ".trim($point['y']).",";
+				}
+				$postGisPolygon = rtrim($postGisPolygon,',');
+				$postGisPolygon .= "))";
+			}
 		}
+		return $postGisPolygon;
 	}
 
 	public function insertKeywordsAndCategoriesIntoDB($metadataId,$resourceType,$resourceId){



More information about the Mapbender_commits mailing list