Files
openlayers/test/spec/ol/geom/polygon.test.js
Tim Schaub 33457c48de Add transform method to geometries
In the typical sequence of parse-transform-render the most efficient place to transform coordinate values is deep within the parser immediately after values have been read (this would avoid a second pass over whatever structure is used to back geometries).  To accomplish this transform during parsing, we could add back parser read options to pass the transform function around.

Until then, a transform method on geometries is straightforward to implement.  This means we do a second pass through coordinate structures to transform, but this is typically done only once immediately after parsing.
2013-09-27 23:18:34 +01:00

155 lines
4.4 KiB
JavaScript

goog.provide('ol.test.geom.Polygon');
describe('ol.geom.Polygon', function() {
var outer = [[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]],
inner1 = [[1, 1], [2, 1], [2, 2], [1, 2], [1, 1]],
inner2 = [[8, 8], [9, 8], [9, 9], [8, 9], [8, 8]];
describe('constructor', function() {
it('creates a polygon from an array', function() {
var poly = new ol.geom.Polygon([outer, inner1, inner2]);
expect(poly).to.be.a(ol.geom.Polygon);
expect(poly).to.be.a(ol.geom.Geometry);
});
});
describe('#rings', function() {
it('is an array of LinearRing', function() {
var poly = new ol.geom.Polygon([outer, inner1, inner2]);
expect(poly.rings.length).to.be(3);
expect(poly.rings[0]).to.be.a(ol.geom.LinearRing);
expect(poly.rings[1]).to.be.a(ol.geom.LinearRing);
expect(poly.rings[2]).to.be.a(ol.geom.LinearRing);
});
var isClockwise = ol.geom.LinearRing.isClockwise;
it('forces exterior ring to be clockwise', function() {
var outer = [[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]];
expect(isClockwise(outer)).to.be(false);
var poly = new ol.geom.Polygon([outer]);
var ring = poly.rings[0];
expect(isClockwise(ring.getCoordinates())).to.be(true);
});
it('forces interior ring to be counter-clockwise', function() {
var outer = [[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]];
var inner = [[2, 2], [2, 8], [8, 8], [8, 2], [2, 2]];
expect(isClockwise(inner)).to.be(true);
var poly = new ol.geom.Polygon([outer, inner]);
var ring = poly.rings[1];
expect(isClockwise(ring.getCoordinates())).to.be(false);
});
});
describe('#dimension', function() {
it('can be 2', function() {
var poly = new ol.geom.Polygon([outer, inner1, inner2]);
expect(poly.dimension).to.be(2);
});
it('can be 3', function() {
var poly = new ol.geom.Polygon([[[10, 20, 30], [40, 50, 60]]]);
expect(poly.dimension).to.be(3);
});
});
describe('#getBounds()', function() {
it('returns the bounding extent', function() {
var poly = new ol.geom.Polygon([outer, inner1, inner2]);
var bounds = poly.getBounds();
expect(bounds[0]).to.be(0);
expect(bounds[2]).to.be(10);
expect(bounds[1]).to.be(0);
expect(bounds[3]).to.be(10);
});
});
describe('#getCoordinates()', function() {
it('returns an array', function() {
var poly = new ol.geom.Polygon([outer, inner1, inner2]);
expect(poly.getCoordinates()).to.eql([outer, inner1, inner2]);
});
});
describe('#transform()', function() {
var forward = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
var inverse = ol.proj.getTransform('EPSG:3857', 'EPSG:4326');
var gg, sm;
beforeEach(function() {
gg = [
[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]],
[[1, 1], [2, 1], [2, 2], [1, 2], [1, 1]],
[[8, 8], [9, 8], [9, 9], [8, 9], [8, 8]]
];
sm = [[
[0, 0], [0, 1118890], [1113195, 1118890], [1113195, 0], [0, 0]
], [
[111319, 111325], [222639, 111325], [222639, 222684],
[111319, 222684], [111319, 111325]
], [
[890556, 893464], [1001875, 893464], [1001875, 1006021],
[890556, 1006021], [890556, 893464]
]];
});
it('forward transforms a polygon in place', function() {
var poly = new ol.geom.Polygon(gg);
poly.transform(forward);
var coordinates = poly.getCoordinates();
var ring;
for (var i = 0, ii = coordinates.length; i < ii; ++i) {
var ring = coordinates[i];
for (var j = 0, jj = ring.length; j < jj; ++j) {
expect(ring[j][0]).to.roughlyEqual(sm[i][j][0], 1);
expect(ring[j][1]).to.roughlyEqual(sm[i][j][1], 1);
}
}
});
it('inverse transforms a polygon in place', function() {
var poly = new ol.geom.Polygon(sm);
poly.transform(inverse);
var coordinates = poly.getCoordinates();
var ring;
for (var i = 0, ii = coordinates.length; i < ii; ++i) {
var ring = coordinates[i];
for (var j = 0, jj = ring.length; j < jj; ++j) {
expect(ring[j][0]).to.roughlyEqual(gg[i][j][0], 0.001);
expect(ring[j][1]).to.roughlyEqual(gg[i][j][1], 0.001);
}
}
});
});
});
goog.require('ol.geom.Geometry');
goog.require('ol.geom.LinearRing');
goog.require('ol.geom.Polygon');
goog.require('ol.proj');