import proj4 from "proj4"; import * as olExtent from "ol/extent"; /** * The parameters passed to the label placement engine * Defines the current viewport so placements can be calculated */ export type PlacementParams = { /** * The width of the viewport in pixels */ widthPx: number; /** * The height of the viewport in pixels */ heightPx: number; /** * The northeast X value of the viewport in offset 160 WGS84 (approx) * degrees. * NOTE: these should be in degrees of the map datum, not "EPSG:1" * SJ-466 is to fix */ neX: number; /** * The northeast Y value of the viewport in offset 160 WGS84 (approx) * degrees. */ neY: number; /** * The southwest X value of the viewport in offset 160 WGS84 (approx) * degrees. */ swX: number; /** * The southwest Y value of the viewport in offset 160 WGS84 (approx) * degrees. */ swY: number; }; export const createPlacementParams = ( mapSizePixels: number[] | undefined, mapExtentMetres: number[], dataProjection: string ) => { if (!mapSizePixels) return undefined; const swMetres = olExtent.getBottomLeft(mapExtentMetres); const neMetres = olExtent.getTopRight(mapExtentMetres); // Convert the map extent corners in the map projection // into offset degrees const mapExtentSW = proj4("EPSG:3857", dataProjection, swMetres); const mapExtentNE = proj4("EPSG:3857", dataProjection, neMetres); return { swX: mapExtentSW[0], swY: mapExtentSW[1], neX: mapExtentNE[0], neY: mapExtentNE[1], widthPx: mapSizePixels[0], heightPx: mapSizePixels[1], }; };