Skip to content

Instantly share code, notes, and snippets.

@JeffJacobson
Created January 30, 2014 20:51
Show Gist options
  • Save JeffJacobson/8718408 to your computer and use it in GitHub Desktop.
Save JeffJacobson/8718408 to your computer and use it in GitHub Desktop.
Project arrays of coordinates using Proj4JS
// For use withhttp://proj4js.org/
/** @typedef {(string|proj4.Proj)} Projection
*
*/
/** @typedef {object} ThisProjectionInfo
* @property {?Projection} inPrj
* @property {?Projection} outPrj
*/
/** @typedef {(Number[]|Array<Array<Number>>|<Array<Array<Array<Number>>>)} ProjectableArray
*
*/
/** For use with the Array.prototype.map() function.
* @param {ProjectableArray} a
* @this {ThisProjectionInfo}
*/
function projectMap(a) {
/*jshint validthis:true*/
return projectCoords(a, this.inPrj, this.outPrj);
/*jshint validthis:false*/
}
/** projects coordinates in an array.
* @param {ProjectableArray} array - An array of numbers or an array containing arrays of numbers.
* @param {Projection} inPrj - input projection.
* @param {Projection} [outPrj] - output projection.
* @returns {ProjectableArray} - The projected version of the input array.
*/
function projectCoords(array, inPrj, outPrj) {
var output;
if (array && array.length) {
if (array[0] instanceof Array) {
output = array.map(projectMap, {
inPrj: inPrj,
outPrj: outPrj
});
} else if (typeof array[0] === "number") {
output = proj4(inPrj, outPrj, array);
}
} else {
output = array;
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment