From 07173c493e0702bc63a2cdadd61fa268e40744e7 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Sat, 9 Nov 2013 17:26:05 +0100 Subject: [PATCH] Refactor ol.geom.Polygon --- src/ol/geom/polygon.js | 58 +++----- test/spec/ol/format/geojsonformat.test.js | 2 +- test/spec/ol/geom/polygon.test.js | 166 ++++++++++++++++++++++ 3 files changed, 188 insertions(+), 38 deletions(-) create mode 100644 test/spec/ol/geom/polygon.test.js diff --git a/src/ol/geom/polygon.js b/src/ol/geom/polygon.js index eb1ad7651f..e0447a2155 100644 --- a/src/ol/geom/polygon.js +++ b/src/ol/geom/polygon.js @@ -1,7 +1,5 @@ goog.provide('ol.geom.Polygon'); -goog.require('goog.asserts'); -goog.require('ol.extent'); goog.require('ol.geom.Geometry'); @@ -9,40 +7,39 @@ goog.require('ol.geom.Geometry'); /** * @constructor * @extends {ol.geom.Geometry} - * @param {ol.geom.RawPolygon} rings Rings. + * @param {ol.geom.RawPolygon} coordinates Coordinates. + * @param {ol.geom.Layout=} opt_layout Layout. */ -ol.geom.Polygon = function(rings) { +ol.geom.Polygon = function(coordinates, opt_layout) { goog.base(this); /** + * @type {Array.} * @private - * @type {ol.geom.RawPolygon} */ - this.rings_ = rings; + this.ends_ = []; + + this.setCoordinates(coordinates, opt_layout); }; goog.inherits(ol.geom.Polygon, ol.geom.Geometry); /** - * @inheritDoc + * @return {ol.geom.RawPolygon} Coordinates. */ -ol.geom.Polygon.prototype.getExtent = function(opt_extent) { - if (this.extentRevision != this.revision) { - this.extent = ol.extent.createOrUpdateFromRings(this.rings_, this.extent); - this.extentRevision = this.revision; - } - goog.asserts.assert(goog.isDef(this.extent)); - return ol.extent.returnOrUpdate(this.extent, opt_extent); +ol.geom.Polygon.prototype.getCoordinates = function() { + return ol.geom.inflateCoordinatess( + this.flatCoordinates, 0, this.ends_, this.stride); }; /** - * @return {ol.geom.RawPolygon} Rings. + * @return {Array.} Ends. */ -ol.geom.Polygon.prototype.getRings = function() { - return this.rings_; +ol.geom.Polygon.prototype.getEnds = function() { + return this.ends_; }; @@ -55,26 +52,13 @@ ol.geom.Polygon.prototype.getType = function() { /** - * @param {ol.geom.RawPolygon} rings Rings. + * @param {ol.geom.RawPolygon} coordinates Coordinates. + * @param {ol.geom.Layout=} opt_layout Layout. */ -ol.geom.Polygon.prototype.setRings = function(rings) { - this.rings_ = rings; +ol.geom.Polygon.prototype.setCoordinates = + function(coordinates, opt_layout) { + this.setLayout(opt_layout, coordinates, 2); + ol.geom.deflateCoordinatess( + this.flatCoordinates, 0, coordinates, this.stride, this.ends_); this.dispatchChangeEvent(); }; - - -/** - * @inheritDoc - */ -ol.geom.Polygon.prototype.transform = function(transformFn) { - var rings = this.rings_; - var i, ii; - for (i = 0, ii = rings.length; i < ii; ++i) { - var coordinates = rings[i]; - var j, jj; - for (j = 0, jj = coordinates.length; j < jj; ++j) { - var coordinate = coordinates[j]; - transformFn(coordinate, coordinate, 2); - } - } -}; diff --git a/test/spec/ol/format/geojsonformat.test.js b/test/spec/ol/format/geojsonformat.test.js index 155bcb02f4..e3027dcb1d 100644 --- a/test/spec/ol/format/geojsonformat.test.js +++ b/test/spec/ol/format/geojsonformat.test.js @@ -83,7 +83,7 @@ describe('ol.format.GeoJSON', function() { expect(feature).to.be.an(ol.Feature); var geometry = feature.getGeometry(); expect(geometry).to.be.an(ol.geom.Polygon); - expect(geometry.getRings()).to.eql([[ + expect(geometry.getCoordinates()).to.eql([[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]]); expect(feature.get('prop0')).to.be('value0'); diff --git a/test/spec/ol/geom/polygon.test.js b/test/spec/ol/geom/polygon.test.js new file mode 100644 index 0000000000..583ef6ede4 --- /dev/null +++ b/test/spec/ol/geom/polygon.test.js @@ -0,0 +1,166 @@ +goog.provide('ol.test.geom.Polygon'); + + +describe('ol.geom.Polygon', function() { + + describe('construct empty', function() { + + var polygon; + beforeEach(function() { + polygon = new ol.geom.Polygon([]); + }); + + it('defaults to layout XY', function() { + expect(polygon.getLayout()).to.be(ol.geom.Layout.XY); + }); + + it('has empty coordinates', function() { + expect(polygon.getCoordinates()).to.be.empty(); + }); + + it('has an empty extent', function() { + expect(ol.extent.isEmpty(polygon.getExtent())).to.be(true); + }); + + it('has empty flat coordinates', function() { + expect(polygon.getFlatCoordinates()).to.be.empty(); + }); + + it('has stride the expected stride', function() { + expect(polygon.getStride()).to.be(2); + }); + + }); + + describe('construct with 2D coordinates', function() { + + var polygon; + beforeEach(function() { + polygon = new ol.geom.Polygon([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]); + }); + + it('has the expected layout', function() { + expect(polygon.getLayout()).to.be(ol.geom.Layout.XY); + }); + + it('has the expected coordinates', function() { + expect(polygon.getCoordinates()).to.eql( + [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]); + }); + + it('has the expected extent', function() { + expect(polygon.getExtent()).to.eql([1, 2, 7, 8]); + }); + + it('has the expected flat coordinates', function() { + expect(polygon.getFlatCoordinates()).to.eql([1, 2, 3, 4, 5, 6, 7, 8]); + }); + + it('has stride the expected stride', function() { + expect(polygon.getStride()).to.be(2); + }); + + }); + + describe('construct with 3D coordinates', function() { + + var polygon; + beforeEach(function() { + polygon = new ol.geom.Polygon( + [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]); + }); + + it('has the expected layout', function() { + expect(polygon.getLayout()).to.be(ol.geom.Layout.XYZ); + }); + + it('has the expected coordinates', function() { + expect(polygon.getCoordinates()).to.eql( + [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]); + }); + + it('has the expected extent', function() { + expect(polygon.getExtent()).to.eql([1, 2, 10, 11]); + }); + + it('has the expected flat coordinates', function() { + expect(polygon.getFlatCoordinates()).to.eql( + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); + }); + + it('has stride the expected stride', function() { + expect(polygon.getStride()).to.be(3); + }); + + }); + + describe('construct with 3D coordinates and layout XYM', function() { + + var polygon; + beforeEach(function() { + polygon = new ol.geom.Polygon( + [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]], + ol.geom.Layout.XYM); + }); + + it('has the expected layout', function() { + expect(polygon.getLayout()).to.be(ol.geom.Layout.XYM); + }); + + it('has the expected coordinates', function() { + expect(polygon.getCoordinates()).to.eql( + [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]); + }); + + it('has the expected extent', function() { + expect(polygon.getExtent()).to.eql([1, 2, 10, 11]); + }); + + it('has the expected flat coordinates', function() { + expect(polygon.getFlatCoordinates()).to.eql( + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]); + }); + + it('has stride the expected stride', function() { + expect(polygon.getStride()).to.be(3); + }); + + }); + + describe('construct with 4D coordinates', function() { + + var polygon; + beforeEach(function() { + polygon = new ol.geom.Polygon( + [[[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 16]]]); + }); + + it('has the expected layout', function() { + expect(polygon.getLayout()).to.be(ol.geom.Layout.XYZM); + }); + + it('has the expected coordinates', function() { + expect(polygon.getCoordinates()).to.eql( + [[[1, 2, 3, 4], [5, 6, 7, 8]], [[9, 10, 11, 12], [13, 14, 15, 16]]]); + }); + + it('has the expected extent', function() { + expect(polygon.getExtent()).to.eql([1, 2, 13, 14]); + }); + + it('has the expected flat coordinates', function() { + expect(polygon.getFlatCoordinates()).to.eql( + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]); + }); + + it('has stride the expected stride', function() { + expect(polygon.getStride()).to.be(4); + }); + + }); + +}); + + +goog.require('ol.extent'); +goog.require('ol.geom.Polygon');