From 94b1167afed793de78cc45a1fe6d57810d439982 Mon Sep 17 00:00:00 2001 From: Benjamin Gerber Date: Mon, 18 Nov 2024 10:57:15 +0100 Subject: [PATCH] feat: add vendor-option graphic-margin --- package-lock.json | 8 +++--- package.json | 2 +- src/SldStyleParser.ts | 66 +++++++++++++++++++++++++++++++++++++++---- 3 files changed, 65 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index 56dbd7b1..110b76f5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "BSD-2-Clause", "dependencies": { "fast-xml-parser": "^4.4.1", - "geostyler-style": "^9.1.0", + "geostyler-style": "^9.2.0", "lodash": "^4.17.21" }, "devDependencies": { @@ -4586,9 +4586,9 @@ } }, "node_modules/geostyler-style": { - "version": "9.1.0", - "resolved": "https://registry.npmjs.org/geostyler-style/-/geostyler-style-9.1.0.tgz", - "integrity": "sha512-kExQDe2mf4YaVMZPKE7h2uxU5qSyAQoX2U2hLXyAWubJjeNoFe0nxt6rKn7C9Q1DE/9DVZopOdNLCXMtqOE6QA==", + "version": "9.2.0", + "resolved": "https://registry.npmjs.org/geostyler-style/-/geostyler-style-9.2.0.tgz", + "integrity": "sha512-LwYkkbgD6VIGEbqcvG7U5hqgWrpWnwTdYtltIGt7bo+toKW9JcSoO6rIpbVPkbpblXJRWYQlbv80D8q3pqo+JQ==", "engines": { "node": ">=20.6.0", "npm": ">=10.0.0" diff --git a/package.json b/package.json index 5ad3a7a1..80d635e4 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ }, "dependencies": { "fast-xml-parser": "^4.4.1", - "geostyler-style": "^9.1.0", + "geostyler-style": "^9.2.0", "lodash": "^4.17.21" }, "devDependencies": { diff --git a/src/SldStyleParser.ts b/src/SldStyleParser.ts index 98903414..c72d7397 100644 --- a/src/SldStyleParser.ts +++ b/src/SldStyleParser.ts @@ -80,6 +80,7 @@ export type ConstructorParams = { boolFilterFields?: string[]; /* optional for reading style (it will be guessed from sld style) and mandatory for writing */ sldVersion?: SldVersion; + withVendorOption?: boolean; symbolizerUnits?: string; parserOptions?: ParserOptions; builderOptions?: XmlBuilderOptions; @@ -275,6 +276,7 @@ export class SldStyleParser implements StyleParser { preserveOrder: true, trimValues: true }); + this.builder = new XMLBuilder({ ...opts?.builderOptions, // Fixed attributes @@ -283,10 +285,16 @@ export class SldStyleParser implements StyleParser { suppressEmptyNode: true, preserveOrder: true }); + if (opts?.sldVersion) { this.sldVersion = opts?.sldVersion; } + this.withVendorOption = true; // FIXME ! + if (opts?.withVendorOption !== undefined) { + this.withVendorOption = opts.withVendorOption; + } + if (opts?.locale) { this.locale = opts.locale; } @@ -386,6 +394,26 @@ export class SldStyleParser implements StyleParser { this._sldVersion = sldVersion; } + /** + * Indicates whether additional GeoServer vendorOption should be included in + * sld write operations. Set to `false` by default. + */ + private _withVendorOption = false; + + /** + * Getter for _withVendorOption + */ + get withVendorOption(): boolean { + return this._withVendorOption; + } + + /** + * Setter for _withVendorOption + */ + set withVendorOption(withVendorOption: boolean) { + this._withVendorOption = withVendorOption; + } + /** * String indicating the SLD version used in reading mode @@ -1865,6 +1893,31 @@ export class SldStyleParser implements StyleParser { }]; } + /** + * FIXME + */ + pushVendorOption(elementArray: any[], name: string, text: string) { + if (this.withVendorOption) { + elementArray.push(this.createVendorOption(name, text)); + } + } + + /** + * FIXME + * @return text + */ + createVendorOption(name: string, text: string) { + const VendorOption = this.getTagName('VendorOption'); + return { + [VendorOption]: [{ + '#text': text, + }], + ':@': { + '@_name': name, + } + }; + } + /** * Get the SLD Object (readable with fast-xml-parser) from a geostyler-style IconSymbolizer. * @@ -2454,16 +2507,17 @@ export class SldStyleParser implements StyleParser { const polygonSymbolizer: any = []; if (fillCssParameters.length > 0 || graphicFill) { - if (!Array.isArray(polygonSymbolizer?.[0]?.[Fill])) { - polygonSymbolizer[0] = { [Fill]: [] }; + const fillArray: any[] = []; + const graphicFillPadding = fillSymbolizer.graphicFillPadding; + if (graphicFillPadding) { + this.pushVendorOption(polygonSymbolizer, 'graphic-margin', `${graphicFillPadding}`); } + polygonSymbolizer.push({ [Fill]: fillArray }); if (fillCssParameters.length > 0) { - polygonSymbolizer[0][Fill].push(...fillCssParameters); + fillArray.push(...fillCssParameters); } if (graphicFill) { - polygonSymbolizer[0][Fill].push({ - GraphicFill: graphicFill - }); + fillArray.push({ GraphicFill: graphicFill }); } }