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

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Mon Jan 14 06:07:29 EST 2008


Author: christoph
Date: 2008-01-14 06:07:29 -0500 (Mon, 14 Jan 2008)
New Revision: 1957

Modified:
   trunk/mapbender/http/javascripts/geometry.js
Log:
added epsg 

Modified: trunk/mapbender/http/javascripts/geometry.js
===================================================================
--- trunk/mapbender/http/javascripts/geometry.js	2008-01-10 16:55:18 UTC (rev 1956)
+++ trunk/mapbender/http/javascripts/geometry.js	2008-01-14 11:07:29 UTC (rev 1957)
@@ -10,124 +10,6 @@
 var nameMultiGeometry = "MultiGeometry";
 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".
  *
@@ -347,6 +229,8 @@
 
 GeometryArray.prototype.importGeoJSON = function (geoJSON) {
 	var isFeatureCollection = false;
+	var featureCollectionEpsg = 4326;
+	var featureEpsg = 4326;
 	//
 	// FEATURE COLLECTION
 	//
@@ -356,14 +240,20 @@
 				case "type":
 					isFeatureCollection = (geoJSON[i] == "FeatureCollection") ? true : false;
 					break;
+				case "crs":
+					if (geoJSON[i]["type"] == "EPSG") {
+						featureCollectionEpsg = geoJSON[i]["properties"];
+						featureEpsg = geoJSON[i]["properties"];
+					}
+					break;
 				case "features":
 					if (isFeatureCollection) {
-						
 						//
 						// FEATURE
 						//
 						var featureArray = geoJSON[i];
 						for (var j = 0; j < featureArray.length; j++) {
+							featureEpsg = featureCollectionEpsg;
 							var currentFeature = featureArray[j];
 							var isFeature = false;
 
@@ -389,6 +279,12 @@
 											}
 											break;
 										
+										case "crs":
+											if (currentFeature[k]["type"] == "EPSG") {
+												featureEpsg = currentFeature[k]["properties"];
+											}
+											break;
+
 										case "geometry":
 											if (isFeature) {
 
@@ -399,56 +295,60 @@
 												for (var l in currentGeometry) {
 													
 													if (typeof(currentGeometry[l]) != "function") {
-														if (l == "type") {
-															geometrytype = currentGeometry[l];
-														}
-														else if (l == "coordinates") {
-															var coordinates = currentGeometry[l];
-															switch (geometrytype) {
-																case "Point":
-																	//
-																	// POINT
-																	//
-																	this.addMember(geomType.point);
+														switch (l) {
+															case "type":
+																geometrytype = currentGeometry[l];
+																break;
+															case "coordinates":
+																var coordinates = currentGeometry[l];
+																switch (geometrytype) {
+																	case "Point":
+																		//
+																		// POINT
+																		//
+																		this.addMember(geomType.point);
+																		
+																		this.get(-1).addGeometry();
+																		this.getGeometry(-1,-1).addPointByCoordinates(coordinates[0], coordinates[1]);
+																		this.getGeometry(-1,-1).setEpsg(featureEpsg);
+																		this.close();
+																		break;
 																	
-																	this.get(-1).addGeometry();
-																	this.getGeometry(-1,-1).addPointByCoordinates(coordinates[0], coordinates[1]);
-																	this.close();
-																	break;
-																
-																case "LineString":
-																	//
-																	// LINESTRING
-																	//
-																	this.addMember(geomType.line);
-																	this.get(-1).addGeometry();
-																	for (var m = 0; m < coordinates.length; m++) {
-																		var currentPoint = coordinates[m];
-																		this.getGeometry(-1,-1).addPointByCoordinates(currentPoint[0], currentPoint[1]);
-																	}
-																	this.close();
-																	break;
-																
-																case "Polygon":
-																	//
-																	// POLYGON
-																	//
-																	this.addMember(geomType.polygon);
-																	for (var m = 0; m < coordinates.length; m++) {
+																	case "LineString":
+																		//
+																		// LINESTRING
+																		//
+																		this.addMember(geomType.line);
 																		this.get(-1).addGeometry();
-																		var currentPolygon = coordinates[m];
-																		for (var n = 0; n < currentPolygon.length; n++) {
-																			var currentPoint = currentPolygon[n];
+																		for (var m = 0; m < coordinates.length; m++) {
+																			var currentPoint = coordinates[m];
 																			this.getGeometry(-1,-1).addPointByCoordinates(currentPoint[0], currentPoint[1]);
 																		}
-																	}
-																	this.close();
-																	break;
-						
-																case "GeometryCollection":
-																	var exc = new Mb_exception("Geometry: GeometryCollections are not yet supported");
-																	break;
-															}
+																		this.getGeometry(-1,-1).setEpsg(featureEpsg);
+																		this.close();
+																		break;
+																	
+																	case "Polygon":
+																		//
+																		// POLYGON
+																		//
+																		this.addMember(geomType.polygon);
+																		for (var m = 0; m < coordinates.length; m++) {
+																			this.get(-1).addGeometry();
+																			var currentPolygon = coordinates[m];
+																			for (var n = 0; n < currentPolygon.length; n++) {
+																				var currentPoint = currentPolygon[n];
+																				this.getGeometry(-1,-1).addPointByCoordinates(currentPoint[0], currentPoint[1]);
+																			}
+																		}
+																		this.getGeometry(-1,-1).setEpsg(featureEpsg);
+																		this.close();
+																		break;
+							
+																	case "GeometryCollection":
+																		var exc = new Mb_exception("Geometry: GeometryCollections are not yet supported");
+																		break;
+																}
 														}
 													}
 												}
@@ -464,7 +364,8 @@
 }
 
 GeometryArray.prototype.toString = function () {
-	var str = "{\"type\": \"FeatureCollection\", \"features\": [";
+	var str = "{\"type\": \"FeatureCollection\", ";
+	str += "\"features\": [";
 
 	// separate: geometries that are from a KML and those which are not
 	var multiGeometriesFromKml = [];
@@ -712,8 +613,14 @@
 };
 
 MultiGeometry.prototype.toStringWithoutProperties = function () {
-	var str = "{\"type\": \"Feature\", \"geometry\": ";
+	var str = "{\"type\": \"Feature\", ";
 
+	var epsg = this.getEpsg();
+	if (epsg) {
+		str = "\"crs\": {\"type\": \"EPSG\", \"properties\": {\"code\": " + epsg + "}}, ";
+	}
+	str += "\"geometry\": ";
+
 	// geometries
 	for (var i = 0, len = this.count(); i < len; i++) {
 		if (i > 0) {
@@ -726,7 +633,18 @@
 	return str;
 };
 
+/**
+ * @return the EPSG code of this geometry.
+ * @type integer
+ */
+MultiGeometry.prototype.getEpsg = function () {
+	if (this.count() > 0) {
+		return this.get(0).getEpsg;
+	}
+	return false;
+}
 
+
 /**
  * @class a Geometry is a List of Point objects. If it is a polygon, the last point has 
  * to equal the first point.
@@ -899,11 +817,38 @@
 		return complete;
 	};
 	
+	/**
+	 * Sets the EPSG of this geometry.
+	 * 
+	 * @param {Integer} someEpsg the EPSG of this geometry.
+	 * @return true if the EPSG could be set; else false
+	 * @type boolean
+	 */
+	this.setEpsg = function (someEpsg) {
+		// TODO: how to check if EPSG code is correct?
+		if (typeof(someEpsg) == "number") {
+			epsg = someEpsg;
+			return true;
+		}
+		var e = new Mb_exception("EPSG code not valid ("+someEpsg+")");
+		return false;
+	};
+	
+	/**
+	 * @return the EPSG code of this geometry.
+	 * @type integer
+	 */
+	this.getEpsg = function () {
+		return epsg;
+	}
+	
 	this.list = [];
 	var dist = [];
 	var totaldist = [];
 	var complete = false;
 
+	var epsg;
+
 	var that = this;
 
 	this.geomType = aGeomtype;
@@ -971,8 +916,14 @@
 
 Geometry.prototype.toString = function () {
 	var str = "";
+	
+	var epsgStr = "";
+	if (epsg) {
+		epsgStr = "\"crs\": {\"type\": \"EPSG\", \"properties\": {\"code\": " + epsg + "}}, ";
+	}
+	
 	if (this.geomType == geomType.polygon) {
-		str += "{\"type\": \"Polygon\", \"coordinates\": [[";
+		str += "{\"type\": \"Polygon\", " + epsgStr + "\"coordinates\": [[";
 		for (var i = 0; i < this.count(); i++) {
 			if (i > 0) {
 				str += ", ";
@@ -982,7 +933,7 @@
 		str += "]]}";
 	}
 	else if (this.geomType == geomType.line) {
-		str += "{\"type\": \"LineString\", \"coordinates\": [";
+		str += "{\"type\": \"LineString\", " + epsgStr + "\"coordinates\": [";
 		for (var i = 0; i < this.count(); i++) {
 			if (i > 0) {
 				str += ", ";
@@ -992,7 +943,7 @@
 		str += "]}";
 	}
 	else if (this.geomType == geomType.point) {
-		str += "{\"type\": \"Point\", \"coordinates\": ";
+		str += "{\"type\": \"Point\", " + epsgStr + "\"coordinates\": ";
 		str += this.get(0).toString();
 		str += "}";
 	}



More information about the Mapbender_commits mailing list