[Mapbender-commits] r2059 - branches/2.5/http/classes

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Tue Feb 5 09:40:49 EST 2008


Author: verenadiewald
Date: 2008-02-05 09:40:48 -0500 (Tue, 05 Feb 2008)
New Revision: 2059

Modified:
   branches/2.5/http/classes/class_gml2.php
Log:
new class_gml2.php with GeoJSON

Modified: branches/2.5/http/classes/class_gml2.php
===================================================================
--- branches/2.5/http/classes/class_gml2.php	2008-02-05 14:39:45 UTC (rev 2058)
+++ branches/2.5/http/classes/class_gml2.php	2008-02-05 14:40:48 UTC (rev 2059)
@@ -16,10 +16,10 @@
 # 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(dirname(__FILE__) . "/class_mb_exception.php");
-require_once(dirname(__FILE__) . "/class_connector.php");
-require_once(dirname(__FILE__) . "/../../conf/mapbender.conf");
-class gml2{
+require_once("class_mb_exception.php");
+require_once("class_connector.php");
+require_once("../../conf/mapbender.conf");
+class gml2 {
 	var $geomtype_point = 'Point';					
 	var $geomtype_polygon = 'Polygon';
 	var $geomtype_line = 'LineString';
@@ -32,6 +32,7 @@
 	var $values = array();
 	var $geometry = array();
 	var $bbox = array();
+	var $doc;
 	
 	
 	function gml2(){
@@ -41,19 +42,140 @@
 		$x = new connector($req);
 		return $x->file;
 	}
-	function parsegml($req){
+
+	function parseFile($req){
 		#$data = implode("",file($req));
 		$x = new connector($req);
 		$data = $x->file;
-		$this->parse_xml($data);		 		
+		$data = $this->removeWhiteSpace($data);
+		$this->parseXML($data);		 		
 	}
+	
+	function parseXML($data) {
+		
+		$this->doc = $data;
+		
+		return $this->toGeoJSON();
+	}
+
+	function removeWhiteSpace ($string) {
+		return preg_replace("/\>(\s)+\</", "><", trim($string));
+	}
+	
+	function sepNameSpace($s){
+		$c = mb_strpos($s,":"); 
+		if($c>0){
+			return mb_substr($s,$c+1);
+		}
+		else{
+			return $s;
+		}		
+	}
+	
+	function toGeoJSON () {
+		$gmlDoc = new SimpleXMLElement($this->doc);
+		
+		$gmlDoc->registerXPathNamespace('xls', 'http://www.opengis.net/xls');
+		$gmlDoc->registerXPathNamespace('gml', 'http://www.opengis.net/gml');
+		
+		// build feature collection
+		$featureCollection = new FeatureCollection();
+		
+		// segments of the featzreCollection
+		$gmlFeatureMembers = $gmlDoc->xpath("//gml:featureMember");
+		
+		$cnt=0;
+		foreach ($gmlFeatureMembers as $gmlFeatureMember) {
+			$featureMember_dom = dom_import_simplexml($gmlFeatureMember);
+			
+			$feature = new Feature();
+			$feature->parse($featureMember_dom);
+			if (isset($feature->geometry)) {
+				$featureCollection->addFeature($feature);
+			}
+			$cnt++;
+		}
+		
+		return $featureCollection->toGeoJSON();
+	}
+
+	
+	/**
+	 * Exports the file to SHP.
+	 * 
+	 * @param string $filenamePrefix the filename without an ending like .shp
+	 */
+	function toShape ($filenamePrefix) {
+		$unique = TMPDIR . "/" . $filenamePrefix;
+		$fShape = $unique.".shp";
+		$fGml = $unique.".gml";
+//		$pathOgr = '/appserver/postgresql/templates/postgresql824/bin/ogr2ogr';
+		$pathOgr = '/usr/bin/ogr2ogr';
+		$w = $this->toFile($fGml);
+
+ 		$exec = $pathOgr.' -f "ESRI Shapefile" "'.$fShape.'" '.$fGml;
+ 		exec($exec);
+ 		
+ 		$exec = 'zip -j '.$unique.' '.$unique.'.shp '.$unique.'.dbf '.$unique.'.shx '.$unique.'.gfs '.$unique.'.gml ';
+ 		exec($exec);
+
+		$exec = 'rm -f '.$unique.' '.$unique.'.shp '.$unique.'.dbf '.$unique.'.shx '.$unique.'.gfs '.$unique.'.gml';
+		exec($exec);
+		
+		$exec = 'chmod 777 '.$unique.'.*';
+		exec($exec);
+		//echo "<a href='../tmp/".$unique.".zip'>Download ".$prefix."<a>";
+	}
+	
+	/**
+	 * Writes a file containing the GML.
+	 * 
+	 * @param string $path the path to the file.
+	 * @param string $path the path to the file.
+	 * @return bool true if the file could be saved; else false.
+	 */
+	function toFile ($file) {
+		$handle = fopen($file, "w");
+		if (!$handle) {
+			$e = new mb_exception("class_gml2.php: Filehandler error (" . $file . ").");
+			return false;
+		}
+		if (!fwrite($handle,$this->__toString())) {
+			$e = new mb_exception("class_gml2.php: Could not write file (" . $file . ").");
+			fclose($handle);
+			return false;
+		}
+		fclose($handle);
+		return true;
+	}
+	
+	function __toString () {
+		return $this->doc;
+	}
+
+
+
+	/**
+	 * @deprecated
+	 */
+	function parsegml($req){
+		#$data = implode("",file($req));
+		$x = new connector($req);
+		$this->parse_xml($x->file);		 		
+	}
+
+	/**
+	 * @deprecated
+	 */
 	function parse_xml($data){
+
+
+		$this->doc = $data;
+
 		$section = false;
 		$geom = false;
 		$boundedBy = false;
 		$coordinates = false;
-		$values = null;
-		$tags = null;
 		$el = -1;
 		$parser = xml_parser_create(CHARSET);
 		xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
@@ -68,15 +190,24 @@
 		xml_parser_free($parser);
 		
 		foreach ($values as $element) {
-			if(mb_strtoupper($this->sepNameSpace($element[tag])) == mb_strtoupper("boundedBy") && $element[type] == "open"){
+			$e = new mb_exception($element[tag]);
+			if(strtoupper($this->sepNameSpace($element[tag])) == strtoupper("boundedBy") && $element[type] == "open"){
 				$boundedBy = true;
 			}
 			if ($boundedBy) {
-				if(mb_strtoupper($this->sepNameSpace($element[tag])) == mb_strtoupper("coordinates")){
-					$this->bbox = explode(",", preg_replace("/,,/","",preg_replace("/ /",",",trim($element[value]))));
+				if(strtoupper($this->sepNameSpace($element[tag])) == strtoupper("coordinates")){
+					$this->bbox = explode(",", str_replace(",,","",str_replace(" ",",",trim($element[value]))));
 					$boundedBy=false;
 				}
 			}
+			if(strtoupper($this->sepNameSpace($element[tag])) == strtoupper("featureMember") && $element[type] == "open"){
+				$this->member++;
+				$this->keys[$this->member] = array();
+				$this->value[$this->member] = array();
+				$this->geometry[$this->member] = array();
+				$section = true;
+				$cnt_geom = 0;
+			}
 			if($section == true){
 				if( in_array($this->sepNameSpace($element[tag]),$this->geometries) && $element[type] == "open"){
 					$geom = true;
@@ -87,15 +218,15 @@
 					$geom = false;
 				}
 				if($geom == true){
-					if (mb_strtoupper($this->sepNameSpace($element[tag])) == mb_strtoupper("coordinates")) {
-						$this->geometry[$this->member][$cnt_geom] =  preg_replace("/,,/","",preg_replace("/ /",",",trim($element[value])));
+					if (strtoupper($this->sepNameSpace($element[tag])) == strtoupper("coordinates")) {
+						$this->geometry[$this->member][$cnt_geom] =  str_replace(",,","",str_replace(" ",",",trim($element[value])));
 						$coordinates = true;
 						// XXX: Increment counter to get all geometries of a feature member, 
 						// comment it out to only show first geometry of featuremember
 						$cnt_geom++;
 					}
 					else if (!$coordinates && trim($element[value])) {
-						$coords = preg_replace("/,,/","",preg_replace("/ /",",",trim($element[value])));
+						$coords = str_replace(",,","",str_replace(" ",",",trim($element[value])));
 						$tmp_array = explode(",", $coords);
 						if (count($tmp_array > 1)) {
 							$this->geometry[$this->member][$cnt_geom] =  $coords;
@@ -106,7 +237,7 @@
 						}
 					}
 				}
-				else if(mb_strtoupper($this->sepNameSpace($element[tag])) == mb_strtoupper("featureMember") && $element[type] == "close"){
+				else if(strtoupper($this->sepNameSpace($element[tag])) == strtoupper("featureMember") && $element[type] == "close"){
 					$section = false;	
 					$el = -1;
 				}
@@ -116,31 +247,19 @@
 					$this->keys[$this->member][$el] = $element[tag];	
 				}
 			}
-			if(mb_strtoupper($this->sepNameSpace($element[tag])) == mb_strtoupper("featureMember") && $element[type] == "open"){
-				$this->member++;
-				$this->keys[$this->member] = array();
-				$this->value[$this->member] = array();
-				$this->geometry[$this->member] = array();
-				$section = true;
-				$cnt_geom = 0;
-			}
 		}
 	}	
-	
-	function sepNameSpace($s){
-		$c = mb_strpos($s,":"); 
-		if($c>0){
-			return mb_substr($s,$c+1);
-		}
-		else{
-			return $s;
-		}		
-	}
-	
+
+	/**
+	 * @deprecated
+	 */
 	function getMemberCount(){
 		return ($this->member+1);	
 	}
 	
+	/**
+	 * @deprecated
+	 */
 	function getValueBySeparatedKey($memberCount,$keyName){
 		for($i=0; $i<count($this->keys[$memberCount]); $i++){
 			if($this->sepNameSpace($this->keys[$memberCount][$i]) == $keyName){
@@ -149,6 +268,9 @@
 		}
 	}
 	
+	/**
+	 * @deprecated
+	 */
 	function getValueByKey($memberCount,$keyName){
 		for($i=0; $i<count($this->keys[$memberCount]); $i++){
 			if($this->keys[$memberCount][$i] == $keyName){
@@ -157,6 +279,9 @@
 		}
 	}
 	
+	/**
+	 * @deprecated
+	 */
 	function getXfromMemberAsString($memberCount,$geomCount){
 		$t = explode(",",$this->geometry[$memberCount][$geomCount]);
 		$x = array();
@@ -166,6 +291,9 @@
 		return implode(",",$x);
 	}
 	
+	/**
+	 * @deprecated
+	 */
 	function getYfromMemberAsString($memberCount,$geomCount){
 		$t = explode(",",$this->geometry[$memberCount][$geomCount]);
 		$y = array();
@@ -175,13 +303,23 @@
 		return implode(",",$y);
 	}
 	
+	/**
+	 * @deprecated
+	 */
 	function getGeometriesFromMember($memberCount){
 		return $this->geometry[$memberCount];	
 	}
 	
+	/**
+	 * @deprecated
+	 */
 	function getGeometryTypeFromMember($memberCount){
 		return 	$this->geomtype[$memberCount];
 	}
+
+	/**
+	 * @deprecated
+	 */
 	function exportGeometriesToJS ($isInFrame) {
 		$prefix = "";
 		if ($isInFrame == true) {
@@ -196,6 +334,9 @@
 		return $js;
 	}
 	
+	/**
+	 * @deprecated
+	 */
 	function exportMemberToJS ($i, $isInFrame) {
 		$prefix = "";
 		if ($isInFrame == true) {
@@ -230,5 +371,417 @@
 //		$js .= "alert(q);\n";
 		return $js;
 	}
+
 }
+
+
+
+
+class FeatureCollection {
+	var $type = "FeatureCollection";
+	var $featureArray = array();
+	
+	public function __construct() {
+		
+	}
+	
+	public function addFeature ($aFeature) {
+		array_push($this->featureArray, $aFeature);
+	}
+	
+	public function toGeoJSON () {
+		$str = "";
+		$len = count($this->featureArray); 
+		if ($len > 0) {
+			$str .= "{\"type\": \"FeatureCollection\", \"features\": [";
+			for ($i=0; $i < $len; $i++) {
+				if ($i > 0) {
+					$str .= ",";
+				}	
+				$str .= $this->featureArray[$i]->toGeoJSON();
+			}
+			$str .= "]}";
+		}
+		return $str;
+	}
+}
+
+class Feature {
+	var $type = "Feature";
+	var $fid;
+	var $geometry = false;
+	var $properties = array();
+	
+	public function __construct() {
+	}
+	
+	function sepNameSpace($s){
+		list($ns,$FeaturePropertyName) = split(":",$s);
+		$nodeName = array('ns' => $ns, 'value' => $FeaturePropertyName);
+		return $nodeName;
+	}
+	
+	public function parse($domNode) {
+		$currentSibling = $domNode->firstChild;
+		
+		$this->fid = $currentSibling->getAttribute("fid");
+		
+		$currentSibling = $currentSibling->firstChild;
+		
+		while ($currentSibling) {
+		
+			$name = $currentSibling->nodeName;
+			$value = $currentSibling->nodeValue;
+			
+			$namespace = $this->sepNameSpace($name);
+			$ns = $namespace['ns'];
+			$columnName = $namespace['value'];
+			
+$e = new mb_exception($ns.":the_geom");
+			if ($name==$ns.":the_geom"){
+				$e = new mb_exception("in if");
+				$geomNode = $currentSibling->firstChild; 
+					$geomType = $geomNode->nodeName;
+//					echo "geomtype: " . $geomType . "<br>";
+					switch ($geomType) {
+						case "gml:Polygon" :
+							$this->geometry = new GMLPolygon();
+							$this->geometry->parsePolygon($geomNode);
+							break;
+						case "gml:LineString" :
+							$this->geometry = new GMLLine();
+							$this->geometry->parseLine($geomNode);
+							break;
+						case "gml:Point" :
+						$e = new mb_exception("in point");
+							$this->geometry = new GMLPoint();
+							$this->geometry->parsePoint($geomNode);
+#							$this->properties["Mapbender:icon"] = "../img/sy_star.png";
+							break;
+						case "gml:MultiLineString" :
+							$this->geometry = new GMLMultiLine();
+							$this->geometry->parseMultiLine($geomNode);
+//							$this->properties["Mapbender:icon"] = "../img/sy_star.png";
+							break;
+						case "gml:MultiPolygon" :
+							$this->geometry = new GMLMultiPolygon();
+							$this->geometry->parseMultiPolygon($geomNode);
+//							$this->properties["Mapbender:icon"] = "../img/sy_star.png";
+							break;
+					}
+			} else {
+					$this->properties[$columnName] = $value;
+			}
+			
+			$currentSibling = $currentSibling->nextSibling;
+		}
+	}
+	
+	public function toGeoJSON () {
+		$str = "";
+		$str .= "{\"type\":\"Feature\", \"id\":\"".$this->fid."\", \"geometry\": ";
+		if ($this->geometry) {
+			$str .= $this->geometry->toGeoJSON();
+		}
+		else {
+			$str .= "\"\"";
+		}
+
+		$str .= ", \"properties\": {";
+		$cnt = 0;
+		foreach ($this->properties as $key => $value) {
+			if ($cnt > 0) {
+				$str .= ",";
+			}
+			$str .= "\"" . $key . "\":\"" . $value . "\"";
+			$cnt ++;
+		}
+		$str .= "}";
+
+		$str .= "}";
+		return $str;
+	}
+}
+
+class GMLLine {
+
+	var $pointArray = array();
+
+	public function __construct() {
+		
+	}
+
+	public function parseLine ($domNode) {
+		$currentSibling = $domNode->firstChild;
+		while ($currentSibling) {
+			
+			foreach(explode(' ',$currentSibling->nodeValue) as $cords){
+				list($x,$y,$z) = explode(',',$cords);
+				$this->addPoint($x, $y);
+			}
+			$currentSibling = $currentSibling->nextSibling;
+		}
+	}
+	
+	private function addPoint ($x, $y) {
+		array_push($this->pointArray, array("x" => $x, "y" => $y));
+	}
+	
+	public function toGeoJSON () {
+		$numberOfPoints = count($this->pointArray);
+		$str = "";
+		if ($numberOfPoints > 0) {
+			$str .= "{\"type\": \"LineString\", \"coordinates\":[";
+			for ($i=0; $i < $numberOfPoints; $i++) {
+				if ($i > 0) {
+					$str .= ",";
+				}
+				$str .= "[".$this->pointArray[$i]["x"].",".$this->pointArray[$i]["y"]."]";
+			}
+			$str .= "]}";
+		}
+		else {
+			$e = new mb_exception("GMLLine: toGeoJSON: this point is null.");
+		}
+		return $str;
+	}
+}
+
+class GMLPoint {
+
+	var $point;
+
+	public function __construct() {
+		
+	}
+
+	public function parsePoint ($domNode) {
+		$currentSibling = $domNode->firstChild;
+		while ($currentSibling) {
+			list($x, $y, $z) = explode(",", $currentSibling->nodeValue);
+			$this->setPoint($x, $y);
+			$currentSibling = $currentSibling->nextSibling;
+		}
+	}
+	
+	private function setPoint ($x, $y) {
+#		echo "x: " . $x . " y: " . $y . "\n";
+		$this->point = array("x" => $x, "y" => $y);
+	}
+	
+	public function toGeoJSON () {
+		$str = "";
+		if ($this->point) {
+			$str .= "{\"type\": \"Point\", \"coordinates\":";
+			$str .= "[".$this->point["x"].",".$this->point["y"]."]";
+			$str .= "}";
+		}
+		else {
+			$e = new mb_exception("GMLPoint: toGeoJSON: this point is null.");
+		}
+		return $str;
+	}
+}
+
+class GMLPolygon {
+
+	var $pointArray = array();
+
+	public function __construct() {
+		
+	}
+
+	public function parsePolygon ($domNode) {
+		$simpleXMLNode = simplexml_import_dom($domNode);
+
+		$simpleXMLNode->registerXPathNamespace('gml', 'http://www.opengis.net/gml');
+		
+		$allCoords = $simpleXMLNode->xpath("gml:outerBoundaryIs/gml:LinearRing/gml:coordinates");
+			
+		$cnt=0;
+		foreach ($allCoords as $Coords) {
+			$coordsDom = dom_import_simplexml($Coords);
+				
+//			$name = $coordsDom->nodeName;
+//			$value = $coordsDom->nodeValue;				
+//			echo "===> name: ".$name. ", Value: ".$value."<br>";
+			
+			foreach(explode(' ',$coordsDom->nodeValue) as $pointCoords){
+
+				list($x,$y,$z) = explode(',',$pointCoords);
+				$this->addPoint($x, $y);
+				}
+			
+			$cnt++;
+		}
+		
+		
+	}
+	
+	private function addPoint ($x, $y) {
+		array_push($this->pointArray, array("x" => $x, "y" => $y));
+	}
+	
+	public function toGeoJSON () {
+		$numberOfPoints = count($this->pointArray);
+		$str = "";
+		if ($numberOfPoints > 0) {
+			$str .= "{\"type\": \"Polygon\", \"coordinates\":[[";
+			for ($i=0; $i < $numberOfPoints; $i++) {
+				if ($i > 0) {
+					$str .= ",";
+				}
+				$str .= "[".$this->pointArray[$i]["x"].",".$this->pointArray[$i]["y"]."]";
+			}
+			$str .= "]]}";
+		}
+		else {
+			$e = new mb_exception("GMLPolygon: toGeoJSON: this point is null.");
+		}
+		return $str;
+	}
+}
+
+class GMLMultiLine {
+
+	var $lineArray = array();
+
+	public function __construct() {
+		
+	}
+
+	public function parseMultiLine ($domNode) {
+		$simpleXMLNode = simplexml_import_dom($domNode);
+
+		$simpleXMLNode->registerXPathNamespace('gml', 'http://www.opengis.net/gml');
+		
+		$allCoords = $simpleXMLNode->xpath("gml:lineStringMember/gml:LineString/gml:coordinates");
+			
+		$cnt=0;
+		foreach ($allCoords as $Coords) {
+			
+			$this->lineArray[$cnt] = array();
+			
+			$coordsDom = dom_import_simplexml($Coords);
+				
+//			$name = $coordsDom->nodeName;
+//			$value = $coordsDom->nodeValue;				
+//			echo "===> name: ".$name. ", Value: ".$value."<br>";
+			
+			foreach(explode(' ',$coordsDom->nodeValue) as $pointCoords){
+				list($x,$y,$z) = explode(',',$pointCoords);
+				$this->addPoint($x, $y, $cnt);
+				}
+			
+			$cnt++;
+		}
+		
+	}
+	
+	private function addPoint ($x, $y, $i) {
+
+		array_push($this->lineArray[$i], array("x" => $x, "y" => $y));
+	}
+	
+	public function toGeoJSON () {
+		$numberlineArray = count($this->lineArray);
+		$str = "";
+		if ($numberlineArray > 0) {
+			$str .= "{\"type\": \"MultiLineString\", \"coordinates\":[";
+			
+			for ($cnt =0; $cnt < $numberlineArray; $cnt++){
+				if ($cnt > 0) {
+						$str .= ",";
+					}
+					$str .="[";
+			
+				for ($i=0; $i < count($this->lineArray[$cnt]); $i++) {
+					if ($i > 0) {
+						$str .= ",";
+					}
+					$str .= "[".$this->lineArray[$cnt][$i]["x"].",".$this->lineArray[$cnt][$i]["y"]."]";
+				}
+				$str .="]";
+			}
+			$str .= "]}";
+			
+		}
+		else {
+			$e = new mb_exception("GMLMultiLine: toGeoJSON: this multiLine is null.");
+		}
+		return $str;
+	}
+}
+
+class GMLMultiPolygon {
+
+	var $polygonArray = array();
+
+	public function __construct() {
+		
+	}
+
+	public function parseMultiPolygon ($domNode) {
+//		echo $domNode->nodeName."<br>";
+		$simpleXMLNode = simplexml_import_dom($domNode);
+
+		$simpleXMLNode->registerXPathNamespace('gml', 'http://www.opengis.net/gml');
+
+		$allCoords = $simpleXMLNode->xpath("gml:polygonMember/gml:Polygon/gml:outerBoundaryIs/gml:LinearRing/gml:coordinates");
+			
+		$cnt=0;
+		foreach ($allCoords as $Coords) {
+			
+			$this->polygonArray[$cnt] = array();
+			
+			$coordsDom = dom_import_simplexml($Coords);
+				
+//			$name = $coordsDom->nodeName;
+//			$value = $coordsDom->nodeValue;				
+//			echo "===> name: ".$name. ", Value: ".$value."<br>";
+
+			foreach(explode(' ',$coordsDom->nodeValue) as $pointCoords){
+				list($x,$y,$z) = explode(',',$pointCoords);
+				$this->addPoint($x, $y, $cnt);
+				}
+			
+			$cnt++;
+		}
+		
+	}
+	
+	private function addPoint ($x, $y, $i) {
+
+		array_push($this->polygonArray[$i], array("x" => $x, "y" => $y));
+	}
+	
+	public function toGeoJSON () {
+		$numberPolygonArray = count($this->polygonArray);
+		$str = "";
+		if ($numberPolygonArray > 0) {
+			$str .= "{\"type\": \"MultiPolygon\", \"coordinates\":[";
+			
+			for ($cnt =0; $cnt < $numberPolygonArray; $cnt++){
+				if ($cnt > 0) {
+						$str .= ",";
+					}
+					$str .="[";
+			
+				for ($i=0; $i < count($this->polygonArray[$cnt]); $i++) {
+					if ($i > 0) {
+						$str .= ",";
+					}
+					$str .= "[".$this->polygonArray[$cnt][$i]["x"].",".$this->polygonArray[$cnt][$i]["y"]."]";
+				}
+				$str .="]";
+			}
+			$str .= "]}";
+			
+		}
+		else {
+			$e = new mb_exception("GMLMultiPolygon: toGeoJSON: this multiLine is null.");
+		}
+		return $str;
+	}
+}
 ?>
\ No newline at end of file



More information about the Mapbender_commits mailing list