import { Map as OlMap } from "ol"; import { IShape } from "./LolOpenLayersMap"; import { Pixel } from "ol/pixel"; import { FeatureLike } from "ol/Feature"; import SimpleGeometry from "ol/geom/SimpleGeometry"; import RenderFeature from "ol/render/Feature"; import { chunk, flatten, min, range } from "lodash-es"; import { squaredDistance, squaredDistanceToSegment } from "ol/coordinate"; import { MultiLineString, MultiPolygon, Polygon } from "ol/geom"; export const HIGHLIGHT_LAYER = "highlight"; interface IFeature { type: "Feature"; properties: Record; geometry: Record; } interface IFeatureCollection { type: "FeatureCollection"; features: IFeature[]; } export function createFeatureCollection(shape: IShape[]): IFeatureCollection { return { type: "FeatureCollection", features: shape .filter((obj) => obj.shape) .map((obj: IShape) => { const { ...allPropertiesExceptGeom } = obj as any; return { type: "Feature", properties: allPropertiesExceptGeom, geometry: obj.shape?.geometry, }; }), }; } /** * Some features have complex borders that alter the clickable envelope. * We test click-ability of these features using geometry instead to fix. * * @param map Open layers map * @param pixel Pixel location on map * @param geometrySizeForClickDetection The radius from the geometry for a valid click * @param feature Feature to test * @return Distance to geometry if matched otherwise null for not matched */ export const featureMatchByGeometry = ( map: OlMap, pixel: Pixel, geometrySizeForClickDetection: number, feature: FeatureLike ): number | null => { const geometry = feature.getGeometry() as | SimpleGeometry | RenderFeature | undefined; if ( geometry != null && typeof geometry.getFlatCoordinates === "function" && typeof geometry.getType === "function" ) { const featurePixels = chunk(geometry.getFlatCoordinates(), 2).map( (coordinate) => map.getPixelFromCoordinate(coordinate) ); const sqHitTolerance = Math.pow(geometrySizeForClickDetection, 2); const type = geometry?.getType(); switch (type) { case "Point": case "MultiPoint": { const matches = featurePixels .map((fp) => squaredDistance(pixel, fp)) .filter((distance) => distance <= sqHitTolerance); return min(matches) ?? null; } case "Polygon": // @ts-expect-error TS7029: Fallthrough case in switch. case "MultiPolygon": // Clicked inside the (multi)polygon if ( map.forEachFeatureAtPixel( pixel, (testFeature: FeatureLike) => feature === testFeature, { hitTolerance: 0 } ) ) { return 0; } // If it's not clicked within the shape then we want to check the edge distance // eslint-disable-next-line no-fallthrough case "LinearRing": case "LineString": case "MultiLineString": { const lineEndsFlatIndex = type === "MultiLineString" ? (geometry as MultiLineString).getEnds() : type === "Polygon" ? (geometry as Polygon).getEnds() : type === "MultiPolygon" ? flatten((geometry as MultiPolygon).getEndss()) : []; const distanceFromEdge = min( // Test each line segment for nearness range(1, featurePixels.length) .map((i) => lineEndsFlatIndex.includes(i * 2) ? null : squaredDistanceToSegment( pixel, featurePixels.slice(i - 1, i + 1) ) ) .filter((v) => v != null) ) ?? null; return distanceFromEdge != null && distanceFromEdge <= sqHitTolerance ? distanceFromEdge : null; } } } console.error( "Unsupported geometry type/class for geometry based click detection", geometry ); // Default to normal render based click detection return 0; };