From e761de83d8817362245f64e415aa2efaa64bb815 Mon Sep 17 00:00:00 2001 From: spwoodcock Date: Mon, 4 Nov 2024 17:29:41 +0000 Subject: [PATCH] fix(typescript): remaining typing errors for flatgeobuf code --- .../lib/components/map/flatgeobuf-layer.svelte | 6 +++--- src/mapper/src/lib/utils/flatgeobuf.ts | 15 +++++++++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/mapper/src/lib/components/map/flatgeobuf-layer.svelte b/src/mapper/src/lib/components/map/flatgeobuf-layer.svelte index 702b953b6..a86bee247 100644 --- a/src/mapper/src/lib/components/map/flatgeobuf-layer.svelte +++ b/src/mapper/src/lib/components/map/flatgeobuf-layer.svelte @@ -31,7 +31,7 @@ const { map, self: sourceId } = updatedSourceContext(); let sourceObj: maplibregl.GeoJSONSource | undefined = $state(); let first = $state(true); - let geojsonData: GeoJSON = $state(); + let geojsonData: GeoJSON = $state({type: 'FeatureCollection', features: []}); // Set currentSourceId as reactive property once determined from context let currentSourceId: string | undefined = $state(); @@ -67,10 +67,10 @@ function addSourceToMap() { if (!$map) return; - const initialData = { + const initialData: maplibregl.SourceSpecification = { type: 'geojson', data: geojsonData, - promoteId + promoteId, }; // Use the currentSourceId in addSource diff --git a/src/mapper/src/lib/utils/flatgeobuf.ts b/src/mapper/src/lib/utils/flatgeobuf.ts index 02ffbb2b3..5b2b218ee 100644 --- a/src/mapper/src/lib/utils/flatgeobuf.ts +++ b/src/mapper/src/lib/utils/flatgeobuf.ts @@ -1,9 +1,16 @@ import { geojson as fgbGeojson } from 'flatgeobuf'; import type { Rect, HeaderMeta } from 'flatgeobuf'; -import type { FeatureCollection, Feature, Polygon, Geometry, GeoJsonProperties } from 'geojson'; +import type { FeatureCollection, Feature, Polygon, Geometry, GeoJsonProperties, Position } from 'geojson'; type bboxType = [number, number, number, number]; +/** + * Type guard to check if extent is a Polygon. + */ +function isPolygon(extent: bboxType | Polygon): extent is Polygon { + return (extent as Polygon).type === 'Polygon' && Array.isArray((extent as Polygon).coordinates); +} + /** * Converts a bounding box array or GeoJSON Polygon to a Rect format required by FlatGeobuf. * @@ -24,10 +31,10 @@ function extentToFgbRect(extent: bboxType | Polygon): Rect { }; } // Check if extent is a Polygon and calculate Rect - else if (extent?.type === 'Polygon' && Array.isArray(extent.coordinates)) { + else if (isPolygon(extent)) { const [firstRing] = extent.coordinates; - const xCoords = firstRing.map((coord) => coord[0]); - const yCoords = firstRing.map((coord) => coord[1]); + const xCoords = firstRing.map((coord: Position) => coord[0]); + const yCoords = firstRing.map((coord: Position) => coord[1]); return { minX: Math.min(...xCoords),