[Mapbender-commits] r9188 - in trunk/mapbender: http/classes http/php http/plugins lib
svn_mapbender at osgeo.org
svn_mapbender at osgeo.org
Mon May 4 06:48:27 PDT 2015
Author: syed
Date: 2015-05-04 06:48:27 -0700 (Mon, 04 May 2015)
New Revision: 9188
Modified:
trunk/mapbender/http/classes/class_kml_ows.php
trunk/mapbender/http/classes/class_kml_parser_ows.php
trunk/mapbender/http/classes/class_wmc.php
trunk/mapbender/http/php/mb_delete_local_data.php
trunk/mapbender/http/php/mb_load_local_data.php
trunk/mapbender/http/php/mb_publish_wmc.php
trunk/mapbender/http/php/mb_unpublish_wmc.php
trunk/mapbender/http/php/mod_GetPublishedData.php
trunk/mapbender/http/php/uploadKml.php
trunk/mapbender/http/plugins/kmlTree.js
trunk/mapbender/http/plugins/mb_digitize_widget.php
trunk/mapbender/lib/mb.ui.displayKmlFeatures.js
Log:
all changes which were made to process the current tickets
Modified: trunk/mapbender/http/classes/class_kml_ows.php
===================================================================
--- trunk/mapbender/http/classes/class_kml_ows.php 2015-05-04 09:48:47 UTC (rev 9187)
+++ trunk/mapbender/http/classes/class_kml_ows.php 2015-05-04 13:48:27 UTC (rev 9188)
@@ -1,649 +1,649 @@
-<?php
-# $Id$
-# http://www.mapbender.org/index.php/class_wmc.php
-# Copyright (C) 2002 CCGIS
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2, or (at your option)
-# any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# 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__)."/../../core/globalSettings.php");
-
-require_once(dirname(__FILE__)."/../classes/class_json.php");
-require_once(dirname(__FILE__)."/../classes/class_point.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.
- *
- * @package KML
- */
-class KML {
-
- //
- //
- // ------------------------------- public -----------------------------------------------
- //
- //
-
- /**
- * The constructor function, currently empty.
- */
- public function __construct() {
- }
-
- public function toSingleLineStringKml() {
- //KML 2.2 output
- $doc = new DOMDocument("1.0", CHARSET);
- $doc->preserveWhiteSpace = false;
-
- // attach kml and Document tag
- $e_kml = $doc->createElementNS("http://earth.google.com/kml/2.2", "kml");
- $e_document = $doc->createElement("Document");
- $e_kml->appendChild($e_document);
- $doc->appendChild($e_kml);
-
- // attach placemarks
- $e = new mb_notice("to string: #placemarks: " . count($this->placemarkArray));
-
- $lineStyleNode = $doc->createElement("Style");
- $lineStyleNode->setAttribute("id", "linestyleExample");
- $lineStyleColorNode = $doc->createElement("color", "7f0000ff");
- $lineStyleWidthNode = $doc->createElement("width", 4);
- $lineStyleNode->appendChild($lineStyleColorNode);
- $lineStyleNode->appendChild($lineStyleWidthNode);
- $e_document->appendChild($lineStyleNode);
-
- //
- // line segments first
- //
- $coordinates = "";
-
- for ($i = 0; $i < count($this->placemarkArray); $i++) {
- $currentPlacemark = $this->placemarkArray[$i];
- $e = new mb_notice("now: " . $i . " of " . (count($this->placemarkArray)-1) . " (is a " . get_class($currentPlacemark) . ")");
-
- switch ($currentPlacemark->getGeometryType()) {
- case "KMLLine" :
- $coordinatesArray = $currentPlacemark->getGeometry()->getPointArray();
- for ($j = 0; $j < count($coordinatesArray); $j++) {
- if (!($j == 0 && $i == 0)) {
- $coordinates .= " ";
- }
- $coordinates .= $coordinatesArray[$j]["x"] . "," . $coordinatesArray[$j]["y"] . "," . $coordinatesArray[$j]["z"];
-
- }
- break;
- }
- }
- // create a placemark tag with a geometry and add it to the document
- $e_coordinates = $doc->createElement("coordinates", $coordinates);
- $e_geometry = $doc->createElement("LineString");
- $e_geometry->appendChild($e_coordinates);
- $e_placemark = $doc->createElement("Placemark");
- $e_placemark->appendChild($e_geometry);
- $e_pl_name = $doc->createElement("name", "Route");
- $e_placemark->appendChild($e_pl_name);
- $e_pl_style = $doc->createElement("styleUrl", "#linestyleExample");
- $e_placemark->appendChild($e_pl_style);
- $e_document->appendChild($e_placemark);
-
-/*
- //
- // now pois
- //
- // attach placemarks
- $e = new mb_notice("to string: #placemarks: " . count($this->placemarkArray));
- for ($i = 0; $i < count($this->placemarkArray); $i++) {
- $currentPlacemark = $this->placemarkArray[$i];
-
- $e = new mb_notice("now: " . $i . " of " . (count($this->placemarkArray)-1) . " (is a " . get_class($currentPlacemark) . ")");
-
- $pl_instructions = $currentPlacemark->getProperty("instruction");
- $pl_name_array = array();
- $pl_name = false;
- $pl_description = false;
- if ($pl_instructions != null) {
- $pl_name_array = explode("|", $pl_instructions);
- }
-
- switch ($currentPlacemark->getGeometryType()) {
- case "KMLPoint" :
- if (count($pl_name_array) > 2) {
- $pl_name = $pl_name_array[0];
- $pl_description = $pl_name_array[1];
- }
- $e_geometry = $doc->createElement("Point");
- $point = $currentPlacemark->getGeometry()->getPoint();
- $coordinates = $point["x"] . "," . $point["y"];
- $e_coordinates = $doc->createElement("coordinates", $coordinates);
- $e_geometry->appendChild($e_coordinates);
- 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);
- if ($pl_name) {
- $e_pl_name = $doc->createElement("name", $pl_name);
- $e_placemark->appendChild($e_pl_name);
- }
- if ($pl_description) {
- $e_pl_description = $doc->createElement("description", $pl_description);
- $e_placemark->appendChild($e_pl_description);
- }
- $e_document->appendChild($e_placemark);
- }
- }
-*/
- return $doc->saveXML();
- }
-
- /**
- * @return string the KML document.
- */
- public function __toString() {
-
-
- if (!$this->kml) {
- //KML 2.2 output
- $doc = new DOMDocument("1.0", CHARSET);
- $doc->preserveWhiteSpace = false;
-
- // attach kml and Document tag
- $e_kml = $doc->createElementNS("http://earth.google.com/kml/2.2", "kml");
- $e_document = $doc->createElement("Document");
- $e_kml->appendChild($e_document);
- $doc->appendChild($e_kml);
-
- // attach placemarks
- $e = new mb_notice("to string: #placemarks: " . count($this->placemarkArray));
- for ($i = 0; $i < count($this->placemarkArray); $i++) {
- $currentPlacemark = $this->placemarkArray[$i];
-
- $e = new mb_notice("now: " . $i . " of " . (count($this->placemarkArray)-1) . " (is a " . get_class($currentPlacemark) . ")");
-
- $pl_instructions = $currentPlacemark->getProperty("instruction");
- $pl_name_array = array();
- $pl_name = false;
- $pl_description = false;
- if ($pl_instructions != null) {
- $pl_name_array = explode("|", $pl_instructions);
- }
-
- switch ($currentPlacemark->getGeometryType()) {
- case "KMLPoint" :
- if (count($pl_name_array) > 2) {
- $pl_name = $pl_name_array[0];
- $pl_description = $pl_name_array[1];
- }
- $e_geometry = $doc->createElement("Point");
- $point = $currentPlacemark->getGeometry()->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 "KMLLine" :
- if (count($pl_name_array) > 2) {
- $pl_description = $pl_name_array[1];
- }
- $e_geometry = $doc->createElement("LineString");
- $coordinatesArray = $currentPlacemark->getGeometry()->getPointArray();
- $coordinates = "";
- for ($j = 0; $j < count($coordinatesArray); $j++) {
- if ($j > 0) {
- $coordinates .= " ";
- }
- $coordinates .= $coordinatesArray[$j]["x"] . "," . $coordinatesArray[$j]["y"] . "," . $coordinatesArray[$j]["z"];
-
- }
- $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);
- if ($pl_name) {
- $e_pl_name = $doc->createElement("name", $pl_name);
- $e_placemark->appendChild($e_pl_name);
- }
- if ($pl_description) {
- $e_pl_description = $doc->createElement("description", $pl_description);
- $e_placemark->appendChild($e_pl_description);
- }
- $e_document->appendChild($e_placemark);
- }
- }
- $this->kml = $doc->saveXML();
- }
- return $this->kml;
- }
-
- /**
- * @return string the ID of this KML.
- */
- public function getId () {
- return $this->id;
- }
-
- /**
- * parses an incoming KML, creates the object,
- * stores the kml in the object and in the database.
- *
- * @param string a KML document.
- * @return boolean true if the parsing succeded, else false.
- */
- public function parseKml ($kml) {
- $this->kml = $kml;
-
- if (!$this->storeInDb()) {
- return false;
- }
-
- $parser = new KmlOwsParser();
- $parser->parseKML($kml, $this->id);
- $this->placemarkArray = $parser->placemarkArray;
- $this->featureCollectionMD = $parser->featureCollectionMD;
- return true;
- }
-
- public function transform($targetEpsg){
- $numberOfPlacemarks = count($this->placemarkArray);
- if ($numberOfPlacemarks > 0) {
- for ($i=0; $i < $numberOfPlacemarks; $i++) {
- $this->placemarkArray[$i]->transform($targetEpsg);
- }
- }
- else {
- $e = new mb_exception("KML: toGeoJSON: this placemarkArray is empty!");
- }
- return true;
- }
- /**
- * parses an incoming GeoJSON, creates the object,
- * stores the kml in the object and in the database.
- *
- * @param string a geoJSON.
- * @return boolean true if the parsing succeded, else false.
- */
- public function parseGeoJSON ($geoJSON) {
- $this->kml = "";
-
- if (!$this->storeInDb()) {
- return false;
- }
-
- $parser = new KmlOwsParser();
- $parser->parseGeoJSON($geoJSON, $this->id);
- $e = new mb_notice("parsing finished...#placemarks: " . count($this->placemarkArray) . " (" . count($parser->placemarkArray) . ")");
- $this->placemarkArray = $parser->placemarkArray;
-
- return true;
- }
-
- /**
- * @return string the geoJSON representation of the KML.
- */
- public function toGeoJSON($str) {
- $numberOfPlacemarks = count($this->placemarkArray);
-
- if ($numberOfPlacemarks > 0) {
- $str .= "\"features\": [";
- for ($i=0; $i < $numberOfPlacemarks; $i++) {
- if ($i > 0) {
- $str .= ",";
- }
- $str .= $this->placemarkArray[$i]->toGeoJSON();
- }
- $str .= "]}";
- }
- else {
- $e = new mb_exception("KML: toGeoJSON: this placemarkArray is empty!");
- }
- return $str;
- }
-
- public function createGeoJSON(){
- $str = "{";
- $str.= "\"type\": \"FeatureCollection\",";
-
- if (sizeof($this->featureCollectionMD) > 0) {
-
- foreach ($this->featureCollectionMD as $key => $value) {
-
- $str.= "\"".$key."\": \"".$value."\",";
- }
-
- }
-
- $completeString = $this->toGeoJSON($str);
- return $completeString;
- }
- 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);
-
- if ($kmlFromDb !== NULL) {
- // load the KML from the database in the DOM object
- $kmlDoc_DOM = new DOMDocument("1.0");
- $kmlDoc_DOM->encoding = CHARSET;
- $kmlDoc_DOM->preserveWhiteSpace = false;
- $kmlDoc_DOM->loadXML($kmlFromDb);
-
- //load the geoJSON
- $json = new Mapbender_JSON();
- $geoObj = $json->decode($geoJSON);
-
- // construct an array that holds all metadata of the placemark
- $metadataObj = $geoObj->properties;
-
- // construct an array that holds all geometries of the placemark
- $geometryObj = $geoObj->geometry;
- $geometryType = $geometryObj->type;
- if ($geometryType == "GeometryCollection") {
- $geometryArray = $geometryObj->geometries;
- }
- else if ($geometryType == "Point" || $geometryType == "LineString" || $geometryType == "Polygon") {
- $geometryArray = array($geometryObj);
- }
- else {
- $e = new mb_exception("class_kml: Invalid geometry type " . $geometryType);
- return false;
- }
-
- //
- // apply the changes
- //
-
- $currentPlacemarkArray = $kmlDoc_DOM->getElementsByTagName("Placemark");
- $currentPlacemark = $currentPlacemarkArray->item($placemarkId);
-
- if ($currentPlacemark) {
- $metadataUpdateSuccessful = $this->updateMetadata($currentPlacemark, $metadataObj);
- $geometryUpdateSuccessful = $this->updateGeometries($currentPlacemark, $geometryArray);
- }
- else {
- $e = new mb_exception("class_kml.php: updateKml: placemark " . $placemarkId . " not found in KML " . $kmlId . ".");
- return false;
- }
-
- if ($metadataUpdateSuccessful && $geometryUpdateSuccessful) {
- $updatedKml = $kmlDoc_DOM->saveXML();
-
- $this->updateInDb($updatedKml, $kmlId);
- }
- else {
- if (!$metadataUpdateSuccessful) {
- $e = new mb_exception("class_kml: Updating the metadata failed, no database update.");
- }
- if (!$geometryUpdateSuccessful) {
- $e = new mb_exception("class_kml: Updating the geometries failed, no database update.");
- }
- return false;
- }
- }
- else {
- $e = new mb_exception("class_kml: No KML found in database, no database update. " . db_error());
- return false;
- }
- return true;
- }
-
- //
- //
- // ------------------------------- private -----------------------------------------------
- //
- //
-
- /**
- * Store this KML in the database, and sets the ID.
- *
- * @return boolean true, if the KML could be stored in the database; else false.
- */
- private function storeInDb () {
- if (Mapbender::session()->get("mb_user_id") && Mapbender::session()->get("mb_user_gui")) {
- $con = db_connect(DBSERVER,OWNER,PW);
- db_select_db(DB,$con);
-
- $sql = "INSERT INTO gui_kml ";
- $sql .= "(fkey_mb_user_id, fkey_gui_id, kml_doc, kml_name, kml_description, kml_timestamp) ";
- $sql .= "VALUES ";
- $sql .= "($1, $2, $3, $4, $5, $6)";
- $v = array (Mapbender::session()->get("mb_user_id"), Mapbender::session()->get("mb_user_gui"), $this->kml, "name", "description", time());
- $t = array ("i", "s", "s", "s", "s", "s");
- $res = db_prep_query($sql, $v, $t);
- if (!$res) {
- $e = new mb_exception("class_kml.php: storeInDb: failed to store KML in database: " . db_error());
- return false;
- }
-
- $this->id = db_insert_id($con, "gui_kml", "kml_id");
- return true;
- }
- else {
- // should be false, but code in caller has to be changed first.
- return true;
- }
- }
-
- /**
- * @param integer the ID of the KML.
- * @return string the KML document with the given ID.
- */
- public function getKmlDocumentFromDB ($kmlId) {
- $con = db_connect(DBSERVER,OWNER,PW);
- db_select_db(DB,$con);
- //get KML from database (check if user is allowed to access)
-
-# for now, do not restrict access
-# $sql = "SELECT kml_doc FROM gui_kml WHERE kml_id = $1 AND fkey_mb_user_id = $2 AND fkey_gui_id = $3 LIMIT 1";
-# $v = array($kmlId, Mapbender::session()->get("mb_user_id"), Mapbender::session()->get("mb_user_gui"));
-# $t = array("i", "i", "s");
-
- $sql = "SELECT kml_doc FROM gui_kml WHERE kml_id = $1 LIMIT 1";
- $v = array($kmlId);
- $t = array("i");
-
- $result = db_prep_query($sql, $v, $t);
- $row = db_fetch_array($result);
- if ($row) {
- return $row["kml_doc"];
- }
- else {
- $e = new mb_exception("class_kml.php: getKMLDocumentFromDB: no KML found for ID " . $kmlId);
- }
- return "";
- }
-
- /**
- * @param string the tag name.
- * @return string the tag name without its namespace.
- */
- private function sepNameSpace($s){
- $c = mb_strpos($s, ":");
- if ($c > 0) {
- $s = mb_substr($s, $c+1);
- }
- return $s;
- }
-
- private function updateGeometries($currentPlacemark, $geometryArray) {
- $cnt = 0;
- $childNodes = $currentPlacemark->childNodes;
-
- foreach ($childNodes as $childNode) {
- $name = $childNode->nodeName;
- if ( in_array($name, array("Point","LineString","Polygon"))) {
- $returnValue = $this->updateGeometry($childNode, $geometryArray[$cnt]);
- if (!$returnValue) {
- return false;
- }
- $cnt ++;
- }
- else if ($name == "MultiGeometry") {
- return $this->updateGeometries($childNode, $geometryArray);
- }
- }
- return true;
- }
-
- private function updateGeometry ($currentNode, $geometry) {
- $json = new Mapbender_JSON();
- $currentNode_SimpleXML = simplexml_import_dom($currentNode);
-
- $currentTypeXml = mb_strtoupper($currentNode->nodeName);
- $currentTypeGeoJson = mb_strtoupper($geometry->type);
-
- if ($currentTypeGeoJson != $currentTypeXml) {
- $e = new mb_exception("class_kml: geometry type mismatch: geoJSON: " . $currentTypeGeoJson . "; XML: " . $currentTypeXml);
- return false;
- }
- if ($currentTypeXml == "POLYGON") {
- // GML 3
- $gmlNode = $currentNode_SimpleXML->{"exterior"}->{"LinearRing"}->{"posList"};
- $kmlNode = $currentNode_SimpleXML->{"outerBoundaryIs"}->{"LinearRing"}->{"coordinates"};
- if ($gmlNode && $gmlNode->asXML()) {
- $currentNode_SimpleXML->{"exterior"}->{"LinearRing"}->{"posList"} = preg_replace("/,/", " ", preg_replace("/\[|\]/", "", $json->encode($geometry->coordinates)));
- }
- // KML 2.2
- else if ($kmlNode && $kmlNode->asXML()) {
- $currentNode_SimpleXML->{"outerBoundaryIs"}->{"LinearRing"}->{"coordinates"} = preg_replace("/\],/", " ", preg_replace("/\][^,]|\[/", "", $json->encode($geometry->coordinates)));
- }
- }
- elseif ($currentTypeXml == "POINT") {
- $gmlNode = $currentNode_SimpleXML->{"pos"};
- $kmlNode = $currentNode_SimpleXML->{"coordinates"};
-
- // GML 3
- if ($gmlNode && $gmlNode->asXML()) {
- $currentNode_SimpleXML->{"pos"} = preg_replace("/,/", " ", preg_replace("/\[|\]/", "", $json->encode($geometry->coordinates)));
- }
- // KML 2.2
- else if ($kmlNode && $kmlNode->asXML()) {
- $currentNode_SimpleXML->{"coordinates"} = preg_replace("/\[|\]/", "", $json->encode($geometry->coordinates));
- }
- }
- elseif ($currentTypeXml == "LINESTRING") {
- $gmlNode = $currentNode_SimpleXML->{"posList"};
- $kmlNode = $currentNode_SimpleXML->{"coordinates"};
-
- // GML 3
- if ($gmlNode && $gmlNode->asXML()) {
- $currentNode_SimpleXML->{"posList"} = preg_replace("/,/", " ", preg_replace("/\[|\]/", "", $json->encode($geometry->coordinates)));
- }
- // KML 2.2
- else if ($kmlNode && $kmlNode->asXML()) {
- $currentNode_SimpleXML->{"coordinates"} = preg_replace("/\[|\]/", "", $json->encode($geometry->coordinates));
- }
- }
- return true;
- }
-
- 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) {
- foreach ($simpleDataNodes as $simpleDataNode) {
- $tmp = dom_import_simplexml($simpleDataNode);
- $name = $tmp->getAttribute("name");
- // if there is a metadata entry, update it
- if (isset($metadataObj->$name)) {
- $tmp->nodeValue = $metadataObj->$name;
- }
- }
-
- $metadataExistsAndUpdateSucceeded = true;
- }
-
- // ...or within a DATA tag
- $dataNodes = $extendedDataNode->{"Data"};
- if ($dataNodes && !$metadataExistsAndUpdateSucceeded) {
- foreach ($dataNodes as $dataNode) {
- $tmp = dom_import_simplexml($dataNode);
- $name = $tmp->getAttribute("name");
- // if there is a metadata entry, update it
- if (isset($metadataObj->$name)) {
- $tmp->nodeValue = $metadataObj->$name;
- }
- }
- $metadataExistsAndUpdateSucceeded = true;
- }
- }
- return $metadataExistsAndUpdateSucceeded;
- }
-
- /**
- * The KML document.
- */
- private $kml;
-
- /**
- * The ID of this KML in the database.
- */
- private $id;
-
- /**
- * An array of {@link KMLPlacemark}
- */
- private $placemarkArray = array();
-}
-?>
+<?php
+# $Id$
+# http://www.mapbender.org/index.php/class_wmc.php
+# Copyright (C) 2002 CCGIS
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# 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__)."/../../core/globalSettings.php");
+
+require_once(dirname(__FILE__)."/../classes/class_json.php");
+require_once(dirname(__FILE__)."/../classes/class_point.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.
+ *
+ * @package KML
+ */
+class KML {
+
+ //
+ //
+ // ------------------------------- public -----------------------------------------------
+ //
+ //
+
+ /**
+ * The constructor function, currently empty.
+ */
+ public function __construct() {
+ }
+
+ public function toSingleLineStringKml() {
+ //KML 2.2 output
+ $doc = new DOMDocument("1.0", CHARSET);
+ $doc->preserveWhiteSpace = false;
+
+ // attach kml and Document tag
+ $e_kml = $doc->createElementNS("http://earth.google.com/kml/2.2", "kml");
+ $e_document = $doc->createElement("Document");
+ $e_kml->appendChild($e_document);
+ $doc->appendChild($e_kml);
+
+ // attach placemarks
+ $e = new mb_notice("to string: #placemarks: " . count($this->placemarkArray));
+
+ $lineStyleNode = $doc->createElement("Style");
+ $lineStyleNode->setAttribute("id", "linestyleExample");
+ $lineStyleColorNode = $doc->createElement("color", "7f0000ff");
+ $lineStyleWidthNode = $doc->createElement("width", 4);
+ $lineStyleNode->appendChild($lineStyleColorNode);
+ $lineStyleNode->appendChild($lineStyleWidthNode);
+ $e_document->appendChild($lineStyleNode);
+
+ //
+ // line segments first
+ //
+ $coordinates = "";
+
+ for ($i = 0; $i < count($this->placemarkArray); $i++) {
+ $currentPlacemark = $this->placemarkArray[$i];
+ $e = new mb_notice("now: " . $i . " of " . (count($this->placemarkArray)-1) . " (is a " . get_class($currentPlacemark) . ")");
+
+ switch ($currentPlacemark->getGeometryType()) {
+ case "KMLLine" :
+ $coordinatesArray = $currentPlacemark->getGeometry()->getPointArray();
+ for ($j = 0; $j < count($coordinatesArray); $j++) {
+ if (!($j == 0 && $i == 0)) {
+ $coordinates .= " ";
+ }
+ $coordinates .= $coordinatesArray[$j]["x"] . "," . $coordinatesArray[$j]["y"] . "," . $coordinatesArray[$j]["z"];
+
+ }
+ break;
+ }
+ }
+ // create a placemark tag with a geometry and add it to the document
+ $e_coordinates = $doc->createElement("coordinates", $coordinates);
+ $e_geometry = $doc->createElement("LineString");
+ $e_geometry->appendChild($e_coordinates);
+ $e_placemark = $doc->createElement("Placemark");
+ $e_placemark->appendChild($e_geometry);
+ $e_pl_name = $doc->createElement("name", "Route");
+ $e_placemark->appendChild($e_pl_name);
+ $e_pl_style = $doc->createElement("styleUrl", "#linestyleExample");
+ $e_placemark->appendChild($e_pl_style);
+ $e_document->appendChild($e_placemark);
+
+/*
+ //
+ // now pois
+ //
+ // attach placemarks
+ $e = new mb_notice("to string: #placemarks: " . count($this->placemarkArray));
+ for ($i = 0; $i < count($this->placemarkArray); $i++) {
+ $currentPlacemark = $this->placemarkArray[$i];
+
+ $e = new mb_notice("now: " . $i . " of " . (count($this->placemarkArray)-1) . " (is a " . get_class($currentPlacemark) . ")");
+
+ $pl_instructions = $currentPlacemark->getProperty("instruction");
+ $pl_name_array = array();
+ $pl_name = false;
+ $pl_description = false;
+ if ($pl_instructions != null) {
+ $pl_name_array = explode("|", $pl_instructions);
+ }
+
+ switch ($currentPlacemark->getGeometryType()) {
+ case "KMLPoint" :
+ if (count($pl_name_array) > 2) {
+ $pl_name = $pl_name_array[0];
+ $pl_description = $pl_name_array[1];
+ }
+ $e_geometry = $doc->createElement("Point");
+ $point = $currentPlacemark->getGeometry()->getPoint();
+ $coordinates = $point["x"] . "," . $point["y"];
+ $e_coordinates = $doc->createElement("coordinates", $coordinates);
+ $e_geometry->appendChild($e_coordinates);
+ 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);
+ if ($pl_name) {
+ $e_pl_name = $doc->createElement("name", $pl_name);
+ $e_placemark->appendChild($e_pl_name);
+ }
+ if ($pl_description) {
+ $e_pl_description = $doc->createElement("description", $pl_description);
+ $e_placemark->appendChild($e_pl_description);
+ }
+ $e_document->appendChild($e_placemark);
+ }
+ }
+*/
+ return $doc->saveXML();
+ }
+
+ /**
+ * @return string the KML document.
+ */
+ public function __toString() {
+
+
+ if (!$this->kml) {
+ //KML 2.2 output
+ $doc = new DOMDocument("1.0", CHARSET);
+ $doc->preserveWhiteSpace = false;
+
+ // attach kml and Document tag
+ $e_kml = $doc->createElementNS("http://earth.google.com/kml/2.2", "kml");
+ $e_document = $doc->createElement("Document");
+ $e_kml->appendChild($e_document);
+ $doc->appendChild($e_kml);
+
+ // attach placemarks
+ $e = new mb_notice("to string: #placemarks: " . count($this->placemarkArray));
+ for ($i = 0; $i < count($this->placemarkArray); $i++) {
+ $currentPlacemark = $this->placemarkArray[$i];
+
+ $e = new mb_notice("now: " . $i . " of " . (count($this->placemarkArray)-1) . " (is a " . get_class($currentPlacemark) . ")");
+
+ $pl_instructions = $currentPlacemark->getProperty("instruction");
+ $pl_name_array = array();
+ $pl_name = false;
+ $pl_description = false;
+ if ($pl_instructions != null) {
+ $pl_name_array = explode("|", $pl_instructions);
+ }
+
+ switch ($currentPlacemark->getGeometryType()) {
+ case "KMLPoint" :
+ if (count($pl_name_array) > 2) {
+ $pl_name = $pl_name_array[0];
+ $pl_description = $pl_name_array[1];
+ }
+ $e_geometry = $doc->createElement("Point");
+ $point = $currentPlacemark->getGeometry()->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 "KMLLine" :
+ if (count($pl_name_array) > 2) {
+ $pl_description = $pl_name_array[1];
+ }
+ $e_geometry = $doc->createElement("LineString");
+ $coordinatesArray = $currentPlacemark->getGeometry()->getPointArray();
+ $coordinates = "";
+ for ($j = 0; $j < count($coordinatesArray); $j++) {
+ if ($j > 0) {
+ $coordinates .= " ";
+ }
+ $coordinates .= $coordinatesArray[$j]["x"] . "," . $coordinatesArray[$j]["y"] . "," . $coordinatesArray[$j]["z"];
+
+ }
+ $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);
+ if ($pl_name) {
+ $e_pl_name = $doc->createElement("name", $pl_name);
+ $e_placemark->appendChild($e_pl_name);
+ }
+ if ($pl_description) {
+ $e_pl_description = $doc->createElement("description", $pl_description);
+ $e_placemark->appendChild($e_pl_description);
+ }
+ $e_document->appendChild($e_placemark);
+ }
+ }
+ $this->kml = $doc->saveXML();
+ }
+ return $this->kml;
+ }
+
+ /**
+ * @return string the ID of this KML.
+ */
+ public function getId () {
+ return $this->id;
+ }
+
+ /**
+ * parses an incoming KML, creates the object,
+ * stores the kml in the object and in the database.
+ *
+ * @param string a KML document.
+ * @return boolean true if the parsing succeded, else false.
+ */
+ public function parseKml ($kml) {
+ $this->kml = $kml;
+
+ if (!$this->storeInDb()) {
+ return false;
+ }
+
+ $parser = new KmlOwsParser();
+ $parser->parseKML($kml, $this->id);
+ $this->placemarkArray = $parser->placemarkArray;
+ $this->featureCollectionMD = $parser->featureCollectionMD;
+ return true;
+ }
+
+ public function transform($targetEpsg){
+ $numberOfPlacemarks = count($this->placemarkArray);
+ if ($numberOfPlacemarks > 0) {
+ for ($i=0; $i < $numberOfPlacemarks; $i++) {
+ $this->placemarkArray[$i]->transform($targetEpsg);
+ }
+ }
+ else {
+ $e = new mb_exception("KML: toGeoJSON: this placemarkArray is empty!");
+ }
+ return true;
+ }
+ /**
+ * parses an incoming GeoJSON, creates the object,
+ * stores the kml in the object and in the database.
+ *
+ * @param string a geoJSON.
+ * @return boolean true if the parsing succeded, else false.
+ */
+ public function parseGeoJSON ($geoJSON) {
+ $this->kml = "";
+
+ if (!$this->storeInDb()) {
+ return false;
+ }
+
+ $parser = new KmlOwsParser();
+ $parser->parseGeoJSON($geoJSON, $this->id);
+ $e = new mb_notice("parsing finished...#placemarks: " . count($this->placemarkArray) . " (" . count($parser->placemarkArray) . ")");
+ $this->placemarkArray = $parser->placemarkArray;
+
+ return true;
+ }
+
+ /**
+ * @return string the geoJSON representation of the KML.
+ */
+ public function toGeoJSON($str) {
+ $numberOfPlacemarks = count($this->placemarkArray);
+
+ if ($numberOfPlacemarks > 0) {
+ $str .= "\"features\": [";
+ for ($i=0; $i < $numberOfPlacemarks; $i++) {
+ if ($i > 0) {
+ $str .= ",";
+ }
+ $str .= $this->placemarkArray[$i]->toGeoJSON();
+ }
+ $str .= "]}";
+ }
+ else {
+ $e = new mb_exception("KML: toGeoJSON: this placemarkArray is empty!");
+ }
+ return $str;
+ }
+
+ public function createGeoJSON(){
+ $str = "{";
+ $str.= "\"type\": \"FeatureCollection\",";
+
+ if (sizeof($this->featureCollectionMD) > 0) {
+
+ foreach ($this->featureCollectionMD as $key => $value) {
+
+ $str.= "\"".$key."\": \"".$value."\",";
+ }
+
+ }
+
+ $completeString = $this->toGeoJSON($str);
+ return $completeString;
+ }
+ 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);
+
+ if ($kmlFromDb !== NULL) {
+ // load the KML from the database in the DOM object
+ $kmlDoc_DOM = new DOMDocument("1.0");
+ $kmlDoc_DOM->encoding = CHARSET;
+ $kmlDoc_DOM->preserveWhiteSpace = false;
+ $kmlDoc_DOM->loadXML($kmlFromDb);
+
+ //load the geoJSON
+ $json = new Mapbender_JSON();
+ $geoObj = $json->decode($geoJSON);
+
+ // construct an array that holds all metadata of the placemark
+ $metadataObj = $geoObj->properties;
+
+ // construct an array that holds all geometries of the placemark
+ $geometryObj = $geoObj->geometry;
+ $geometryType = $geometryObj->type;
+ if ($geometryType == "GeometryCollection") {
+ $geometryArray = $geometryObj->geometries;
+ }
+ else if ($geometryType == "Point" || $geometryType == "LineString" || $geometryType == "Polygon") {
+ $geometryArray = array($geometryObj);
+ }
+ else {
+ $e = new mb_exception("class_kml: Invalid geometry type " . $geometryType);
+ return false;
+ }
+
+ //
+ // apply the changes
+ //
+
+ $currentPlacemarkArray = $kmlDoc_DOM->getElementsByTagName("Placemark");
+ $currentPlacemark = $currentPlacemarkArray->item($placemarkId);
+
+ if ($currentPlacemark) {
+ $metadataUpdateSuccessful = $this->updateMetadata($currentPlacemark, $metadataObj);
+ $geometryUpdateSuccessful = $this->updateGeometries($currentPlacemark, $geometryArray);
+ }
+ else {
+ $e = new mb_exception("class_kml.php: updateKml: placemark " . $placemarkId . " not found in KML " . $kmlId . ".");
+ return false;
+ }
+
+ if ($metadataUpdateSuccessful && $geometryUpdateSuccessful) {
+ $updatedKml = $kmlDoc_DOM->saveXML();
+
+ $this->updateInDb($updatedKml, $kmlId);
+ }
+ else {
+ if (!$metadataUpdateSuccessful) {
+ $e = new mb_exception("class_kml: Updating the metadata failed, no database update.");
+ }
+ if (!$geometryUpdateSuccessful) {
+ $e = new mb_exception("class_kml: Updating the geometries failed, no database update.");
+ }
+ return false;
+ }
+ }
+ else {
+ $e = new mb_exception("class_kml: No KML found in database, no database update. " . db_error());
+ return false;
+ }
+ return true;
+ }
+
+ //
+ //
+ // ------------------------------- private -----------------------------------------------
+ //
+ //
+
+ /**
+ * Store this KML in the database, and sets the ID.
+ *
+ * @return boolean true, if the KML could be stored in the database; else false.
+ */
+ private function storeInDb () {
+ if (Mapbender::session()->get("mb_user_id") && Mapbender::session()->get("mb_user_gui")) {
+ $con = db_connect(DBSERVER,OWNER,PW);
+ db_select_db(DB,$con);
+
+ $sql = "INSERT INTO gui_kml ";
+ $sql .= "(fkey_mb_user_id, fkey_gui_id, kml_doc, kml_name, kml_description, kml_timestamp) ";
+ $sql .= "VALUES ";
+ $sql .= "($1, $2, $3, $4, $5, $6)";
+ $v = array (Mapbender::session()->get("mb_user_id"), Mapbender::session()->get("mb_user_gui"), $this->kml, "name", "description", time());
+ $t = array ("i", "s", "s", "s", "s", "s");
+ $res = db_prep_query($sql, $v, $t);
+ if (!$res) {
+ $e = new mb_exception("class_kml.php: storeInDb: failed to store KML in database: " . db_error());
+ return false;
+ }
+
+ $this->id = db_insert_id($con, "gui_kml", "kml_id");
+ return true;
+ }
+ else {
+ // should be false, but code in caller has to be changed first.
+ return true;
+ }
+ }
+
+ /**
+ * @param integer the ID of the KML.
+ * @return string the KML document with the given ID.
+ */
+ public function getKmlDocumentFromDB ($kmlId) {
+ $con = db_connect(DBSERVER,OWNER,PW);
+ db_select_db(DB,$con);
+ //get KML from database (check if user is allowed to access)
+
+# for now, do not restrict access
+# $sql = "SELECT kml_doc FROM gui_kml WHERE kml_id = $1 AND fkey_mb_user_id = $2 AND fkey_gui_id = $3 LIMIT 1";
+# $v = array($kmlId, Mapbender::session()->get("mb_user_id"), Mapbender::session()->get("mb_user_gui"));
+# $t = array("i", "i", "s");
+
+ $sql = "SELECT kml_doc FROM gui_kml WHERE kml_id = $1 LIMIT 1";
+ $v = array($kmlId);
+ $t = array("i");
+
+ $result = db_prep_query($sql, $v, $t);
+ $row = db_fetch_array($result);
+ if ($row) {
+ return $row["kml_doc"];
+ }
+ else {
+ $e = new mb_exception("class_kml.php: getKMLDocumentFromDB: no KML found for ID " . $kmlId);
+ }
+ return "";
+ }
+
+ /**
+ * @param string the tag name.
+ * @return string the tag name without its namespace.
+ */
+ private function sepNameSpace($s){
+ $c = mb_strpos($s, ":");
+ if ($c > 0) {
+ $s = mb_substr($s, $c+1);
+ }
+ return $s;
+ }
+
+ private function updateGeometries($currentPlacemark, $geometryArray) {
+ $cnt = 0;
+ $childNodes = $currentPlacemark->childNodes;
+
+ foreach ($childNodes as $childNode) {
+ $name = $childNode->nodeName;
+ if ( in_array($name, array("Point","LineString","Polygon"))) {
+ $returnValue = $this->updateGeometry($childNode, $geometryArray[$cnt]);
+ if (!$returnValue) {
+ return false;
+ }
+ $cnt ++;
+ }
+ else if ($name == "MultiGeometry") {
+ return $this->updateGeometries($childNode, $geometryArray);
+ }
+ }
+ return true;
+ }
+
+ private function updateGeometry ($currentNode, $geometry) {
+ $json = new Mapbender_JSON();
+ $currentNode_SimpleXML = simplexml_import_dom($currentNode);
+
+ $currentTypeXml = mb_strtoupper($currentNode->nodeName);
+ $currentTypeGeoJson = mb_strtoupper($geometry->type);
+
+ if ($currentTypeGeoJson != $currentTypeXml) {
+ $e = new mb_exception("class_kml: geometry type mismatch: geoJSON: " . $currentTypeGeoJson . "; XML: " . $currentTypeXml);
+ return false;
+ }
+ if ($currentTypeXml == "POLYGON") {
+ // GML 3
+ $gmlNode = $currentNode_SimpleXML->{"exterior"}->{"LinearRing"}->{"posList"};
+ $kmlNode = $currentNode_SimpleXML->{"outerBoundaryIs"}->{"LinearRing"}->{"coordinates"};
+ if ($gmlNode && $gmlNode->asXML()) {
+ $currentNode_SimpleXML->{"exterior"}->{"LinearRing"}->{"posList"} = preg_replace("/,/", " ", preg_replace("/\[|\]/", "", $json->encode($geometry->coordinates)));
+ }
+ // KML 2.2
+ else if ($kmlNode && $kmlNode->asXML()) {
+ $currentNode_SimpleXML->{"outerBoundaryIs"}->{"LinearRing"}->{"coordinates"} = preg_replace("/\],/", " ", preg_replace("/\][^,]|\[/", "", $json->encode($geometry->coordinates)));
+ }
+ }
+ elseif ($currentTypeXml == "POINT") {
+ $gmlNode = $currentNode_SimpleXML->{"pos"};
+ $kmlNode = $currentNode_SimpleXML->{"coordinates"};
+
+ // GML 3
+ if ($gmlNode && $gmlNode->asXML()) {
+ $currentNode_SimpleXML->{"pos"} = preg_replace("/,/", " ", preg_replace("/\[|\]/", "", $json->encode($geometry->coordinates)));
+ }
+ // KML 2.2
+ else if ($kmlNode && $kmlNode->asXML()) {
+ $currentNode_SimpleXML->{"coordinates"} = preg_replace("/\[|\]/", "", $json->encode($geometry->coordinates));
+ }
+ }
+ elseif ($currentTypeXml == "LINESTRING") {
+ $gmlNode = $currentNode_SimpleXML->{"posList"};
+ $kmlNode = $currentNode_SimpleXML->{"coordinates"};
+
+ // GML 3
+ if ($gmlNode && $gmlNode->asXML()) {
+ $currentNode_SimpleXML->{"posList"} = preg_replace("/,/", " ", preg_replace("/\[|\]/", "", $json->encode($geometry->coordinates)));
+ }
+ // KML 2.2
+ else if ($kmlNode && $kmlNode->asXML()) {
+ $currentNode_SimpleXML->{"coordinates"} = preg_replace("/\[|\]/", "", $json->encode($geometry->coordinates));
+ }
+ }
+ return true;
+ }
+
+ 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) {
+ foreach ($simpleDataNodes as $simpleDataNode) {
+ $tmp = dom_import_simplexml($simpleDataNode);
+ $name = $tmp->getAttribute("name");
+ // if there is a metadata entry, update it
+ if (isset($metadataObj->$name)) {
+ $tmp->nodeValue = $metadataObj->$name;
+ }
+ }
+
+ $metadataExistsAndUpdateSucceeded = true;
+ }
+
+ // ...or within a DATA tag
+ $dataNodes = $extendedDataNode->{"Data"};
+ if ($dataNodes && !$metadataExistsAndUpdateSucceeded) {
+ foreach ($dataNodes as $dataNode) {
+ $tmp = dom_import_simplexml($dataNode);
+ $name = $tmp->getAttribute("name");
+ // if there is a metadata entry, update it
+ if (isset($metadataObj->$name)) {
+ $tmp->nodeValue = $metadataObj->$name;
+ }
+ }
+ $metadataExistsAndUpdateSucceeded = true;
+ }
+ }
+ return $metadataExistsAndUpdateSucceeded;
+ }
+
+ /**
+ * The KML document.
+ */
+ private $kml;
+
+ /**
+ * The ID of this KML in the database.
+ */
+ private $id;
+
+ /**
+ * An array of {@link KMLPlacemark}
+ */
+ private $placemarkArray = array();
+}
+?>
Modified: trunk/mapbender/http/classes/class_kml_parser_ows.php
===================================================================
--- trunk/mapbender/http/classes/class_kml_parser_ows.php 2015-05-04 09:48:47 UTC (rev 9187)
+++ trunk/mapbender/http/classes/class_kml_parser_ows.php 2015-05-04 13:48:27 UTC (rev 9188)
@@ -1,450 +1,450 @@
-<?php
-# $Id$
-# http://www.mapbender.org/index.php/class_wmc.php
-# Copyright (C) 2002 CCGIS
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2, or (at your option)
-# any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# 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__)."/../../core/globalSettings.php");
-require_once(dirname(__FILE__)."/../classes/class_json.php");
-require_once(dirname(__FILE__)."/../classes/class_point.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
- */
- class KmlOwsParser {
- var $placemarkArray = array();
- var $featureCollectionMD = array();
-
- public function __construct() {
- }
-
- public function parseGeoJSON ($geoJSON, $kmlId) {
-
-// $e = new mb_notice("GEOJSON: " . $geoJSON);
- $json = new Mapbender_JSON();
- $geometryFromGeoJSON = $json->decode($geoJSON);
- $id = 0;
-
- if (gettype($geometryFromGeoJSON) == "object" && $geometryFromGeoJSON->type == "FeatureCollection") {
- if ($geometryFromGeoJSON->crs->type == "EPSG" && $geometryFromGeoJSON->crs->properties->code) {
- $epsg = $geometryFromGeoJSON->crs->properties->code;
- }
- // create Placemarks
- for ($i = 0; $i < count($geometryFromGeoJSON->features); $i++) {
- $e = new mb_notice("parsing plm #" . $i . "...length of placemarkArray: " . count($this->placemarkArray));
- $feature = $geometryFromGeoJSON->features[$i];
- if (gettype($feature) == "object" && $feature->type == "Feature") {
- if ($feature->geometry->crs->type == "EPSG") {
- $epsg = $feature->geometry->crs->properties->code;
- }
- if (!$epsg) {
- $e = new mb_notice("EPSG is not set! Aborting...(" . $epsg . ")");
- $currentGeometry = false;
- }
- $geometry = $feature->geometry;
-
- //TODO: missing Polygon and MultiGeometry
- switch ($geometry->type) {
- case "LineString" :
- $coordinateList = "";
- for ($j = 0; $j < count($geometry->coordinates); $j++) {
- if ($j > 0) {
- $coordinateList .= " ";
- }
- $coordinateList .= implode(",", $geometry->coordinates[$j]);
- }
- $currentGeometry = new KMLLine($coordinateList, $epsg);
- break;
- case "Point" :
- $coordinateList = implode(",", $geometry->coordinates);
- $currentGeometry = new KMLPoint($coordinateList, $epsg);
- break;
- }
-
- if ($currentGeometry) {
- $currentPlacemark = new KMLPlacemark($currentGeometry);
-
- if (gettype($feature->properties) == "object") {
-
- 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);
- $e = new mb_notice("adding to placemarkArray (current length: " . count($this->placemarkArray) . ")");
- array_push($this->placemarkArray, $currentPlacemark);
- $e = new mb_notice("added...new length: " . count($this->placemarkArray));
- $id ++;
- }
- }
- }
- }
- }
- return true;
- }
-
- public function parseKML ($kml, $kmlId) {
- $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");
- $xpath->registerNamespace("http://www.opengis.net/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
- */
- $placemarkTagArray = $doc->getElementsByTagName("Placemark");
-
- if (count($placemarkTagArray) > 0) {
- $id = 0;
-
- foreach ($placemarkTagArray as $node) {
- $geometryArray = $this->getGeometryArrayFromPlacemarkOrMultigeometryNode($node);
- $metadataArray = $this->getMetadataFromPlacemarkNode($node);
- /*
- * For a placemark, the geometryArray should only contain 1 geometry!
- */
- for ($i=0; $i < count($geometryArray); $i++) {
- $currentPlacemark = new KMLPlacemark($geometryArray[$i]);
-
- $currentPlacemark->setProperty("Mapbender:kml", true);
- $currentPlacemark->setProperty("Mapbender:name", "unknown");
- $currentPlacemark->setProperty("Mapbender:id", $kmlId);
- $currentPlacemark->setProperty("Mapbender:placemarkId", $id);
- foreach ($metadataArray as $key => $value) {
- $currentPlacemark->setProperty($key, $value);
- }
-
- // add description and name:
- $namesNode = $node->getElementsByTagName('name');
- if($namesNode->length > 0){
- $name = trim($namesNode->item(0)->nodeValue);
- }
- $descriptionsNode = $node->getElementsByTagName('description');
- if($descriptionsNode->length > 0){
- $description = trim($descriptionsNode->item(0)->nodeValue);
- }
- //TODO: dont add the properties twice!
- // $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 ++;
- }
- }
- else {
- $e = new mb_exception("class_kml.php: KMLOWSParser: No placemarks found in KML.");
- return false;
- }
-
-
-
- /**
- * add metadata for the feature-collection
- *
- */
- $extendedData = $xpath->query('//*[local-name() = "kml"]/*[local-name() = "Document"]/*[local-name() = "ExtendedData"]');
- if ($extendedData->length > 0) { // check for metadata
-
- foreach ($extendedData as $metaData) {
- $data = $metaData->getElementsByTagName('Data');
- foreach ($data as $metadataValue) {
-
-
- foreach($metadataValue->attributes as $attribute_name => $attribute_node){
- // * @var DOMNode $attribute_node
- $this->featureCollectionMD[$attribute_node->nodeValue] = $metadataValue->nodeValue;
- }
- }
-
- }
-
- }
-
- return true;
- }
-
- /**
- * Returns an associative array, containing metadata
- */
- private function getMetadataFromPlacemarkNode ($node) {
- $children = $node->childNodes;
-
- $metadataArray = array();
-
- // search "ExtendedData" tag
- foreach ($children as $child) {
- if (mb_strtoupper($this->sepNameSpace($child->nodeName)) == "EXTENDEDDATA") {
- $extendedDataNode = $child;
- $extDataChildren = $extendedDataNode->childNodes;
-
- // search "Data" or "SchemaData" tag
- foreach ($extDataChildren as $extDataChild) {
- if (mb_strtoupper($this->sepNameSpace($extDataChild->nodeName)) == "SCHEMADATA") {
- $simpleDataNode = $extDataChild->firstChild;
- while ($simpleDataNode !== NULL) {
- if (mb_strtoupper($this->sepNameSpace($simpleDataNode->nodeName)) == "SIMPLEDATA") {
- $name = $simpleDataNode->getAttribute("name");
- $value = $simpleDataNode->nodeValue;
- $metadataArray[$name] = $value;
- }
- $simpleDataNode = $simpleDataNode->nextSibling;
- }
- }
- if (mb_strtoupper($this->sepNameSpace($extDataChild->nodeName)) == "DATA") {
- $dataNode = $extDataChild;
- $name = $dataNode->getAttribute("name");
- $metadataArray[$name] = $dataNode->nodeValue;
- }
- }
- }
- if(mb_strtoupper($this->sepNameSpace($child->nodeName)) == "STYLE"){
- $hrefNodes = $child->getElementsByTagName("href");
- if($hrefNodes->length > 0){
- $href = $hrefNodes->item(0)->nodeValue;
- $metadataArray["iconurl"] = $href;
- }
-
- }
- }
- return $metadataArray;
- }
-
- /**
- * Given a "Point" node, this function returns the geometry (KMLPoint)
- * from within the node.
- */
- private function getGeometryFromPointNode ($node) {
- $coordinatesNode = $this->getCoordinatesNode($node);
- $geomString = $coordinatesNode->nodeValue;
- return new KMLPoint($geomString, 4326);
- }
-
- /**
- * Given a "LineString" node, this function returns the geometry (KMLLine)
- * from within the node.
- */
- private function getGeometryFromLinestringNode ($node) {
- $coordinatesNode = $this->getCoordinatesNode($node);
- $geomString = $coordinatesNode->nodeValue;
- return new KMLLine($geomString, 4326);
- }
-
- /**
- * Given a "Polygon" node, this function returns the geometry (KMLPolygon)
- * from within the node.
- */
- private function getGeometryFromPolygonNode ($node) {
- $polygon = null;
-
- $children = $node->childNodes;
-
- // create new KMLPolygon
- foreach ($children as $child) {
- if (mb_strtoupper($this->sepNameSpace($child->nodeName)) == "EXTERIOR" ||
- mb_strtoupper($this->sepNameSpace($child->nodeName)) == "OUTERBOUNDARYIS") {
- // create a new Linear Ring
- $outerBoundary = $this->getGeometryFromLinearRingNode($child);
- $polygon = new KMLPolygon($outerBoundary);
- }
- }
-
- if ($polygon !== null) {
- // append inner boundaries to KMLPolygon
- foreach ($children as $child) {
- if (mb_strtoupper($this->sepNameSpace($child->nodeName)) == "INTERIOR" ||
- mb_strtoupper($this->sepNameSpace($child->nodeName)) == "INNERBOUNDARYIS") {
- // create a new Linear Ring
- $innerBoundary = $this->getGeometryFromLinearRingNode($child);
- $polygon->appendInnerBoundary($innerBoundary);
- }
- }
- }
- return $polygon;
- }
-
- /**
- * Given a "OuterBoundaryIs" or "InnerBoundaryIs" node, this function
- * returns the geometry (KMLLinearRing) within the child node named "linearring"
- */
- private function getGeometryFromLinearRingNode ($node) {
- $children = $node->childNodes;
- foreach($children as $child) {
- if (mb_strtoupper($this->sepNameSpace($child->nodeName)) == "LINEARRING") {
- $coordinatesNode = $this->getCoordinatesNode($child);
- $geomString = $coordinatesNode->nodeValue;
- return new KMLLinearRing($geomString);
- }
- }
- return null;
- }
-
- /**
- * Checks if the child nodes of a given KML node contains any geometries and
- * returns an array of geometries (KMLPoint, KMLPolygon, KMLLinestring and KMLMultigeometry)
- */
- private function getGeometryArrayFromPlacemarkOrMultigeometryNode ($node) {
- $geometryArray = array();
-
- $children = $node->childNodes;
- foreach($children as $child) {
- if (mb_strtoupper($this->sepNameSpace($child->nodeName)) == "POINT") {
- array_push($geometryArray, $this->getGeometryFromPointNode($child));
- }
- elseif (mb_strtoupper($this->sepNameSpace($child->nodeName)) == "POLYGON") {
- array_push($geometryArray, $this->getGeometryFromPolygonNode($child));
- }
- elseif (mb_strtoupper($this->sepNameSpace($child->nodeName)) == "LINESTRING") {
- array_push($geometryArray, $this->getGeometryFromLinestringNode($child));
- }
- elseif (mb_strtoupper($this->sepNameSpace($child->nodeName)) == "MULTIGEOMETRY") {
- $geometryArray = $this->getGeometryArrayFromPlacemarkOrMultigeometryNode($child);
- $multigeometry = new KMLMultiGeometry();
-
- for ($i=0; $i < count($geometryArray); $i++) {
- $multigeometry->append($geometryArray[$i]);
- }
- array_push($geometryArray, $multigeometry);
- }
- }
- return $geometryArray;
- }
-
- /**
- * Returns the child node with node name "coordinates" of a given KML node.
- * If no node is found, null is returned.
- */
- private function getCoordinatesNode ($node) {
- $children = $node->childNodes;
- foreach($children as $child) {
- if (mb_strtoupper($this->sepNameSpace($child->nodeName)) == "POSLIST" ||
- mb_strtoupper($this->sepNameSpace($child->nodeName)) == "POS" ||
- mb_strtoupper($this->sepNameSpace($child->nodeName)) == "COORDINATES") {
- return $child;
- }
- }
- return null;
- }
-
- private function sepNameSpace($s){
- $c = mb_strpos($s,":");
- if($c>0){
- return mb_substr($s,$c+1);
- }
- else{
- return $s;
- }
- }
-}
-?>
+<?php
+# $Id$
+# http://www.mapbender.org/index.php/class_wmc.php
+# Copyright (C) 2002 CCGIS
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# 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__)."/../../core/globalSettings.php");
+require_once(dirname(__FILE__)."/../classes/class_json.php");
+require_once(dirname(__FILE__)."/../classes/class_point.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
+ */
+ class KmlOwsParser {
+ var $placemarkArray = array();
+ var $featureCollectionMD = array();
+
+ public function __construct() {
+ }
+
+ public function parseGeoJSON ($geoJSON, $kmlId) {
+
+// $e = new mb_notice("GEOJSON: " . $geoJSON);
+ $json = new Mapbender_JSON();
+ $geometryFromGeoJSON = $json->decode($geoJSON);
+ $id = 0;
+
+ if (gettype($geometryFromGeoJSON) == "object" && $geometryFromGeoJSON->type == "FeatureCollection") {
+ if ($geometryFromGeoJSON->crs->type == "EPSG" && $geometryFromGeoJSON->crs->properties->code) {
+ $epsg = $geometryFromGeoJSON->crs->properties->code;
+ }
+ // create Placemarks
+ for ($i = 0; $i < count($geometryFromGeoJSON->features); $i++) {
+ $e = new mb_notice("parsing plm #" . $i . "...length of placemarkArray: " . count($this->placemarkArray));
+ $feature = $geometryFromGeoJSON->features[$i];
+ if (gettype($feature) == "object" && $feature->type == "Feature") {
+ if ($feature->geometry->crs->type == "EPSG") {
+ $epsg = $feature->geometry->crs->properties->code;
+ }
+ if (!$epsg) {
+ $e = new mb_notice("EPSG is not set! Aborting...(" . $epsg . ")");
+ $currentGeometry = false;
+ }
+ $geometry = $feature->geometry;
+
+ //TODO: missing Polygon and MultiGeometry
+ switch ($geometry->type) {
+ case "LineString" :
+ $coordinateList = "";
+ for ($j = 0; $j < count($geometry->coordinates); $j++) {
+ if ($j > 0) {
+ $coordinateList .= " ";
+ }
+ $coordinateList .= implode(",", $geometry->coordinates[$j]);
+ }
+ $currentGeometry = new KMLLine($coordinateList, $epsg);
+ break;
+ case "Point" :
+ $coordinateList = implode(",", $geometry->coordinates);
+ $currentGeometry = new KMLPoint($coordinateList, $epsg);
+ break;
+ }
+
+ if ($currentGeometry) {
+ $currentPlacemark = new KMLPlacemark($currentGeometry);
+
+ if (gettype($feature->properties) == "object") {
+
+ 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);
+ $e = new mb_notice("adding to placemarkArray (current length: " . count($this->placemarkArray) . ")");
+ array_push($this->placemarkArray, $currentPlacemark);
+ $e = new mb_notice("added...new length: " . count($this->placemarkArray));
+ $id ++;
+ }
+ }
+ }
+ }
+ }
+ return true;
+ }
+
+ public function parseKML ($kml, $kmlId) {
+ $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");
+ $xpath->registerNamespace("http://www.opengis.net/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
+ */
+ $placemarkTagArray = $doc->getElementsByTagName("Placemark");
+
+ if (count($placemarkTagArray) > 0) {
+ $id = 0;
+
+ foreach ($placemarkTagArray as $node) {
+ $geometryArray = $this->getGeometryArrayFromPlacemarkOrMultigeometryNode($node);
+ $metadataArray = $this->getMetadataFromPlacemarkNode($node);
+ /*
+ * For a placemark, the geometryArray should only contain 1 geometry!
+ */
+ for ($i=0; $i < count($geometryArray); $i++) {
+ $currentPlacemark = new KMLPlacemark($geometryArray[$i]);
+
+ $currentPlacemark->setProperty("Mapbender:kml", true);
+ $currentPlacemark->setProperty("Mapbender:name", "unknown");
+ $currentPlacemark->setProperty("Mapbender:id", $kmlId);
+ $currentPlacemark->setProperty("Mapbender:placemarkId", $id);
+ foreach ($metadataArray as $key => $value) {
+ $currentPlacemark->setProperty($key, $value);
+ }
+
+ // add description and name:
+ $namesNode = $node->getElementsByTagName('name');
+ if($namesNode->length > 0){
+ $name = trim($namesNode->item(0)->nodeValue);
+ }
+ $descriptionsNode = $node->getElementsByTagName('description');
+ if($descriptionsNode->length > 0){
+ $description = trim($descriptionsNode->item(0)->nodeValue);
+ }
+ //TODO: dont add the properties twice!
+ // $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 ++;
+ }
+ }
+ else {
+ $e = new mb_exception("class_kml.php: KMLOWSParser: No placemarks found in KML.");
+ return false;
+ }
+
+
+
+ /**
+ * add metadata for the feature-collection
+ *
+ */
+ $extendedData = $xpath->query('//*[local-name() = "kml"]/*[local-name() = "Document"]/*[local-name() = "ExtendedData"]');
+ if ($extendedData->length > 0) { // check for metadata
+
+ foreach ($extendedData as $metaData) {
+ $data = $metaData->getElementsByTagName('Data');
+ foreach ($data as $metadataValue) {
+
+
+ foreach($metadataValue->attributes as $attribute_name => $attribute_node){
+ // * @var DOMNode $attribute_node
+ $this->featureCollectionMD[$attribute_node->nodeValue] = $metadataValue->nodeValue;
+ }
+ }
+
+ }
+
+ }
+
+ return true;
+ }
+
+ /**
+ * Returns an associative array, containing metadata
+ */
+ private function getMetadataFromPlacemarkNode ($node) {
+ $children = $node->childNodes;
+
+ $metadataArray = array();
+
+ // search "ExtendedData" tag
+ foreach ($children as $child) {
+ if (mb_strtoupper($this->sepNameSpace($child->nodeName)) == "EXTENDEDDATA") {
+ $extendedDataNode = $child;
+ $extDataChildren = $extendedDataNode->childNodes;
+
+ // search "Data" or "SchemaData" tag
+ foreach ($extDataChildren as $extDataChild) {
+ if (mb_strtoupper($this->sepNameSpace($extDataChild->nodeName)) == "SCHEMADATA") {
+ $simpleDataNode = $extDataChild->firstChild;
+ while ($simpleDataNode !== NULL) {
+ if (mb_strtoupper($this->sepNameSpace($simpleDataNode->nodeName)) == "SIMPLEDATA") {
+ $name = $simpleDataNode->getAttribute("name");
+ $value = $simpleDataNode->nodeValue;
+ $metadataArray[$name] = $value;
+ }
+ $simpleDataNode = $simpleDataNode->nextSibling;
+ }
+ }
+ if (mb_strtoupper($this->sepNameSpace($extDataChild->nodeName)) == "DATA") {
+ $dataNode = $extDataChild;
+ $name = $dataNode->getAttribute("name");
+ $metadataArray[$name] = $dataNode->nodeValue;
+ }
+ }
+ }
+ if(mb_strtoupper($this->sepNameSpace($child->nodeName)) == "STYLE"){
+ $hrefNodes = $child->getElementsByTagName("href");
+ if($hrefNodes->length > 0){
+ $href = $hrefNodes->item(0)->nodeValue;
+ $metadataArray["iconurl"] = $href;
+ }
+
+ }
+ }
+ return $metadataArray;
+ }
+
+ /**
+ * Given a "Point" node, this function returns the geometry (KMLPoint)
+ * from within the node.
+ */
+ private function getGeometryFromPointNode ($node) {
+ $coordinatesNode = $this->getCoordinatesNode($node);
+ $geomString = $coordinatesNode->nodeValue;
+ return new KMLPoint($geomString, 4326);
+ }
+
+ /**
+ * Given a "LineString" node, this function returns the geometry (KMLLine)
+ * from within the node.
+ */
+ private function getGeometryFromLinestringNode ($node) {
+ $coordinatesNode = $this->getCoordinatesNode($node);
+ $geomString = $coordinatesNode->nodeValue;
+ return new KMLLine($geomString, 4326);
+ }
+
+ /**
+ * Given a "Polygon" node, this function returns the geometry (KMLPolygon)
+ * from within the node.
+ */
+ private function getGeometryFromPolygonNode ($node) {
+ $polygon = null;
+
+ $children = $node->childNodes;
+
+ // create new KMLPolygon
+ foreach ($children as $child) {
+ if (mb_strtoupper($this->sepNameSpace($child->nodeName)) == "EXTERIOR" ||
+ mb_strtoupper($this->sepNameSpace($child->nodeName)) == "OUTERBOUNDARYIS") {
+ // create a new Linear Ring
+ $outerBoundary = $this->getGeometryFromLinearRingNode($child);
+ $polygon = new KMLPolygon($outerBoundary);
+ }
+ }
+
+ if ($polygon !== null) {
+ // append inner boundaries to KMLPolygon
+ foreach ($children as $child) {
+ if (mb_strtoupper($this->sepNameSpace($child->nodeName)) == "INTERIOR" ||
+ mb_strtoupper($this->sepNameSpace($child->nodeName)) == "INNERBOUNDARYIS") {
+ // create a new Linear Ring
+ $innerBoundary = $this->getGeometryFromLinearRingNode($child);
+ $polygon->appendInnerBoundary($innerBoundary);
+ }
+ }
+ }
+ return $polygon;
+ }
+
+ /**
+ * Given a "OuterBoundaryIs" or "InnerBoundaryIs" node, this function
+ * returns the geometry (KMLLinearRing) within the child node named "linearring"
+ */
+ private function getGeometryFromLinearRingNode ($node) {
+ $children = $node->childNodes;
+ foreach($children as $child) {
+ if (mb_strtoupper($this->sepNameSpace($child->nodeName)) == "LINEARRING") {
+ $coordinatesNode = $this->getCoordinatesNode($child);
+ $geomString = $coordinatesNode->nodeValue;
+ return new KMLLinearRing($geomString);
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Checks if the child nodes of a given KML node contains any geometries and
+ * returns an array of geometries (KMLPoint, KMLPolygon, KMLLinestring and KMLMultigeometry)
+ */
+ private function getGeometryArrayFromPlacemarkOrMultigeometryNode ($node) {
+ $geometryArray = array();
+
+ $children = $node->childNodes;
+ foreach($children as $child) {
+ if (mb_strtoupper($this->sepNameSpace($child->nodeName)) == "POINT") {
+ array_push($geometryArray, $this->getGeometryFromPointNode($child));
+ }
+ elseif (mb_strtoupper($this->sepNameSpace($child->nodeName)) == "POLYGON") {
+ array_push($geometryArray, $this->getGeometryFromPolygonNode($child));
+ }
+ elseif (mb_strtoupper($this->sepNameSpace($child->nodeName)) == "LINESTRING") {
+ array_push($geometryArray, $this->getGeometryFromLinestringNode($child));
+ }
+ elseif (mb_strtoupper($this->sepNameSpace($child->nodeName)) == "MULTIGEOMETRY") {
+ $geometryArray = $this->getGeometryArrayFromPlacemarkOrMultigeometryNode($child);
+ $multigeometry = new KMLMultiGeometry();
+
+ for ($i=0; $i < count($geometryArray); $i++) {
+ $multigeometry->append($geometryArray[$i]);
+ }
+ array_push($geometryArray, $multigeometry);
+ }
+ }
+ return $geometryArray;
+ }
+
+ /**
+ * Returns the child node with node name "coordinates" of a given KML node.
+ * If no node is found, null is returned.
+ */
+ private function getCoordinatesNode ($node) {
+ $children = $node->childNodes;
+ foreach($children as $child) {
+ if (mb_strtoupper($this->sepNameSpace($child->nodeName)) == "POSLIST" ||
+ mb_strtoupper($this->sepNameSpace($child->nodeName)) == "POS" ||
+ mb_strtoupper($this->sepNameSpace($child->nodeName)) == "COORDINATES") {
+ return $child;
+ }
+ }
+ return null;
+ }
+
+ private function sepNameSpace($s){
+ $c = mb_strpos($s,":");
+ if($c>0){
+ return mb_substr($s,$c+1);
+ }
+ else{
+ return $s;
+ }
+ }
+}
+?>
Modified: trunk/mapbender/http/classes/class_wmc.php
===================================================================
--- trunk/mapbender/http/classes/class_wmc.php 2015-05-04 09:48:47 UTC (rev 9187)
+++ trunk/mapbender/http/classes/class_wmc.php 2015-05-04 13:48:27 UTC (rev 9188)
@@ -653,7 +653,7 @@
$e = new mb_notice("class_wmc.php - updateUrlsInDb - create new DataURL Element!");
//$layerXML = $layer->asXML();
//$dom_layer = dom_import_string($layerXML);
-
+
/*$dom_layer = dom_import_simplexml($layer);
$dom = new DOMDocument('1.0');
$dom_layer = $dom->importNode($dom_layer, true);
@@ -680,14 +680,14 @@
</xs:sequence>
*/
- //check if Abstract exists - if it does DataURL came after Abstract, if not DataURL will be after Title Element
+ //check if Abstract exists - if it does DataURL came after Abstract, if not DataURL will be after Title Element
if (count($layer->Abstract) > 0) {
$e = new mb_notice("class_wmc.php - updateUrlsInDb - Abstract found!");
//put it after Abstract Element
//New element to be inserted
$insert = new SimpleXMLElement('<DataURL></DataURL>');
//<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="'.$downloadOptionsUrl.'"/>
- // Get the Abstract element for this layer
+ // Get the Abstract element for this layer
$target = current($WMCDoc->xpath("/wmc:ViewContext/wmc:LayerList/wmc:Layer[wmc:Extension/mapbender:layer_id='".(integer)$layerId."']/wmc:Abstract"));
// Insert the new element after the last nodeA
$this->simplexml_insert_after($insert, $target);
@@ -698,7 +698,7 @@
//put it after Title Element
$insert = new SimpleXMLElement("<DataURL></DataURL>");
//<OnlineResource xlink:type='simple' xlink:href='".$downloadOptionsUrl."'/>
- // Get the Abstract element for this layer
+ // Get the Abstract element for this layer
$target = current($WMCDoc->xpath("/wmc:ViewContext/wmc:LayerList/wmc:Layer[wmc:Extension/mapbender:layer_id='".(integer)$layerId."']/wmc:Title"));
// Insert the new element after the last nodeA
$this->simplexml_insert_after($insert, $target);
@@ -736,7 +736,7 @@
//check mimetype
if (isset($styleObject->LegendURL->attributes()->format) && isset($style[(integer)$layerId][(string)$styleObject->Name][(string)$styleObject->LegendURL->attributes()->format])) {
$mimeType = (string)$styleObject->LegendURL->attributes()->format;
-
+
} else {
$mimeType = 'image/png';
}
@@ -745,7 +745,7 @@
$e = new mb_notice("class_wmc: exchange old legendurl url : ".$layer->StyleList->Style->LegendURL->OnlineResource->attributes('xlink', true)->href." with new legendurl: ".$style[(integer)$layerId][(string)$styleObject->Name][$mimeType]);
$layer->StyleList->Style->LegendURL->OnlineResource->attributes('xlink', true)->href = $style[(integer)$layerId][(string)$styleObject->Name][$mimeType];
}
-
+
}
}
}
@@ -1008,7 +1008,7 @@
public function update_existing($xml,$id) {
$sql = "UPDATE mb_user_wmc SET wmc = $1 WHERE wmc_serial_id = $2";
$v = array($xml,$id);
- $t = array("s","i");
+ $t = array("s","s");
$res = db_prep_query($sql,$v,$t);
if(db_error()) { $e = new mb_exception("There was an error saving an updated WMC"); }
}
Modified: trunk/mapbender/http/php/mb_delete_local_data.php
===================================================================
--- trunk/mapbender/http/php/mb_delete_local_data.php 2015-05-04 09:48:47 UTC (rev 9187)
+++ trunk/mapbender/http/php/mb_delete_local_data.php 2015-05-04 13:48:27 UTC (rev 9188)
@@ -25,10 +25,11 @@
$wmc = new wmc();
// get post parameter
$serial_id;
-$wmc_id = $_POST["id"];
+$wmc_serial_id = $_POST["id"];
+var_dump($wmc_serial_id);die;
// sql statement to get the wmc object and the serial_id
-$sql = 'Select wmc,wmc_serial_id from mb_user_wmc where wmc_id = $1 and wmc_has_local_data = 1 and fkey_user_id = $2;';
-$v = array($wmc_id, $user_id);
+$sql = 'Select wmc from mb_user_wmc where wmc_serial_id = $1 and wmc_has_local_data = 1 and fkey_user_id = $2;';
+$v = array($wmc_serial_id, $user_id);
$t = array("c", "i");
$res = db_prep_query($sql, $v, $t);
// fetch result
@@ -36,19 +37,17 @@
$wmc->createFromXml($row['wmc']);
$wmc->generalExtensionArray['KMLS'] = null;
$wmc->generalExtensionArray['KMLORDER'] = null;
- $wmc->wmc_id = $wmc_id;
+ $wmc->wmc_serial_id = $wmc_serial_id;
$wmc->has_local_data = 0;
$wmc->local_data_public = 0;
$wmc->local_data_size = '0';
$newWmcXml = $wmc->toXml();
- // set the serial_id
- $serial_id = $row['wmc_serial_id'];
- if (is_int(intval($wmc->wmc_id))) {
+ if (is_int(intval($wmc->wmc_serial_id))) {
$wmc->update_existing($newWmcXml, $serial_id);
// send sql to update the local-data flag
- $sql = 'UPDATE mb_user_wmc mb SET wmc_has_local_data = 0 WHERE wmc_id = $1';
- $v = array($wmc_id);
+ $sql = 'UPDATE mb_user_wmc mb SET wmc_has_local_data = 0 WHERE wmc_serial_id = $1';
+ $v = array($wmc_serial_id);
$t = array("s");
$res = db_prep_query($sql, $v, $t);
Modified: trunk/mapbender/http/php/mb_load_local_data.php
===================================================================
--- trunk/mapbender/http/php/mb_load_local_data.php 2015-05-04 09:48:47 UTC (rev 9187)
+++ trunk/mapbender/http/php/mb_load_local_data.php 2015-05-04 13:48:27 UTC (rev 9188)
@@ -23,24 +23,22 @@
$wmc = new wmc();
-$wmc_id = $_POST["id"];
+$wmc_serial_id = $_POST["id"];
$form_target = $self;
-$sql = 'select wmc from mb_user_wmc where wmc_id = $1 and wmc_has_local_data = 1 and (fkey_user_id = $2 or wmc_local_data_public = 1);';
+$sql = 'select wmc from mb_user_wmc where wmc_serial_id = $1 and wmc_has_local_data = 1 and (fkey_user_id = $2 or wmc_local_data_public = 1);';
-$v = array($wmc_id, $user_id);
+$v = array($wmc_serial_id, $user_id);
$t = array("i", "i");
$res = db_prep_query($sql, $v, $t);
header("Content-Type: application/json");
-if($row = db_fetch_array($res)){
+if ($row = db_fetch_array($res)) {
$wmc->createFromXml($row['wmc']);
$kmls = $wmc->generalExtensionArray['KMLS'];
echo($kmls);
} else {
echo('{}');
}
-
-?>
Modified: trunk/mapbender/http/php/mb_publish_wmc.php
===================================================================
--- trunk/mapbender/http/php/mb_publish_wmc.php 2015-05-04 09:48:47 UTC (rev 9187)
+++ trunk/mapbender/http/php/mb_publish_wmc.php 2015-05-04 13:48:27 UTC (rev 9188)
@@ -1,9 +1,10 @@
<?php
/**
- * @version Changed: ### 2015-02-12 14:11:41 UTC ###
+ * @version Changed: ### 2015-04-27 12:31:33 UTC ###
* @author Raphael.Syed <raphael.syed at WhereGroup.com> http://WhereGroup.com
*/
+
require_once(dirname(__FILE__)."/../php/mb_validateSession.php");
// require_once(dirname(__FILE__) . "/../classes/class_wmc.php");
/**
@@ -11,33 +12,30 @@
*/
$user_id = Mapbender::session()->get("mb_user_id");
-$wmc_id = $_POST["wmc_id"];
+$wmc_serial_id = $_POST["wmc_serial_id"];
$dataMode = $_POST['mode'];
$dataLicense = $_POST['license'];
-
-if($dataMode === 'getLicenseMode'){
-
+if ($dataMode === 'getLicenseMode') {
$sql = 'SELECT symbollink,description,isopen FROM termsofuse WHERE name = $1';
$v = array($dataLicense);
$t = array("s");
header('Content-Type: application/json');
$res = db_prep_query($sql, $v, $t);
- while($row = db_fetch_array($res)){
+ while ($row = db_fetch_array($res)) {
echo json_encode($row);
}
-}else if($dataMode === 'saveLicenseMode'){
-
+} else if ($dataMode === 'saveLicenseMode') {
// #1 Update wmc_local_data_public
- $sql = 'UPDATE mb_user_wmc mb SET wmc_local_data_public = 1, wmc_local_data_fkey_termsofuse_id = t.termsofuse_id from termsofuse t WHERE mb.wmc_id = $1 AND mb.fkey_user_id = $2 AND t.name = $3';
- $v = array($wmc_id,$user_id,$dataLicense);
+ $sql = 'UPDATE mb_user_wmc mb SET wmc_local_data_public = 1, wmc_local_data_fkey_termsofuse_id = t.termsofuse_id from termsofuse t WHERE mb.wmc_serial_id = $1 AND mb.fkey_user_id = $2 AND t.name = $3';
+ $v = array($wmc_serial_id,$user_id,$dataLicense);
$t = array("s","i","s");
header('Content-Type: application/json');
$res = db_prep_query($sql, $v, $t);
-}else if($dataMode === 'getAllLicencesMode'){
+} else if ($dataMode === 'getAllLicencesMode') {
$openData_only = $_POST['openData_only'];
$resultArray = array();
$sql = 'SELECT name FROM termsofuse where isopen = $1';
@@ -46,12 +44,9 @@
$res = db_prep_query($sql, $v, $t);
header('Content-Type: application/json');
//echo db_fetch_array($res);
- while($row = db_fetch_array($res)){
+ while ($row = db_fetch_array($res)) {
$resultArray[]= $row;
}
echo json_encode($resultArray);
}
-
-
-?>
Modified: trunk/mapbender/http/php/mb_unpublish_wmc.php
===================================================================
--- trunk/mapbender/http/php/mb_unpublish_wmc.php 2015-05-04 09:48:47 UTC (rev 9187)
+++ trunk/mapbender/http/php/mb_unpublish_wmc.php 2015-05-04 13:48:27 UTC (rev 9188)
@@ -11,7 +11,7 @@
*/
$user_id = Mapbender::session()->get("mb_user_id");
-$wmc_id = $_POST["wmc_id"];
+$wmc_serial_id = $_POST["wmc_serial_id"];
// $wmc = new wmc();
@@ -19,11 +19,11 @@
-// $sql = 'UPDATE mb_user_wmc SET wmc_local_data_public=0, wmc_local_data_fkey_termsofuse_id =8 WHERE wmc_id= $1 AND fkey_user_id = $2'; // $sql = 'select wmc from mb_user_wmc where wmc_id = $1 and wmc_has_local_data = 1 and fkey_user_id = $2;';
-$sql = 'UPDATE mb_user_wmc SET wmc_local_data_public=0, wmc_local_data_fkey_termsofuse_id = null WHERE wmc_id= $1 AND fkey_user_id = $2'; // $sql = 'select wmc from mb_user_wmc where wmc_id = $1 and wmc_has_local_data = 1 and fkey_user_id = $2;';
-// $sql = 'SELECT wmc_local_data_public FROM mb_user_wmc WHERE wmc_id= $1 AND fkey_user_id = $2'; // $sql = 'select wmc from mb_user_wmc where wmc_id = $1 and wmc_has_local_data = 1 and fkey_user_id = $2;';
+// $sql = 'UPDATE mb_user_wmc SET wmc_local_data_public=0, wmc_local_data_fkey_termsofuse_id =8 WHERE wmc_serial_id= $1 AND fkey_user_id = $2'; // $sql = 'select wmc from mb_user_wmc where wmc_serial_id = $1 and wmc_has_local_data = 1 and fkey_user_id = $2;';
+$sql = 'UPDATE mb_user_wmc SET wmc_local_data_public=0, wmc_local_data_fkey_termsofuse_id = null WHERE wmc_serial_id= $1 AND fkey_user_id = $2'; // $sql = 'select wmc from mb_user_wmc where wmc_serial_id = $1 and wmc_has_local_data = 1 and fkey_user_id = $2;';
+// $sql = 'SELECT wmc_local_data_public FROM mb_user_wmc WHERE wmc_serial_id= $1 AND fkey_user_id = $2'; // $sql = 'select wmc from mb_user_wmc where wmc_serial_id = $1 and wmc_has_local_data = 1 and fkey_user_id = $2;';
-$v = array($wmc_id, $user_id);
+$v = array($wmc_serial_id, $user_id);
$t = array("i", "i");
$res = db_prep_query($sql, $v, $t);
Modified: trunk/mapbender/http/php/mod_GetPublishedData.php
===================================================================
--- trunk/mapbender/http/php/mod_GetPublishedData.php 2015-05-04 09:48:47 UTC (rev 9187)
+++ trunk/mapbender/http/php/mod_GetPublishedData.php 2015-05-04 13:48:27 UTC (rev 9188)
@@ -1,6 +1,5 @@
<?php
-
/**
* @version Changed: ### 2015-02-23 14:00:42 UTC ###
* @author Raphael.Syed <raphael.syed at WhereGroup.com> http://WhereGroup.com
@@ -18,32 +17,17 @@
// get data from session and request
$user_id = Mapbender::session()->get("mb_user_id");
-$wmc_id = $_GET["wmc_id"];
+$wmc_serial_id = $_GET["wmc_id"];
$outputFormat = $_GET['outputFormat'];
-$wmc_serial_id;
-
-//get the wmc_serial_id from database
-
-$sql = 'SELECT wmc_serial_id FROM mb_user_wmc WHERE wmc_id = $1';
-$v = array($wmc_id);
-$t = array("c");
-$res = db_prep_query($sql, $v, $t);
-//fetch the array
-if($row = db_fetch_array($res)){
-
- $wmc_serial_id = $row;
-
-}
-
//create a WMC object from a WMC in the database
-$xml = wmc::getDocument($wmc_serial_id[0]);
+$xml = wmc::getDocument($wmc_serial_id);
$myWmc = new wmc();
$myWmc->createFromXml($xml);
# Decode from JSON to array
-foreach ($myWmc->generalExtensionArray as $key => &$value) {
- $value = json_decode($value,true);
+foreach ($myWmc->generalExtensionArray as $key => &$value) {
+ $value = json_decode($value, true);
}
// create and numerically indexed array
$kmls = array_values($myWmc->generalExtensionArray["KMLS"]);
@@ -53,7 +37,6 @@
$file = "myDataCollection.".$outputFormat;
if (isset($kmls[0]["data"]['@context'])) {
-
$file = rawurldecode($kmls[0]["data"]['@context']['title']).'.'.$outputFormat;
}
@@ -65,15 +48,14 @@
$zipName = '../tmp/myDataCollection.zip';
$fileName = "myDataCollection.zip";
- if ($zip->open($zipName, ZIPARCHIVE::CREATE)=== TRUE) {
+ if ($zip->open($zipName, ZIPARCHIVE::CREATE) === true) {
// counting filenames
$fileCounter = 1;
// loop over every featureCollection
foreach ($kmls as $key => $value) {
-
$geoJson = $kmls[$fileCounter-1]["data"];
- $dataFile = createFile($outputFormat,$geoJson);
- $zip->addFromString('myFeatureCollection_'.$fileCounter.'.'.$outputFormat,$dataFile);
+ $dataFile = createFile($outputFormat, $geoJson);
+ $zip->addFromString('myFeatureCollection_'.$fileCounter.'.'.$outputFormat, $dataFile);
// increment the counter
$fileCounter +=1;
@@ -81,16 +63,16 @@
$zip->close();
}
-// set the headers to return a zip to the client
-header('Content-Type: application/zip');
-header('Content-Disposition: attachment; filename="'.$filename.'"');
-header('Content-Length: '.filesize($zipName) );
+ // set the headers to return a zip to the client
+ header('Content-Type: application/zip');
+ header('Content-Disposition: attachment; filename="'.$filename.'"');
+ header('Content-Length: '.filesize($zipName));
-readfile($zipName);
+ readfile($zipName);
-//remove zip from harddisk
-exec(escapeshellcmd('rm ../tmp/myDataCollection.zip'));
-die;
+ //remove zip from harddisk
+ exec(escapeshellcmd('rm ../tmp/myDataCollection.zip'));
+ die;
} else {
// set headers to force the download for a single file
@@ -100,7 +82,7 @@
header("Content-Type: application/download");
//parse the geojson from the array
$geoJson = $kmls[0]["data"];
- $dataFile = createFile($outputFormat,$geoJson);
+ $dataFile = createFile($outputFormat, $geoJson);
echo $dataFile;
}
@@ -111,12 +93,11 @@
* @param string $u the file location
* @return the gpx-file
*/
-function kml_to_gpx($u){
-
+function kml_to_gpx($u)
+{
$u_parts = pathinfo($u); //array of url parts
$u_ext = strtoupper($u_parts['extension']);
if ($u_ext== "KML") {
-
$dom_kml = new DOMDocument();
$dom_kml->load($u);
@@ -270,7 +251,7 @@
}
}
}
- foreach ($placemark->getElementsByTagName('Polygon') as $lineString) {
+ foreach ($placemark->getElementsByTagName('Polygon') as $lineString) {
foreach ($lineString->getElementsByTagName('coordinates') as $coordinates) {
//add the new track
$gpx_trk = $dom_gpx->createElement('trk');
@@ -323,66 +304,68 @@
* create a utcdate
* @return utcdate objekt]
*/
-function utcdate() {
+function utcdate()
+{
return gmdate("Y-m-d\Th:i:s\Z");
}
-function createFile ($outputFormat, $geoJson) {
+function createFile($outputFormat, $geoJson)
+{
// handle the different outputformat
-if ($outputFormat == 'geojson') {
- // create the json-file
- $geoJsonFile = '../tmp/myDataCollection.geojson';
- // put the contents to the created file
- file_put_contents($geoJsonFile, json_encode($geoJson));
- // create a string from the created file
- $fileString = file_get_contents('../tmp/myDataCollection.geojson');
- // return the string
- return $fileString;
- die;
+ if ($outputFormat == 'geojson') {
+ // create the json-file
+ $geoJsonFile = '../tmp/myDataCollection.geojson';
+ // put the contents to the created file
+ file_put_contents($geoJsonFile, json_encode($geoJson));
+ // create a string from the created file
+ $fileString = file_get_contents('../tmp/myDataCollection.geojson');
+ // return the string
+ return $fileString;
+ die;
-}elseif ($outputFormat == 'gpx') {
- //convert geojson to kml
+ } elseif ($outputFormat == 'gpx') {
+ //convert geojson to kml
- //create the geojson-file temporary
- $temp_geojson = '../tmp/myDataCollection.geojson';
- // write the file
- file_put_contents($temp_geojson,json_encode($geoJson));
- //transform the geojson to kml
- $unique = '../tmp/myDataCollection';
- $fGeojson = $unique.".geojson";
- $fKml = $unique.".kml";
- $pathOgr = '/usr/bin/ogr2ogr';
- // execute ogr2ogr
- $exec = $pathOgr.' -f KML '.$fKml.' '.$fGeojson;
- exec(escapeshellcmd($exec));
- //enter location of KML file here
- $kml = "../tmp/myDataCollection.kml";
- // create gpx from kml
- return kml_to_gpx($kml);
- die;
+ //create the geojson-file temporary
+ $temp_geojson = '../tmp/myDataCollection.geojson';
+ // write the file
+ file_put_contents($temp_geojson, json_encode($geoJson));
+ //transform the geojson to kml
+ $unique = '../tmp/myDataCollection';
+ $fGeojson = $unique.".geojson";
+ $fKml = $unique.".kml";
+ $pathOgr = '/usr/bin/ogr2ogr';
+ // execute ogr2ogr
+ $exec = $pathOgr.' -f KML '.$fKml.' '.$fGeojson;
+ exec(escapeshellcmd($exec));
+ //enter location of KML file here
+ $kml = "../tmp/myDataCollection.kml";
+ // create gpx from kml
+ return kml_to_gpx($kml);
+ die;
-}elseif ($outputFormat == 'kml') {
- //convert geojson to kml
+ } elseif ($outputFormat == 'kml') {
+ //convert geojson to kml
- //create the geojson-file temporary
- $temp_geojson = '../tmp/myDataCollection.geojson';
- // write the file
- file_put_contents($temp_geojson,json_encode($geoJson));
- //transform the file to kml
- $unique = '../tmp/myDataCollection';
- $fGeojson = $unique.".geojson";
- $fKml = $unique.".kml";
- $pathOgr = '/usr/bin/ogr2ogr';
- //execute ogr2ogr to transfrom json to kml
- $exec = $pathOgr.' -f KML '.$fKml.' '.$fGeojson;
- exec(escapeshellcmd($exec));
- // create string from kml-file
- $fileString = file_get_contents('../tmp/myDataCollection.kml');
- //return the string
+ //create the geojson-file temporary
+ $temp_geojson = '../tmp/myDataCollection.geojson';
+ // write the file
+ file_put_contents($temp_geojson, json_encode($geoJson));
+ //transform the file to kml
+ $unique = '../tmp/myDataCollection';
+ $fGeojson = $unique.".geojson";
+ $fKml = $unique.".kml";
+ $pathOgr = '/usr/bin/ogr2ogr';
+ //execute ogr2ogr to transfrom json to kml
+ $exec = $pathOgr.' -f KML '.$fKml.' '.$fGeojson;
+ exec(escapeshellcmd($exec));
+ // create string from kml-file
+ $fileString = file_get_contents('../tmp/myDataCollection.kml');
+ //return the string
- return $fileString;
- die;
-}
+ return $fileString;
+ die;
+ }
}
Modified: trunk/mapbender/http/php/uploadKml.php
===================================================================
--- trunk/mapbender/http/php/uploadKml.php 2015-05-04 09:48:47 UTC (rev 9187)
+++ trunk/mapbender/http/php/uploadKml.php 2015-05-04 13:48:27 UTC (rev 9188)
@@ -39,7 +39,6 @@
echo $content;
}
if(preg_match('/.(xml|gpx)$/', $_FILES['kml']['name'])) {
- // var_dump($content);die;
header("Content-Type: text/plain");
echo $content;
}
Modified: trunk/mapbender/http/plugins/kmlTree.js
===================================================================
--- trunk/mapbender/http/plugins/kmlTree.js 2015-05-04 09:48:47 UTC (rev 9187)
+++ trunk/mapbender/http/plugins/kmlTree.js 2015-05-04 13:48:27 UTC (rev 9188)
@@ -652,6 +652,9 @@
$featureList = $("<ul />");
$kmlEntry.append($featureList);
+ var pointCount = 1;
+ var polygonCount = 1;
+ var linestringCount = 1;
for (var i = obj.data.features.length - 1; i >= 0; --i) { //FIXME: for feature without the "type: FeatureCollection ",change functionallity: if (obj.data.type == "Feature") ...
var multi = obj.data.features[i].geometry.type.match(/^Multi/i);
var toggle = '';
@@ -668,8 +671,23 @@
title = obj.data.features[i].properties.title;
} else {
- title = 'Title undefined';
+ switch (obj.data.features[i].geometry.type){
+ case 'Point':
+ title = 'point_'+pointCount;
+ pointCount += 1;
+ break;
+ case 'Polygon':
+ title = 'polygon_'+polygonCount;
+ polygonCount += 1;
+ break;
+ case 'LineString':
+ title = 'linestring_'+linestringCount;
+ linestringCount += 1;
+ break;
+ }
+ // title = 'Title undefined';
+
}
// title = obj.data.features[i].properties.name;
Modified: trunk/mapbender/http/plugins/mb_digitize_widget.php
===================================================================
--- trunk/mapbender/http/plugins/mb_digitize_widget.php 2015-05-04 09:48:47 UTC (rev 9187)
+++ trunk/mapbender/http/plugins/mb_digitize_widget.php 2015-05-04 13:48:27 UTC (rev 9188)
@@ -757,8 +757,11 @@
});
feature.properties.updated = new Date().toISOString();
//get parent and change updated
- $('#mapframe1').data('kml')._kmls[url].data['@context'].updated = new Date().toISOString();
+ if($('#mapframe1').data('kml')._kmls[url].data.hasOwnProperty('@context')){
+ $('#mapframe1').data('kml')._kmls[url].data['@context'].updated = new Date().toISOString();
+ }
+
attributesDialog.dialog('close');
editDialog.dialog('close');
kml.refresh(url);
Modified: trunk/mapbender/lib/mb.ui.displayKmlFeatures.js
===================================================================
--- trunk/mapbender/lib/mb.ui.displayKmlFeatures.js 2015-05-04 09:48:47 UTC (rev 9187)
+++ trunk/mapbender/lib/mb.ui.displayKmlFeatures.js 2015-05-04 13:48:27 UTC (rev 9188)
@@ -481,10 +481,18 @@
},
complete: function() {
if (icon == "false" || icon === false) {
- multi.e.setElement("Mapbender:iconOffsetX", -10);
- multi.e.setElement("Mapbender:iconOffsetY", -34);
- multi.e.setElement("Mapbender:icon", "../img/marker/red.png");
+ multi.e.setElement("iconOffsetX", -10);
+ multi.e.setElement("iconOffsetY", -34);
+ multi.e.setElement("icon", "../img/marker/red.png");
}
+ multi.e.setElement("marker-size", 34); // default value 'medium, small, large' is not allowed from canvas
+ multi.e.setElement("marker-symbol", "");
+ multi.e.setElement("marker-color", "#7e7e7e");
+ multi.e.setElement("stroke", "#555555");
+ multi.e.setElement("stroke-opacity", 1.0);
+ multi.e.setElement("stroke-width", 2);
+ multi.e.setElement("fill", "#555555");
+ multi.e.setElement("fill-opacity", 0.5);
var feat = JSON.parse(multi.toString());
itm.data.features.push(feat);
More information about the Mapbender_commits
mailing list