[OpenLayers-Commits] r10971 - in trunk/openlayers: lib/OpenLayers/Format tests/Format

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Thu Dec 16 14:14:44 EST 2010


Author: tschaub
Date: 2010-12-16 11:14:44 -0800 (Thu, 16 Dec 2010)
New Revision: 10971

Modified:
   trunk/openlayers/lib/OpenLayers/Format/WKT.js
   trunk/openlayers/tests/Format/WKT.html
Log:
Serializing features with OpenLayers.Geometry.Collection geometries as GEOMETRYCOLLECTION in WKT. p=strk, r=me (closes #2706).

Modified: trunk/openlayers/lib/OpenLayers/Format/WKT.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Format/WKT.js	2010-12-16 17:22:44 UTC (rev 10970)
+++ trunk/openlayers/lib/OpenLayers/Format/WKT.js	2010-12-16 19:14:44 UTC (rev 10971)
@@ -95,7 +95,7 @@
      */
     write: function(features) {
         var collection, geometry, type, data, isCollection;
-        if(features.constructor == Array) {
+        if (features.constructor == Array) {
             collection = features;
             isCollection = true;
         } else {
@@ -103,31 +103,45 @@
             isCollection = false;
         }
         var pieces = [];
-        if(isCollection) {
+        if (isCollection) {
             pieces.push('GEOMETRYCOLLECTION(');
         }
-        for(var i=0, len=collection.length; i<len; ++i) {
-            if(isCollection && i>0) {
+        for (var i=0, len=collection.length; i<len; ++i) {
+            if (isCollection && i>0) {
                 pieces.push(',');
             }
             geometry = collection[i].geometry;
-            type = geometry.CLASS_NAME.split('.')[2].toLowerCase();
-            if(!this.extract[type]) {
-                return null;
-            }
-            if (this.internalProjection && this.externalProjection) {
-                geometry = geometry.clone();
-                geometry.transform(this.internalProjection, 
-                                   this.externalProjection);
-            }                       
-            data = this.extract[type].apply(this, [geometry]);
-            pieces.push(type.toUpperCase() + '(' + data + ')');
+            pieces.push(this.extractGeometry(geometry));
         }
-        if(isCollection) {
+        if (isCollection) {
             pieces.push(')');
         }
         return pieces.join('');
     },
+
+    /**
+     * Method: extractGeometry
+     * Entry point to construct the WKT for a single Geometry object.
+     *
+     * Parameters:
+     * geometry - {<OpenLayers.Geometry.Geometry>}
+     *
+     * Returns:
+     * {String} A WKT string of representing the geometry
+     */
+    extractGeometry: function(geometry) {
+        var type = geometry.CLASS_NAME.split('.')[2].toLowerCase();
+        if (!this.extract[type]) {
+            return null;
+        }
+        if (this.internalProjection && this.externalProjection) {
+            geometry = geometry.clone();
+            geometry.transform(this.internalProjection, this.externalProjection);
+        }                       
+        var wktType = type == 'collection' ? 'GEOMETRYCOLLECTION' : type.toUpperCase();
+        var data = wktType + '(' + this.extract[type].apply(this, [geometry]) + ')';
+        return data;
+    },
     
     /**
      * Object with properties corresponding to the geometry types.
@@ -207,7 +221,7 @@
         /**
          * Return an array of polygon arrays from a multipolygon.
          * @param {<OpenLayers.Geometry.MultiPolygon>} multipolygon
-         * @returns {Array} An array of polygon arrays representing
+         * @returns {String} An array of polygon arrays representing
          *                  the multipolygon
          */
         'multipolygon': function(multipolygon) {
@@ -218,6 +232,19 @@
                            ')');
             }
             return array.join(',');
+        },
+
+        /**
+         * Return the WKT portion between 'GEOMETRYCOLLECTION(' and ')' for an <OpenLayers.Geometry.Collection>
+         * @param {<OpenLayers.Geometry.Collection>} collection
+         * @returns {String} internal WKT representation of the collection
+         */
+        'collection': function(collection) {
+            var array = [];
+            for(var i=0, len=collection.components.length; i<len; ++i) {
+                array.push(this.extractGeometry.apply(this, [collection.components[i]]));
+            }
+            return array.join(',');
         }
 
     },

Modified: trunk/openlayers/tests/Format/WKT.html
===================================================================
--- trunk/openlayers/tests/Format/WKT.html	2010-12-16 17:22:44 UTC (rev 10970)
+++ trunk/openlayers/tests/Format/WKT.html	2010-12-16 19:14:44 UTC (rev 10971)
@@ -80,8 +80,15 @@
             polygons[1].geometry 
         ])
     ); 
+
+    var collection = new OpenLayers.Feature.Vector(
+        new OpenLayers.Geometry.Collection([ 
+            points[0].geometry,
+            linestrings[0].geometry
+        ])
+    ); 
      
-    var collection = [points[0], linestrings[0]]; 
+    var geom_array = [points[0], linestrings[0]]; 
      
     function test_Format_WKT_constructor(t) { 
         t.plan(4); 
@@ -96,7 +103,7 @@
     }
 
     function test_Format_WKT_write(t) { 
-        t.plan(7); 
+        t.plan(8); 
 
         var format = new OpenLayers.Format.WKT(); 
 
@@ -161,19 +168,27 @@
                                  points[11].geometry.x + " " + points[11].geometry.y + "," + 
                                  points[9].geometry.x + " " + points[9].geometry.y + ")))", 
              "format correctly writes MultiPolygon WKT"); 
-         
-        // test a geometrycollection 
+
+        // test geometrycollection
         t.eq(format.write(collection), 
              "GEOMETRYCOLLECTION(POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")," + 
                                 "LINESTRING(" + points[0].geometry.x + " " + points[0].geometry.y + "," + 
                                                 points[1].geometry.x + " " + points[1].geometry.y + "," + 
                                                 points[2].geometry.x + " " + points[2].geometry.y + "))", 
              "format correctly writes GeometryCollection WKT"); 
+         
+        // test writing an array of geometries 
+        t.eq(format.write(geom_array), 
+             "GEOMETRYCOLLECTION(POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")," + 
+                                "LINESTRING(" + points[0].geometry.x + " " + points[0].geometry.y + "," + 
+                                                points[1].geometry.x + " " + points[1].geometry.y + "," + 
+                                                points[2].geometry.x + " " + points[2].geometry.y + "))", 
+             "format correctly writes WKT for an array of Geometries"); 
  
     }
 
     function test_Format_WKT_read(t) { 
-        t.plan(7); 
+        t.plan(6); 
 
         var format = new OpenLayers.Format.WKT(); 
          
@@ -206,13 +221,6 @@
         t.ok(multipolygon.geometry.equals(format.read(format.write(multipolygon)).geometry), 
              "format correctly reads MultiPolygon WKT"); 
          
-        // test a geometrycollection 
-        t.eq(format.write(collection), 
-             "GEOMETRYCOLLECTION(POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")," + 
-                                "LINESTRING(" + points[0].geometry.x + " " + points[0].geometry.y + "," + 
-                                                points[1].geometry.x + " " + points[1].geometry.y + "," + 
-                                                points[2].geometry.x + " " + points[2].geometry.y + "))", 
-             "format correctly writes GeometryCollection WKT"); 
  
     }
 



More information about the Commits mailing list