Add ol.geom.Polygon#getOrientedFlatCoordinates
This commit is contained in:
@@ -50,6 +50,18 @@ ol.geom.Polygon = function(coordinates, opt_layout) {
|
||||
*/
|
||||
this.maxDeltaRevision_ = -1;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {number}
|
||||
*/
|
||||
this.orientedRevision_ = -1;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Array.<number>}
|
||||
*/
|
||||
this.orientedFlatCoordinates_ = null;
|
||||
|
||||
this.setCoordinates(coordinates, opt_layout);
|
||||
|
||||
};
|
||||
@@ -151,6 +163,26 @@ ol.geom.Polygon.prototype.getLinearRings = function() {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {Array.<number>} Oriented flat coordinates.
|
||||
*/
|
||||
ol.geom.Polygon.prototype.getOrientedFlatCoordinates = function() {
|
||||
if (this.orientedRevision_ != this.getRevision()) {
|
||||
var flatCoordinates = this.flatCoordinates;
|
||||
if (ol.geom.flat.linearRingsAreOriented(
|
||||
flatCoordinates, 0, this.ends_, this.stride)) {
|
||||
this.orientedFlatCoordinates_ = flatCoordinates;
|
||||
} else {
|
||||
this.orientedFlatCoordinates_ = flatCoordinates.slice();
|
||||
this.orientedFlatCoordinates_.length = ol.geom.flat.orientLinearRings(
|
||||
this.orientedFlatCoordinates_, 0, this.ends_, this.stride);
|
||||
}
|
||||
this.orientedRevision_ = this.getRevision();
|
||||
}
|
||||
return this.orientedFlatCoordinates_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
|
||||
@@ -94,6 +94,29 @@ describe('ol.geom.Polygon', function() {
|
||||
expect(polygon.containsCoordinate(insideInner)).to.be(false);
|
||||
});
|
||||
|
||||
describe('#getOrientedFlatCoordinates', function() {
|
||||
|
||||
it('reverses the outer ring if necessary', function() {
|
||||
outerRing.reverse();
|
||||
polygon = new ol.geom.Polygon([outerRing, innerRing]);
|
||||
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
|
||||
});
|
||||
|
||||
it('reverses inner rings if necessary', function() {
|
||||
innerRing.reverse();
|
||||
polygon = new ol.geom.Polygon([outerRing, innerRing]);
|
||||
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
|
||||
});
|
||||
|
||||
it('reverses all rings if necessary', function() {
|
||||
outerRing.reverse();
|
||||
innerRing.reverse();
|
||||
polygon = new ol.geom.Polygon([outerRing, innerRing]);
|
||||
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('construct with 3D coordinates', function() {
|
||||
@@ -142,6 +165,29 @@ describe('ol.geom.Polygon', function() {
|
||||
expect(polygon.containsCoordinate(insideInner)).to.be(false);
|
||||
});
|
||||
|
||||
describe('#getOrientedFlatCoordinates', function() {
|
||||
|
||||
it('reverses the outer ring if necessary', function() {
|
||||
outerRing.reverse();
|
||||
polygon = new ol.geom.Polygon([outerRing, innerRing]);
|
||||
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
|
||||
});
|
||||
|
||||
it('reverses inner rings if necessary', function() {
|
||||
innerRing.reverse();
|
||||
polygon = new ol.geom.Polygon([outerRing, innerRing]);
|
||||
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
|
||||
});
|
||||
|
||||
it('reverses all rings if necessary', function() {
|
||||
outerRing.reverse();
|
||||
innerRing.reverse();
|
||||
polygon = new ol.geom.Polygon([outerRing, innerRing]);
|
||||
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('construct with 3D coordinates and layout XYM', function() {
|
||||
@@ -191,6 +237,29 @@ describe('ol.geom.Polygon', function() {
|
||||
expect(polygon.containsCoordinate(insideInner)).to.be(false);
|
||||
});
|
||||
|
||||
describe('#getOrientedFlatCoordinates', function() {
|
||||
|
||||
it('reverses the outer ring if necessary', function() {
|
||||
outerRing.reverse();
|
||||
polygon = new ol.geom.Polygon([outerRing, innerRing]);
|
||||
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
|
||||
});
|
||||
|
||||
it('reverses inner rings if necessary', function() {
|
||||
innerRing.reverse();
|
||||
polygon = new ol.geom.Polygon([outerRing, innerRing]);
|
||||
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
|
||||
});
|
||||
|
||||
it('reverses all rings if necessary', function() {
|
||||
outerRing.reverse();
|
||||
innerRing.reverse();
|
||||
polygon = new ol.geom.Polygon([outerRing, innerRing]);
|
||||
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('construct with 4D coordinates', function() {
|
||||
@@ -248,6 +317,31 @@ describe('ol.geom.Polygon', function() {
|
||||
expect(polygon.containsCoordinate(insideInner2)).to.be(false);
|
||||
});
|
||||
|
||||
describe('#getOrientedFlatCoordinates', function() {
|
||||
|
||||
it('reverses the outer ring if necessary', function() {
|
||||
outerRing.reverse();
|
||||
polygon = new ol.geom.Polygon([outerRing, innerRing1, innerRing2]);
|
||||
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
|
||||
});
|
||||
|
||||
it('reverses inner rings if necessary', function() {
|
||||
innerRing1.reverse();
|
||||
innerRing2.reverse();
|
||||
polygon = new ol.geom.Polygon([outerRing, innerRing1, innerRing2]);
|
||||
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
|
||||
});
|
||||
|
||||
it('reverses all rings if necessary', function() {
|
||||
outerRing.reverse();
|
||||
innerRing1.reverse();
|
||||
innerRing2.reverse();
|
||||
polygon = new ol.geom.Polygon([outerRing, innerRing1, innerRing2]);
|
||||
expect(polygon.getOrientedFlatCoordinates()).to.eql(flatCoordinates);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('with a simple polygon', function() {
|
||||
|
||||
Reference in New Issue
Block a user