Skip to content

Commit

Permalink
fix(typescript): remaining typing errors for flatgeobuf code
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Nov 4, 2024
1 parent 0ffc39d commit e761de8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/mapper/src/lib/components/map/flatgeobuf-layer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions src/mapper/src/lib/utils/flatgeobuf.ts
Original file line number Diff line number Diff line change
@@ -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.
*
Expand All @@ -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),
Expand Down

0 comments on commit e761de8

Please sign in to comment.