Accept a destination array for transforms

This allows in-place transforms.
This commit is contained in:
Tim Schaub
2013-03-03 20:24:14 +01:00
parent 7a5e6a06d8
commit 40bde4056b
6 changed files with 67 additions and 37 deletions

View File

@@ -109,9 +109,9 @@ ol.Extent.prototype.getTopRight = function() {
*/
ol.Extent.prototype.transform = function(transformFn) {
var input = [this.minX, this.minY, this.maxX, this.maxY];
var output = transformFn(input, 2);
return new ol.Extent(Math.min(output[0], output[2]),
Math.min(output[1], output[3]),
Math.max(output[0], output[2]),
Math.max(output[1], output[3]));
input = transformFn(input, input, 2);
return new ol.Extent(Math.min(input[0], input[2]),
Math.min(input[1], input[3]),
Math.max(input[0], input[2]),
Math.max(input[1], input[3]));
};

View File

@@ -233,6 +233,7 @@ goog.exportProperty(
/**
* @private
* @param {Array.<number>} input Input coordinate values.
* @param {Array.<number>=} opt_output Output array of coordinate values.
* @param {number=} opt_dimension Dimension (default is 2).
* @return {Array.<number>} Output coordinate values.
*/

View File

@@ -398,20 +398,24 @@ ol.projection.getTransform = function(source, destination) {
transform =
/**
* @param {Array.<number>} input Input coordinate values.
* @param {Array.<number>=} opt_output Output array of coordinates.
* @param {number=} opt_dimension Dimension.
* @return {Array.<number>} Output coordinate values.
*/
function(input, opt_dimension) {
function(input, opt_output, opt_dimension) {
var length = input.length,
dimension = goog.isDef(opt_dimension) ? opt_dimension : 2,
output, proj4jsPoint;
if (dimension > 2) {
// preserve values beyond second dimension
output = input.slice();
} else {
output = new Array(length);
dimension = opt_dimension > 1 ? opt_dimension : 2,
output = opt_output;
if (!goog.isDef(output)) {
if (dimension > 2) {
// preserve values beyond second dimension
output = input.slice();
} else {
output = new Array(length);
}
}
goog.asserts.assert(output.length % dimension === 0);
var proj4jsPoint;
for (var i = 0; i < length; i += dimension) {
proj4jsPoint = new Proj4js.Point(input[i], input[i + 1]);
proj4jsPoint = Proj4js.transform(
@@ -449,21 +453,23 @@ ol.projection.getTransformFromCodes = function(sourceCode, destinationCode) {
/**
* @param {Array.<number>} input Input coordinate array.
* @param {Array.<number>=} opt_output Output array of coordinate values.
* @param {number=} opt_dimension Dimension.
* @return {Array.<number>} Input coordinate array (same array as input).
*/
ol.projection.identityTransform = function(input, opt_dimension) {
ol.projection.identityTransform = function(input, opt_output, opt_dimension) {
return input;
};
/**
* @param {Array.<number>} input Input coordinate array.
* @param {Array.<number>=} opt_output Output array of coordinate values.
* @param {number=} opt_dimension Dimension.
* @return {Array.<number>} Output coordinate array (new array, same coordinate
* values).
*/
ol.projection.cloneTransform = function(input, opt_dimension) {
ol.projection.cloneTransform = function(input, opt_output, opt_dimension) {
return input.slice();
};

View File

@@ -73,18 +73,22 @@ ol.projection.EPSG3857.PROJECTIONS = goog.array.map(
* Transformation from EPSG:4326 to EPSG:3857.
*
* @param {Array.<number>} input Input array of coordinate values.
* @param {Array.<number>=} opt_output Output array of coordinate values.
* @param {number=} opt_dimension Dimension (default is 2).
* @return {Array.<number>} Output array of coordinate values.
*/
ol.projection.EPSG3857.fromEPSG4326 = function(input, opt_dimension) {
ol.projection.EPSG3857.fromEPSG4326 = function(
input, opt_output, opt_dimension) {
var length = input.length,
dimension = goog.isDef(opt_dimension) ? opt_dimension : 2,
output;
if (dimension > 2) {
// preserve values beyond second dimension
output = input.slice();
} else {
output = new Array(length);
dimension = opt_dimension > 1 ? opt_dimension : 2,
output = opt_output;
if (!goog.isDef(output)) {
if (dimension > 2) {
// preserve values beyond second dimension
output = input.slice();
} else {
output = new Array(length);
}
}
goog.asserts.assert(output.length % dimension === 0);
for (var i = 0; i < length; i += dimension) {
@@ -100,18 +104,21 @@ ol.projection.EPSG3857.fromEPSG4326 = function(input, opt_dimension) {
* Transformation from EPSG:3857 to EPSG:4326.
*
* @param {Array.<number>} input Input array of coordinate values.
* @param {Array.<number>=} opt_output Output array of coordinate values.
* @param {number=} opt_dimension Dimension (default is 2).
* @return {Array.<number>} Output array of coordinate values.
*/
ol.projection.EPSG3857.toEPSG4326 = function(input, opt_dimension) {
ol.projection.EPSG3857.toEPSG4326 = function(input, opt_output, opt_dimension) {
var length = input.length,
dimension = goog.isDef(opt_dimension) ? opt_dimension : 2,
output;
if (dimension > 2) {
// preserve values beyond second dimension
output = input.slice();
} else {
output = new Array(length);
dimension = opt_dimension > 1 ? opt_dimension : 2,
output = opt_output;
if (!goog.isDef(output)) {
if (dimension > 2) {
// preserve values beyond second dimension
output = input.slice();
} else {
output = new Array(length);
}
}
goog.asserts.assert(output.length % dimension === 0);
for (var i = 0; i < length; i += dimension) {

View File

@@ -2,10 +2,11 @@ goog.provide('ol.TransformFunction');
/**
* A transform function accepts an array of input coordinate values and an
* optional dimension (default should be 2). The function transforms the
* coordinate values and returns an array of the same length as the input.
* A transform function accepts an array of input coordinate values, an optional
* output array, and an optional dimension (default should be 2). The function
* transforms the input coordinate values, populates the output array, and
* returns the output array.
*
* @typedef {function(Array.<number>, number=): Array.<number>}
* @typedef {function(Array.<number>, Array.<number>=, number=): Array.<number>}
*/
ol.TransformFunction;

View File

@@ -215,6 +215,21 @@ describe('ol.projection', function() {
expect(output[5]).toRoughlyEqual(52.4827802220782, 1e-9);
});
it('accepts an optional destination array', function() {
var transform = ol.projection.getTransformFromCodes(
'EPSG:3857', 'EPSG:4326');
var input = [-12000000, 5000000];
var output = [];
var got = transform(input, output);
expect(got).toBe(output);
expect(output[0]).toRoughlyEqual(-107.79783409434258, 1e-9);
expect(output[1]).toRoughlyEqual(40.91627447067577, 1e-9);
expect(input).toEqual([-12000000, 5000000]);
});
it('accepts a dimension', function() {
var transform = ol.projection.getTransformFromCodes(
'GOOGLE', 'EPSG:4326');
@@ -225,7 +240,7 @@ describe('ol.projection', function() {
-626172.13571216376, 6887893.4928337997, 100,
-12000000, 5000000, 200,
-626172.13571216376, 6887893.4928337997, 300
], dimension);
], undefined, dimension);
expect(output[0]).toRoughlyEqual(-5.625, 1e-9);
expect(output[1]).toRoughlyEqual(52.4827802220782, 1e-9);
@@ -247,7 +262,7 @@ describe('ol.projection', function() {
it('removes functions cached by addTransform', function() {
var foo = new ol.Projection('foo', units, extent);
var bar = new ol.Projection('bar', units, extent);
var transform = function(intput, dimension) {return input};
var transform = function(input, output, dimension) {return input};
ol.projection.addTransform(foo, bar, transform);
expect(ol.projection.transforms_).not.toBeUndefined();
expect(ol.projection.transforms_.foo).not.toBeUndefined();