[Mapbender-commits] r1867 - trunk/mapbender/http/javascripts

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Wed Nov 28 12:28:38 EST 2007


Author: christoph
Date: 2007-11-28 12:28:38 -0500 (Wed, 28 Nov 2007)
New Revision: 1867

Modified:
   trunk/mapbender/http/javascripts/geometry.js
Log:
new kml features

Modified: trunk/mapbender/http/javascripts/geometry.js
===================================================================
--- trunk/mapbender/http/javascripts/geometry.js	2007-11-28 16:39:53 UTC (rev 1866)
+++ trunk/mapbender/http/javascripts/geometry.js	2007-11-28 17:28:38 UTC (rev 1867)
@@ -11,6 +11,123 @@
 var nameGeometry = "Geometry";
 
 
+function geoJsonToGeometryArray(geoJSON) {
+	var geomArray;
+	isFeatureCollection = false;
+	/*
+	 * FEATURE COLLECTION
+	 */
+//	console.profile();
+	for (var i in geoJSON) {
+		if (typeof(geoJSON[i]) != "function") {
+			if (i == "type" && geoJSON[i] == "FeatureCollection") {
+				geomArray = new GeometryArray();
+				isFeatureCollection = true;
+//				console.log("FeatureCollection");
+			}
+			if (i == "features" && isFeatureCollection) {
+				/*
+				 * FEATURES
+				 */
+				var featureArray = geoJSON[i];
+				for (var j = 0; j < featureArray.length; j++) {
+					var currentFeature = featureArray[j];
+					var isFeature = false;
+					for (var k in currentFeature) {
+						if (typeof(currentFeature[k]) != "function") {
+							var geometrytype;
+							if (k == "type" && currentFeature[k] == "Feature") {
+								isFeature = true;
+//								console.log("Feature");
+							}
+							if (k == "properties") {
+								var properties = currentFeature[k];
+								
+								// NOT YET IMPLEMENTED
+								if (geometrytype != "GeometryCollection") {
+									for (var l in properties) {
+										if (typeof(properties[l]) != "function") {
+											geomArray.get(-1).e.setElement(l, properties[l]);
+										}
+									}
+								}
+							}
+							if (k == "geometry" && isFeature) {
+								/*
+								 * GEOMETRY
+								 */
+								var currentGeometry = currentFeature[k];
+								for (var l in currentGeometry) {
+									
+									if (typeof(currentGeometry[l]) != "function") {
+										if (l == "type") {
+											geometrytype = currentGeometry[l];
+										}
+										else if (l == "coordinates") {
+											var coordinates = currentGeometry[l];
+											if (geometrytype == "Point") {
+//												console.log("Point");
+												/*
+												 * POINT
+												 */
+												geomArray.addMember(geomType.point);
+												
+												geomArray.get(-1).addGeometry();
+												geomArray.getGeometry(-1,-1).addPointByCoordinates(coordinates[0], coordinates[1]);
+//												console.log("Coordinates %s", coordinates);
+												geomArray.close();
+											}
+											else if (geometrytype == "LineString") {
+												/*
+												 * LINESTRING
+												 */
+												geomArray.addMember(geomType.line);
+												geomArray.get(-1).addGeometry();
+												for (var m = 0; m < coordinates.length; m++) {
+													var currentPoint = coordinates[m];
+													geomArray.getGeometry(-1,-1).addPointByCoordinates(currentPoint[0], currentPoint[1]);
+//													console.log("Coordinates %s", currentPoint);
+												}
+												geomArray.close();
+											}
+											else if (geometrytype == "Polygon") {
+//												console.log("Polygon");
+												/*
+												 * POLYGON
+												 */
+												geomArray.addMember(geomType.polygon);
+												for (var m = 0; m < coordinates.length; m++) {
+													geomArray.get(-1).addGeometry();
+													var currentPolygon = coordinates[m];
+													for (var n = 0; n < currentPolygon.length; n++) {
+														var currentPoint = currentPolygon[n];
+														geomArray.getGeometry(-1,-1).addPointByCoordinates(currentPoint[0], currentPoint[1]);
+//														console.log("Coordinates %s", currentPoint);
+													}
+												}
+												geomArray.close();
+											}
+											// NOT YET IMPLEMENTED
+											else if (geometrytype == "GeometryCollection") {
+												//var exc = new Mb_exception("Geometry: GeometryCollections are not yet supported");
+											}
+										}
+									}
+								}
+							}
+						}
+					}
+				}
+			}
+		}
+	}
+//	console.profileEnd();
+	return geomArray;
+}
+
+function () {
+}
+
 /**
  * @class A class representing geometry types "polygon", "line" and "point".
  *
@@ -90,7 +207,8 @@
  * @type Geometry 
  */
 GeometryArray.prototype.getGeometry = function(i,j){
-	return this.get(i).get(j);
+	var tmp = this.get(i);
+	return tmp.get(j);
 };
 
 /**
@@ -191,7 +309,92 @@
 		this.get(i).updateAllPointsLike(oldP, newP);
 	}
 };
+
+GeometryArray.prototype.placemarkToString = function (placemarkId) {
+	var str = "{\"type\": \"Feature\", \"geometry\": ";
+
+	// get geometries with placemarkId
+	var geometriesFromPlacemark = [];
+	for (var i = 0, len = this.count(); i < len; i++) {
+		if (this.get(i).isFromKml() && this.get(i).e.getElementValueByName("Mapbender:placemarkId") == placemarkId) {
+			geometriesFromPlacemark.push(i);
+		}
+	}	
+
+	if (geometriesFromPlacemark.length > 1) {
+		str += "{\"type\": \"GeometryCollection\", \"geometries\": [";
+		for (var i = 0; i < geometriesFromPlacemark.length; i++) {
+			if (i > 0) {
+				str += ",";
+			}	
+			str += this.get(geometriesFromPlacemark[i]).placemarkToString();
+		}
+		str += "]}";
+
+		// metadata is the same for all indices...get from index 0
+		var propString = this.get(geometriesFromPlacemark[0]).e.toString();
+		if (propString) {
+			str += "," + propString;
+		}
+	}
+	else if (geometriesFromPlacemark.length === 1) {
+		str += this.get(geometriesFromPlacemark[0]).placemarkToString();
+	}
+
+	str += "}";
+	return str;
+}
+
+GeometryArray.prototype.toString = function () {
+	var str = "{\"type\": \"FeatureCollection\", \"features\": [";
+
+	// separate: geometries that are from a KML and those which are not
+	var multiGeometriesFromKml = [];
+	var multiGeometriesNotFromKml = [];
+	for (var i = 0, len = this.count(); i < len; i++) {
+		if (this.get(i).isFromKml()) {
+			var placemarkId = this.get(i).e.getElementValueByName("Mapbender:placemarkId");
+
+			// only add placemark ids once!
+			for (var j = 0; j < multiGeometriesFromKml && isFound === false; j++) {
+				var isFound = false;
+				if (multiGeometriesFromKml == placemarkId) {
+					isFound == true;
+				}
+			}
+			if (!isFound) {
+				multiGeometriesFromKml.push(placemarkId);
+			}
+		}
+		else {
+			multiGeometriesNotFromKml.push(i);
+		}
+	}
+
+	// add geometries not from KML
+	for (var i = 0, len = multiGeometriesNotFromKml.length; i < len; i++) {
+		if (i > 0) {
+			str += ",";
+		}		
+		str += this.get(multiGeometriesNotFromKml[i]).toString();
+	}
 	
+	// add geometries from KML
+	if (multiGeometriesNotFromKml.length > 0 && multiGeometriesFromKml.length > 0) {
+		str += ",";
+	}
+
+	for (var i=0; i < multiGeometriesFromKml.length; i++) {
+		if (i > 0) {
+			str += ",";
+		}	
+		str += this.placemarkToString();
+	}
+
+	str += "]}";
+	return str;
+}
+	
 /**
  * @class a MultiGeometry is a List of Geometry objects
  *
@@ -357,7 +560,55 @@
 	return true;
 };
 
+MultiGeometry.prototype.isFromKml = function () {
+	if (this.e.getElementValueByName("Mapbender:kml")) {
+		return true;
+	}
+	return false;
+}
 
+MultiGeometry.prototype.toString = function () {
+	var str = this.toStringWithoutProperties();
+	
+	// properties
+	var propString = this.e.toString();
+	if (propString) {
+		str += "," + propString;
+	}
+
+	str += "}";
+	
+	return str;
+};
+
+MultiGeometry.prototype.placemarkToString = function () {
+	var str = "";
+	// geometries
+	for (var i = 0, len = this.count(); i < len; i++) {
+		if (i > 0) {
+			str += ",";
+		}		
+		str += this.get(i).toString();
+	}
+	return str;
+};
+
+MultiGeometry.prototype.toStringWithoutProperties = function () {
+	var str = "{\"type\": \"Feature\", \"geometry\": ";
+
+	// geometries
+	for (var i = 0, len = this.count(); i < len; i++) {
+		if (i > 0) {
+			str += ",";
+		}		
+		str += this.get(i).toString();
+	}
+	str += "}";
+	
+	return str;
+};
+
+
 /**
  * @class a Geometry is a List of Point objects. If it is a polygon, the last point has 
  * to equal the first point.
@@ -407,8 +658,9 @@
 	 * @param {Float} y y value of the point
 	 */	
 	this.addPointByCoordinates = function(x,y){
-		this.add(new Point(x,y));
-		updateDist();
+		var newPoint = new Point(x,y);
+		this.add(newPoint);
+//		updateDist();
 	};
 
 	/**
@@ -461,12 +713,15 @@
 	 * @private
 	 */	
 	var updateDist = function(){
+/*
+ * DISABLED BECAUSE IT IS TOO SLOW
 		dist[0] = 0;		
 		totaldist[0] = 0;		
 		for (var i = 1 ; i < that.count(); i++){
 			dist[i] = that.get(i-1).dist(that.get(i));
 			totaldist[i] = totaldist[i-1] + dist[i];
 		}
+*/
 	};
 	/**
 	 * gets the distance between the last and last but one point of this {@link Geometry}.
@@ -596,7 +851,39 @@
 	return true;
 };
 
+Geometry.prototype.toString = function () {
+	var str = "";
+	if (this.geomType == geomType.polygon) {
+		str += "{\"type\": \"Polygon\", \"coordinates\": [[";
+		for (var i = 0; i < this.count(); i++) {
+			if (i > 0) {
+				str += ", ";
+			}
+			str += this.get(i).toString();
+		}
+		str += "]]}";
+	}
+	else if (this.geomType == geomType.line) {
+		str += "{\"type\": \"LineString\", \"coordinates\": [";
+		for (var i = 0; i < this.count(); i++) {
+			if (i > 0) {
+				str += ", ";
+			}
+			str += this.get(i).toString();
+		}
+		str += "]}";
+	}
+	else if (this.geomType == geomType.point) {
+		str += "{\"type\": \"Point\", \"coordinates\": ";
+		str += this.get(0).toString();
+		str += "}";
+	}
+	
+	return str;
+}
 
+
+
 /**
  * @class an array of elements, each consisting of a name/value pair
  *
@@ -676,7 +963,7 @@
  * @type Integer, Boolean
  */
 Wfs_element.prototype.getElementIndexByName = function(elementName){
-	for (var j = 0 ; j < this.count() ; j++){
+	for (var j = 0, len = this.count() ; j < len ; j++){
 		if (this.getName(j) == elementName) {return j;}
 	}
 	return false;
@@ -695,6 +982,23 @@
 	return this.getValue(i);
 };
 
+Wfs_element.prototype.toString = function () {
+	var str = "";
+	if (this.count() > 0) {
+		str += "\"properties\": {";
+		
+		for (i = 0, len = this.count(); i < len; i++) {
+			if (i > 0) {
+				str += ",";
+			}		
+			var key = this.getName(i);
+			var value = this.getValue(i);
+			str += "\"" + key + "\":\"" + value + "\"";
+		}
+		str += "}";
+	}
+	return str;
+}
 
 /**
  * @class a {@link Canvas} contains a {@link DivTag} that holds graphics rendered by {@link jsGraphics}
@@ -928,6 +1232,36 @@
 		}
 	};
 
+	this.setMouseOver = function (callback) {
+		for (var i=0; i<targets.length; i++){
+			if (typeof(canvas[i]) !== 'undefined') {
+				canvas[i].canvasDivTag.getTag().onmouseover = function () {
+					callback();
+				}
+			}
+		}
+	}
+	
+	this.setMouseOut = function (callback) {
+		for (var i=0; i<targets.length; i++){
+			if (typeof(canvas[i]) !== 'undefined') {
+				canvas[i].canvasDivTag.getTag().onmouseout = function () {
+					callback();
+				}
+			}
+		}
+	}
+	
+	this.setMouseClick = function (callback) {
+		for (var i=0; i<targets.length; i++){
+			if (typeof(canvas[i]) !== 'undefined') {
+				canvas[i].canvasDivTag.getTag().onclick = function () {
+					callback();
+				}
+			}
+		}
+	}
+	
 	var lineWidth = aLineWidth;
 	var tagname = 'mod_gaz_draw'+aTagName;
 	var style = aStyle;



More information about the Mapbender_commits mailing list