Add a transform method to geometries
This accepts CRS identifiers for source and destination, transforms the geometry in place, and returns a reference to the geometry.
This commit is contained in:
@@ -4,6 +4,7 @@ goog.provide('ol.geom.GeometryType');
|
|||||||
goog.require('goog.asserts');
|
goog.require('goog.asserts');
|
||||||
goog.require('goog.functions');
|
goog.require('goog.functions');
|
||||||
goog.require('ol.Observable');
|
goog.require('ol.Observable');
|
||||||
|
goog.require('ol.proj');
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -166,6 +167,24 @@ ol.geom.Geometry.prototype.getType = goog.abstractMethod;
|
|||||||
ol.geom.Geometry.prototype.applyTransform = goog.abstractMethod;
|
ol.geom.Geometry.prototype.applyTransform = goog.abstractMethod;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform a geometry from one coordinate reference system to another.
|
||||||
|
* Modifies the geometry in place.
|
||||||
|
*
|
||||||
|
* @param {ol.proj.ProjectionLike} source The current projection. Can be a
|
||||||
|
* string identifier or a {@link ol.proj.Projection} object.
|
||||||
|
* @param {ol.proj.ProjectionLike} destination The desired projection. Can be a
|
||||||
|
* string identifier or a {@link ol.proj.Projection} object.
|
||||||
|
* @return {ol.geom.Geometry} This geometry. Note that original geometry is
|
||||||
|
* modified in place.
|
||||||
|
* @todo api
|
||||||
|
*/
|
||||||
|
ol.geom.Geometry.prototype.transform = function(source, destination) {
|
||||||
|
this.applyTransform(ol.proj.getTransform(source, destination));
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {ol.Coordinate}
|
* @typedef {ol.Coordinate}
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -230,6 +230,25 @@ describe('ol.geom.MultiPoint', function() {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -142,6 +142,31 @@ describe('ol.geom.Point', function() {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user