From f436f2d5e28b492556ab9a573aa5ea83aead56c1 Mon Sep 17 00:00:00 2001 From: Olle Markljung Date: Sun, 6 Nov 2016 12:56:44 +0100 Subject: [PATCH] Adds support for M, Z and ZM WKT formatting --- src/ol/format/wkt.js | 107 +++++++- test/spec/ol/format/wkt.test.js | 438 ++++++++++++++++++++++++++++++++ 2 files changed, 533 insertions(+), 12 deletions(-) diff --git a/src/ol/format/wkt.js b/src/ol/format/wkt.js index f1f39c45f4..5c6076367c 100644 --- a/src/ol/format/wkt.js +++ b/src/ol/format/wkt.js @@ -6,12 +6,14 @@ goog.require('ol.format.Feature'); goog.require('ol.format.TextFeature'); goog.require('ol.geom.GeometryCollection'); goog.require('ol.geom.GeometryType'); +goog.require('ol.geom.GeometryLayout'); goog.require('ol.geom.LineString'); goog.require('ol.geom.MultiLineString'); goog.require('ol.geom.MultiPoint'); goog.require('ol.geom.MultiPolygon'); goog.require('ol.geom.Point'); goog.require('ol.geom.Polygon'); +goog.require('ol.geom.SimpleGeometry'); /** @@ -49,6 +51,27 @@ ol.inherits(ol.format.WKT, ol.format.TextFeature); ol.format.WKT.EMPTY = 'EMPTY'; +/** + * @const + * @type {string} + */ +ol.format.WKT.Z = 'Z'; + + +/** + * @const + * @type {string} + */ +ol.format.WKT.M = 'M'; + + +/** + * @const + * @type {string} + */ +ol.format.WKT.ZM = 'ZM'; + + /** * @param {ol.geom.Point} geom Point geometry. * @return {string} Coordinates part of Point as WKT. @@ -59,7 +82,7 @@ ol.format.WKT.encodePointGeometry_ = function(geom) { if (coordinates.length === 0) { return ''; } - return coordinates[0] + ' ' + coordinates[1]; + return coordinates.join(' '); }; @@ -102,7 +125,7 @@ ol.format.WKT.encodeLineStringGeometry_ = function(geom) { var coordinates = geom.getCoordinates(); var array = []; for (var i = 0, ii = coordinates.length; i < ii; ++i) { - array.push(coordinates[i][0] + ' ' + coordinates[i][1]); + array.push(coordinates[i].join(' ')); } return array.join(','); }; @@ -155,6 +178,23 @@ ol.format.WKT.encodeMultiPolygonGeometry_ = function(geom) { return array.join(','); }; +/** + * @param {ol.geom.SimpleGeometry} geom SimpleGeometry geometry. + * @return {string} Potential dimensional information for WKT type. + * @private + */ +ol.format.WKT.encodeGeometryLayout_ = function(geom) { + var layout = geom.getLayout(); + var dimInfo = ''; + if (layout === ol.geom.GeometryLayout.XYZ || layout === ol.geom.GeometryLayout.XYZM) { + dimInfo += ol.format.WKT.Z; + } + if (layout === ol.geom.GeometryLayout.XYM || layout === ol.geom.GeometryLayout.XYZM) { + dimInfo += ol.format.WKT.M; + } + return dimInfo; +}; + /** * Encode a geometry as WKT. @@ -168,6 +208,12 @@ ol.format.WKT.encode_ = function(geom) { ol.DEBUG && console.assert(geometryEncoder, 'geometryEncoder should be defined'); var enc = geometryEncoder(geom); type = type.toUpperCase(); + if (geom instanceof ol.geom.SimpleGeometry) { + var dimInfo = ol.format.WKT.encodeGeometryLayout_(geom); + if (dimInfo.length > 0) { + type += ' ' + dimInfo; + } + } if (enc.length === 0) { return type + ' ' + ol.format.WKT.EMPTY; } @@ -194,7 +240,7 @@ ol.format.WKT.GeometryEncoder_ = { /** * Parse a WKT string. * @param {string} wkt WKT string. - * @return {ol.geom.Geometry|ol.geom.GeometryCollection|undefined} + * @return {ol.geom.Geometry|undefined} * The geometry created. * @private */ @@ -535,10 +581,10 @@ ol.format.WKT.Parser = function(lexer) { this.token_; /** - * @type {number} + * @type {ol.geom.GeometryLayout} * @private */ - this.dimension_ = 2; + this.layout_ = ol.geom.GeometryLayout.XY; }; @@ -550,6 +596,16 @@ ol.format.WKT.Parser.prototype.consume_ = function() { this.token_ = this.lexer_.nextToken(); }; +/** + * Tests if the given type matches the type of the current token. + * @param {ol.format.WKT.TokenType} type Token type. + * @return {boolean} Whether the token matches the given type. + */ +ol.format.WKT.Parser.prototype.isTokenType = function(type) { + var isMatch = this.token_.type == type; + return isMatch; +}; + /** * If the given type matches the current token, consume it. @@ -557,7 +613,7 @@ ol.format.WKT.Parser.prototype.consume_ = function() { * @return {boolean} Whether the token matches the given type. */ ol.format.WKT.Parser.prototype.match = function(type) { - var isMatch = this.token_.type == type; + var isMatch = this.isTokenType(type); if (isMatch) { this.consume_(); } @@ -567,7 +623,7 @@ ol.format.WKT.Parser.prototype.match = function(type) { /** * Try to parse the tokens provided by the lexer. - * @return {ol.geom.Geometry|ol.geom.GeometryCollection} The geometry. + * @return {ol.geom.Geometry} The geometry. */ ol.format.WKT.Parser.prototype.parse = function() { this.consume_(); @@ -579,13 +635,39 @@ ol.format.WKT.Parser.prototype.parse = function() { /** - * @return {!(ol.geom.Geometry|ol.geom.GeometryCollection)} The geometry. + * Try to parse the dimensional info. + * @return {ol.geom.GeometryLayout} The layout. + * @private + */ +ol.format.WKT.Parser.prototype.parseGeometryLayout_ = function() { + var layout = ol.geom.GeometryLayout.XY; + var dimToken = this.token_; + if (this.isTokenType(ol.format.WKT.TokenType.TEXT)) { + var dimInfo = dimToken.value; + if (dimInfo === ol.format.WKT.Z) { + layout = ol.geom.GeometryLayout.XYZ; + } else if (dimInfo === ol.format.WKT.M) { + layout = ol.geom.GeometryLayout.XYM; + } else if (dimInfo === ol.format.WKT.ZM) { + layout = ol.geom.GeometryLayout.XYZM; + } + if (layout !== ol.geom.GeometryLayout.XY) { + this.consume_(); + } + } + return layout; +}; + + +/** + * @return {!ol.geom.Geometry} The geometry. * @private */ ol.format.WKT.Parser.prototype.parseGeometry_ = function() { var token = this.token_; if (this.match(ol.format.WKT.TokenType.TEXT)) { var geomType = token.value; + this.layout_ = this.parseGeometryLayout_(); if (geomType == ol.geom.GeometryType.GEOMETRY_COLLECTION.toUpperCase()) { var geometries = this.parseGeometryCollectionText_(); return new ol.geom.GeometryCollection(geometries); @@ -596,7 +678,7 @@ ol.format.WKT.Parser.prototype.parseGeometry_ = function() { throw new Error('Invalid geometry type: ' + geomType); } var coordinates = parser.call(this); - return new ctor(coordinates); + return new ctor(coordinates, this.layout_); } } throw new Error(this.formatErrorMessage_()); @@ -737,7 +819,8 @@ ol.format.WKT.Parser.prototype.parseMultiPolygonText_ = function() { */ ol.format.WKT.Parser.prototype.parsePoint_ = function() { var coordinates = []; - for (var i = 0; i < this.dimension_; ++i) { + var dimensions = this.layout_.length; + for (var i = 0; i < dimensions; ++i) { var token = this.token_; if (this.match(ol.format.WKT.TokenType.NUMBER)) { coordinates.push(token.value); @@ -745,7 +828,7 @@ ol.format.WKT.Parser.prototype.parsePoint_ = function() { break; } } - if (coordinates.length == this.dimension_) { + if (coordinates.length == dimensions) { return coordinates; } throw new Error(this.formatErrorMessage_()); @@ -809,7 +892,7 @@ ol.format.WKT.Parser.prototype.parsePolygonTextList_ = function() { * @private */ ol.format.WKT.Parser.prototype.isEmptyGeometry_ = function() { - var isEmpty = this.token_.type == ol.format.WKT.TokenType.TEXT && + var isEmpty = this.isTokenType(ol.format.WKT.TokenType.TEXT) && this.token_.value == ol.format.WKT.EMPTY; if (isEmpty) { this.consume_(); diff --git a/test/spec/ol/format/wkt.test.js b/test/spec/ol/format/wkt.test.js index 87ac3c34f7..94de1034f9 100644 --- a/test/spec/ol/format/wkt.test.js +++ b/test/spec/ol/format/wkt.test.js @@ -138,6 +138,39 @@ describe('ol.format.WKT', function() { expect(geom.getCoordinates()).to.eql([30, 10]); }); + it('Point Z read / written correctly', function() { + var wkt = 'POINT Z(30 10 5)'; + var geom = format.readGeometry(wkt); + expect(geom.getCoordinates()).to.eql([30, 10, 5]); + expect(format.writeGeometry(geom)).to.eql(wkt); + // test whitespace when reading + wkt = 'POINT Z (30 10 5)'; + geom = format.readGeometry(wkt); + expect(geom.getCoordinates()).to.eql([30, 10, 5]); + }); + + it('Point M read / written correctly', function() { + var wkt = 'POINT M(30 10 5)'; + var geom = format.readGeometry(wkt); + expect(geom.getCoordinates()).to.eql([30, 10, 5]); + expect(format.writeGeometry(geom)).to.eql(wkt); + // test whitespace when reading + wkt = 'POINT M (30 10 5)'; + geom = format.readGeometry(wkt); + expect(geom.getCoordinates()).to.eql([30, 10, 5]); + }); + + it('Point ZM read / written correctly', function() { + var wkt = 'POINT ZM(30 10 5 0.1)'; + var geom = format.readGeometry(wkt); + expect(geom.getCoordinates()).to.eql([30, 10, 5, 0.1]); + expect(format.writeGeometry(geom)).to.eql(wkt); + // test whitespace when reading + wkt = 'POINT ZM (30 10 5 0.1)'; + geom = format.readGeometry(wkt); + expect(geom.getCoordinates()).to.eql([30, 10, 5, 0.1]); + }); + it('MultiPoint read / written correctly', function() { // there are two forms to test var wkt = 'MULTIPOINT((10 40),(40 30),(20 20),(30 10))'; @@ -160,6 +193,72 @@ describe('ol.format.WKT', function() { expect(points[3].getCoordinates()).to.eql([30, 10]); }); + it('MultiPoint Z read / written correctly', function() { + // there are two forms to test + var wkt = 'MULTIPOINT Z((10 40 1),(40 30 2),(20 20 3),(30 10 4))'; + var geom = format.readGeometry(wkt); + var points = geom.getPoints(); + expect(points.length).to.eql(4); + expect(points[0].getCoordinates()).to.eql([10, 40, 1]); + expect(points[1].getCoordinates()).to.eql([40, 30, 2]); + expect(points[2].getCoordinates()).to.eql([20, 20, 3]); + expect(points[3].getCoordinates()).to.eql([30, 10, 4]); + expect(format.writeGeometry(geom)).to.eql(wkt); + // this has whitespace and no standardized parentheses + wkt = 'MULTIPOINT Z (10 40 1, 40 30 2, 20 20 3, 30 10 4)'; + geom = format.readGeometry(wkt); + points = geom.getPoints(); + expect(points.length).to.eql(4); + expect(points[0].getCoordinates()).to.eql([10, 40, 1]); + expect(points[1].getCoordinates()).to.eql([40, 30, 2]); + expect(points[2].getCoordinates()).to.eql([20, 20, 3]); + expect(points[3].getCoordinates()).to.eql([30, 10, 4]); + }); + + it('MultiPoint M read / written correctly', function() { + // there are two forms to test + var wkt = 'MULTIPOINT M((10 40 1),(40 30 2),(20 20 3),(30 10 4))'; + var geom = format.readGeometry(wkt); + var points = geom.getPoints(); + expect(points.length).to.eql(4); + expect(points[0].getCoordinates()).to.eql([10, 40, 1]); + expect(points[1].getCoordinates()).to.eql([40, 30, 2]); + expect(points[2].getCoordinates()).to.eql([20, 20, 3]); + expect(points[3].getCoordinates()).to.eql([30, 10, 4]); + expect(format.writeGeometry(geom)).to.eql(wkt); + // this has whitespace and no standardized parentheses + wkt = 'MULTIPOINT M (10 40 1, 40 30 2, 20 20 3, 30 10 4)'; + geom = format.readGeometry(wkt); + points = geom.getPoints(); + expect(points.length).to.eql(4); + expect(points[0].getCoordinates()).to.eql([10, 40, 1]); + expect(points[1].getCoordinates()).to.eql([40, 30, 2]); + expect(points[2].getCoordinates()).to.eql([20, 20, 3]); + expect(points[3].getCoordinates()).to.eql([30, 10, 4]); + }); + + it('MultiPoint ZM read / written correctly', function() { + // there are two forms to test + var wkt = 'MULTIPOINT ZM((10 40 1 0.1),(40 30 2 0.1),(20 20 3 0.1),(30 10 4 0.1))'; + var geom = format.readGeometry(wkt); + var points = geom.getPoints(); + expect(points.length).to.eql(4); + expect(points[0].getCoordinates()).to.eql([10, 40, 1, 0.1]); + expect(points[1].getCoordinates()).to.eql([40, 30, 2, 0.1]); + expect(points[2].getCoordinates()).to.eql([20, 20, 3, 0.1]); + expect(points[3].getCoordinates()).to.eql([30, 10, 4, 0.1]); + expect(format.writeGeometry(geom)).to.eql(wkt); + // this has whitespace and no standardized parentheses + wkt = 'MULTIPOINT ZM (10 40 1 0.1,40 30 2 0.1,20 20 3 0.1,30 10 4 0.1)'; + geom = format.readGeometry(wkt); + points = geom.getPoints(); + expect(points.length).to.eql(4); + expect(points[0].getCoordinates()).to.eql([10, 40, 1, 0.1]); + expect(points[1].getCoordinates()).to.eql([40, 30, 2, 0.1]); + expect(points[2].getCoordinates()).to.eql([20, 20, 3, 0.1]); + expect(points[3].getCoordinates()).to.eql([30, 10, 4, 0.1]); + }); + it('LineString read / written correctly', function() { var wkt = 'LINESTRING(30 10,10 30,40 40)'; var geom = format.readGeometry(wkt); @@ -173,6 +272,45 @@ describe('ol.format.WKT', function() { expect(geom.getCoordinates()).to.eql([[30, 10], [10, 30], [40, 40]]); }); + it('LineString Z read / written correctly', function() { + var wkt = 'LINESTRING Z(30 10 1,10 30 2,40 40 3)'; + var geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('LineString'); + expect(geom.getCoordinates()).to.eql([[30, 10, 1], [10, 30, 2], [40, 40, 3]]); + expect(format.writeGeometry(geom)).to.eql(wkt); + // test whitespace when reading + wkt = 'LINESTRING Z (30 10 1, 10 30 2, 40 40 3)'; + geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('LineString'); + expect(geom.getCoordinates()).to.eql([[30, 10, 1], [10, 30, 2], [40, 40, 3]]); + }); + + it('LineString M read / written correctly', function() { + var wkt = 'LINESTRING M(30 10 1,10 30 2,40 40 3)'; + var geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('LineString'); + expect(geom.getCoordinates()).to.eql([[30, 10, 1], [10, 30, 2], [40, 40, 3]]); + expect(format.writeGeometry(geom)).to.eql(wkt); + // test whitespace when reading + wkt = 'LINESTRING M (30 10 1, 10 30 2, 40 40 3)'; + geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('LineString'); + expect(geom.getCoordinates()).to.eql([[30, 10, 1], [10, 30, 2], [40, 40, 3]]); + }); + + it('LineString ZM read / written correctly', function() { + var wkt = 'LINESTRING ZM(30 10 1 0.1,10 30 2 0.1,40 40 3 0.1)'; + var geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('LineString'); + expect(geom.getCoordinates()).to.eql([[30, 10, 1, 0.1], [10, 30, 2, 0.1], [40, 40, 3, 0.1]]); + expect(format.writeGeometry(geom)).to.eql(wkt); + // test whitespace when reading + wkt = 'LINESTRING ZM (30 10 1 0.1, 10 30 2 0.1, 40 40 3 0.1)'; + geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('LineString'); + expect(geom.getCoordinates()).to.eql([[30, 10, 1, 0.1], [10, 30, 2, 0.1], [40, 40, 3, 0.1]]); + }); + it('MultiLineString read / written correctly', function() { var wkt = 'MULTILINESTRING((10 10,20 20,10 40),' + '(40 40,30 30,40 20,30 10))'; @@ -197,6 +335,78 @@ describe('ol.format.WKT', function() { [[10, 10], [20, 20], [10, 40]]); }); + it('MultiLineString Z read / written correctly', function() { + var wkt = 'MULTILINESTRING Z((10 10 1,20 20 2,10 40 3),' + + '(40 40 1,30 30 2,40 20 3,30 10 4))'; + var geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('MultiLineString'); + var linestrings = geom.getLineStrings(); + expect(linestrings.length).to.eql(2); + expect(linestrings[0].getType()).to.eql('LineString'); + expect(linestrings[0].getCoordinates()).to.eql( + [[10, 10, 1], [20, 20, 2], [10, 40, 3]]); + expect(format.writeGeometry(geom)).to.eql(wkt); + // test whitespace when reading + wkt = 'MULTILINESTRING Z ( (10 10 1, 20 20 2, 10 40 3), ' + + '(40 40 1, 30 30 2, 40 20 3, 30 10 4) )'; + geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('MultiLineString'); + linestrings = geom.getLineStrings(); + expect(linestrings.length).to.eql(2); + expect(linestrings[0].getType()).to.eql( + 'LineString'); + expect(linestrings[0].getCoordinates()).to.eql( + [[10, 10, 1], [20, 20, 2], [10, 40, 3]]); + }); + + it('MultiLineString M read / written correctly', function() { + var wkt = 'MULTILINESTRING M((10 10 1,20 20 2,10 40 3),' + + '(40 40 1,30 30 2,40 20 3,30 10 4))'; + var geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('MultiLineString'); + var linestrings = geom.getLineStrings(); + expect(linestrings.length).to.eql(2); + expect(linestrings[0].getType()).to.eql('LineString'); + expect(linestrings[0].getCoordinates()).to.eql( + [[10, 10, 1], [20, 20, 2], [10, 40, 3]]); + expect(format.writeGeometry(geom)).to.eql(wkt); + // test whitespace when reading + wkt = 'MULTILINESTRING M ( (10 10 1, 20 20 2, 10 40 3), ' + + '(40 40 1, 30 30 2, 40 20 3, 30 10 4) )'; + geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('MultiLineString'); + linestrings = geom.getLineStrings(); + expect(linestrings.length).to.eql(2); + expect(linestrings[0].getType()).to.eql( + 'LineString'); + expect(linestrings[0].getCoordinates()).to.eql( + [[10, 10, 1], [20, 20, 2], [10, 40, 3]]); + }); + + it('MultiLineString ZM read / written correctly', function() { + var wkt = 'MULTILINESTRING ZM((10 10 1 0.1,20 20 2 0.1,10 40 3 0.1),' + + '(40 40 1 0.1,30 30 2 0.1,40 20 3 0.1,30 10 4 0.1))'; + var geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('MultiLineString'); + var linestrings = geom.getLineStrings(); + expect(linestrings.length).to.eql(2); + expect(linestrings[0].getType()).to.eql('LineString'); + expect(linestrings[0].getCoordinates()).to.eql( + [[10, 10, 1, 0.1], [20, 20, 2, 0.1], [10, 40, 3, 0.1]]); + expect(format.writeGeometry(geom)).to.eql(wkt); + // test whitespace when reading + wkt = 'MULTILINESTRING ZM ( (10 10 1 0.1, 20 20 2 0.1, 10 40 3 0.1), ' + + '(40 40 1 0.1, 30 30 2 0.1, 40 20 3 0.1, 30 10 4 0.1) )'; + geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('MultiLineString'); + linestrings = geom.getLineStrings(); + expect(linestrings.length).to.eql(2); + expect(linestrings[0].getType()).to.eql( + 'LineString'); + expect(linestrings[0].getCoordinates()).to.eql( + [[10, 10, 1, 0.1], [20, 20, 2, 0.1], [10, 40, 3, 0.1]]); + }); + it('Polygon read / written correctly', function() { var wkt = 'POLYGON((30 10,10 20,20 40,40 40,30 10))'; var geom = format.readGeometry(wkt); @@ -233,6 +443,114 @@ describe('ol.format.WKT', function() { [[30, 10], [10, 20], [20, 40], [40, 40], [30, 10]]); }); + it('Polygon Z read / written correctly', function() { + var wkt = 'POLYGON Z((30 10 1,10 20 2,20 40 3,40 40 4,30 10 1))'; + var geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('Polygon'); + var rings = geom.getLinearRings(); + expect(rings.length).to.eql(1); + expect(rings[0].getType()).to.eql('LinearRing'); + expect(rings[0].getCoordinates()).to.eql( + [[30, 10, 1], [10, 20, 2], [20, 40, 3], [40, 40, 4], [30, 10, 1]]); + expect(format.writeGeometry(geom)).to.eql(wkt); + + // note that WKT doesn't care about winding order, we do + wkt = 'POLYGON Z((35 10 1,10 20 2,15 40 3,45 45 4,35 10 1),(20 30 1,30 20 2,35 35 3,20 30 1))'; + geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('Polygon'); + rings = geom.getLinearRings(); + expect(rings.length).to.eql(2); + expect(rings[0].getType()).to.eql('LinearRing'); + expect(rings[1].getType()).to.eql('LinearRing'); + expect(rings[0].getCoordinates()).to.eql( + [[35, 10, 1], [10, 20, 2], [15, 40, 3], [45, 45, 4], [35, 10, 1]]); + expect(rings[1].getCoordinates()).to.eql( + [[20, 30, 1], [30, 20, 2], [35, 35, 3], [20, 30, 1]]); + expect(format.writeGeometry(geom)).to.eql(wkt); + + // test whitespace when reading + wkt = 'POLYGON Z ( (30 10 1, 10 20 2, 20 40 3, 40 40 4, 30 10 1) )'; + geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('Polygon'); + rings = geom.getLinearRings(); + expect(rings.length).to.eql(1); + expect(rings[0].getType()).to.eql('LinearRing'); + expect(rings[0].getCoordinates()).to.eql( + [[30, 10, 1], [10, 20, 2], [20, 40, 3], [40, 40, 4], [30, 10, 1]]); + }); + + it('Polygon M read / written correctly', function() { + var wkt = 'POLYGON M((30 10 1,10 20 2,20 40 3,40 40 4,30 10 1))'; + var geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('Polygon'); + var rings = geom.getLinearRings(); + expect(rings.length).to.eql(1); + expect(rings[0].getType()).to.eql('LinearRing'); + expect(rings[0].getCoordinates()).to.eql( + [[30, 10, 1], [10, 20, 2], [20, 40, 3], [40, 40, 4], [30, 10, 1]]); + expect(format.writeGeometry(geom)).to.eql(wkt); + + // note that WKT doesn't care about winding order, we do + wkt = 'POLYGON M((35 10 1,10 20 2,15 40 3,45 45 4,35 10 1),(20 30 1,30 20 2,35 35 3,20 30 1))'; + geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('Polygon'); + rings = geom.getLinearRings(); + expect(rings.length).to.eql(2); + expect(rings[0].getType()).to.eql('LinearRing'); + expect(rings[1].getType()).to.eql('LinearRing'); + expect(rings[0].getCoordinates()).to.eql( + [[35, 10, 1], [10, 20, 2], [15, 40, 3], [45, 45, 4], [35, 10, 1]]); + expect(rings[1].getCoordinates()).to.eql( + [[20, 30, 1], [30, 20, 2], [35, 35, 3], [20, 30, 1]]); + expect(format.writeGeometry(geom)).to.eql(wkt); + + // test whitespace when reading + wkt = 'POLYGON M ( (30 10 1, 10 20 2, 20 40 3, 40 40 4, 30 10 1) )'; + geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('Polygon'); + rings = geom.getLinearRings(); + expect(rings.length).to.eql(1); + expect(rings[0].getType()).to.eql('LinearRing'); + expect(rings[0].getCoordinates()).to.eql( + [[30, 10, 1], [10, 20, 2], [20, 40, 3], [40, 40, 4], [30, 10, 1]]); + }); + + it('Polygon ZM read / written correctly', function() { + var wkt = 'POLYGON ZM((30 10 1 0.1,10 20 2 0.1,20 40 3 0.1,40 40 4 0.1,30 10 1 0.1))'; + var geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('Polygon'); + var rings = geom.getLinearRings(); + expect(rings.length).to.eql(1); + expect(rings[0].getType()).to.eql('LinearRing'); + expect(rings[0].getCoordinates()).to.eql( + [[30, 10, 1, 0.1], [10, 20, 2, 0.1], [20, 40, 3, 0.1], [40, 40, 4, 0.1], [30, 10, 1, 0.1]]); + expect(format.writeGeometry(geom)).to.eql(wkt); + + // note that WKT doesn't care about winding order, we do + wkt = 'POLYGON ZM((35 10 1 0.1,10 20 2 0.1,15 40 3 0.1,45 45 4 0.1,35 10 1 0.1),(20 30 1 0.1,30 20 2 0.1,35 35 3 0.1,20 30 1 0.1))'; + geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('Polygon'); + rings = geom.getLinearRings(); + expect(rings.length).to.eql(2); + expect(rings[0].getType()).to.eql('LinearRing'); + expect(rings[1].getType()).to.eql('LinearRing'); + expect(rings[0].getCoordinates()).to.eql( + [[35, 10, 1, 0.1], [10, 20, 2, 0.1], [15, 40, 3, 0.1], [45, 45, 4, 0.1], [35, 10, 1, 0.1]]); + expect(rings[1].getCoordinates()).to.eql( + [[20, 30, 1, 0.1], [30, 20, 2, 0.1], [35, 35, 3, 0.1], [20, 30, 1, 0.1]]); + expect(format.writeGeometry(geom)).to.eql(wkt); + + // test whitespace when reading + wkt = 'POLYGON ZM ( (30 10 1 0.1, 10 20 2 0.1, 20 40 3 0.1, 40 40 4 0.1, 30 10 1 0.1) )'; + geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('Polygon'); + rings = geom.getLinearRings(); + expect(rings.length).to.eql(1); + expect(rings[0].getType()).to.eql('LinearRing'); + expect(rings[0].getCoordinates()).to.eql( + [[30, 10, 1, 0.1], [10, 20, 2, 0.1], [20, 40, 3, 0.1], [40, 40, 4, 0.1], [30, 10, 1, 0.1]]); + }); + it('MultiPolygon read / written correctly', function() { // note that WKT doesn't care about winding order, we do var wkt = 'MULTIPOLYGON(((40 40,45 30,20 45,40 40)),' + @@ -273,6 +591,126 @@ describe('ol.format.WKT', function() { [[30, 20], [20, 25], [20, 15], [30, 20]]); }); + it('MultiPolygon Z read / written correctly', function() { + // note that WKT doesn't care about winding order, we do + var wkt = 'MULTIPOLYGON Z(((40 40 1,45 30 2,20 45 3,40 40 1)),' + + '((20 35 1,45 20 2,30 5 3,10 10 4,10 30 5,20 35 1),(30 20 1,20 25 2,20 15 3,30 20 1)))'; + var geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('MultiPolygon'); + var polygons = geom.getPolygons(); + expect(polygons.length).to.eql(2); + expect(polygons[0].getType()).to.eql('Polygon'); + expect(polygons[1].getType()).to.eql('Polygon'); + expect(polygons[0].getLinearRings().length).to.eql(1); + expect(polygons[1].getLinearRings().length).to.eql(2); + expect(polygons[0].getLinearRings()[0].getCoordinates()).to.eql( + [[40, 40 , 1], [45, 30, 2], [20, 45, 3], [40, 40, 1]]); + expect(polygons[1].getLinearRings()[0].getCoordinates()).to.eql( + [[20, 35, 1], [45, 20, 2], [30, 5, 3], [10, 10, 4], [10, 30, 5], [20, 35, 1]]); + expect(polygons[1].getLinearRings()[1].getCoordinates()).to.eql( + [[30, 20, 1], [20, 25, 2], [20, 15, 3], [30, 20, 1]]); + expect(format.writeGeometry(geom)).to.eql(wkt); + + // test whitespace when reading + wkt = 'MULTIPOLYGON Z ( ( ( 40 40 1,45 30 2, 20 45 3 ,40 40 1 )) ,' + + '( (20 35 1, 45 20 2,30 5 3,10 10 4,10 30 5,20 35 1), ' + + '( 30 20 1, 20 25 2,20 15 3 ,30 20 1 ) ))'; + geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('MultiPolygon'); + polygons = geom.getPolygons(); + expect(polygons.length).to.eql(2); + expect(polygons[0].getType()).to.eql('Polygon'); + expect(polygons[1].getType()).to.eql('Polygon'); + expect(polygons[0].getLinearRings().length).to.eql(1); + expect(polygons[1].getLinearRings().length).to.eql(2); + expect(polygons[0].getLinearRings()[0].getCoordinates()).to.eql( + [[40, 40, 1], [45, 30, 2], [20, 45, 3], [40, 40, 1]]); + expect(polygons[1].getLinearRings()[0].getCoordinates()).to.eql( + [[20, 35, 1], [45, 20, 2], [30, 5, 3], [10, 10, 4], [10, 30, 5], [20, 35, 1]]); + expect(polygons[1].getLinearRings()[1].getCoordinates()).to.eql( + [[30, 20, 1], [20, 25, 2], [20, 15, 3], [30, 20, 1]]); + }); + + it('MultiPolygon M read / written correctly', function() { + // note that WKT doesn't care about winding order, we do + var wkt = 'MULTIPOLYGON M(((40 40 1,45 30 2,20 45 3,40 40 1)),' + + '((20 35 1,45 20 2,30 5 3,10 10 4,10 30 5,20 35 1),(30 20 1,20 25 2,20 15 3,30 20 1)))'; + var geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('MultiPolygon'); + var polygons = geom.getPolygons(); + expect(polygons.length).to.eql(2); + expect(polygons[0].getType()).to.eql('Polygon'); + expect(polygons[1].getType()).to.eql('Polygon'); + expect(polygons[0].getLinearRings().length).to.eql(1); + expect(polygons[1].getLinearRings().length).to.eql(2); + expect(polygons[0].getLinearRings()[0].getCoordinates()).to.eql( + [[40, 40 , 1], [45, 30, 2], [20, 45, 3], [40, 40, 1]]); + expect(polygons[1].getLinearRings()[0].getCoordinates()).to.eql( + [[20, 35, 1], [45, 20, 2], [30, 5, 3], [10, 10, 4], [10, 30, 5], [20, 35, 1]]); + expect(polygons[1].getLinearRings()[1].getCoordinates()).to.eql( + [[30, 20, 1], [20, 25, 2], [20, 15, 3], [30, 20, 1]]); + expect(format.writeGeometry(geom)).to.eql(wkt); + + // test whitespace when reading + wkt = 'MULTIPOLYGON M ( ( ( 40 40 1,45 30 2, 20 45 3 ,40 40 1 )) ,' + + '( (20 35 1, 45 20 2,30 5 3,10 10 4,10 30 5,20 35 1), ' + + '( 30 20 1, 20 25 2,20 15 3 ,30 20 1 ) ))'; + geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('MultiPolygon'); + polygons = geom.getPolygons(); + expect(polygons.length).to.eql(2); + expect(polygons[0].getType()).to.eql('Polygon'); + expect(polygons[1].getType()).to.eql('Polygon'); + expect(polygons[0].getLinearRings().length).to.eql(1); + expect(polygons[1].getLinearRings().length).to.eql(2); + expect(polygons[0].getLinearRings()[0].getCoordinates()).to.eql( + [[40, 40, 1], [45, 30, 2], [20, 45, 3], [40, 40, 1]]); + expect(polygons[1].getLinearRings()[0].getCoordinates()).to.eql( + [[20, 35, 1], [45, 20, 2], [30, 5, 3], [10, 10, 4], [10, 30, 5], [20, 35, 1]]); + expect(polygons[1].getLinearRings()[1].getCoordinates()).to.eql( + [[30, 20, 1], [20, 25, 2], [20, 15, 3], [30, 20, 1]]); + }); + + it('MultiPolygon ZM read / written correctly', function() { + // note that WKT doesn't care about winding order, we do + var wkt = 'MULTIPOLYGON ZM(((40 40 1 0.1,45 30 2 0.1,20 45 3 0.1,40 40 1 0.1)),' + + '((20 35 1 0.1,45 20 2 0.1,30 5 3 0.1,10 10 4 0.1,10 30 5 0.1,20 35 1 0.1),(30 20 1 0.1,20 25 2 0.1,20 15 3 0.1,30 20 1 0.1)))'; + var geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('MultiPolygon'); + var polygons = geom.getPolygons(); + expect(polygons.length).to.eql(2); + expect(polygons[0].getType()).to.eql('Polygon'); + expect(polygons[1].getType()).to.eql('Polygon'); + expect(polygons[0].getLinearRings().length).to.eql(1); + expect(polygons[1].getLinearRings().length).to.eql(2); + expect(polygons[0].getLinearRings()[0].getCoordinates()).to.eql( + [[40, 40 , 1, 0.1], [45, 30, 2, 0.1], [20, 45, 3, 0.1], [40, 40, 1, 0.1]]); + expect(polygons[1].getLinearRings()[0].getCoordinates()).to.eql( + [[20, 35, 1, 0.1], [45, 20, 2, 0.1], [30, 5, 3, 0.1], [10, 10, 4, 0.1], [10, 30, 5, 0.1], [20, 35, 1, 0.1]]); + expect(polygons[1].getLinearRings()[1].getCoordinates()).to.eql( + [[30, 20, 1, 0.1], [20, 25, 2, 0.1], [20, 15, 3, 0.1], [30, 20, 1, 0.1]]); + expect(format.writeGeometry(geom)).to.eql(wkt); + + // test whitespace when reading + wkt = 'MULTIPOLYGON ZM ( ( ( 40 40 1 0.1,45 30 2 0.1, 20 45 3 0.1 ,40 40 1 0.1 )) ,' + + '( (20 35 1 0.1, 45 20 2 0.1,30 5 3 0.1,10 10 4 0.1,10 30 5 0.1,20 35 1 0.1), ' + + '( 30 20 1 0.1, 20 25 2 0.1,20 15 3 0.1 ,30 20 1 0.1 ) ))'; + geom = format.readGeometry(wkt); + expect(geom.getType()).to.eql('MultiPolygon'); + polygons = geom.getPolygons(); + expect(polygons.length).to.eql(2); + expect(polygons[0].getType()).to.eql('Polygon'); + expect(polygons[1].getType()).to.eql('Polygon'); + expect(polygons[0].getLinearRings().length).to.eql(1); + expect(polygons[1].getLinearRings().length).to.eql(2); + expect(polygons[0].getLinearRings()[0].getCoordinates()).to.eql( + [[40, 40, 1, 0.1], [45, 30, 2, 0.1], [20, 45, 3, 0.1], [40, 40, 1, 0.1]]); + expect(polygons[1].getLinearRings()[0].getCoordinates()).to.eql( + [[20, 35, 1, 0.1], [45, 20, 2, 0.1], [30, 5, 3, 0.1], [10, 10, 4, 0.1], [10, 30, 5, 0.1], [20, 35, 1, 0.1]]); + expect(polygons[1].getLinearRings()[1].getCoordinates()).to.eql( + [[30, 20, 1, 0.1], [20, 25, 2, 0.1], [20, 15, 3, 0.1], [30, 20, 1, 0.1]]); + }); + it('Empty geometries read / written correctly', function() { var wkts = [ 'POINT', 'LINESTRING', 'POLYGON', 'MULTIPOINT', 'MULTILINESTRING', 'MULTIPOLYGON'