UNPKG

geostyler-openlayers-parser

Version:

GeoStyler Style Parser implementation for OpenLayers styles

313 lines (312 loc) 10.1 kB
import e from "./Util/OlFlatStyleUtil.js"; import p from "./Util/OlStyleUtil.js"; class y { /** * The name of the OlFlatStyleParser. */ static title = "OpenLayers FlatStyle Parser"; unsupportedProperties = { Symbolizer: { MarkSymbolizer: { wellKnownName: { support: "partial", info: "Only circle is supported" }, avoidEdges: "none", blur: "none", fillOpacity: "none", offset: "none", offsetAnchor: "none", pitchAlignment: "none", pitchScale: "none", radiusUnit: "none", rotate: "none", strokeWidthUnit: "none", visibility: "none" }, FillSymbolizer: { antialias: "none", fillOpacity: "none", graphicFill: "none", graphicFillPadding: "none", outlineCap: "none", outlineColor: "none", outlineDasharray: "none", outlineJoin: "none", outlineOpacity: "none", outlineWidth: "none", outlineWidthUnit: "none", visibility: "none" }, IconSymbolizer: { allowOverlap: "none", anchor: "none", avoidEdges: "none", color: "none", format: "none", haloBlur: "none", haloColor: "none", haloOpacity: "none", haloWidth: "none", haloWidthUnit: "none", keepUpright: "none", offsetAnchor: "none", optional: "none", padding: "none", pitchAlignment: "none", rotationAlignment: "none", size: "none", sizeUnit: "none", textFit: "none", textFitPadding: "none", visibility: "none" }, LineSymbolizer: { blur: "none", gapWidth: "none", gapWidthUnit: "none", gradient: "none", graphicFill: "none", graphicFillPadding: "none", graphicStroke: "none", roundLimit: "none", spacing: "none", spacingUnit: "none", visibility: "none", widthUnit: "none" }, RasterSymbolizer: "none", TextSymbolizer: { allowOverlap: "none", anchor: "none", avoidEdges: "none", fontStyle: "none", fontWeight: "none", haloBlur: "none", haloWidthUnit: "none", keepUpright: "none", letterSpacing: "none", letterSpacingUnit: "none", lineHeight: "none", lineHeightUnit: "none", maxWidth: "none", offsetAnchor: "none", optional: "none", pitchAlignment: "none", rotationAlignment: "none", transform: "none", visibility: "none" } }, Function: { numberFormat: "none", strAbbreviate: "none", strCapitalize: "none", strConcat: "none", strEndsWith: "none", strEqualsIgnoreCase: "none", strIndexOf: "none", strLastIndexOf: "none", strLength: "none", strMatches: "none", strReplace: "none", strStartsWith: "none", strStripAccents: "none", strSubstring: "none", strSubstringStart: "none", strToLowerCase: "none", strToUpperCase: "none", strTrim: "none", acos: "none", asin: "none", exp: "none", log: "none", max: "none", min: "none", pi: "none", random: "none", rint: "none", tan: "none", toDegrees: "none", toNumber: "none", toRadians: "none", double2bool: "none", parseBoolean: "none", step: "none" } }; title = "OpenLayers FlatStyle Parser"; flatStyleToGeoStylerFillSymbolizer(o) { const [n, r] = e.isExpression(o["fill-color"]) ? [e.olExpressionToGsExpression(o["fill-color"])] : e.getColorAndOpacity(o["fill-color"]); return { kind: "Fill", color: n, opacity: r }; } flatStyleToGeoStylerLineSymbolizer(o) { const [n, r] = e.isExpression(o["stroke-color"]) ? [e.olExpressionToGsExpression(o["stroke-color"])] : e.getColorAndOpacity(o["stroke-color"]); return { kind: "Line", color: n, opacity: r, width: e.olExpressionToGsExpression(o["stroke-width"]), cap: e.olExpressionToGsExpression(o["stroke-line-cap"]), join: e.olExpressionToGsExpression(o["stroke-line-join"]), dasharray: e.olExpressionToGsExpression(o["stroke-line-dash"]), dashOffset: e.olExpressionToGsExpression(o["stroke-line-dash-offset"]), miterLimit: e.olExpressionToGsExpression(o["stroke-miter-limit"]), perpendicularOffset: e.olExpressionToGsExpression(o["stroke-offset"]) }; } flatStyleToGeoStylerTextSymbolizer(o) { const [n, r] = e.isExpression(o["text-fill-color"]) ? [e.olExpressionToGsExpression(o["text-fill-color"])] : e.getColorAndOpacity(o["text-fill-color"]), [i, t] = e.isExpression(o["text-stroke-color"]) ? [e.olExpressionToGsExpression(o["text-stroke-color"])] : e.getColorAndOpacity(o["text-stroke-color"]); let s, l; return e.isExpression(o["text-font"]) ? s = [e.olExpressionToGsExpression(o["text-font"])] : o["text-font"] && (s = [p.getFontNameFromOlFont(o["text-font"])], l = p.getSizeFromOlFont(o["text-font"])), { kind: "Text", label: e.olExpressionToGsExpression(o["text-value"]), color: n, opacity: r, haloColor: i, haloOpacity: t, haloWidth: e.olExpressionToGsExpression(o["text-stroke-width"]), font: s, size: l, maxAngle: e.olExpressionToGsExpression(o["text-max-angle"]), offset: [ e.olExpressionToGsExpression(o["text-offset-x"]), e.olExpressionToGsExpression(o["text-offset-y"]) ], // TODO check if values of ol placement are the same as values of geostyler placement placement: e.olExpressionToGsExpression( o["text-placement"] ), repeat: e.olExpressionToGsExpression(o["text-repeat"]), rotate: e.olExpressionToGsExpression(o["text-rotation"]), justify: e.olExpressionToGsExpression( o["text-justify"] ), padding: e.olExpressionToGsExpression(o["text-padding"]) }; } flatStyleToGeoStylerIconSymbolizer(o) { return { kind: "Icon", image: o["icon-src"], offset: e.olExpressionToGsExpression( o["icon-offset"] ), opacity: e.olExpressionToGsExpression(o["icon-opacity"]), rotate: e.olExpressionToGsExpression(o["icon-rotation"]), // we use the icon-width here to be consistent with OlStyleParser size: e.olExpressionToGsExpression(o["icon-width"]) }; } flatCircleStyleToGeoStylerMarkSymbolizer(o) { const [n, r] = e.isExpression(o["circle-fill-color"]) ? [e.olExpressionToGsExpression(o["circle-fill-color"])] : e.getColorAndOpacity(o["circle-fill-color"]), [i, t] = e.isExpression(o["circle-stroke-color"]) ? [e.olExpressionToGsExpression(o["circle-stroke-color"])] : e.getColorAndOpacity(o["circle-stroke-color"]); return { kind: "Mark", wellKnownName: "circle", radius: e.olExpressionToGsExpression(o["circle-radius"]), color: n, fillOpacity: r, strokeColor: i, strokeOpacity: t, strokeWidth: e.olExpressionToGsExpression(o["circle-stroke-width"]) }; } flatStyleToGeoStylerSymbolizers(o) { const n = []; return e.hasFlatFill(o) && n.push(this.flatStyleToGeoStylerFillSymbolizer(o)), e.hasFlatStroke(o) && n.push(this.flatStyleToGeoStylerLineSymbolizer(o)), e.hasFlatText(o) && n.push(this.flatStyleToGeoStylerTextSymbolizer(o)), e.hasFlatIcon(o) && n.push(this.flatStyleToGeoStylerIconSymbolizer(o)), e.hasFlatCircle(o) && n.push(this.flatCircleStyleToGeoStylerMarkSymbolizer(o)), n; } flatRuleToGeoStylerRule(o, n) { let r; e.isFlatStyle(o.style) ? r = this.flatStyleToGeoStylerSymbolizers(o.style) : r = o.style.map(this.flatStyleToGeoStylerSymbolizers, this).flat(); const i = { name: `OL Style Rule ${n}`, symbolizers: r }; return o.filter && (i.filter = e.olFilterToGsFilter(o.filter)), i; } flatRuleArrayToGeoStylerStyle(o) { return { name: "OL Style", rules: o.map(this.flatRuleToGeoStylerRule, this) }; } flatStyleArrayToGeoStylerStyle(o) { return { name: "OL Style", rules: o.map((n, r) => ({ name: `OL Style Rule ${r}`, symbolizers: this.flatStyleToGeoStylerSymbolizers(n) })) }; } flatStyleToGeoStylerStyle(o) { return { name: "OL Style", rules: [{ name: "OL Style Rule 0", symbolizers: this.flatStyleToGeoStylerSymbolizers(o) }] }; } flatStyleLikeToGeoStylerStyle(o) { if (e.isFlatStyleArray(o)) return this.flatStyleArrayToGeoStylerStyle(o); if (e.isFlatRuleArray(o)) return this.flatRuleArrayToGeoStylerStyle(o); if (e.isFlatStyle(o)) return this.flatStyleToGeoStylerStyle(o); throw new Error("Provided argument is not a valid FlatStyleLike."); } /** * The readStyle implementation of the GeoStyler-Style StyleParser interface. * It reads an OpenLayers FlatStyle and returns a Promise. * * The Promise itself resolves with a GeoStyler-Style Style. * * @param flatStyleLike The style to be parsed * @return The Promise resolving with the GeoStyler-Style Style */ readStyle(o) { return new Promise((n) => { try { const r = this.flatStyleLikeToGeoStylerStyle(o); n({ output: r }); } catch (r) { n({ errors: [r] }); } }); } /** * The writeStyle implementation of the GeoStyler-Style StyleParser interface. * It reads a GeoStyler-Style Style and returns a Promise containing the FlatStyle. * * @param geoStylerStyle A GeoStyler-Style Style. * @return The Promise resolving with one of above mentioned style types. */ writeStyle(o) { return new Promise((n) => { try { n({ output: {}, warnings: ["Not implemented yet"] }); } catch (r) { n({ errors: [r] }); } }); } } export { y as OlFlatStyleParser, y as default };