-
Notifications
You must be signed in to change notification settings - Fork 0
Map layers refactoring #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import React, { useEffect, useMemo } from 'react'; | ||
| import geolib from '@gisatcz/deckgl-geolib'; | ||
| import { Layer } from '@deck.gl/core'; | ||
| import { LayerSourceProps } from './LayerManager'; | ||
| import { parseDatasourceConfiguration } from '../../../shared/models/parsers.datasources'; | ||
|
|
||
| const CogBitmapLayer = geolib.CogBitmapLayer; | ||
|
|
||
| /** | ||
| * A React component that creates and manages a COG (Cloud Optimized GeoTIFF) layer. | ||
| * This component uses the `CogBitmapLayer` from `@gisatcz/deckgl-geolib` to render raster data. | ||
| * | ||
| * @param {LayerSourceProps} props - The props for the COGLayerSource component. | ||
| * @param {RenderingLayer} props.layer - The layer configuration object. | ||
| * @param {(id: string, instance: Layer | null) => void} props.onLayerUpdate - Callback to handle updates to the layer instance. | ||
| * @returns {null} This component does not render any DOM elements. | ||
| */ | ||
| export const COGLayerSource = React.memo(({ layer, onLayerUpdate }: LayerSourceProps) => { | ||
| // Destructure properties from the layer configuration | ||
| const { isActive, key, opacity, datasource } = layer; | ||
| const { url, configuration } = datasource; | ||
|
|
||
| // Ensure the URL is provided in the datasource | ||
| if (!url) { | ||
| throw new Error(`COGLayerSource: Missing url in datasource: ${key}`); | ||
| } | ||
|
|
||
| // Parse the datasource configuration | ||
| const config = parseDatasourceConfiguration(configuration); | ||
| if (!config) { | ||
| // Log a warning if the configuration is missing | ||
| console.warn(`COGLayerSource: Missing configuration in datasource: ${key}`); | ||
| } | ||
|
|
||
| // Extract COG bitmap options from the parsed configuration | ||
| const cogBitmapOptions = config?.cogBitmapOptions; | ||
| if (!cogBitmapOptions) { | ||
| // Log a warning if the COG bitmap options are missing | ||
| console.warn(`COGLayerSource: Missing cogBitmapOptions in datasource configuration: ${key}`); | ||
| } | ||
|
|
||
| /** | ||
| * Memoize the creation of the CogBitmapLayer instance to avoid unnecessary re-renders. | ||
| * The layer instance is recreated only when its dependencies change. | ||
| */ | ||
| const layerInstance: Layer = useMemo(() => { | ||
| if (!cogBitmapOptions) { | ||
| console.warn(`No COG bitmap options found for layer ${key}. Ensure the datasource is configured correctly.`); | ||
| return null; | ||
| } | ||
| return new CogBitmapLayer({ | ||
| id: key, | ||
| rasterData: url, | ||
| isTiled: true, | ||
| opacity: opacity ?? 1, | ||
| visible: isActive, | ||
| cogBitmapOptions, | ||
| }); | ||
| /* TODO: Since cogBitmapOptions is derived from configuration, which originally is a string | ||
| (from ptr-be-core model HasConfiguration) and later parsed to an object, | ||
| we need to stringify it here to avoid infinite render loops due to object reference changes. */ | ||
| }, [url, isActive, key, opacity, JSON.stringify(cogBitmapOptions)]); | ||
|
||
|
|
||
| /** | ||
| * Effect hook to handle layer updates. | ||
| * The `onLayerUpdate` callback is called with the layer instance when the component mounts | ||
| * and with `null` when the component unmounts. | ||
| */ | ||
| useEffect(() => { | ||
| onLayerUpdate(key, layerInstance); | ||
| return () => onLayerUpdate(key, null); // Cleanup on unmount | ||
| }, [layerInstance, key, onLayerUpdate]); | ||
|
|
||
| // This component does not render any DOM elements | ||
| return null; | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.