[OpenLayers-Commits] r3156 - sandbox/tschaub/geojson/lib/OpenLayers/Format

commits at openlayers.org commits at openlayers.org
Thu May 17 14:01:20 EDT 2007


Author: tschaub
Date: 2007-05-17 14:01:17 -0400 (Thu, 17 May 2007)
New Revision: 3156

Modified:
   sandbox/tschaub/geojson/lib/OpenLayers/Format/GeoJSON.js
Log:
add write support for multi-point, linestring, and polygon

Modified: sandbox/tschaub/geojson/lib/OpenLayers/Format/GeoJSON.js
===================================================================
--- sandbox/tschaub/geojson/lib/OpenLayers/Format/GeoJSON.js	2007-05-17 17:03:07 UTC (rev 3155)
+++ sandbox/tschaub/geojson/lib/OpenLayers/Format/GeoJSON.js	2007-05-17 18:01:17 UTC (rev 3156)
@@ -72,11 +72,11 @@
         if(!(obj.coordinates instanceof Array)) {
             throw "Geometry must have coordinates array: " + obj;
         }
-        if(!this.parseCoords[obj.type]) {
+        if(!this.parseCoords[obj.type.toLowerCase()]) {
             throw "Unsupported geometry type: " + obj.type;
         }
         try {
-            geometry = this.parseCoords[obj.type].apply(this, [obj.coordinates]);
+            geometry = this.parseCoords[obj.type.toLowerCase()].apply(this, [obj.coordinates]);
         } catch(err) {
             // deal with bad coordinates
             throw err;
@@ -96,13 +96,14 @@
          * @returns {OpenLayers.Geometry} A geometry
          * @private
          */
-        "Point": function(array) {
+        "point": function(array) {
             if(array.length != 1) {
                 throw "Bad point coordinates: " + array;
             }
             if(array[0].length != 2) {
                 throw "Only 2D points are supported: " + array[0];
             }
+            // let this [[x, y]] be gone please
             return new OpenLayers.Geometry.Point(array[0][0], array[0][1]);
         },
         
@@ -112,13 +113,29 @@
          * @returns {OpenLayers.Geometry} A geometry
          * @private
          */
-        "Line": function(array) {
+        "multipoint": function(array) {
             var points = [];
             var p = null;
             for(var i=0; i<array.length; ++i) {
-                p = this.parseCoords["Point"].apply(this, [[array[i]]]);
+                p = this.parseCoords["point"].apply(this, [[array[i]]]);
                 points.push(p);
             }
+            return new OpenLayers.Geometry.MultiPoint(points);
+        },
+
+        /**
+         * Convert a coordinate array from GeoJSON into an OpenLayers.Geometry.
+         * @param {Object} array The coordinates array from the GeoJSON fragment
+         * @returns {OpenLayers.Geometry} A geometry
+         * @private
+         */
+        "linestring": function(array) {
+            var points = [];
+            var p = null;
+            for(var i=0; i<array.length; ++i) {
+                p = this.parseCoords["point"].apply(this, [[array[i]]]);
+                points.push(p);
+            }
             return new OpenLayers.Geometry.LineString(points);
         },
         
@@ -128,16 +145,49 @@
          * @returns {OpenLayers.Geometry} A geometry
          * @private
          */
-        "Polygon": function(array) {
+        "multilinestring": function(array) {
+            var lines = [];
+            var l = null;
+            for(var i=0; i<array.length; ++i) {
+                l = this.parseCoords["linestring"].apply(this, [array[i]]);
+                lines.push(l);
+            }
+            return new OpenLayers.Geometry.MultiLineString(lines);
+        },
+        
+        /**
+         * Convert a coordinate array from GeoJSON into an OpenLayers.Geometry.
+         * @param {Object} array The coordinates array from the GeoJSON fragment
+         * @returns {OpenLayers.Geometry} A geometry
+         * @private
+         */
+        "polygon": function(array) {
             var rings = [];
             var r, l;
             for(var i=0; i<array.length; ++i) {
-                l = this.parseCoords["Line"].apply(this, [array[i]]);
+                l = this.parseCoords["linestring"].apply(this, [array[i]]);
                 r = new OpenLayers.Geometry.LinearRing(l.components);
                 rings.push(r);
             }
             return new OpenLayers.Geometry.Polygon(rings);
+        },
+        
+        /**
+         * Convert a coordinate array from GeoJSON into an OpenLayers.Geometry.
+         * @param {Object} array The coordinates array from the GeoJSON fragment
+         * @returns {OpenLayers.Geometry} A geometry
+         * @private
+         */
+        "multipolygon": function(array) {
+            var polys = [];
+            var p = null;
+            for(var i=0; i<array.length; ++i) {
+                p = this.parseCoords["polygon"].apply(this, [array[i]]);
+                polys.push(p);
+            }
+            return new OpenLayers.Geometry.MultiPolygon(polys);
         }
+
     },
 
     /**
@@ -158,10 +208,8 @@
             geometry = feature.geometry;
             type = geometry.CLASS_NAME.split('.')[2];
             data = this.extract[type.toLowerCase()].apply(this, [geometry]);
-            if (type == "LineString") {
-                type = "Line";
-            }
-            if (type == "Point") {
+            // please let this go away
+            if(type == "Point") {
                 data = [data];
             }    
             array.push({
@@ -208,7 +256,7 @@
         },
         
         /**
-         * Return an array of coordinate arrays from a line.
+         * Return an array of coordinate arrays from a linestring.
          * @param {OpenLayers.Geometry.LineString} linestring
          * @returns {Array} An array of coordinate arrays representing
          *                  the linestring
@@ -222,8 +270,8 @@
         },
 
         /**
-         * Return an array of linestring arrays from a line.
-         * @param {OpenLayers.Geometry.MultiLineString} line
+         * Return an array of linestring arrays from a linestring.
+         * @param {OpenLayers.Geometry.MultiLineString} linestring
          * @returns {Array} An array of linestring arrays representing
          *                  the multilinestring
          */



More information about the Commits mailing list