From 25eb9845b03c180122417acb8ce8ec75138bc46f Mon Sep 17 00:00:00 2001 From: Kai Volland Date: Thu, 28 Nov 2024 14:12:23 +0100 Subject: [PATCH] fix: add some basic type checks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … to improve error handling --- package-lock.json | 2 +- package.json | 2 +- src/LegendRenderer/LegendRenderer.ts | 13 +++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 84e3f85..2bf1acd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -30,7 +30,7 @@ "eslint-plugin-react": "^7.24.0", "eslint-plugin-react-hooks": "^4.2.0", "extensionless": "^1.9.9", - "geostyler-style": "9.2.0", + "geostyler-style": "^9.2.0", "jsdom": "^24.1.0", "ol": "^9.1.0", "rimraf": "^3.0.2", diff --git a/package.json b/package.json index 71e005e..251c497 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "eslint-plugin-react": "^7.24.0", "eslint-plugin-react-hooks": "^4.2.0", "extensionless": "^1.9.9", - "geostyler-style": "9.2.0", + "geostyler-style": "^9.2.0", "jsdom": "^24.1.0", "ol": "^9.1.0", "rimraf": "^3.0.2", diff --git a/src/LegendRenderer/LegendRenderer.ts b/src/LegendRenderer/LegendRenderer.ts index 1863f9d..071a8e1 100644 --- a/src/LegendRenderer/LegendRenderer.ts +++ b/src/LegendRenderer/LegendRenderer.ts @@ -5,6 +5,10 @@ import OlGeomPolygon from 'ol/geom/Polygon'; import OlGeomLineString from 'ol/geom/LineString'; import OlStyle from 'ol/style/Style'; import Renderer from 'ol/render/canvas/Immediate'; +import { + isRule, + isSymbolizer +} from 'geostyler-style'; import { create as createTransform } from 'ol/transform'; import { Style, @@ -126,6 +130,10 @@ export class LegendRenderer { * @param {Symbolizer} symbolizer the symbolizer object */ getGeometryForSymbolizer(symbolizer: Symbolizer): OlGeometry { + if (!isSymbolizer(symbolizer)) { + throw new Error('Invalid symbolizer'); + } + const kind = symbolizer.kind; switch (kind) { case 'Mark': @@ -154,6 +162,10 @@ export class LegendRenderer { * @param {Object} rule the geostyler rule */ getRuleIcon(rule: Rule): Promise { + if (!isRule(rule)) { + return Promise.reject('Invalid rule'); + } + const canvas = document.createElement('canvas'); canvas.setAttribute('width', `${iconSize[0]}`); canvas.setAttribute('height', `${iconSize[1]}`); @@ -163,6 +175,7 @@ export class LegendRenderer { const transform = createTransform(); const renderer = new Renderer(context as CanvasRenderingContext2D, pixelRatio, extent, transform, 0); const geoms: OlGeometry[] = []; + rule.symbolizers.forEach(symbolizer => geoms.push(this.getGeometryForSymbolizer(symbolizer))); const styleParser = new OlStyleParser();