Factor out ol.geom.flat.area

This commit is contained in:
Tom Payne
2014-03-12 12:08:24 +01:00
parent 20ee1dc992
commit becd1318bd
7 changed files with 105 additions and 87 deletions

View File

@@ -0,0 +1,66 @@
goog.provide('ol.geom.flat.area');
goog.require('ol.geom.flat');
/**
* @param {Array.<number>} 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.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.
* @param {Array.<number>} 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.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.
* @param {Array.<Array.<number>>} 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;
};

View File

@@ -198,28 +198,6 @@ ol.geom.flat.inflateCoordinatesss =
};
/**
* @param {Array.<number>} 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.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.
@@ -275,25 +253,6 @@ ol.geom.flat.linearRingIsClockwise =
};
/**
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.
* @param {Array.<number>} 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.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.
@@ -434,26 +393,6 @@ ol.geom.flat.linearRingssAreOriented =
};
/**
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.
* @param {Array.<Array.<number>>} 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.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.

View File

@@ -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);
};

View File

@@ -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);
};

View File

@@ -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);
};

View File

@@ -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');

View File

@@ -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() {