import proj4 from "proj4"; import { chunk, flattenDeep, uniq } from "lodash-es"; // We draw the map in EPSG:3857 metres - https://epsg.io/3857 const maxResolutionEpsg3857 = 40075342.49 / 256; const zoomFactor = 2; // Resolution is in metres per pixel export const zoomToResolution = (zoomLevel: number) => maxResolutionEpsg3857 / Math.pow(zoomFactor, zoomLevel); export const resolutionToZoom = (resolution: number) => Math.log(maxResolutionEpsg3857 / resolution) / Math.log(zoomFactor); /* Convert from geographical EPSG4326 into map pixels (via grid metres) @param zoomLevel the zoom level, defined as above as log(zoomFactor)(maxResolution/resolution in metres per pixel) @param mapCentre the map centre in metres @param mapPixels the map viewport size in pixels @param geogs the longitude in latitude in EPSG4326 @return the map coordinate in pixels */ export const geogsToPixels = ( zoomLevel: number, mapCentre: number[], mapPixels: number[], geogs: number[] | undefined | null ) => metresToPixels(zoomLevel, mapCentre, mapPixels, geogsToMetres(geogs)); export const metresToPixels = ( zoomLevel: number, mapCentre: number[], mapPixels: number[], metres: number[] | undefined | null ) => { if (!metres) return null; const resolution = zoomToResolution(zoomLevel); const pixelsFromCentre = metres.map( (v, idx) => (v - mapCentre[idx]) / resolution ); if ( Math.abs(pixelsFromCentre[0]) > mapPixels[0] / 2 + 100 || Math.abs(pixelsFromCentre[1]) > mapPixels[1] / 2 + 100 ) return null; return pixelsFromCentre.map((p, idx) => p + mapPixels[idx] / 2); }; export const pixelsToMetres = ( zoomLevel: number, mapCentre: number[], mapPixels: number[], srcPixels: number[] ) => { if (!srcPixels) return null; const resolution = zoomToResolution(zoomLevel); return srcPixels.map( (v, idx) => (v - mapPixels[idx] / 2) * resolution + mapCentre[idx] ); }; // Convert from geog degrees to metres // This uses EPSG:4326 == WGS84 // This should take the map projection - because we aren't defining distortions // the error (just the ellipsoid change) is minimal export const geogsToMetres = ( geogs: number[] | undefined | null ): number[] | undefined | null => geogs && proj4("EPSG:4326", "EPSG:3857", geogs); export const shapeLimits = ( shape?: Record ): number[] | undefined => { return geometryLimits(shape?.geometry); }; export const geogCoordinatesToMetres = (coordinates: any[]): any[] => { if (typeof coordinates[0] === "number") { return geogsToMetres(coordinates as number[]) as number[]; } return coordinates.map((c) => geogCoordinatesToMetres(c)); }; // NOTE: returned value is congruent to ol/Extent export const geometryLimits = ( geometry?: Record ): number[] | undefined => { if (!geometry) return undefined; const coords = geometry.coordinates; const points = chunk(flattenDeep(coords) as number[], 2); let bbox = [Infinity, Infinity, -Infinity, -Infinity]; points.forEach((xy) => { bbox[0] = Math.min(bbox[0], xy[0]) ?? -Infinity; bbox[1] = Math.min(bbox[1], xy[1]) ?? -Infinity; bbox[2] = Math.max(bbox[2], xy[0]) ?? -Infinity; bbox[3] = Math.max(bbox[3], xy[1]) ?? -Infinity; }); return bbox; }; export const shapeCentre = ( shape?: Record ): number[] | undefined => { const lim = shapeLimits(shape); if (!lim) return undefined; return [(lim[2] + lim[0]) / 2, (lim[3] + lim[1]) / 2]; }; /** * Clip a line to an oblong * * @param lineCoords the start and end of the line * @param swX the SW corner X * @param neX the NE corner X * @param swY the SW corner X * @param neY the NE corner Y */ export const clipLineToOblong = ( lineCoords: number[][], swX: number, neX: number, swY: number, neY: number ) => { // Calculate the equation `y=mx+c` of our line // and then substitute this to determine each // possible line crossing const m = (lineCoords[1][1] - lineCoords[0][1]) / (lineCoords[1][0] - lineCoords[0][0]); const c = lineCoords[0][1] - m * lineCoords[0][0]; const intercepts = [ lineCoords[0], lineCoords[1], [swX, m * swX + c], [neX, m * neX + c], [(swY - c) / m, swY], [(neY - c) / m, neY], ]; // Determine the limits of the line const minX = Math.min(lineCoords[0][0], lineCoords[1][0]); const minY = Math.min(lineCoords[0][1], lineCoords[1][1]); const maxX = Math.max(lineCoords[0][0], lineCoords[1][0]); const maxY = Math.max(lineCoords[0][1], lineCoords[1][1]); // Filter intercepts where the line actually crosses the // bouncing box, expect maximum of 2 values const useIntercepts = uniq( intercepts.filter(([x, y]) => { if (x < swX || x > neX) return false; if (y < swY || y > neY) return false; if (x < minX || x > maxX) return false; return !(y < minY || y > maxY); }) ); // if the line is outside the bounding box, return null if (useIntercepts.length === 0) return null; return useIntercepts.slice(0, 2); };