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) { write: function(features) {
var collection, geometry, type, data, isCollection; var collection, geometry, type, data, isCollection;
if(features.constructor == Array) { if (features.constructor == Array) {
collection = features; collection = features;
isCollection = true; isCollection = true;
} else { } else {
@@ -103,31 +103,45 @@ OpenLayers.Format.WKT = OpenLayers.Class(OpenLayers.Format, {
isCollection = false; isCollection = false;
} }
var pieces = []; var pieces = [];
if(isCollection) { if (isCollection) {
pieces.push('GEOMETRYCOLLECTION('); pieces.push('GEOMETRYCOLLECTION(');
} }
for(var i=0, len=collection.length; i<len; ++i) { for (var i=0, len=collection.length; i<len; ++i) {
if(isCollection && i>0) { if (isCollection && i>0) {
pieces.push(','); pieces.push(',');
} }
geometry = collection[i].geometry; geometry = collection[i].geometry;
type = geometry.CLASS_NAME.split('.')[2].toLowerCase(); pieces.push(this.extractGeometry(geometry));
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 + ')');
} }
if(isCollection) { if (isCollection) {
pieces.push(')'); pieces.push(')');
} }
return pieces.join(''); 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. * 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. * Return an array of polygon arrays from a multipolygon.
* @param {<OpenLayers.Geometry.MultiPolygon>} multipolygon * @param {<OpenLayers.Geometry.MultiPolygon>} multipolygon
* @returns {Array} An array of polygon arrays representing * @returns {String} An array of polygon arrays representing
* the multipolygon * the multipolygon
*/ */
'multipolygon': function(multipolygon) { 'multipolygon': function(multipolygon) {
@@ -218,6 +232,19 @@ OpenLayers.Format.WKT = OpenLayers.Class(OpenLayers.Format, {
')'); ')');
} }
return array.join(','); 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(',');
} }
}, },

View File

@@ -80,8 +80,15 @@
polygons[1].geometry 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) { function test_Format_WKT_constructor(t) {
t.plan(4); t.plan(4);
@@ -96,7 +103,7 @@
} }
function test_Format_WKT_write(t) { function test_Format_WKT_write(t) {
t.plan(7); t.plan(8);
var format = new OpenLayers.Format.WKT(); var format = new OpenLayers.Format.WKT();
@@ -161,19 +168,27 @@
points[11].geometry.x + " " + points[11].geometry.y + "," + points[11].geometry.x + " " + points[11].geometry.y + "," +
points[9].geometry.x + " " + points[9].geometry.y + ")))", points[9].geometry.x + " " + points[9].geometry.y + ")))",
"format correctly writes MultiPolygon WKT"); "format correctly writes MultiPolygon WKT");
// test a geometrycollection // test geometrycollection
t.eq(format.write(collection), t.eq(format.write(collection),
"GEOMETRYCOLLECTION(POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")," + "GEOMETRYCOLLECTION(POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")," +
"LINESTRING(" + 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[1].geometry.x + " " + points[1].geometry.y + "," +
points[2].geometry.x + " " + points[2].geometry.y + "))", points[2].geometry.x + " " + points[2].geometry.y + "))",
"format correctly writes GeometryCollection WKT"); "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) { function test_Format_WKT_read(t) {
t.plan(7); t.plan(6);
var format = new OpenLayers.Format.WKT(); var format = new OpenLayers.Format.WKT();
@@ -206,13 +221,6 @@
t.ok(multipolygon.geometry.equals(format.read(format.write(multipolygon)).geometry), t.ok(multipolygon.geometry.equals(format.read(format.write(multipolygon)).geometry),
"format correctly reads MultiPolygon WKT"); "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");
} }