#708 - make the WKT format like the other vector formats - serialize/deserialize features instead of geometries

git-svn-id: http://svn.openlayers.org/trunk/openlayers@3158 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2007-05-21 14:24:55 +00:00
parent 6bbecdbb36
commit 7819bd1743
4 changed files with 165 additions and 124 deletions

View File

@@ -78,7 +78,7 @@
} }
function displayWKT(feature) { function displayWKT(feature) {
var str = wkt.write(feature.geometry); var str = wkt.write(feature);
// not a good idea in general, just for this demo // not a good idea in general, just for this demo
str = str.replace(/,/g, ', '); str = str.replace(/,/g, ', ');
document.getElementById('info').innerHTML = str; document.getElementById('info').innerHTML = str;
@@ -86,19 +86,18 @@
function parseWKT() { function parseWKT() {
var element = document.getElementById('wkt'); var element = document.getElementById('wkt');
var collection = wkt.read(element.value); var features = wkt.read(element.value);
var bounds; var bounds;
if(collection) { if(features) {
if(collection.constructor != Array) { if(features.constructor != Array) {
collection = [collection]; features = [features];
} }
var features = []; for(var i=0; i<features.length; ++i) {
for(var i=0; i<collection.length; ++i) {
features.push(new OpenLayers.Feature.Vector(collection[i]));
if (!bounds) { if (!bounds) {
bounds = collection[i].getBounds(); bounds = features[i].geometry.getBounds();
} else {
bounds.extend(features[i].geometry.getBounds());
} }
bounds.extend(collection[i].getBounds());
} }
vectors.addFeatures(features); vectors.addFeatures(features);

View File

@@ -25,38 +25,41 @@ OpenLayers.Format.WKT.prototype =
}, },
/** /**
* Deserialize a WKT string and return an OpenLayers.Geometry or an array * Deserialize a WKT string and return an OpenLayers.Feature.Vector or an
* of OpenLayers.Geometry. Supports WKT for POINT, MULTIPOINT, LINESTRING, * array of OpenLayers.Feature.Vector. Supports WKT for POINT, MULTIPOINT,
* MULTILINESTRING, POLYGON, MULTIPOLYGON, and GEOMETRYCOLLECTION. * LINESTRING, MULTILINESTRING, POLYGON, MULTIPOLYGON, and
* GEOMETRYCOLLECTION.
* @param {String} wkt A WKT string * @param {String} wkt A WKT string
* @returns {OpenLayers.Geometry|Array} A geometry or array of geometries * @returns {OpenLayers.Feature.Vector|Array} A feature or array of
* for GEOMETRYCOLLECTION WKT. * features for
* GEOMETRYCOLLECTION WKT.
*/ */
read: function(wkt) { read: function(wkt) {
var geometry, type, str; var features, type, str;
var matches = this.regExes.typeStr.exec(wkt); var matches = this.regExes.typeStr.exec(wkt);
if(matches) { if(matches) {
type = matches[1].toLowerCase(); type = matches[1].toLowerCase();
str = matches[2]; str = matches[2];
if(this.parse[type]) { if(this.parse[type]) {
geometry = this.parse[type].apply(this, [str]); features = this.parse[type].apply(this, [str]);
} }
} }
return geometry; return features;
}, },
/** /**
* Serialize a geometry or array of geometries into a WKT string. * Serialize a feature or array of features into a WKT string.
* @param {OpenLayers.Geometry|Array} geom A geometry or array of geometries * @param {OpenLayers.Feature.Vector|Array} features A feature or array of
* features
* @returns {String} The WKT string representation of the input geometries * @returns {String} The WKT string representation of the input geometries
*/ */
write: function(geom) { write: function(features) {
var collection, geometry, type, data, isCollection; var collection, geometry, type, data, isCollection;
if(geom.constructor == Array) { if(features.constructor == Array) {
collection = geom; collection = features;
isCollection = true; isCollection = true;
} else { } else {
collection = [geom]; collection = [features];
isCollection = false; isCollection = false;
} }
var pieces = []; var pieces = [];
@@ -67,7 +70,7 @@ OpenLayers.Format.WKT.prototype =
if(isCollection && i>0) { if(isCollection && i>0) {
pieces.push(','); pieces.push(',');
} }
geometry = collection[i]; geometry = collection[i].geometry;
type = geometry.CLASS_NAME.split('.')[2].toLowerCase(); type = geometry.CLASS_NAME.split('.')[2].toLowerCase();
if(!this.extract[type]) { if(!this.extract[type]) {
return null; return null;
@@ -178,47 +181,58 @@ OpenLayers.Format.WKT.prototype =
*/ */
parse: { parse: {
/** /**
* Return point geometry given a point WKT fragment. * Return point feature given a point WKT fragment.
* @param {String} str A WKT fragment representing the point * @param {String} str A WKT fragment representing the point
* @returns {OpenLayers.Geometry.Point} A point geometry * @returns {OpenLayers.Feature.Vector} A point feature
* @private
*/ */
'point': function(str) { 'point': function(str) {
var coords = str.trim().split(this.regExes.spaces); var coords = str.trim().split(this.regExes.spaces);
return new OpenLayers.Geometry.Point(coords[0], coords[1]); return new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(coords[0], coords[1])
);
}, },
/** /**
* Return a multipoint geometry given a multipoint WKT fragment. * Return a multipoint feature given a multipoint WKT fragment.
* @param {String} A WKT fragment representing the multipoint * @param {String} A WKT fragment representing the multipoint
* @returns {OpenLayers.Geometry.MultiPoint} A multipoint geometry * @returns {OpenLayers.Feature.Vector} A multipoint feature
* @private
*/ */
'multipoint': function(str) { 'multipoint': function(str) {
var points = str.trim().split(','); var points = str.trim().split(',');
var components = []; var components = [];
for(var i=0; i<points.length; ++i) { for(var i=0; i<points.length; ++i) {
components.push(this.parse.point.apply(this, [points[i]])); components.push(this.parse.point.apply(this, [points[i]]).geometry);
} }
return new OpenLayers.Geometry.MultiPoint(components); return new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.MultiPoint(components)
);
}, },
/** /**
* Return a linestring geometry given a linestring WKT fragment. * Return a linestring feature given a linestring WKT fragment.
* @param {String} A WKT fragment representing the linestring * @param {String} A WKT fragment representing the linestring
* @returns {OpenLayers.Geometry.LineString} A linestring geometry * @returns {OpenLayers.Feature.Vector} A linestring feature
* @private
*/ */
'linestring': function(str) { 'linestring': function(str) {
var points = str.trim().split(','); var points = str.trim().split(',');
var components = []; var components = [];
for(var i=0; i<points.length; ++i) { for(var i=0; i<points.length; ++i) {
components.push(this.parse.point.apply(this, [points[i]])); components.push(this.parse.point.apply(this, [points[i]]).geometry);
} }
return new OpenLayers.Geometry.LineString(components); console.log(components);
return new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LineString(components)
);
}, },
/** /**
* Return a multilinestring geometry given a multilinestring WKT fragment. * Return a multilinestring feature given a multilinestring WKT fragment.
* @param {String} A WKT fragment representing the multilinestring * @param {String} A WKT fragment representing the multilinestring
* @returns {OpenLayers.Geometry.LineString} A multilinestring geometry * @returns {OpenLayers.Feature.Vector} A multilinestring feature
* @private
*/ */
'multilinestring': function(str) { 'multilinestring': function(str) {
var line; var line;
@@ -226,15 +240,18 @@ OpenLayers.Format.WKT.prototype =
var components = []; var components = [];
for(var i=0; i<lines.length; ++i) { for(var i=0; i<lines.length; ++i) {
line = lines[i].replace(this.regExes.trimParens, '$1'); line = lines[i].replace(this.regExes.trimParens, '$1');
components.push(this.parse.linestring.apply(this, [line])); components.push(this.parse.linestring.apply(this, [line]).geometry);
} }
return new OpenLayers.Geometry.MultiLineString(components); return new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.MultiLineString(components)
);
}, },
/** /**
* Return a polygon geometry given a polygon WKT fragment. * Return a polygon feature given a polygon WKT fragment.
* @param {String} A WKT fragment representing the polygon * @param {String} A WKT fragment representing the polygon
* @returns {OpenLayers.Geometry.Polygon} A polygon geometry * @returns {OpenLayers.Feature.Vector} A polygon feature
* @private
*/ */
'polygon': function(str) { 'polygon': function(str) {
var ring, linestring, linearring; var ring, linestring, linearring;
@@ -242,17 +259,20 @@ OpenLayers.Format.WKT.prototype =
var components = []; var components = [];
for(var i=0; i<rings.length; ++i) { for(var i=0; i<rings.length; ++i) {
ring = rings[i].replace(this.regExes.trimParens, '$1'); ring = rings[i].replace(this.regExes.trimParens, '$1');
linestring = this.parse.linestring.apply(this, [ring]); linestring = this.parse.linestring.apply(this, [ring]).geometry;
linearring = new OpenLayers.Geometry.LinearRing(linestring.components); linearring = new OpenLayers.Geometry.LinearRing(linestring.components)
components.push(linearring); components.push(linearring);
} }
return new OpenLayers.Geometry.Polygon(components); return new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Polygon(components)
);
}, },
/** /**
* Return a multipolygon geometry given a multipolygon WKT fragment. * Return a multipolygon feature given a multipolygon WKT fragment.
* @param {String} A WKT fragment representing the multipolygon * @param {String} A WKT fragment representing the multipolygon
* @returns {OpenLayers.Geometry.MultiPolygon} A multipolygon geometry * @returns {OpenLayers.Feature.Vector} A multipolygon feature
* @private
*/ */
'multipolygon': function(str) { 'multipolygon': function(str) {
var polygon; var polygon;
@@ -260,15 +280,18 @@ OpenLayers.Format.WKT.prototype =
var components = []; var components = [];
for(var i=0; i<polygons.length; ++i) { for(var i=0; i<polygons.length; ++i) {
polygon = polygons[i].replace(this.regExes.trimParens, '$1'); polygon = polygons[i].replace(this.regExes.trimParens, '$1');
components.push(this.parse.polygon.apply(this, [polygon])); components.push(this.parse.polygon.apply(this, [polygon]).geometry);
} }
return new OpenLayers.Geometry.MultiPolygon(components); return new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.MultiPolygon(components)
);
}, },
/** /**
* Return an array of geometries given a geometrycollection WKT fragment. * Return an array of features given a geometrycollection WKT fragment.
* @param {String} A WKT fragment representing the geometrycollection * @param {String} A WKT fragment representing the geometrycollection
* @returns {Array} An array of OpenLayers.Geometry * @returns {Array} An array of OpenLayers.Feature.Vector
* @private
*/ */
'geometrycollection': function(str) { 'geometrycollection': function(str) {
// separate components of the collection with | // separate components of the collection with |

View File

@@ -5,6 +5,7 @@
/** /**
* @class * @class
* @requires OpenLayers/Format/WKT.js * @requires OpenLayers/Format/WKT.js
* @requires OpenLayers/Feature/Vector.js
*/ */
OpenLayers.Geometry = OpenLayers.Class.create(); OpenLayers.Geometry = OpenLayers.Class.create();
OpenLayers.Geometry.prototype = { OpenLayers.Geometry.prototype = {
@@ -150,7 +151,9 @@ OpenLayers.Geometry.prototype = {
* @type String * @type String
*/ */
toString: function() { toString: function() {
return OpenLayers.Format.WKT.prototype.write(this); return OpenLayers.Format.WKT.prototype.write(
new OpenLayers.Feature.Vector(this)
);
}, },
/** @final @type String */ /** @final @type String */

View File

@@ -5,37 +5,53 @@
var points = []; var points = [];
for(var i=0; i<12; ++i) { for(var i=0; i<12; ++i) {
points.push(new OpenLayers.Geometry.Point(Math.random() * 100, points.push(new OpenLayers.Feature.Vector(
Math.random() * 100)); new OpenLayers.Geometry.Point(Math.random() * 100,
Math.random() * 100))
);
} }
var multipoint = new OpenLayers.Geometry.MultiPoint([ var multipoint = new OpenLayers.Feature.Vector(
points[0], points[1], points[2] new OpenLayers.Geometry.MultiPoint([
]); points[0].geometry, points[1].geometry, points[2].geometry
])
);
var linestrings = [ var linestrings = [
new OpenLayers.Geometry.LineString([points[0], points[1], points[2]]), new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LineString([points[3], points[4], points[5]]) new OpenLayers.Geometry.LineString([points[0].geometry, points[1].geometry, points[2].geometry])
),
new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LineString([points[3].geometry, points[4].geometry, points[5].geometry])
)
]; ];
var multilinestring = new OpenLayers.Geometry.MultiLineString([ var multilinestring = new OpenLayers.Feature.Vector(
linestrings[0], linestrings[1] new OpenLayers.Geometry.MultiLineString([
]); linestrings[0].geometry, linestrings[1].geometry
])
);
var rings = [ var rings = [
new OpenLayers.Geometry.LinearRing([points[0], points[1], points[2]]), new OpenLayers.Geometry.LinearRing([points[0].geometry, points[1].geometry, points[2].geometry]),
new OpenLayers.Geometry.LinearRing([points[3], points[4], points[5]]), new OpenLayers.Geometry.LinearRing([points[3].geometry, points[4].geometry, points[5].geometry]),
new OpenLayers.Geometry.LinearRing([points[6], points[7], points[8]]), new OpenLayers.Geometry.LinearRing([points[6].geometry, points[7].geometry, points[8].geometry]),
new OpenLayers.Geometry.LinearRing([points[9], points[10], points[11]]) new OpenLayers.Geometry.LinearRing([points[9].geometry, points[10].geometry, points[11].geometry])
]; ];
var polygons = [ var polygons = [
new OpenLayers.Geometry.Polygon([rings[0], rings[1]]), new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Polygon([rings[2], rings[3]]) new OpenLayers.Geometry.Polygon([rings[0], rings[1]])
),
new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Polygon([rings[2], rings[3]])
)
]; ];
var multipolygon = new OpenLayers.Geometry.MultiPolygon([ var multipolygon = new OpenLayers.Feature.Vector(
polygons[0], polygons[1] new OpenLayers.Geometry.MultiPolygon([
]); polygons[0].geometry, polygons[1].geometry
])
);
var collection = [points[0], linestrings[0]]; var collection = [points[0], linestrings[0]];
@@ -53,74 +69,74 @@
function test_Format_WKT_write(t) { function test_Format_WKT_write(t) {
t.plan(7); t.plan(7);
var format = new OpenLayers.Format.WKT(); var format = new OpenLayers.Format.WKT();
// test a point // test a point
t.eq(format.write(points[0]), t.eq(format.write(points[0]),
"POINT(" + points[0].x + " " + points[0].y + ")", "POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")",
"format correctly writes Point WKT"); "format correctly writes Point WKT");
// test a multipoint // test a multipoint
t.eq(format.write(multipoint), t.eq(format.write(multipoint),
"MULTIPOINT(" + points[0].x + " " + points[0].y + "," + "MULTIPOINT(" + points[0].geometry.x + " " + points[0].geometry.y + "," +
points[1].x + " " + points[1].y + "," + points[1].geometry.x + " " + points[1].geometry.y + "," +
points[2].x + " " + points[2].y + ")", points[2].geometry.x + " " + points[2].geometry.y + ")",
"format correctly writes MultiPoint WKT"); "format correctly writes MultiPoint WKT");
// test a linestring // test a linestring
t.eq(format.write(linestrings[0]), t.eq(format.write(linestrings[0]),
"LINESTRING(" + points[0].x + " " + points[0].y + "," + "LINESTRING(" + points[0].geometry.x + " " + points[0].geometry.y + "," +
points[1].x + " " + points[1].y + "," + points[1].geometry.x + " " + points[1].geometry.y + "," +
points[2].x + " " + points[2].y + ")", points[2].geometry.x + " " + points[2].geometry.y + ")",
"format correctly writes LineString WKT"); "format correctly writes LineString WKT");
// test a multilinestring // test a multilinestring
t.eq(format.write(multilinestring), t.eq(format.write(multilinestring),
"MULTILINESTRING((" + points[0].x + " " + points[0].y + "," + "MULTILINESTRING((" + points[0].geometry.x + " " + points[0].geometry.y + "," +
points[1].x + " " + points[1].y + "," + points[1].geometry.x + " " + points[1].geometry.y + "," +
points[2].x + " " + points[2].y + ")," + points[2].geometry.x + " " + points[2].geometry.y + ")," +
"(" + points[3].x + " " + points[3].y + "," + "(" + points[3].geometry.x + " " + points[3].geometry.y + "," +
points[4].x + " " + points[4].y + "," + points[4].geometry.x + " " + points[4].geometry.y + "," +
points[5].x + " " + points[5].y + "))", points[5].geometry.x + " " + points[5].geometry.y + "))",
"format correctly writes MultiLineString WKT"); "format correctly writes MultiLineString WKT");
// test a polygon // test a polygon
t.eq(format.write(polygons[0]), t.eq(format.write(polygons[0]),
"POLYGON((" + points[0].x + " " + points[0].y + "," + "POLYGON((" + points[0].geometry.x + " " + points[0].geometry.y + "," +
points[1].x + " " + points[1].y + "," + points[1].geometry.x + " " + points[1].geometry.y + "," +
points[2].x + " " + points[2].y + "," + points[2].geometry.x + " " + points[2].geometry.y + "," +
points[0].x + " " + points[0].y + ")," + points[0].geometry.x + " " + points[0].geometry.y + ")," +
"(" + points[3].x + " " + points[3].y + "," + "(" + points[3].geometry.x + " " + points[3].geometry.y + "," +
points[4].x + " " + points[4].y + "," + points[4].geometry.x + " " + points[4].geometry.y + "," +
points[5].x + " " + points[5].y + "," + points[5].geometry.x + " " + points[5].geometry.y + "," +
points[3].x + " " + points[3].y + "))", points[3].geometry.x + " " + points[3].geometry.y + "))",
"format correctly writes Polygon WKT"); "format correctly writes Polygon WKT");
// test a multipolygon // test a multipolygon
t.eq(format.write(multipolygon), t.eq(format.write(multipolygon),
"MULTIPOLYGON(((" + points[0].x + " " + points[0].y + "," + "MULTIPOLYGON(((" + points[0].geometry.x + " " + points[0].geometry.y + "," +
points[1].x + " " + points[1].y + "," + points[1].geometry.x + " " + points[1].geometry.y + "," +
points[2].x + " " + points[2].y + "," + points[2].geometry.x + " " + points[2].geometry.y + "," +
points[0].x + " " + points[0].y + ")," + points[0].geometry.x + " " + points[0].geometry.y + ")," +
"(" + points[3].x + " " + points[3].y + "," + "(" + points[3].geometry.x + " " + points[3].geometry.y + "," +
points[4].x + " " + points[4].y + "," + points[4].geometry.x + " " + points[4].geometry.y + "," +
points[5].x + " " + points[5].y + "," + points[5].geometry.x + " " + points[5].geometry.y + "," +
points[3].x + " " + points[3].y + "))," + points[3].geometry.x + " " + points[3].geometry.y + "))," +
"((" + points[6].x + " " + points[6].y + "," + "((" + points[6].geometry.x + " " + points[6].geometry.y + "," +
points[7].x + " " + points[7].y + "," + points[7].geometry.x + " " + points[7].geometry.y + "," +
points[8].x + " " + points[8].y + "," + points[8].geometry.x + " " + points[8].geometry.y + "," +
points[6].x + " " + points[6].y + ")," + points[6].geometry.x + " " + points[6].geometry.y + ")," +
"(" + points[9].x + " " + points[9].y + "," + "(" + points[9].geometry.x + " " + points[9].geometry.y + "," +
points[10].x + " " + points[10].y + "," + points[10].geometry.x + " " + points[10].geometry.y + "," +
points[11].x + " " + points[11].y + "," + points[11].geometry.x + " " + points[11].geometry.y + "," +
points[9].x + " " + points[9].y + ")))", points[9].geometry.x + " " + points[9].geometry.y + ")))",
"format correctly writes MultiPolygon WKT"); "format correctly writes MultiPolygon WKT");
// test a geometrycollection // test a geometrycollection
t.eq(format.write(collection), t.eq(format.write(collection),
"GEOMETRYCOLLECTION(POINT(" + points[0].x + " " + points[0].y + ")," + "GEOMETRYCOLLECTION(POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")," +
"LINESTRING(" + points[0].x + " " + points[0].y + "," + "LINESTRING(" + points[0].geometry.x + " " + points[0].geometry.y + "," +
points[1].x + " " + points[1].y + "," + points[1].geometry.x + " " + points[1].geometry.y + "," +
points[2].x + " " + points[2].y + "))", points[2].geometry.x + " " + points[2].geometry.y + "))",
"format correctly writes GeometryCollection WKT"); "format correctly writes GeometryCollection WKT");
} }
@@ -134,35 +150,35 @@
*/ */
// test a point // test a point
t.ok(points[0].equals(format.read(format.write(points[0]))), t.ok(points[0].geometry.equals(format.read(format.write(points[0])).geometry),
"format correctly reads Point WKT"); "format correctly reads Point WKT");
// test a multipoint // test a multipoint
t.ok(multipoint.equals(format.read(format.write(multipoint))), t.ok(multipoint.geometry.equals(format.read(format.write(multipoint)).geometry),
"format correctly reads MultiPoint WKT"); "format correctly reads MultiPoint WKT");
// test a linestring // test a linestring
t.ok(linestrings[0].equals(format.read(format.write(linestrings[0]))), t.ok(linestrings[0].geometry.equals(format.read(format.write(linestrings[0])).geometry),
"format correctly reads LineString WKT"); "format correctly reads LineString WKT");
// test a multilinestring // test a multilinestring
t.ok(multilinestring.equals(format.read(format.write(multilinestring))), t.ok(multilinestring.geometry.equals(format.read(format.write(multilinestring)).geometry),
"format correctly reads MultiLineString WKT"); "format correctly reads MultiLineString WKT");
// test a polygon // test a polygon
t.ok(polygons[0].equals(format.read(format.write(polygons[0]))), t.ok(polygons[0].geometry.equals(format.read(format.write(polygons[0])).geometry),
"format correctly reads Polygon WKT"); "format correctly reads Polygon WKT");
// test a multipolygon // test a multipolygon
t.ok(multipolygon.equals(format.read(format.write(multipolygon))), t.ok(multipolygon.geometry.equals(format.read(format.write(multipolygon)).geometry),
"format correctly reads MultiPolygon WKT"); "format correctly reads MultiPolygon WKT");
// test a geometrycollection // test a geometrycollection
t.eq(format.write(collection), t.eq(format.write(collection),
"GEOMETRYCOLLECTION(POINT(" + points[0].x + " " + points[0].y + ")," + "GEOMETRYCOLLECTION(POINT(" + points[0].geometry.x + " " + points[0].geometry.y + ")," +
"LINESTRING(" + points[0].x + " " + points[0].y + "," + "LINESTRING(" + points[0].geometry.x + " " + points[0].geometry.y + "," +
points[1].x + " " + points[1].y + "," + points[1].geometry.x + " " + points[1].geometry.y + "," +
points[2].x + " " + points[2].y + "))", points[2].geometry.x + " " + points[2].geometry.y + "))",
"format correctly writes GeometryCollection WKT"); "format correctly writes GeometryCollection WKT");
} }