package.proj.transforms.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ol Show documentation
Show all versions of ol Show documentation
OpenLayers mapping library
The newest version!
/**
* @module ol/proj/transforms
*/
import {isEmpty} from '../obj.js';
/**
* @private
* @type {!Object>}
*/
let transforms = {};
/**
* Clear the transform cache.
*/
export function clear() {
transforms = {};
}
/**
* Registers a conversion function to convert coordinates from the source
* projection to the destination projection.
*
* @param {import("./Projection.js").default} source Source.
* @param {import("./Projection.js").default} destination Destination.
* @param {import("../proj.js").TransformFunction} transformFn Transform.
*/
export function add(source, destination, transformFn) {
const sourceCode = source.getCode();
const destinationCode = destination.getCode();
if (!(sourceCode in transforms)) {
transforms[sourceCode] = {};
}
transforms[sourceCode][destinationCode] = transformFn;
}
/**
* Unregisters the conversion function to convert coordinates from the source
* projection to the destination projection. This method is used to clean up
* cached transforms during testing.
*
* @param {import("./Projection.js").default} source Source projection.
* @param {import("./Projection.js").default} destination Destination projection.
* @return {import("../proj.js").TransformFunction} transformFn The unregistered transform.
*/
export function remove(source, destination) {
const sourceCode = source.getCode();
const destinationCode = destination.getCode();
const transform = transforms[sourceCode][destinationCode];
delete transforms[sourceCode][destinationCode];
if (isEmpty(transforms[sourceCode])) {
delete transforms[sourceCode];
}
return transform;
}
/**
* Get a transform given a source code and a destination code.
* @param {string} sourceCode The code for the source projection.
* @param {string} destinationCode The code for the destination projection.
* @return {import("../proj.js").TransformFunction|undefined} The transform function (if found).
*/
export function get(sourceCode, destinationCode) {
let transform;
if (sourceCode in transforms && destinationCode in transforms[sourceCode]) {
transform = transforms[sourceCode][destinationCode];
}
return transform;
}