-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mapper): flatgeobuf maplibre component for loading features (#1851)
* fix: add 'anon' defualt if no username provided for event * docs: comments describing parts of tasks store * fix: on hover effect for tasks using promoteId * feat: add selectedTaskGeom to task store when task area selected * feat: wip add flatgeobuf layer wrapper component around GeoJSON component * feat: flatgeobuf logic only load bbox and filter by extent * fix(typescript): remaining typing errors for flatgeobuf code
- Loading branch information
1 parent
293f44d
commit 6724b3f
Showing
9 changed files
with
350 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/mapper/src/lib/components/map/flatgeobuf-layer.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<script lang="ts"> | ||
import type { Snippet } from 'svelte'; | ||
import { onDestroy } from 'svelte'; | ||
import { GeoJSON as MapLibreGeoJSON } from 'svelte-maplibre'; | ||
import { getId, updatedSourceContext, addSource, removeSource } from 'svelte-maplibre'; | ||
import type { HeaderMeta } from 'flatgeobuf'; | ||
import type { GeoJSON, Polygon, FeatureCollection } from 'geojson'; | ||
import { flatgeobufToGeoJson, filterGeomsCentroidsWithin } from '$lib/utils/flatgeobuf'; | ||
type bboxType = [number, number, number, number]; | ||
interface Props { | ||
id?: string; | ||
url: string; | ||
extent?: bboxType | Polygon | null; | ||
extractGeomCols: boolean, | ||
metadataFunc?: (headerMetadata: HeaderMeta) => void; | ||
promoteId?: string; | ||
children?: Snippet; | ||
} | ||
let { | ||
id = getId('flatgeobuf'), | ||
url, | ||
extent, | ||
extractGeomCols = false, | ||
promoteId = undefined, | ||
metadataFunc, | ||
children, | ||
}: Props = $props(); | ||
const { map, self: sourceId } = updatedSourceContext(); | ||
let sourceObj: maplibregl.GeoJSONSource | undefined = $state(); | ||
let first = $state(true); | ||
let geojsonData: GeoJSON = $state({type: 'FeatureCollection', features: []}); | ||
// Set currentSourceId as reactive property once determined from context | ||
let currentSourceId: string | undefined = $state(); | ||
$effect(() => { | ||
currentSourceId = $sourceId; | ||
}); | ||
// Deserialise flatgeobuf to GeoJSON, reactive to bbox/extent changes | ||
async function updateGeoJSONData() { | ||
const featcol: FeatureCollection | null = await flatgeobufToGeoJson(url, extent, metadataFunc, extractGeomCols); | ||
// If there is no data, set to an empty FeatureCollection to avoid | ||
// re-adding layer if the bbox extent is updated | ||
if (!featcol) { | ||
geojsonData = { | ||
type: 'FeatureCollection', | ||
features: [], | ||
}; | ||
} else if (extent && "type" in extent && extent.type === 'Polygon') { | ||
geojsonData = filterGeomsCentroidsWithin(featcol, extent); | ||
} else { | ||
geojsonData = featcol; | ||
} | ||
currentSourceId = id; | ||
addSourceToMap(); | ||
} | ||
$effect(() => { | ||
updateGeoJSONData(); | ||
}); | ||
function addSourceToMap() { | ||
if (!$map) return; | ||
const initialData: maplibregl.SourceSpecification = { | ||
type: 'geojson', | ||
data: geojsonData, | ||
promoteId, | ||
}; | ||
// Use the currentSourceId in addSource | ||
addSource($map, currentSourceId!, initialData, (sourceId) => sourceId === currentSourceId, () => { | ||
sourceObj = $map?.getSource(currentSourceId!) as maplibregl.GeoJSONSource; | ||
first = true; | ||
}); | ||
} | ||
// Update data only if source already exists | ||
$effect(() => { | ||
if (sourceObj && geojsonData) { | ||
if (first) { | ||
first = false; | ||
} else { | ||
sourceObj.setData(geojsonData); | ||
} | ||
} | ||
}); | ||
onDestroy(() => { | ||
if (sourceObj && $map) { | ||
removeSource($map, currentSourceId!, sourceObj); | ||
currentSourceId = undefined; | ||
sourceObj = undefined; | ||
} | ||
}); | ||
</script> | ||
|
||
<MapLibreGeoJSON id={currentSourceId} data={geojsonData} promoteId={promoteId}> | ||
{@render children?.()} | ||
</MapLibreGeoJSON> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.