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

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Thu Jan 10 11:55:18 EST 2008


Author: christoph
Date: 2008-01-10 11:55:18 -0500 (Thu, 10 Jan 2008)
New Revision: 1956

Modified:
   trunk/mapbender/http/classes/class_kml.php
   trunk/mapbender/http/classes/class_kml_geometry.php
   trunk/mapbender/http/classes/class_kml_line.php
   trunk/mapbender/http/classes/class_kml_linearring.php
   trunk/mapbender/http/classes/class_kml_multigeometry.php
   trunk/mapbender/http/classes/class_kml_ows.php
   trunk/mapbender/http/classes/class_kml_parser_ows.php
   trunk/mapbender/http/classes/class_kml_placemark.php
   trunk/mapbender/http/classes/class_kml_point.php
   trunk/mapbender/http/classes/class_kml_polygon.php
Log:
dirname in includes

geoJson input for KML class

isGeometry is now a static function

Modified: trunk/mapbender/http/classes/class_kml.php
===================================================================
--- trunk/mapbender/http/classes/class_kml.php	2008-01-09 15:04:49 UTC (rev 1955)
+++ trunk/mapbender/http/classes/class_kml.php	2008-01-10 16:55:18 UTC (rev 1956)
@@ -16,7 +16,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-require_once("../../conf/mapbender.conf");
+require_once(dirname(__FILE__)."/../../conf/mapbender.conf");
 
 class kml {
 

Modified: trunk/mapbender/http/classes/class_kml_geometry.php
===================================================================
--- trunk/mapbender/http/classes/class_kml_geometry.php	2008-01-09 15:04:49 UTC (rev 1955)
+++ trunk/mapbender/http/classes/class_kml_geometry.php	2008-01-10 16:55:18 UTC (rev 1956)
@@ -17,13 +17,13 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
-require_once("../classes/class_mb_exception.php");
+require_once(dirname(__FILE__)."/../classes/class_mb_exception.php");
 
-require_once("../classes/class_kml_polygon.php");
-require_once("../classes/class_kml_linearring.php");
-require_once("../classes/class_kml_line.php");
-require_once("../classes/class_kml_point.php");
-require_once("../classes/class_kml_multigeometry.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_polygon.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_linearring.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_line.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_point.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_multigeometry.php");
 
 /**
  * An abstract class representing a geometry, which is any of the following:
@@ -42,7 +42,7 @@
 	 * 						{@link KMLMultiGeometry}, or instance of 
 	 * 						another class extending KMLGeometry.
 	 */
-	protected function isGeometry($obj) {
+	protected static function isGeometry($obj) {
 		if ($obj instanceof KMLGeometry) {
 			return true;
 		}
@@ -52,5 +52,9 @@
 		$e = new mb_warning("class_kml_geometry.php: isGeometry: not a geometry, but " . $type);
 		return false;
 	}
+	
+	public function getGeometryType () {
+		return get_class($this);
+	} 
 }
 ?>
\ No newline at end of file

Modified: trunk/mapbender/http/classes/class_kml_line.php
===================================================================
--- trunk/mapbender/http/classes/class_kml_line.php	2008-01-09 15:04:49 UTC (rev 1955)
+++ trunk/mapbender/http/classes/class_kml_line.php	2008-01-10 16:55:18 UTC (rev 1956)
@@ -17,8 +17,8 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
-require_once("../classes/class_mb_exception.php");
-require_once("../classes/class_kml_geometry.php");
+require_once(dirname(__FILE__)."/../classes/class_mb_exception.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_geometry.php");
 
 /**
  * Represents a line string, consisting of an array of points.
@@ -92,6 +92,13 @@
 	}
 
 	/**
+	 * @return array an array of points as associative array, coordinates as ["x"] and ["y"]
+	 */
+	public function getPointArray () {
+		return $this->pointArray;
+	}
+	
+	/**
 	 * An array of points, with a point being an associative 
 	 * array consisting of attributes "x" and "y".
 	 */

Modified: trunk/mapbender/http/classes/class_kml_linearring.php
===================================================================
--- trunk/mapbender/http/classes/class_kml_linearring.php	2008-01-09 15:04:49 UTC (rev 1955)
+++ trunk/mapbender/http/classes/class_kml_linearring.php	2008-01-10 16:55:18 UTC (rev 1956)
@@ -17,8 +17,8 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
-require_once("../classes/class_mb_exception.php");
-require_once("../classes/class_kml_line.php");
+require_once(dirname(__FILE__)."/../classes/class_mb_exception.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_line.php");
 
 /**
  * Represents a linear ring, consisting of an array of points. 

Modified: trunk/mapbender/http/classes/class_kml_multigeometry.php
===================================================================
--- trunk/mapbender/http/classes/class_kml_multigeometry.php	2008-01-09 15:04:49 UTC (rev 1955)
+++ trunk/mapbender/http/classes/class_kml_multigeometry.php	2008-01-10 16:55:18 UTC (rev 1956)
@@ -17,8 +17,8 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
-require_once("../classes/class_mb_exception.php");
-require_once("../classes/class_kml_geometry.php");
+require_once(dirname(__FILE__)."/../classes/class_mb_exception.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_geometry.php");
 
 /**
  * Represents a multi geometry, consisting of an array of geometries 
@@ -70,7 +70,7 @@
 	 * @return bool		true, if appending the geometry succeeded; else false.
 	 */
 	public function append ($aGeometry) {
-		if ($this->isGeometry($aGeometry)) {
+		if (KMLGeometry::isGeometry($aGeometry)) {
 			array_push($this->geometryArray, $aGeometry);
 			return true;
 		}

Modified: trunk/mapbender/http/classes/class_kml_ows.php
===================================================================
--- trunk/mapbender/http/classes/class_kml_ows.php	2008-01-09 15:04:49 UTC (rev 1955)
+++ trunk/mapbender/http/classes/class_kml_ows.php	2008-01-10 16:55:18 UTC (rev 1956)
@@ -20,13 +20,13 @@
 session_start();
 mb_internal_encoding("UTF-8");
 
-require_once("../../conf/mapbender.conf");
-require_once("../extensions/JSON.php");
-require_once("../classes/class_mb_exception.php");
+require_once(dirname(__FILE__)."/../../conf/mapbender.conf");
+require_once(dirname(__FILE__)."/../extensions/JSON.php");
+require_once(dirname(__FILE__)."/../classes/class_mb_exception.php");
 
-require_once("../classes/class_kml_geometry.php");
-require_once("../classes/class_kml_placemark.php");
-require_once("../classes/class_kml_parser_ows.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_geometry.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_placemark.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_parser_ows.php");;
 
 /**
  * Allows parsing a KML file, extracting the placemarks.
@@ -53,6 +53,7 @@
 	public function __toString() {
 
 		if (!$this->kml) {
+			//KML 2.2 output
 			$doc = new DOMDocument("1.0", CHARSET);
 			$doc->preserveWhiteSpace = false;
 	
@@ -63,12 +64,71 @@
 			$doc->appendChild($e_kml);
 	
 			// attach placemarks
-			
+			for ($i = 0; $i < $this->placemarkArray; $i++) {
+				$currentPlacemark = $this->placemarkArray[$i];
+
+				switch ($this->placemarkArray[$i]->getGeometryType()) {
+					case "KMLPoint" :
+						$e_geometry = $doc->createElement("Point");
+						$point = $currentPlacemark->getPoint();
+						$coordinates = $point->x . "," . $point->y;
+						$e_coordinates = $doc->createElement("coordinates", $coordinates);
+						$e_geometry->appendChild($e_coordinates);
+						break;
+					
+/* TODO:Polygons
+					case "KMLPolygon" :
+						$e_geometry = $doc->createElement("Polygon");
+						$e_outer = $doc->createElement("OuterBoundaryIs");
+						$e_outer_lr = $doc->createElement("LinearRing");
+						$outer_coordinates = ""; // TODO: get coords from placemark
+						$e_outer_coordinates = $doc->createElement("Coordinates", $outer_coordinates);
+						
+						$e_outer_lr->appendChild($e_outer_coordinates);
+						$e_outer->appendChild($e_outer_lr);
+						
+						for ($j = 0; $j < $currentPlacemark; $j++) {
+							
+						}
+						
+						$e_geometry->appendChild($e_coordinates);
+					outerBoundaryIs"}->{"LinearRing"}->{"coordinates
+					
+						break;
+*/					
+					case "KMLLineString" :
+						$e_geometry = $doc->createElement("LineString");
+						$coordinatesArray = $currentPlacemark->getPointArray();
+						$coordinates = "";
+						for ($j = 0; $j < count($coordinatesArray); $j++) {
+							if ($j > 0) {
+								$coordinates .= ",";
+							}
+							$coordinates .= $coordinatesArray[$j]->x . "," . $coordinatesArray[$j]->y;
+							
+						}
+						$e_coordinates = $doc->createElement("coordinates", $coordinates);
+						$e_geometry->appendChild($e_coordinates);
+						break;
+
+/*	TODO: Multigeometries				
+					case "KMLMultiGeometry" :
+						break;
+*/
+				}
+				// create a placemark tag with a geometry and add it to the document
+				if ($e_geometry) {
+					$e_placemark = $doc->createElement("Placemark");
+					$e_placemark->appendChild($e_geometry);
+					$e_document->appendChild($e_placemark);
+				}
+			}
 			$this->kml = $doc->saveXML();
 		}
 		return $this->kml;
 	}
 	
+	
 	/**
 	 * parses an incoming KML, creates the object, 
 	 * stores the kml in the object and in the database.
@@ -83,7 +143,8 @@
 			return false;
 		}
 
-		$parser = new KmlOwsParser($kml, $this->id);
+		$parser = new KmlOwsParser();
+		$parser->parseKML($kml, $this->id);
 		$this->placemarkArray = $parser->placemarkArray; 
 
 		return true;
@@ -130,6 +191,17 @@
 		return $str;
 	}
 
+	private function updateInDb($kmlDoc, $kmlId) {
+		$sql = "UPDATE gui_kml SET kml_doc = $1 WHERE kml_id = $2";
+		$v = array($kmlDoc, $kmlId);
+		$t = array("s", "i");
+		$result = db_prep_query($sql, $v, $t);
+		if (!$result) {
+			$e = new mb_exception("class_kml: kml update failed! " . db_error());
+			return false;
+		}
+	}
+
 	public function updateKml ($kmlId, $placemarkId, $geoJSON) {
 		$kmlFromDb = $this->getKmlDocumentFromDB($kmlId);
 		 
@@ -180,14 +252,7 @@
 			if ($metadataUpdateSuccessful && $geometryUpdateSuccessful) {
 				$updatedKml = $kmlDoc_DOM->saveXML();
 
-				$sql = "UPDATE gui_kml SET kml_doc = $1 WHERE kml_id = $2";
-				$v = array($updatedKml, $kmlId);
-				$t = array("s", "i");
-				$result = db_prep_query($sql, $v, $t);
-				if (!$result) {
-					$e = new mb_exception("class_kml: kml update failed! " . db_error());
-					return false;
-				}
+				$this->updateInDb($updatedKml, $kmlId);
 			}
 			else {
 				if (!$metadataUpdateSuccessful) {
@@ -313,11 +378,11 @@
 			// GML 3
 			$gmlNode = $currentNode_SimpleXML->{"exterior"}->{"LinearRing"}->{"posList"};
 			$kmlNode = $currentNode_SimpleXML->{"outerBoundaryIs"}->{"LinearRing"}->{"coordinates"};
-			if ($gmlNode->asXML()) {
+			if ($gmlNode && $gmlNode->asXML()) {
 				$currentNode_SimpleXML->{"exterior"}->{"LinearRing"}->{"posList"} = preg_replace("/,/", " ", preg_replace("/\[|\]/", "", $json->encode($geometry->coordinates)));
 			}
 			// KML 2.2
-			else if ($kmlNode->asXML()) {
+			else if ($kmlNode && $kmlNode->asXML()) {
 				$currentNode_SimpleXML->{"outerBoundaryIs"}->{"LinearRing"}->{"coordinates"} = preg_replace("/\],/", " ", preg_replace("/\][^,]|\[/", "", $json->encode($geometry->coordinates)));
 			}
 		}
@@ -326,11 +391,11 @@
 			$kmlNode = $currentNode_SimpleXML->{"coordinates"};
 
 			// GML 3
-			if ($gmlNode->asXML()) {
+			if ($gmlNode && $gmlNode->asXML()) {
 				$currentNode_SimpleXML->{"pos"} = preg_replace("/,/", " ", preg_replace("/\[|\]/", "", $json->encode($geometry->coordinates)));
 			}
 			// KML 2.2
-			else if ($kmlNode->asXML()) {
+			else if ($kmlNode && $kmlNode->asXML()) {
 				$currentNode_SimpleXML->{"coordinates"} = preg_replace("/\[|\]/", "", $json->encode($geometry->coordinates));
 			}
 		}
@@ -339,11 +404,11 @@
 			$kmlNode = $currentNode_SimpleXML->{"coordinates"};
 
 			// GML 3
-			if ($gmlNode->asXML()) {
+			if ($gmlNode && $gmlNode->asXML()) {
 				$currentNode_SimpleXML->{"posList"} = preg_replace("/,/", " ", preg_replace("/\[|\]/", "", $json->encode($geometry->coordinates)));
 			}
 			// KML 2.2
-			else if ($kmlNode->asXML()) {
+			else if ($kmlNode && $kmlNode->asXML()) {
 				$currentNode_SimpleXML->{"coordinates"} = preg_replace("/\[|\]/", "", $json->encode($geometry->coordinates));
 			}
 		}
@@ -351,9 +416,13 @@
 	}
 
 	private function updateMetadata($currentPlacemark, $metadataObj) {
+		$metadataExistsAndUpdateSucceeded = true;
+		
 		$currentPlacemark_SimpleXML = simplexml_import_dom($currentPlacemark);
 		$extendedDataNode = $currentPlacemark_SimpleXML->{"ExtendedData"};
 		if ($extendedDataNode) {
+			$metadataExistsAndUpdateSucceeded = false;
+
 			// Either, data is within a SCHEMADATA tag...
 			$simpleDataNodes = $extendedDataNode->{"SchemaData"}->{"SimpleData"};
 			if ($simpleDataNodes) {
@@ -365,11 +434,13 @@
 						$tmp->nodeValue = $metadataObj->$name;
 					}										
 				}
+
+				$metadataExistsAndUpdateSucceeded = true;
 			}
 
 			// ...or within a DATA tag
 			$dataNodes = $extendedDataNode->{"Data"};
-			if ($dataNodes) {
+			if ($dataNodes && !$metadataExistsAndUpdateSucceeded) {
 				foreach ($dataNodes as $dataNode) {
 					$tmp = dom_import_simplexml($dataNode);
 					$name = $tmp->getAttribute("name");
@@ -378,10 +449,10 @@
 						$tmp->nodeValue = $metadataObj->$name;
 					}										
 				}
+				$metadataExistsAndUpdateSucceeded = true;
 			}
-			return true;
 		}
-		return false;
+		return $metadataExistsAndUpdateSucceeded;
 	}
 
 	/**

Modified: trunk/mapbender/http/classes/class_kml_parser_ows.php
===================================================================
--- trunk/mapbender/http/classes/class_kml_parser_ows.php	2008-01-09 15:04:49 UTC (rev 1955)
+++ trunk/mapbender/http/classes/class_kml_parser_ows.php	2008-01-10 16:55:18 UTC (rev 1956)
@@ -17,13 +17,14 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
-require_once("../classes/class_mb_exception.php");
-require_once("../classes/class_kml_polygon.php");
-require_once("../classes/class_kml_linearring.php");
-require_once("../classes/class_kml_line.php");
-require_once("../classes/class_kml_point.php");
-require_once("../classes/class_kml_multigeometry.php");
-require_once("../classes/class_kml_placemark.php");
+require_once(dirname(__FILE__)."/../extensions/JSON.php");
+require_once(dirname(__FILE__)."/../classes/class_mb_exception.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_polygon.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_linearring.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_line.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_point.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_multigeometry.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_placemark.php");
 
 /**
  * @package KML
@@ -31,8 +32,52 @@
  class KmlOwsParser {
 	var $placemarkArray = array();
 	
-	public function __construct($kml, $kmlId) {
+	public function __construct() {
+	}
+	
+	public function parseGeoJSON ($geoJSON, $kmlId) {
+		$json = new Services_JSON();
+		$geometryFromGeoJSON = $json->decode($geoJSON);
+		$id = 0;
 		
+		if (gettype($geometryFromGeoJSON) == "object" && $geometryFromGeoJSON->type == "FeatureCollection") {
+			// create Placemarks
+			for ($i = 0; $i < count($geometryFromGeoJSON->features); $i++) {
+				$feature = $geometryFromGeoJSON->features[$i];
+				if (gettype($feature) == "object" && $feature->type == "Feature") {
+					$geometry = $feature->geometry;
+
+					switch ($geometry->type) {
+						case "LineString" :
+							$pointList = implode(",", $geometry->coordinates);
+							$coordinateList = implode(",", $pointList);
+							$currentGeometry = new KMLLine($coordinateList);
+							break;
+						case "Point" :
+							$coordinateList = implode(",", $geometry->coordinates);
+							$currentGeometry = new KMLPoint($coordinateList);
+							break;
+					}
+					if ($currentGeometry) {
+						$currentPlacemark = new KMLPlacemark($currentGeometry);
+
+						foreach ($feature->properties as $key => $value) {
+							$currentPlacemark->setProperty($key, $value);
+						}
+						$currentPlacemark->setProperty("Mapbender:kml", true);
+						$currentPlacemark->setProperty("Mapbender:name", "unknown");
+						$currentPlacemark->setProperty("Mapbender:id", $kmlId);
+						$currentPlacemark->setProperty("Mapbender:placemarkId", $id);
+						array_push($this->placemarkArray, $currentPlacemark);
+						$id ++;
+					}
+				}
+			}
+		}
+		return true;
+	}
+		
+	public function parseKML ($kml, $kmlId) {
 		$doc = new DOMDocument("1.0");
 		$doc->preserveWhiteSpace = false;
 		$doc->loadXML($kml);

Modified: trunk/mapbender/http/classes/class_kml_placemark.php
===================================================================
--- trunk/mapbender/http/classes/class_kml_placemark.php	2008-01-09 15:04:49 UTC (rev 1955)
+++ trunk/mapbender/http/classes/class_kml_placemark.php	2008-01-10 16:55:18 UTC (rev 1956)
@@ -17,7 +17,7 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
-require_once("../classes/class_mb_exception.php");
+require_once(dirname(__FILE__)."/../classes/class_mb_exception.php");
 
 /**
  * A Placemark consists of a geometry of type {@link KMLPoint}, {@link KMLPolygon}, 
@@ -94,6 +94,17 @@
 		}
 		return $str;
 	}
+	
+	/**
+	 * @return string class name of geometry
+	 */
+	public function getGeometryType () {
+		if (KMLGeometry::isGeometry($this->geometry)) {
+			return $geometry->getGeometryType();
+		}
+		$e = new mb_exception("class_kml_placemark.php: getGeometryType: Geometry not set.");
+		return "";
+	}
 
 	private $geometry;
 	private $properties = array();

Modified: trunk/mapbender/http/classes/class_kml_point.php
===================================================================
--- trunk/mapbender/http/classes/class_kml_point.php	2008-01-09 15:04:49 UTC (rev 1955)
+++ trunk/mapbender/http/classes/class_kml_point.php	2008-01-10 16:55:18 UTC (rev 1956)
@@ -19,8 +19,8 @@
  * 				along with this program; if not, write to the Free Software
  * 				Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
-require_once("../classes/class_mb_exception.php");
-require_once("../classes/class_kml_geometry.php");
+require_once(dirname(__FILE__)."/../classes/class_mb_exception.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_geometry.php");
 
 /**
  * Represents a point, consisting of a single point geometry.
@@ -68,8 +68,15 @@
 		$e = new mb_exception("KMLPoint: toGeoJSON: this point is null.");
 		return "";
 	}
-	
+
 	/**
+	 * @return array a point as associative array, coordinates as ["x"] and ["y"]
+	 */
+	public function getPoint () {
+		return $this->point;
+	}
+		
+	/**
 	 * @var	float[] an associative array, with "x", and "y" being float values.
 	 */
 	private $point;

Modified: trunk/mapbender/http/classes/class_kml_polygon.php
===================================================================
--- trunk/mapbender/http/classes/class_kml_polygon.php	2008-01-09 15:04:49 UTC (rev 1955)
+++ trunk/mapbender/http/classes/class_kml_polygon.php	2008-01-10 16:55:18 UTC (rev 1956)
@@ -19,9 +19,9 @@
  * 				Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
-require_once("../classes/class_mb_exception.php");
-require_once("../classes/class_kml_geometry.php");
-require_once("../classes/class_kml_linearring.php");
+require_once(dirname(__FILE__)."/../classes/class_mb_exception.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_geometry.php");
+require_once(dirname(__FILE__)."/../classes/class_kml_linearring.php");
 
 /**
  * Represents a polygon, consisting of 1 outer boundary and 0..n inner boundaries 



More information about the Mapbender_commits mailing list