diff --git a/src/ol/geom/flat/areaflatgeom.js b/src/ol/geom/flat/areaflatgeom.js new file mode 100644 index 0000000000..54479f0fe1 --- /dev/null +++ b/src/ol/geom/flat/areaflatgeom.js @@ -0,0 +1,66 @@ +goog.provide('ol.geom.flat.area'); + +goog.require('ol.geom.flat'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @return {number} Area. + */ +ol.geom.flat.area.linearRing = function(flatCoordinates, offset, end, stride) { + var twiceArea = 0; + var x1 = flatCoordinates[end - stride]; + var y1 = flatCoordinates[end - stride + 1]; + for (; offset < end; offset += stride) { + var x2 = flatCoordinates[offset]; + var y2 = flatCoordinates[offset + 1]; + twiceArea += y1 * x2 - x1 * y2; + x1 = x2; + y1 = y2; + } + return twiceArea / 2; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} ends Ends. + * @param {number} stride Stride. + * @return {number} Area. + */ +ol.geom.flat.area.linearRings = + function(flatCoordinates, offset, ends, stride) { + var area = 0; + var i, ii; + for (i = 0, ii = ends.length; i < ii; ++i) { + var end = ends[i]; + area += ol.geom.flat.area.linearRing(flatCoordinates, offset, end, stride); + offset = end; + } + return area; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>} endss Endss. + * @param {number} stride Stride. + * @return {number} Area. + */ +ol.geom.flat.area.linearRingss = + function(flatCoordinates, offset, endss, stride) { + var area = 0; + var i, ii; + for (i = 0, ii = endss.length; i < ii; ++i) { + var ends = endss[i]; + area += + ol.geom.flat.area.linearRings(flatCoordinates, offset, ends, stride); + offset = ends[ends.length - 1]; + } + return area; +}; diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index 42df2388b7..4e69680230 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -198,28 +198,6 @@ ol.geom.flat.inflateCoordinatesss = }; -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @return {number} Area. - */ -ol.geom.flat.linearRingArea = function(flatCoordinates, offset, end, stride) { - var twiceArea = 0; - var x1 = flatCoordinates[end - stride]; - var y1 = flatCoordinates[end - stride + 1]; - for (; offset < end; offset += stride) { - var x2 = flatCoordinates[offset]; - var y2 = flatCoordinates[offset + 1]; - twiceArea += y1 * x2 - x1 * y2; - x1 = x2; - y1 = y2; - } - return twiceArea / 2; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. @@ -275,25 +253,6 @@ ol.geom.flat.linearRingIsClockwise = }; -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.} ends Ends. - * @param {number} stride Stride. - * @return {number} Area. - */ -ol.geom.flat.linearRingsArea = function(flatCoordinates, offset, ends, stride) { - var area = 0; - var i, ii; - for (i = 0, ii = ends.length; i < ii; ++i) { - var end = ends[i]; - area += ol.geom.flat.linearRingArea(flatCoordinates, offset, end, stride); - offset = end; - } - return area; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. @@ -434,26 +393,6 @@ ol.geom.flat.linearRingssAreOriented = }; -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>} endss Endss. - * @param {number} stride Stride. - * @return {number} Area. - */ -ol.geom.flat.linearRingssArea = - function(flatCoordinates, offset, endss, stride) { - var area = 0; - var i, ii; - for (i = 0, ii = endss.length; i < ii; ++i) { - var ends = endss[i]; - area += ol.geom.flat.linearRingsArea(flatCoordinates, offset, ends, stride); - offset = ends[ends.length - 1]; - } - return area; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. diff --git a/src/ol/geom/linearring.js b/src/ol/geom/linearring.js index 19e7214b9e..0b6158bbdb 100644 --- a/src/ol/geom/linearring.js +++ b/src/ol/geom/linearring.js @@ -4,6 +4,7 @@ goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.simplify'); @@ -73,7 +74,7 @@ ol.geom.LinearRing.prototype.closestPointXY = * @todo stability experimental */ ol.geom.LinearRing.prototype.getArea = function() { - return ol.geom.flat.linearRingArea( + return ol.geom.flat.area.linearRing( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride); }; diff --git a/src/ol/geom/multipolygon.js b/src/ol/geom/multipolygon.js index 9cc90a7a25..d37817582e 100644 --- a/src/ol/geom/multipolygon.js +++ b/src/ol/geom/multipolygon.js @@ -8,6 +8,7 @@ goog.require('ol.geom.MultiPoint'); goog.require('ol.geom.Polygon'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.simplify'); @@ -142,7 +143,7 @@ ol.geom.MultiPolygon.prototype.containsXY = function(x, y) { * @todo stability experimental */ ol.geom.MultiPolygon.prototype.getArea = function() { - return ol.geom.flat.linearRingssArea( + return ol.geom.flat.area.linearRingss( this.getOrientedFlatCoordinates(), 0, this.endss_, this.stride); }; diff --git a/src/ol/geom/polygon.js b/src/ol/geom/polygon.js index 3507cc5152..85c9e24cec 100644 --- a/src/ol/geom/polygon.js +++ b/src/ol/geom/polygon.js @@ -8,6 +8,7 @@ goog.require('ol.geom.LinearRing'); goog.require('ol.geom.Point'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.simplify'); @@ -132,7 +133,7 @@ ol.geom.Polygon.prototype.containsXY = function(x, y) { * @todo stability experimental */ ol.geom.Polygon.prototype.getArea = function() { - return ol.geom.flat.linearRingsArea( + return ol.geom.flat.area.linearRings( this.getOrientedFlatCoordinates(), 0, this.ends_, this.stride); }; diff --git a/test/spec/ol/geom/flat/areaflatgeom.test.js b/test/spec/ol/geom/flat/areaflatgeom.test.js new file mode 100644 index 0000000000..18a87e28d3 --- /dev/null +++ b/test/spec/ol/geom/flat/areaflatgeom.test.js @@ -0,0 +1,33 @@ +goog.provide('ol.test.geom.flat.area'); + +describe('ol.geom.flat.area', function() { + + describe('ol.geom.flat.area.linearRing', function() { + + it('calcaultes the area of a triangle', function() { + var area = ol.geom.flat.area.linearRing([0, 0, 0.5, 1, 1, 0], 0, 6, 2); + expect(area).to.be(0.5); + }); + + it('calculates the area of a unit square', function() { + var area = + ol.geom.flat.area.linearRing([0, 0, 0, 1, 1, 1, 1, 0], 0, 8, 2); + expect(area).to.be(1); + }); + + }); + + describe('ol.geom.flat.area.linearRings', function() { + + it('calculates the area with holes', function() { + var area = ol.geom.flat.area.linearRings( + [0, 0, 0, 3, 3, 3, 3, 0, 1, 1, 2, 1, 2, 2, 1, 2], 0, [8, 16], 2); + expect(area).to.be(8); + }); + + }); + +}); + +goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.area'); diff --git a/test/spec/ol/geom/flatgeom.test.js b/test/spec/ol/geom/flatgeom.test.js index 75328a9660..1ffa7ed5bc 100644 --- a/test/spec/ol/geom/flatgeom.test.js +++ b/test/spec/ol/geom/flatgeom.test.js @@ -83,19 +83,6 @@ describe('ol.geom.flat', function() { expect(coordinatess).to.eql([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]); }); - }); - describe('ol.geom.flat.linearRingArea', function() { - - it('calcaultes the area of a triangle', function() { - var area = ol.geom.flat.linearRingArea([0, 0, 0.5, 1, 1, 0], 0, 6, 2); - expect(area).to.be(0.5); - }); - - it('calculates the area of a unit square', function() { - var area = ol.geom.flat.linearRingArea([0, 0, 0, 1, 1, 1, 1, 0], 0, 8, 2); - expect(area).to.be(1); - }); - }); describe('ol.geom.flat.linearRingIsClockwise', function() { @@ -116,16 +103,6 @@ describe('ol.geom.flat', function() { }); - describe('ol.geom.flat.linearRingsArea', function() { - - it('calculates the area with holes', function() { - var area = ol.geom.flat.linearRingsArea( - [0, 0, 0, 3, 3, 3, 3, 0, 1, 1, 2, 1, 2, 2, 1, 2], 0, [8, 16], 2); - expect(area).to.be(8); - }); - - }); - describe('ol.geom.flat.reverseCoordinates', function() { describe('with a stride of 2', function() {