Merge pull request #2036 from tschaub/transform

More convenient geometry transforms.
This commit is contained in:
Tim Schaub
2014-05-02 13:52:47 -06:00
18 changed files with 221 additions and 48 deletions

View File

@@ -425,12 +425,13 @@ describe('ol.extent', function() {
});
describe('transform', function() {
describe('#applyTransform()', function() {
it('does transform', function() {
var transformFn = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
var sourceExtent = [-15, -30, 45, 60];
var destinationExtent = ol.extent.transform(sourceExtent, transformFn);
var destinationExtent = ol.extent.applyTransform(
sourceExtent, transformFn);
expect(destinationExtent).not.to.be(undefined);
expect(destinationExtent).not.to.be(null);
// FIXME check values with third-party tool
@@ -456,7 +457,8 @@ describe('ol.extent', function() {
return output;
};
var sourceExtent = [-15, -30, 45, 60];
var destinationExtent = ol.extent.transform(sourceExtent, transformFn);
var destinationExtent = ol.extent.applyTransform(
sourceExtent, transformFn);
expect(destinationExtent).not.to.be(undefined);
expect(destinationExtent).not.to.be(null);
expect(destinationExtent[0]).to.be(-45);

View File

@@ -207,7 +207,7 @@ describe('ol.geom.Circle', function() {
it('throws an exception', function() {
expect(function() {
circle.transform(ol.proj.identityTransform);
circle.applyTransform(ol.proj.identityTransform);
}).to.throwException();
});

View File

@@ -139,6 +139,35 @@ describe('ol.geom.GeometryCollection', function() {
});
describe('#transform()', function() {
var line, multi, point;
beforeEach(function() {
point = new ol.geom.Point([10, 20]);
line = new ol.geom.LineString([[10, 20], [30, 40]]);
multi = new ol.geom.GeometryCollection([point, line]);
});
it('transforms all geometries', function() {
multi.transform('EPSG:4326', 'EPSG:3857');
var geometries = multi.getGeometries();
expect(geometries[0]).to.be.a(ol.geom.Point);
expect(geometries[1]).to.be.a(ol.geom.LineString);
var coords = geometries[0].getCoordinates();
expect(coords[0]).to.roughlyEqual(1113194.90, 1e-2);
expect(coords[1]).to.roughlyEqual(2273030.92, 1e-2);
coords = geometries[1].getCoordinates();
expect(coords[0][0]).to.roughlyEqual(1113194.90, 1e-2);
expect(coords[0][1]).to.roughlyEqual(2273030.92, 1e-2);
expect(coords[1][0]).to.roughlyEqual(3339584.72, 1e-2);
expect(coords[1][1]).to.roughlyEqual(4865942.27, 1e-2);
});
});
});

View File

@@ -192,6 +192,63 @@ describe('ol.geom.MultiPoint', function() {
});
describe('#applyTransform()', function() {
var multi, transform;
beforeEach(function() {
multi = new ol.geom.MultiPoint([[1, 2], [3, 4]]);
transform = sinon.spy();
});
it('calls a transform function', function() {
multi.applyTransform(transform);
expect(transform.calledOnce).to.be(true);
var args = transform.firstCall.args;
expect(args).to.have.length(3);
expect(args[0]).to.be(multi.getFlatCoordinates()); // input coords
expect(args[1]).to.be(multi.getFlatCoordinates()); // output coords
expect(args[2]).to.be(2); // dimension
});
it('allows for modification of coordinates', function() {
var mod = function(input, output, dimension) {
var copy = input.slice();
for (var i = 0, ii = copy.length; i < ii; i += dimension) {
output[i] = copy[i + 1];
output[i + 1] = copy[i];
}
};
multi.applyTransform(mod);
expect(multi.getCoordinates()).to.eql([[2, 1], [4, 3]]);
});
it('returns undefined', function() {
var got = multi.applyTransform(transform);
expect(got).to.be(undefined);
});
});
describe('#transform()', function() {
it('transforms a geometry given CRS identifiers', function() {
var multi = new ol.geom.MultiPoint([[-111, 45], [111, -45]]).transform(
'EPSG:4326', 'EPSG:3857');
expect(multi).to.be.a(ol.geom.MultiPoint);
var coords = multi.getCoordinates();
expect(coords[0][0]).to.roughlyEqual(-12356463.47, 1e-2);
expect(coords[0][1]).to.roughlyEqual(5621521.48, 1e-2);
expect(coords[1][0]).to.roughlyEqual(12356463.47, 1e-2);
expect(coords[1][1]).to.roughlyEqual(-5621521.48, 1e-2);
});
});
});

View File

@@ -106,6 +106,67 @@ describe('ol.geom.Point', function() {
});
describe('#applyTransform()', function() {
var point, transform;
beforeEach(function() {
point = new ol.geom.Point([1, 2]);
transform = sinon.spy();
});
it('calls a transform function', function() {
point.applyTransform(transform);
expect(transform.calledOnce).to.be(true);
var args = transform.firstCall.args;
expect(args).to.have.length(3);
expect(args[0]).to.be(point.getFlatCoordinates()); // input coords
expect(args[1]).to.be(point.getFlatCoordinates()); // output coords
expect(args[2]).to.be(2); // dimension
});
it('allows for modification of coordinates', function() {
var mod = function(input, output, dimension) {
var copy = input.slice();
output[1] = copy[0];
output[0] = copy[1];
};
point.applyTransform(mod);
expect(point.getCoordinates()).to.eql([2, 1]);
});
it('returns undefined', function() {
var got = point.applyTransform(transform);
expect(got).to.be(undefined);
});
});
describe('#transform()', function() {
it('transforms a geometry given CRS identifiers', function() {
var point = new ol.geom.Point([-111, 45]).transform(
'EPSG:4326', 'EPSG:3857');
expect(point).to.be.a(ol.geom.Point);
var coords = point.getCoordinates();
expect(coords[0]).to.roughlyEqual(-12356463.47, 1e-2);
expect(coords[1]).to.roughlyEqual(5621521.48, 1e-2);
});
it('modifies the original', function() {
var point = new ol.geom.Point([-111, 45]);
point.transform('EPSG:4326', 'EPSG:3857');
var coords = point.getCoordinates();
expect(coords[0]).to.roughlyEqual(-12356463.47, 1e-2);
expect(coords[1]).to.roughlyEqual(5621521.48, 1e-2);
});
});
});