/** * If we are using automated label placement, move and rotate text to correct * position * * @param feature the feature we are placing * @param style the style with text * @param layerStyleContext the context for the layer * @param internalContext - the internal context */ import { FeatureLike } from "ol/Feature"; import { Style } from "ol/style"; import { AutoLabelPlacementType, InternalContextType, LolOpenLayersMapContextType, StyleContext, } from "../context/LolOpenLayersMapContext"; import { flatten } from "lodash-es"; import olText from "ol/style/Text"; import ImageStyle from "ol/style/Image"; import { StyleFunction, StyleLike } from "ol/style/Style"; import { HighlightStyle, StyleWithContext, } from "../layerDefinition/LolOpenLayersVectorLayerDef"; import { Feature } from "ol"; import { Geometry } from "ol/geom"; import RenderFeature from "ol/render/Feature"; import log from "../utils/Log"; /** * styleWithContext - return * @param _what - when enabled a text for debugging * @param baseStyle - the base style for a layer or a label layer * @param mapContext - the mapContext we get the dynamic `getStyleContext` from * @param internalContext - the internal context * @param layerName - the *base* layer name (including when we style label layers) */ export const styleWithContext = ( _what: string, baseStyle: Style | StyleLike | StyleWithContext | undefined, mapContext: LolOpenLayersMapContextType | undefined, internalContext: InternalContextType, layerName: string ) => { if (!baseStyle) return undefined; return ( feature: Feature | RenderFeature, resolution: number ): Style | Style[] => { const layerStyleContext = mapContext?.getStyleContext(layerName); const wantedStyle = ( baseStyle instanceof Function ? baseStyle(feature, resolution, layerStyleContext) : baseStyle ) as Style | Style[]; return featureStyles( feature, wantedStyle, layerStyleContext, internalContext ); }; }; /** * Creates a function which can be applied to a feature, this will style that feature * depending on the features current state, such as highlighted and/or missing label */ export const createFeatureStyle = ( highlightStyle: HighlightStyle, getLayerStyleContext: (() => StyleContext | undefined) | undefined, internalContext: InternalContextType, baseStyle: StyleLike | StyleWithContext ): StyleFunction => { return (feature: Feature | RenderFeature, resolution: number) => { const layerStyleContext = getLayerStyleContext?.(); const highlighted = highlightStyle && (layerStyleContext?.highlightIds || []).includes( feature.getId() ?? feature.get("id") ); const base = featureStyles( feature, (baseStyle instanceof Function ? baseStyle(feature, resolution, layerStyleContext) : baseStyle) as Style, layerStyleContext, internalContext ) as Style; return highlighted ? highlightStyle instanceof Function ? highlightStyle(base, feature as Feature, resolution) : (highlightStyle as Style) : base; }; }; export const featureStyles = ( feature: FeatureLike, style: Style | Style[], layerStyleContext: StyleContext | undefined, internalContext: InternalContextType ): Style | Style[] => { const wasArray = Array.isArray(style); const wantedStyles = flatten([style]); const adjustedStyles = wantedStyles.map((wantedStyle) => { const styleWithLabelHidden = () => { wantedStyle.setText(undefined as unknown as olText); wantedStyle.setImage(null as unknown as ImageStyle); return wantedStyle; }; const featureId = feature.getId() ?? feature.get("id"); if ( !wantedStyle.getText() || !layerStyleContext?.placements || !featureId ) { log.debug( `DEBUG: featureStyles: no text or placements for ${featureId}:${feature.get( "labelText" )}` ); return wantedStyle; } if ( internalContext?.ref()?.placementInProgress() && internalContext?.ref()?.showOnlyPlacedLabels() ) { log.debug( `DEBUG: featureStyles: Placement in progress hiding feature ${featureId}` ); return styleWithLabelHidden(); } const featurePlacement = layerStyleContext.placements[featureId as number]; if (!featurePlacement) { log.debug( `DEBUG: featureStyles: No placement show label for ${featureId}` ); return wantedStyle; } if (featurePlacement.hide) { log.debug(`DEBUG: featureStyles: Label hidden for ${featureId}`); return styleWithLabelHidden(); } if (!featurePlacement.offsetPixels) { log.debug(`DEBUG: featureStyles:no offset for ${featureId}`); return wantedStyle; } log.debug( `DEBUG: featureStyles: apply placement ${featureId} = ${JSON.stringify( featurePlacement )}` ); if ( internalContext?.ref()?.autoLabelPlacementType() === AutoLabelPlacementType.OFFSET ) { // We have a placement (offsetX, offsetY, rotate) for this label // with "point" mode we do not need to rotate the offset // offsets are positive down and to the right // https://openlayers.org/en/latest/apidoc/module-ol_style_Text-Text.html wantedStyle .getText() .setOffsetX( wantedStyle.getText().getOffsetX() + featurePlacement.offsetPixels[0] ); wantedStyle .getText() .setOffsetY( wantedStyle.getText().getOffsetY() + featurePlacement.offsetPixels[1] ); wantedStyle.getText().setRotation(-(featurePlacement.rotateRads ?? 0)); wantedStyle.getText().setPlacement("point"); // Always use point mode for placement, this will rotate // and also update offset for any associated image const image = wantedStyle.getImage(); if (image) { // and also update offset for any associated image const image = wantedStyle.getImage(); if (image) { // A placement here can be along line (offsetPixels[1]==0, rotateRads is valid) // or over point (rotateRads is null) if (featurePlacement.rotateRads) { // offset zero, probably along a line (this will work anyway in the case where rot=0) image.setDisplacement([ featurePlacement.offsetPixels[0] * Math.cos(featurePlacement.rotateRads), featurePlacement.offsetPixels[0] * Math.sin(featurePlacement.rotateRads), ]); } else { // Placement is over point with a non-zero y // note: y-axis displacement is the opposite to how offset works image.setDisplacement([ featurePlacement.offsetPixels[0] + 1, // needs 1px to have text centered horizontally -featurePlacement.offsetPixels[1] + 1, // needs 1px to have text centered vertically ]); } } } } else if ( internalContext?.ref()?.autoLabelPlacementType() === AutoLabelPlacementType.ABSOLUTE && featurePlacement?.labelCenterPoint ) { wantedStyle.setGeometry(featurePlacement.labelCenterPoint); wantedStyle.getText().setRotation(featurePlacement?.rotateRads ?? 0); wantedStyle.getText().setPlacement("point"); } return wantedStyle; }); // We match the array or not that we were passed // Opemlayers doesn't care, over-introspective tests do return wasArray ? adjustedStyles : adjustedStyles[0]; };