Serializing features with OpenLayers.Geometry.Collection geometries as GEOMETRYCOLLECTION in WKT. p=strk, r=me (closes #2706).

git-svn-id: http://svn.openlayers.org/trunk/openlayers@10971 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2010-12-16 19:14:44 +00:00
parent e9e2aec77d
commit e00a21541d
2 changed files with 64 additions and 29 deletions

View File

@@ -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; 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 @@ OpenLayers.Format.WKT = OpenLayers.Class(OpenLayers.Format, {
/**
* 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 @@ OpenLayers.Format.WKT = OpenLayers.Class(OpenLayers.Format, {
')');
}
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(',');
}
},