Add ol.geom.flatLinearRingIsClockwise
This commit is contained in:
@@ -360,6 +360,31 @@ ol.geom.flatLinearRingContainsXY =
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Array.<number>} flatCoordinates Flat coordinates.
|
||||||
|
* @param {number} offset Offset.
|
||||||
|
* @param {number} end End.
|
||||||
|
* @param {number} stride Stride.
|
||||||
|
* @return {boolean} Is clockwise.
|
||||||
|
*/
|
||||||
|
ol.geom.flatLinearRingIsClockwise =
|
||||||
|
function(flatCoordinates, offset, end, stride) {
|
||||||
|
// http://tinyurl.com/clockwise-method
|
||||||
|
// https://github.com/OSGeo/gdal/blob/trunk/gdal/ogr/ogrlinearring.cpp
|
||||||
|
var edge = 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];
|
||||||
|
edge += (x2 - x1) * (y2 + y1);
|
||||||
|
x1 = x2;
|
||||||
|
y1 = y2;
|
||||||
|
}
|
||||||
|
return edge > 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Array.<number>} flatCoordinates Flat coordinates.
|
* @param {Array.<number>} flatCoordinates Flat coordinates.
|
||||||
* @param {number} offset Offset.
|
* @param {number} offset Offset.
|
||||||
|
|||||||
@@ -54,6 +54,24 @@ describe('ol.geom', function() {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('ol.geom.flatLinearRingIsClockwise', function() {
|
||||||
|
|
||||||
|
it('identifies clockwise rings', function() {
|
||||||
|
var flatCoordinates = [0, 1, 1, 4, 4, 3, 3, 0];
|
||||||
|
var isClockwise = ol.geom.flatLinearRingIsClockwise(
|
||||||
|
flatCoordinates, 0, flatCoordinates.length, 2);
|
||||||
|
expect(isClockwise).to.be(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('identifies anti-clockwise rings', function() {
|
||||||
|
var flatCoordinates = [2, 2, 3, 2, 3, 3, 2, 3];
|
||||||
|
var isClockwise = ol.geom.flatLinearRingIsClockwise(
|
||||||
|
flatCoordinates, 0, flatCoordinates.length, 2);
|
||||||
|
expect(isClockwise).to.be(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
describe('ol.geom.reverseFlatCoordinates', function() {
|
describe('ol.geom.reverseFlatCoordinates', function() {
|
||||||
|
|
||||||
describe('with a stride of 2', function() {
|
describe('with a stride of 2', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user