+
+ setSld(xml) {
+ this.#sld = SLDReader.Reader(xml);
+ }
+
+ /** Get a FeatureTypeStyle (https://nieuwlandgeo.github.io/SLDReader/api.html#FeatureTypeStyle). */
+ getFeatureTypeStyle(styledLayerName, styleName) {
+ const sldLayer = SLDReader.getLayer(this.#sld, styledLayerName);
+ const style = styleName === undefined ? SLDReader.getStyle(sldLayer) : SLDReader.getStyle(sldLayer, styleName);
+ // OpenLayers can only use one definition
+ const featureTypeStyle = style.featuretypestyles[0];
+ return featureTypeStyle;
+ }
+
+ applyStyle(layerName, styledLayerName, styleName) {
+ const layer = this.getLayerByName(layerName);
+ const featureTypeStyle = this.getFeatureTypeStyle(styledLayerName, styleName);
+ const viewProjection = this.#map.getView().getProjection();
+ const olStyleFunction = SLDReader.createOlStyleFunction(featureTypeStyle, {
+ // Use the convertResolution option to calculate a more accurate resolution.
+ convertResolution: viewResolution => {
+ const viewCenter = this.#map.getView().getCenter();
+ return getPointResolution(viewProjection, viewResolution, viewCenter);
+ },
+ // If you use point icons with an ExternalGraphic, you have to use imageLoadCallback
+ // to update the vector layer when an image finishes loading.
+ // If you do not do this, the image will only be visible after next layer pan/zoom.
+ imageLoadedCallback: () => {
+ layer.changed();
+ },
+ });
+ layer.setStyle(olStyleFunction);
+ }
+