diff --git a/lib/OpenLayers/Format/WKT.js b/lib/OpenLayers/Format/WKT.js index b662fbf407..1cbb5ea875 100644 --- a/lib/OpenLayers/Format/WKT.js +++ b/lib/OpenLayers/Format/WKT.js @@ -95,7 +95,7 @@ OpenLayers.Format.WKT = OpenLayers.Class(OpenLayers.Format, { */ 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 @@ OpenLayers.Format.WKT = OpenLayers.Class(OpenLayers.Format, { isCollection = false; } var pieces = []; - if(isCollection) { + if (isCollection) { pieces.push('GEOMETRYCOLLECTION('); } - for(var i=0, len=collection.length; i0) { + for (var i=0, len=collection.length; i0) { 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 - {} + * + * 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 @@ OpenLayers.Format.WKT = OpenLayers.Class(OpenLayers.Format, { /** * Return an array of polygon arrays from a multipolygon. * @param {} 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 @@ OpenLayers.Format.WKT = OpenLayers.Class(OpenLayers.Format, { ')'); } return array.join(','); + }, + + /** + * Return the WKT portion between 'GEOMETRYCOLLECTION(' and ')' for an + * @param {} collection + * @returns {String} internal WKT representation of the collection + */ + 'collection': function(collection) { + var array = []; + for(var i=0, len=collection.components.length; i