From e90776a0998bdce4ea9226b05752b287f5d9c442 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 2 Dec 2013 12:05:19 +0100 Subject: [PATCH] Add ol.geom.flat.linearRingsArea --- src/ol/geom/flatgeom.js | 25 +++++++++++++++++++++++++ test/spec/ol/geom/flatgeom.test.js | 10 ++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index 93913e83e0..43215c08aa 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -371,6 +371,31 @@ ol.geom.flat.linearRingPerimeter = }; +/** + * @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]; + var linearRingArea = + ol.geom.flat.linearRingArea(flatCoordinates, offset, end, stride); + if (i === 0) { + area += linearRingArea; + } else { + area -= linearRingArea; + } + offset = end; + } + return area; +}; + + /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. diff --git a/test/spec/ol/geom/flatgeom.test.js b/test/spec/ol/geom/flatgeom.test.js index 9d709d12dd..ce08820e21 100644 --- a/test/spec/ol/geom/flatgeom.test.js +++ b/test/spec/ol/geom/flatgeom.test.js @@ -159,6 +159,16 @@ 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, 1, 2, 2, 2, 2, 1], 0, [8, 16], 2); + expect(area).to.be(8); + }); + + }); + describe('ol.geom.flat.reverseCoordinates', function() { describe('with a stride of 2', function() {