From 6b5e7cfd53bef8c9af700dd3d56c339a35af0278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20K=C3=BCnzi?= Date: Thu, 17 Oct 2024 11:15:36 +0200 Subject: [PATCH] Support function in geometries: - Issue : in some cases, the style.getGeometry() function would return a function rather than an object, causing issues when trying to set the geometry later in the code as we were calling geometry(undefined) instead of geometry(feature). - Fix : we check that the geometry is either an object, or a function which returns a non-null / non-undefined value, and we also use the function the geometry contains when it's not an Object. --- src/VectorEncoder.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/VectorEncoder.ts b/src/VectorEncoder.ts index 7f9a02d..f8901a4 100644 --- a/src/VectorEncoder.ts +++ b/src/VectorEncoder.ts @@ -187,9 +187,17 @@ export default class VectorEncoder { // handling more cases than we actually do let geometry: any = style.getGeometry(); let geojsonFeature; - if (geometry) { + // In some cases, the geometries are objects, in other cases they're features. + // We need to make sure that either the geometry is an object, or that the feature it contains returns + // a non-null / non-undefined value. + if (geometry && ((geometry instanceof Object && typeof geometry === 'object') || geometry(feature))) { const styledFeature = feature.clone(); - styledFeature.setGeometry(geometry); + if (geometry instanceof Object && typeof geometry === 'object') { + styledFeature.setGeometry(geometry); + } + else { + styledFeature.setGeometry(geometry(feature)); + } geojsonFeature = this.geojsonFormat.writeFeatureObject(styledFeature); geojsonFeatures.push(geojsonFeature); } else {