From 23dc855c2f3067a98324bb20db19d1d46aaf7f0f Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 10 Mar 2014 15:01:40 +0100 Subject: [PATCH 01/16] Check arguments passed to ol.geom.Polygon#setFlatCoordinates --- src/ol/geom/polygon.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ol/geom/polygon.js b/src/ol/geom/polygon.js index 667a121e43..a068cb6ec4 100644 --- a/src/ol/geom/polygon.js +++ b/src/ol/geom/polygon.js @@ -1,5 +1,6 @@ goog.provide('ol.geom.Polygon'); +goog.require('goog.asserts'); goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); goog.require('ol.geom.LinearRing'); @@ -257,6 +258,13 @@ ol.geom.Polygon.prototype.setCoordinates = function(coordinates, opt_layout) { */ ol.geom.Polygon.prototype.setFlatCoordinates = function(layout, flatCoordinates, ends) { + if (goog.isNull(flatCoordinates)) { + goog.asserts.assert(!goog.isNull(ends) && ends.length === 0); + } else if (ends.length === 0) { + goog.asserts.assert(flatCoordinates.length === 0); + } else { + goog.asserts.assert(flatCoordinates.length == ends[ends.length - 1]); + } this.setFlatCoordinatesInternal(layout, flatCoordinates); this.ends_ = ends; this.dispatchChangeEvent(); From ee5a9d6a4e7cac503e3a160d9a873d8b9bdecafe Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 10 Mar 2014 15:01:52 +0100 Subject: [PATCH 02/16] Check arguments passed to ol.geom.MultiLineString#setFlatCoordinates --- src/ol/geom/multilinestring.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/ol/geom/multilinestring.js b/src/ol/geom/multilinestring.js index b096e2fe65..c075a5bd6b 100644 --- a/src/ol/geom/multilinestring.js +++ b/src/ol/geom/multilinestring.js @@ -222,6 +222,13 @@ ol.geom.MultiLineString.prototype.setCoordinates = */ ol.geom.MultiLineString.prototype.setFlatCoordinates = function(layout, flatCoordinates, ends) { + if (goog.isNull(flatCoordinates)) { + goog.asserts.assert(!goog.isNull(ends) && ends.length === 0); + } else if (ends.length === 0) { + goog.asserts.assert(flatCoordinates.length === 0); + } else { + goog.asserts.assert(flatCoordinates.length == ends[ends.length - 1]); + } this.setFlatCoordinatesInternal(layout, flatCoordinates); this.ends_ = ends; this.dispatchChangeEvent(); From 470790b8114a162dd632071345790231180a1be4 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 10 Mar 2014 15:05:09 +0100 Subject: [PATCH 03/16] Check arguments passed to ol.geom.MultiPolygon#setFlatCoordinates --- src/ol/geom/multipolygon.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ol/geom/multipolygon.js b/src/ol/geom/multipolygon.js index 214398858a..8c5b4cd885 100644 --- a/src/ol/geom/multipolygon.js +++ b/src/ol/geom/multipolygon.js @@ -274,6 +274,14 @@ ol.geom.MultiPolygon.prototype.setCoordinates = */ ol.geom.MultiPolygon.prototype.setFlatCoordinates = function(layout, flatCoordinates, endss) { + goog.asserts.assert(!goog.isNull(endss)); + if (goog.isNull(flatCoordinates) || flatCoordinates.length === 0) { + goog.asserts.assert(endss.length === 0); + } else { + goog.asserts.assert(endss.length > 0); + var ends = endss[endss.length - 1]; + goog.asserts.assert(flatCoordinates.length == ends[ends.length - 1]); + } this.setFlatCoordinatesInternal(layout, flatCoordinates); this.endss_ = endss; this.dispatchChangeEvent(); From 94cfb3205a1102735cbbbcae8463fd8e247adcf7 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 10 Mar 2014 15:11:59 +0100 Subject: [PATCH 04/16] Add ol.geom.LineString#appendCoordinate --- src/ol/geom/linestring.exports | 1 + src/ol/geom/linestring.js | 16 ++++++++++++++++ test/spec/ol/geom/linestring.test.js | 7 +++++++ 3 files changed, 24 insertions(+) diff --git a/src/ol/geom/linestring.exports b/src/ol/geom/linestring.exports index f5c50bdf5c..f1f95ce7c1 100644 --- a/src/ol/geom/linestring.exports +++ b/src/ol/geom/linestring.exports @@ -1,4 +1,5 @@ @exportSymbol ol.geom.LineString +@exportProperty ol.geom.LineString.prototype.appendCoordinate @exportProperty ol.geom.LineString.prototype.clone @exportProperty ol.geom.LineString.prototype.getCoordinateAtM @exportProperty ol.geom.LineString.prototype.getCoordinates diff --git a/src/ol/geom/linestring.js b/src/ol/geom/linestring.js index 989c9c88bb..8ac6ec912e 100644 --- a/src/ol/geom/linestring.js +++ b/src/ol/geom/linestring.js @@ -1,5 +1,7 @@ goog.provide('ol.geom.LineString'); +goog.require('goog.array'); +goog.require('goog.asserts'); goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); goog.require('ol.geom.SimpleGeometry'); @@ -50,6 +52,20 @@ ol.geom.LineString = function(coordinates, opt_layout) { goog.inherits(ol.geom.LineString, ol.geom.SimpleGeometry); +/** + * @param {ol.Coordinate} coordinate Coordinate. + */ +ol.geom.LineString.prototype.appendCoordinate = function(coordinate) { + goog.asserts.assert(coordinate.length == this.stride); + if (goog.isNull(this.flatCoordinates)) { + this.flatCoordinates = coordinate.slice(); + } else { + goog.array.extend(this.flatCoordinates, coordinate); + } + this.dispatchChangeEvent(); +}; + + /** * @inheritDoc */ diff --git a/test/spec/ol/geom/linestring.test.js b/test/spec/ol/geom/linestring.test.js index a09ee352be..f4546bc59e 100644 --- a/test/spec/ol/geom/linestring.test.js +++ b/test/spec/ol/geom/linestring.test.js @@ -37,6 +37,13 @@ describe('ol.geom.LineString', function() { expect(lineString.getStride()).to.be(2); }); + it('can append coordinates', function() { + lineString.appendCoordinate([1, 2]); + expect(lineString.getCoordinates()).to.eql([[1, 2]]); + lineString.appendCoordinate([3, 4]); + expect(lineString.getCoordinates()).to.eql([[1, 2], [3, 4]]); + }); + }); describe('construct with 2D coordinates', function() { From 03911e6c6bdeea56d894c34e5260a13a08376751 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 10 Mar 2014 15:25:31 +0100 Subject: [PATCH 05/16] Add ol.geom.Polygon#appendLinearRing --- src/ol/geom/polygon.exports | 1 + src/ol/geom/polygon.js | 16 ++++++++++++++++ test/spec/ol/geom/polygon.test.js | 11 +++++++++++ 3 files changed, 28 insertions(+) diff --git a/src/ol/geom/polygon.exports b/src/ol/geom/polygon.exports index 7500491823..9f3fa03a83 100644 --- a/src/ol/geom/polygon.exports +++ b/src/ol/geom/polygon.exports @@ -1,4 +1,5 @@ @exportSymbol ol.geom.Polygon +@exportProperty ol.geom.Polygon.prototype.appendLinearRing @exportProperty ol.geom.Polygon.prototype.clone @exportProperty ol.geom.Polygon.prototype.getArea @exportProperty ol.geom.Polygon.prototype.getCoordinates diff --git a/src/ol/geom/polygon.js b/src/ol/geom/polygon.js index a068cb6ec4..dea5c42022 100644 --- a/src/ol/geom/polygon.js +++ b/src/ol/geom/polygon.js @@ -1,5 +1,6 @@ goog.provide('ol.geom.Polygon'); +goog.require('goog.array'); goog.require('goog.asserts'); goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); @@ -71,6 +72,21 @@ ol.geom.Polygon = function(coordinates, opt_layout) { goog.inherits(ol.geom.Polygon, ol.geom.SimpleGeometry); +/** + * @param {ol.geom.LinearRing} linearRing Linear ring. + */ +ol.geom.Polygon.prototype.appendLinearRing = function(linearRing) { + goog.asserts.assert(linearRing.getLayout() == this.layout); + if (goog.isNull(this.flatCoordinates)) { + this.flatCoordinates = linearRing.getFlatCoordinates().slice(); + } else { + goog.array.extend(this.flatCoordinates, linearRing.getFlatCoordinates()); + } + this.ends_.push(this.flatCoordinates.length); + this.dispatchChangeEvent(); +}; + + /** * @inheritDoc */ diff --git a/test/spec/ol/geom/polygon.test.js b/test/spec/ol/geom/polygon.test.js index a245dc21ca..ace0feb311 100644 --- a/test/spec/ol/geom/polygon.test.js +++ b/test/spec/ol/geom/polygon.test.js @@ -37,6 +37,17 @@ describe('ol.geom.Polygon', function() { expect(polygon.getStride()).to.be(2); }); + it('can append linear rings', function() { + polygon.appendLinearRing( + new ol.geom.LinearRing([[1, 2], [3, 4], [5, 6]])); + expect(polygon.getCoordinates()).to.eql( + [[[1, 2], [3, 4], [5, 6]]]); + polygon.appendLinearRing( + new ol.geom.LinearRing([[7, 8], [9, 10], [11, 12]])); + expect(polygon.getCoordinates()).to.eql( + [[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10], [11, 12]]]); + }); + }); describe('construct with 2D coordinates', function() { From c2d4ffaba1b76333c18b7b596545421ff30ad556 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 10 Mar 2014 15:40:05 +0100 Subject: [PATCH 06/16] Add ol.geom.Polygon#getLinearRing --- src/ol/geom/polygon.exports | 1 + src/ol/geom/polygon.js | 16 ++++++++++++++++ test/spec/ol/geom/polygon.test.js | 5 +++++ 3 files changed, 22 insertions(+) diff --git a/src/ol/geom/polygon.exports b/src/ol/geom/polygon.exports index 9f3fa03a83..214d5e02c6 100644 --- a/src/ol/geom/polygon.exports +++ b/src/ol/geom/polygon.exports @@ -4,6 +4,7 @@ @exportProperty ol.geom.Polygon.prototype.getArea @exportProperty ol.geom.Polygon.prototype.getCoordinates @exportProperty ol.geom.Polygon.prototype.getInteriorPoint +@exportProperty ol.geom.Polygon.prototype.getLinearRing @exportProperty ol.geom.Polygon.prototype.getLinearRings @exportProperty ol.geom.Polygon.prototype.getType @exportProperty ol.geom.Polygon.prototype.setCoordinates diff --git a/src/ol/geom/polygon.js b/src/ol/geom/polygon.js index dea5c42022..1cd2d1fd4e 100644 --- a/src/ol/geom/polygon.js +++ b/src/ol/geom/polygon.js @@ -178,6 +178,22 @@ ol.geom.Polygon.prototype.getInteriorPoint = function() { }; +/** + * @param {number} index Index. + * @return {ol.geom.LinearRing} Linear ring. + */ +ol.geom.Polygon.prototype.getLinearRing = function(index) { + goog.asserts.assert(0 <= index && index < this.ends_.length); + if (index < 0 || this.ends_.length <= index) { + return null; + } + var linearRing = new ol.geom.LinearRing(null); + linearRing.setFlatCoordinates(this.layout, this.flatCoordinates.slice( + index === 0 ? 0 : this.ends_[index - 1], this.ends_[index])); + return linearRing; +}; + + /** * @return {Array.} Linear rings. * @todo stability experimental diff --git a/test/spec/ol/geom/polygon.test.js b/test/spec/ol/geom/polygon.test.js index ace0feb311..fd1254dcb5 100644 --- a/test/spec/ol/geom/polygon.test.js +++ b/test/spec/ol/geom/polygon.test.js @@ -84,6 +84,11 @@ describe('ol.geom.Polygon', function() { expect(polygon.getStride()).to.be(2); }); + it('can return individual rings', function() { + expect(polygon.getLinearRing(0).getCoordinates()).to.eql(outerRing); + expect(polygon.getLinearRing(1).getCoordinates()).to.eql(innerRing); + }); + it('has the expected rings', function() { var linearRings = polygon.getLinearRings(); expect(linearRings).to.be.an(Array); From 033e2e7dd84e9fc9237e472fb1a0d51858788f2f Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 10 Mar 2014 15:45:18 +0100 Subject: [PATCH 07/16] Add ol.geom.MultiPoint#appendPoint --- src/ol/geom/multipoint.exports | 1 + src/ol/geom/multipoint.js | 16 ++++++++++++++++ test/spec/ol/geom/multipoint.test.js | 8 ++++++++ 3 files changed, 25 insertions(+) diff --git a/src/ol/geom/multipoint.exports b/src/ol/geom/multipoint.exports index 97dff56f42..11a911aabb 100644 --- a/src/ol/geom/multipoint.exports +++ b/src/ol/geom/multipoint.exports @@ -1,4 +1,5 @@ @exportSymbol ol.geom.MultiPoint +@exportProperty ol.geom.MultiPoint.prototype.appendPoint @exportProperty ol.geom.MultiPoint.prototype.clone @exportProperty ol.geom.MultiPoint.prototype.getCoordinates @exportProperty ol.geom.MultiPoint.prototype.getPoints diff --git a/src/ol/geom/multipoint.js b/src/ol/geom/multipoint.js index ad1d767e7a..7903c010b2 100644 --- a/src/ol/geom/multipoint.js +++ b/src/ol/geom/multipoint.js @@ -1,5 +1,7 @@ goog.provide('ol.geom.MultiPoint'); +goog.require('goog.array'); +goog.require('goog.asserts'); goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); goog.require('ol.geom.Point'); @@ -22,6 +24,20 @@ ol.geom.MultiPoint = function(coordinates, opt_layout) { goog.inherits(ol.geom.MultiPoint, ol.geom.SimpleGeometry); +/** + * @param {ol.geom.Point} point Point. + */ +ol.geom.MultiPoint.prototype.appendPoint = function(point) { + goog.asserts.assert(point.getLayout() == this.layout); + if (goog.isNull(this.flatCoordinates)) { + this.flatCoordinates = point.getFlatCoordinates().slice(); + } else { + goog.array.extend(this.flatCoordinates, point.getFlatCoordinates()); + } + this.dispatchChangeEvent(); +}; + + /** * @inheritDoc */ diff --git a/test/spec/ol/geom/multipoint.test.js b/test/spec/ol/geom/multipoint.test.js index 336b5558c9..476fabb6d7 100644 --- a/test/spec/ol/geom/multipoint.test.js +++ b/test/spec/ol/geom/multipoint.test.js @@ -37,6 +37,13 @@ describe('ol.geom.MultiPoint', function() { expect(multiPoint.getStride()).to.be(2); }); + it('can append points', function() { + multiPoint.appendPoint(new ol.geom.Point([1, 2])); + expect(multiPoint.getCoordinates()).to.eql([[1, 2]]); + multiPoint.appendPoint(new ol.geom.Point([3, 4])); + expect(multiPoint.getCoordinates()).to.eql([[1, 2], [3, 4]]); + }); + }); describe('construct with 2D coordinates', function() { @@ -170,3 +177,4 @@ describe('ol.geom.MultiPoint', function() { goog.require('ol.extent'); goog.require('ol.geom.MultiPoint'); +goog.require('ol.geom.Point'); From 361d31b5965938d4a2291efcae11cbbddf2b0bc8 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 10 Mar 2014 15:55:49 +0100 Subject: [PATCH 08/16] Construct individual points directly from flat coordinates --- src/ol/geom/multipoint.js | 12 ++++++++---- test/spec/ol/geom/multipoint.test.js | 11 +++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/ol/geom/multipoint.js b/src/ol/geom/multipoint.js index 7903c010b2..871dfa2ce5 100644 --- a/src/ol/geom/multipoint.js +++ b/src/ol/geom/multipoint.js @@ -90,12 +90,16 @@ ol.geom.MultiPoint.prototype.getCoordinates = function() { * @todo stability experimental */ ol.geom.MultiPoint.prototype.getPoints = function() { - // FIXME we should construct the points from the flat coordinates - var coordinates = this.getCoordinates(); + var flatCoordinates = this.flatCoordinates; + var layout = this.layout; + var stride = this.stride; + /** @type {Array.} */ var points = []; var i, ii; - for (i = 0, ii = coordinates.length; i < ii; ++i) { - points.push(new ol.geom.Point(coordinates[i])); + for (i = 0, ii = flatCoordinates.length; i < ii; i += stride) { + var point = new ol.geom.Point(null); + point.setFlatCoordinates(layout, flatCoordinates.slice(i, i + stride)); + points.push(point); } return points; }; diff --git a/test/spec/ol/geom/multipoint.test.js b/test/spec/ol/geom/multipoint.test.js index 476fabb6d7..5c2f66148e 100644 --- a/test/spec/ol/geom/multipoint.test.js +++ b/test/spec/ol/geom/multipoint.test.js @@ -132,6 +132,17 @@ describe('ol.geom.MultiPoint', function() { expect(multiPoint.getStride()).to.be(3); }); + it('can return all points', function() { + var points = multiPoint.getPoints(); + expect(points).to.have.length(2); + expect(points[0]).to.be.an(ol.geom.Point); + expect(points[0].getLayout()).to.be(ol.geom.GeometryLayout.XYM); + expect(points[0].getCoordinates()).to.eql([1, 2, 3]); + expect(points[1]).to.be.an(ol.geom.Point); + expect(points[1].getLayout()).to.be(ol.geom.GeometryLayout.XYM); + expect(points[1].getCoordinates()).to.eql([4, 5, 6]); + }); + }); describe('construct with 4D coordinates', function() { From ddfce24f0594fe7f0f6430c5d21960c69bbe0272 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 10 Mar 2014 15:56:16 +0100 Subject: [PATCH 09/16] Add ol.geom.MultiPoint#getPoint --- src/ol/geom/multipoint.exports | 1 + src/ol/geom/multipoint.js | 18 ++++++++++++++++++ test/spec/ol/geom/multipoint.test.js | 9 +++++++++ 3 files changed, 28 insertions(+) diff --git a/src/ol/geom/multipoint.exports b/src/ol/geom/multipoint.exports index 11a911aabb..b00ddcae09 100644 --- a/src/ol/geom/multipoint.exports +++ b/src/ol/geom/multipoint.exports @@ -2,6 +2,7 @@ @exportProperty ol.geom.MultiPoint.prototype.appendPoint @exportProperty ol.geom.MultiPoint.prototype.clone @exportProperty ol.geom.MultiPoint.prototype.getCoordinates +@exportProperty ol.geom.MultiPoint.prototype.getPoint @exportProperty ol.geom.MultiPoint.prototype.getPoints @exportProperty ol.geom.MultiPoint.prototype.getType @exportProperty ol.geom.MultiPoint.prototype.setCoordinates diff --git a/src/ol/geom/multipoint.js b/src/ol/geom/multipoint.js index 871dfa2ce5..931761f350 100644 --- a/src/ol/geom/multipoint.js +++ b/src/ol/geom/multipoint.js @@ -85,6 +85,24 @@ ol.geom.MultiPoint.prototype.getCoordinates = function() { }; +/** + * @param {number} index Index. + * @return {ol.geom.Point} Point. + */ +ol.geom.MultiPoint.prototype.getPoint = function(index) { + var n = goog.isNull(this.flatCoordinates) ? + 0 : this.flatCoordinates.length / this.stride; + goog.asserts.assert(0 <= index && index < n); + if (index < 0 || n <= index) { + return null; + } + var point = new ol.geom.Point(null); + point.setFlatCoordinates(this.layout, this.flatCoordinates.slice( + index * this.stride, (index + 1) * this.stride)); + return point; +}; + + /** * @return {Array.} Points. * @todo stability experimental diff --git a/test/spec/ol/geom/multipoint.test.js b/test/spec/ol/geom/multipoint.test.js index 5c2f66148e..26535273c6 100644 --- a/test/spec/ol/geom/multipoint.test.js +++ b/test/spec/ol/geom/multipoint.test.js @@ -132,6 +132,15 @@ describe('ol.geom.MultiPoint', function() { expect(multiPoint.getStride()).to.be(3); }); + it('can return individual points', function() { + var point0 = multiPoint.getPoint(0); + expect(point0.getLayout()).to.be(ol.geom.GeometryLayout.XYM); + expect(point0.getCoordinates()).to.eql([1, 2, 3]); + var point1 = multiPoint.getPoint(1); + expect(point1.getLayout()).to.be(ol.geom.GeometryLayout.XYM); + expect(point1.getCoordinates()).to.eql([4, 5, 6]); + }); + it('can return all points', function() { var points = multiPoint.getPoints(); expect(points).to.have.length(2); From 669f21ca29bdbc379ef21d4aa51e1053fbfdb97c Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 10 Mar 2014 16:04:05 +0100 Subject: [PATCH 10/16] Construct individual line strings directly from flat coordinates --- src/ol/geom/multilinestring.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/ol/geom/multilinestring.js b/src/ol/geom/multilinestring.js index c075a5bd6b..ee773a1c51 100644 --- a/src/ol/geom/multilinestring.js +++ b/src/ol/geom/multilinestring.js @@ -136,12 +136,19 @@ ol.geom.MultiLineString.prototype.getEnds = function() { * @todo stability experimental */ ol.geom.MultiLineString.prototype.getLineStrings = function() { - // FIXME we should construct the line strings from the flat coordinates - var coordinates = this.getCoordinates(); + var flatCoordinates = this.flatCoordinates; + var ends = this.ends_; + var layout = this.layout; + /** @type {Array.} */ var lineStrings = []; + var offset = 0; var i, ii; - for (i = 0, ii = coordinates.length; i < ii; ++i) { - lineStrings.push(new ol.geom.LineString(coordinates[i])); + for (i = 0, ii = ends.length; i < ii; ++i) { + var end = ends[i]; + var lineString = new ol.geom.LineString(null); + lineString.setFlatCoordinates(layout, flatCoordinates.slice(offset, end)); + lineStrings.push(lineString); + offset = end; } return lineStrings; }; From 7ee4fb8a2e9e95a7669349aef1b1199b5a9a7ca3 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 10 Mar 2014 16:13:06 +0100 Subject: [PATCH 11/16] Add ol.geom.MultiLineString#getLineString --- src/ol/geom/multilinestring.exports | 1 + src/ol/geom/multilinestring.js | 16 ++++++++++++++++ test/spec/ol/geom/multilinestring.test.js | 11 +++++++++++ 3 files changed, 28 insertions(+) diff --git a/src/ol/geom/multilinestring.exports b/src/ol/geom/multilinestring.exports index 23b3e5987e..aa3eb024a0 100644 --- a/src/ol/geom/multilinestring.exports +++ b/src/ol/geom/multilinestring.exports @@ -2,6 +2,7 @@ @exportProperty ol.geom.MultiLineString.prototype.clone @exportProperty ol.geom.MultiLineString.prototype.getCoordinateAtM @exportProperty ol.geom.MultiLineString.prototype.getCoordinates +@exportProperty ol.geom.MultiLineString.prototype.getLineString @exportProperty ol.geom.MultiLineString.prototype.getLineStrings @exportProperty ol.geom.MultiLineString.prototype.getType @exportProperty ol.geom.MultiLineString.prototype.setCoordinates diff --git a/src/ol/geom/multilinestring.js b/src/ol/geom/multilinestring.js index ee773a1c51..4c825db9a3 100644 --- a/src/ol/geom/multilinestring.js +++ b/src/ol/geom/multilinestring.js @@ -131,6 +131,22 @@ ol.geom.MultiLineString.prototype.getEnds = function() { }; +/** + * @param {number} index Index. + * @return {ol.geom.LineString} LineString. + */ +ol.geom.MultiLineString.prototype.getLineString = function(index) { + goog.asserts.assert(0 <= index && index < this.ends_.length); + if (index < 0 || this.ends_.length <= index) { + return null; + } + var lineString = new ol.geom.LineString(null); + lineString.setFlatCoordinates(this.layout, this.flatCoordinates.slice( + index === 0 ? 0 : this.ends_[index - 1], this.ends_[index])); + return lineString; +}; + + /** * @return {Array.} LineStrings. * @todo stability experimental diff --git a/test/spec/ol/geom/multilinestring.test.js b/test/spec/ol/geom/multilinestring.test.js index 8fe2d205a0..cfa2a2c853 100644 --- a/test/spec/ol/geom/multilinestring.test.js +++ b/test/spec/ol/geom/multilinestring.test.js @@ -142,6 +142,17 @@ describe('ol.geom.MultiLineString', function() { expect(multiLineString.getStride()).to.be(3); }); + it('can return individual line strings', function() { + var lineString0 = multiLineString.getLineString(0); + expect(lineString0).to.be.an(ol.geom.LineString); + expect(lineString0.getLayout()).to.be(ol.geom.GeometryLayout.XYM); + expect(lineString0.getCoordinates()).to.eql([[1, 2, 3], [4, 5, 6]]); + var lineString1 = multiLineString.getLineString(1); + expect(lineString1).to.be.an(ol.geom.LineString); + expect(lineString1.getLayout()).to.be(ol.geom.GeometryLayout.XYM); + expect(lineString1.getCoordinates()).to.eql([[7, 8, 9], [10, 11, 12]]); + }); + describe('#getCoordinateAtM', function() { describe('with extrapolation and interpolation', function() { From 7a51b4c7b6ca24a9fe0797bc4eb9846da899ef1e Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 10 Mar 2014 16:18:12 +0100 Subject: [PATCH 12/16] Add ol.geom.MultiLineString#appendLineString --- src/ol/geom/multilinestring.exports | 1 + src/ol/geom/multilinestring.js | 16 ++++++++++++++++ test/spec/ol/geom/multilinestring.test.js | 11 +++++++++++ 3 files changed, 28 insertions(+) diff --git a/src/ol/geom/multilinestring.exports b/src/ol/geom/multilinestring.exports index aa3eb024a0..9390428f2a 100644 --- a/src/ol/geom/multilinestring.exports +++ b/src/ol/geom/multilinestring.exports @@ -1,4 +1,5 @@ @exportSymbol ol.geom.MultiLineString +@exportProperty ol.geom.MultiLineString.prototype.appendLineString @exportProperty ol.geom.MultiLineString.prototype.clone @exportProperty ol.geom.MultiLineString.prototype.getCoordinateAtM @exportProperty ol.geom.MultiLineString.prototype.getCoordinates diff --git a/src/ol/geom/multilinestring.js b/src/ol/geom/multilinestring.js index 4c825db9a3..816abef889 100644 --- a/src/ol/geom/multilinestring.js +++ b/src/ol/geom/multilinestring.js @@ -47,6 +47,22 @@ ol.geom.MultiLineString = function(coordinates, opt_layout) { goog.inherits(ol.geom.MultiLineString, ol.geom.SimpleGeometry); +/** + * @param {ol.geom.LineString} lineString LineString. + */ +ol.geom.MultiLineString.prototype.appendLineString = function(lineString) { + goog.asserts.assert(lineString.getLayout() == this.layout); + if (goog.isNull(this.flatCoordinates)) { + this.flatCoordinates = lineString.getFlatCoordinates().slice(); + } else { + goog.array.extend( + this.flatCoordinates, lineString.getFlatCoordinates().slice()); + } + this.ends_.push(this.flatCoordinates.length); + this.dispatchChangeEvent(); +}; + + /** * @inheritDoc */ diff --git a/test/spec/ol/geom/multilinestring.test.js b/test/spec/ol/geom/multilinestring.test.js index cfa2a2c853..76d18f9ec1 100644 --- a/test/spec/ol/geom/multilinestring.test.js +++ b/test/spec/ol/geom/multilinestring.test.js @@ -37,6 +37,17 @@ describe('ol.geom.MultiLineString', function() { expect(multiLineString.getStride()).to.be(2); }); + it('can append line strings', function() { + multiLineString.appendLineString( + new ol.geom.LineString([[1, 2], [3, 4]])); + expect(multiLineString.getCoordinates()).to.eql( + [[[1, 2], [3, 4]]]); + multiLineString.appendLineString( + new ol.geom.LineString([[5, 6], [7, 8]])); + expect(multiLineString.getCoordinates()).to.eql( + [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]); + }); + }); describe('construct with 2D coordinates', function() { From cb27ae582621a131775926769138fda62c5b77e4 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 10 Mar 2014 16:34:42 +0100 Subject: [PATCH 13/16] Add test for ol.geom.MultiPolygon#getPolygons --- test/spec/ol/geom/multipolygon.test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/spec/ol/geom/multipolygon.test.js b/test/spec/ol/geom/multipolygon.test.js index 564ff9e90d..b515b1d475 100644 --- a/test/spec/ol/geom/multipolygon.test.js +++ b/test/spec/ol/geom/multipolygon.test.js @@ -20,6 +20,18 @@ describe('ol.geom.MultiPolygon', function() { ]); }); + it('can return all polygons', function() { + var polygons = multiPolygon.getPolygons(); + expect(polygons).to.be.an(Array); + expect(polygons).to.have.length(2); + expect(polygons[0]).to.be.an(ol.geom.Polygon); + expect(polygons[0].getCoordinates()).to.eql( + [[[0, 0], [0, 2], [1, 1], [2, 0]]]); + expect(polygons[1]).to.be.an(ol.geom.Polygon); + expect(polygons[1].getCoordinates()).to.eql( + [[[3, 0], [4, 1], [5, 2], [5, 0]]]); + }); + describe('#getSimplifiedGeometry', function() { it('returns the expected result', function() { @@ -38,3 +50,4 @@ describe('ol.geom.MultiPolygon', function() { goog.require('ol.geom.MultiPolygon'); +goog.require('ol.geom.Polygon'); From 719384ad1444bcb3d94268cad914153069a80f96 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 10 Mar 2014 16:36:35 +0100 Subject: [PATCH 14/16] Tidy up ol.geom.MultiPolygon#getPolygons --- src/ol/geom/multipolygon.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/ol/geom/multipolygon.js b/src/ol/geom/multipolygon.js index 8c5b4cd885..ebab3694e1 100644 --- a/src/ol/geom/multipolygon.js +++ b/src/ol/geom/multipolygon.js @@ -217,17 +217,16 @@ ol.geom.MultiPolygon.prototype.getPolygons = function() { var offset = 0; var i, ii, j, jj; for (i = 0, ii = endss.length; i < ii; ++i) { - var ends = endss[i]; + var ends = endss[i].slice(); var end = ends[ends.length - 1]; - var polygon = new ol.geom.Polygon(null); - var polygonEnds = ends.slice(); if (offset !== 0) { - for (j = 0, jj = polygonEnds.length; j < jj; ++j) { - polygonEnds[j] = polygonEnds[j] - offset; + for (j = 0, jj = ends.length; j < jj; ++j) { + ends[j] -= offset; } } + var polygon = new ol.geom.Polygon(null); polygon.setFlatCoordinates( - layout, flatCoordinates.slice(offset, end), polygonEnds); + layout, flatCoordinates.slice(offset, end), ends); polygons.push(polygon); offset = end; } From c8bbeca06e23b3f8b567268abcdd2ce5ed1a6bf7 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 10 Mar 2014 16:40:19 +0100 Subject: [PATCH 15/16] Add ol.geom.MultiPolygon#getPolygon --- src/ol/geom/multipolygon.exports | 1 + src/ol/geom/multipolygon.js | 31 ++++++++++++++++++++++++++ test/spec/ol/geom/multipolygon.test.js | 11 +++++++++ 3 files changed, 43 insertions(+) diff --git a/src/ol/geom/multipolygon.exports b/src/ol/geom/multipolygon.exports index 07d0d094b8..13168e0436 100644 --- a/src/ol/geom/multipolygon.exports +++ b/src/ol/geom/multipolygon.exports @@ -3,6 +3,7 @@ @exportProperty ol.geom.MultiPolygon.prototype.getArea @exportProperty ol.geom.MultiPolygon.prototype.getCoordinates @exportProperty ol.geom.MultiPolygon.prototype.getInteriorPoints +@exportProperty ol.geom.MultiPolygon.prototype.getPolygon @exportProperty ol.geom.MultiPolygon.prototype.getPolygons @exportProperty ol.geom.MultiPolygon.prototype.getType @exportProperty ol.geom.MultiPolygon.prototype.setCoordinates diff --git a/src/ol/geom/multipolygon.js b/src/ol/geom/multipolygon.js index ebab3694e1..f70cf78d6c 100644 --- a/src/ol/geom/multipolygon.js +++ b/src/ol/geom/multipolygon.js @@ -205,6 +205,37 @@ ol.geom.MultiPolygon.prototype.getSimplifiedGeometryInternal = }; +/** + * @param {number} index Index. + * @return {ol.geom.Polygon} Polygon. + */ +ol.geom.MultiPolygon.prototype.getPolygon = function(index) { + goog.asserts.assert(0 <= index && index < this.endss_.length); + if (index < 0 || this.endss_.length <= index) { + return null; + } + var offset; + if (index === 0) { + offset = 0; + } else { + var prevEnds = this.endss_[index - 1]; + offset = prevEnds[prevEnds.length - 1]; + } + var ends = this.endss_[index].slice(); + var end = ends[ends.length - 1]; + if (offset !== 0) { + var i, ii; + for (i = 0, ii = ends.length; i < ii; ++i) { + ends[i] -= offset; + } + } + var polygon = new ol.geom.Polygon(null); + polygon.setFlatCoordinates( + this.layout, this.flatCoordinates.slice(offset, end), ends); + return polygon; +}; + + /** * @return {Array.} Polygons. * @todo stability experimental diff --git a/test/spec/ol/geom/multipolygon.test.js b/test/spec/ol/geom/multipolygon.test.js index b515b1d475..0c21f9fb00 100644 --- a/test/spec/ol/geom/multipolygon.test.js +++ b/test/spec/ol/geom/multipolygon.test.js @@ -20,6 +20,17 @@ describe('ol.geom.MultiPolygon', function() { ]); }); + it('can return individual polygons', function() { + var polygon0 = multiPolygon.getPolygon(0); + expect(polygon0).to.be.an(ol.geom.Polygon); + expect(polygon0.getCoordinates()).to.eql( + [[[0, 0], [0, 2], [1, 1], [2, 0]]]); + var polygon1 = multiPolygon.getPolygon(1); + expect(polygon1).to.be.an(ol.geom.Polygon); + expect(polygon1.getCoordinates()).to.eql( + [[[3, 0], [4, 1], [5, 2], [5, 0]]]); + }); + it('can return all polygons', function() { var polygons = multiPolygon.getPolygons(); expect(polygons).to.be.an(Array); From b77f0e7d3aee8cdfb50f4febb78b4a7748336a21 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 10 Mar 2014 16:53:12 +0100 Subject: [PATCH 16/16] Add ol.geom.MultiPolygon#appendPolygon --- src/ol/geom/multipolygon.exports | 1 + src/ol/geom/multipolygon.js | 25 +++++++++++++++++++++++++ test/spec/ol/geom/multipolygon.test.js | 22 ++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/src/ol/geom/multipolygon.exports b/src/ol/geom/multipolygon.exports index 13168e0436..6f67842bc3 100644 --- a/src/ol/geom/multipolygon.exports +++ b/src/ol/geom/multipolygon.exports @@ -1,4 +1,5 @@ @exportSymbol ol.geom.MultiPolygon +@exportProperty ol.geom.MultiPolygon.prototype.appendPolygon @exportProperty ol.geom.MultiPolygon.prototype.clone @exportProperty ol.geom.MultiPolygon.prototype.getArea @exportProperty ol.geom.MultiPolygon.prototype.getCoordinates diff --git a/src/ol/geom/multipolygon.js b/src/ol/geom/multipolygon.js index f70cf78d6c..acd04669da 100644 --- a/src/ol/geom/multipolygon.js +++ b/src/ol/geom/multipolygon.js @@ -72,6 +72,31 @@ ol.geom.MultiPolygon = function(coordinates, opt_layout) { goog.inherits(ol.geom.MultiPolygon, ol.geom.SimpleGeometry); +/** + * @param {ol.geom.Polygon} polygon Polygon. + */ +ol.geom.MultiPolygon.prototype.appendPolygon = function(polygon) { + goog.asserts.assert(polygon.getLayout() == this.layout); + /** @type {Array.} */ + var ends; + if (goog.isNull(this.flatCoordinates)) { + this.flatCoordinates = polygon.getFlatCoordinates().slice(); + ends = polygon.getEnds().slice(); + this.endss_.push(); + } else { + var offset = this.flatCoordinates.length; + goog.array.extend(this.flatCoordinates, polygon.getFlatCoordinates()); + ends = polygon.getEnds().slice(); + var i, ii; + for (i = 0, ii = ends.length; i < ii; ++i) { + ends[i] += offset; + } + } + this.endss_.push(ends); + this.dispatchChangeEvent(); +}; + + /** * @inheritDoc */ diff --git a/test/spec/ol/geom/multipolygon.test.js b/test/spec/ol/geom/multipolygon.test.js index 0c21f9fb00..03dad6ab5b 100644 --- a/test/spec/ol/geom/multipolygon.test.js +++ b/test/spec/ol/geom/multipolygon.test.js @@ -10,6 +10,28 @@ describe('ol.geom.MultiPolygon', function() { }).not.to.throwException(); }); + describe('with an empty MultiPolygon', function() { + + var multiPolygon; + beforeEach(function() { + multiPolygon = new ol.geom.MultiPolygon(null); + }); + + it('can append polygons', function() { + multiPolygon.appendPolygon( + new ol.geom.Polygon([[[0, 0], [0, 2], [1, 1], [2, 0]]])); + expect(multiPolygon.getCoordinates()).to.eql( + [[[[0, 0], [0, 2], [1, 1], [2, 0]]]]); + multiPolygon.appendPolygon( + new ol.geom.Polygon([[[3, 0], [4, 1], [5, 2], [5, 0]]])); + expect(multiPolygon.getCoordinates()).to.eql([ + [[[0, 0], [0, 2], [1, 1], [2, 0]]], + [[[3, 0], [4, 1], [5, 2], [5, 0]]] + ]); + }); + + }); + describe('with a simple MultiPolygon', function() { var multiPolygon;