86 lines
2.4 KiB
JavaScript
86 lines
2.4 KiB
JavaScript
import proj from './Proj';
|
|
import transform from './transform';
|
|
var wgs84 = proj('WGS84');
|
|
|
|
function transformer(from, to, coords, enforceAxis) {
|
|
var transformedArray, out, keys;
|
|
if (Array.isArray(coords)) {
|
|
transformedArray = transform(from, to, coords, enforceAxis) || {x: NaN, y: NaN};
|
|
if (coords.length > 2) {
|
|
if ((typeof from.name !== 'undefined' && from.name === 'geocent') || (typeof to.name !== 'undefined' && to.name === 'geocent')) {
|
|
if (typeof transformedArray.z === 'number') {
|
|
return [transformedArray.x, transformedArray.y, transformedArray.z].concat(coords.splice(3));
|
|
} else {
|
|
return [transformedArray.x, transformedArray.y, coords[2]].concat(coords.splice(3));
|
|
}
|
|
} else {
|
|
return [transformedArray.x, transformedArray.y].concat(coords.splice(2));
|
|
}
|
|
} else {
|
|
return [transformedArray.x, transformedArray.y];
|
|
}
|
|
} else {
|
|
out = transform(from, to, coords, enforceAxis);
|
|
keys = Object.keys(coords);
|
|
if (keys.length === 2) {
|
|
return out;
|
|
}
|
|
keys.forEach(function (key) {
|
|
if ((typeof from.name !== 'undefined' && from.name === 'geocent') || (typeof to.name !== 'undefined' && to.name === 'geocent')) {
|
|
if (key === 'x' || key === 'y' || key === 'z') {
|
|
return;
|
|
}
|
|
} else {
|
|
if (key === 'x' || key === 'y') {
|
|
return;
|
|
}
|
|
}
|
|
out[key] = coords[key];
|
|
});
|
|
return out;
|
|
}
|
|
}
|
|
|
|
function checkProj(item) {
|
|
if (item instanceof proj) {
|
|
return item;
|
|
}
|
|
if (item.oProj) {
|
|
return item.oProj;
|
|
}
|
|
return proj(item);
|
|
}
|
|
|
|
function proj4(fromProj, toProj, coord) {
|
|
fromProj = checkProj(fromProj);
|
|
var single = false;
|
|
var obj;
|
|
if (typeof toProj === 'undefined') {
|
|
toProj = fromProj;
|
|
fromProj = wgs84;
|
|
single = true;
|
|
} else if (typeof toProj.x !== 'undefined' || Array.isArray(toProj)) {
|
|
coord = toProj;
|
|
toProj = fromProj;
|
|
fromProj = wgs84;
|
|
single = true;
|
|
}
|
|
toProj = checkProj(toProj);
|
|
if (coord) {
|
|
return transformer(fromProj, toProj, coord);
|
|
} else {
|
|
obj = {
|
|
forward: function (coords, enforceAxis) {
|
|
return transformer(fromProj, toProj, coords, enforceAxis);
|
|
},
|
|
inverse: function (coords, enforceAxis) {
|
|
return transformer(toProj, fromProj, coords, enforceAxis);
|
|
}
|
|
};
|
|
if (single) {
|
|
obj.oProj = toProj;
|
|
}
|
|
return obj;
|
|
}
|
|
}
|
|
export default proj4; |