-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add zarr support to exploration page
- Loading branch information
1 parent
b34e3b4
commit 56111aa
Showing
5 changed files
with
253 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ [email protected] | |
|
||
API_RASTER_ENDPOINT='https://staging-raster.delta-backend.com' | ||
API_STAC_ENDPOINT='https://staging-stac.delta-backend.com' | ||
API_XARRAY_ENDPOINT='https://dev-titiler-xarray.delta-backend.com/tilejson.json' | ||
|
||
# If the app is being served in from a subfolder, the domain url must be set. | ||
# For example, if the app is served from /mysite: | ||
|
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
167 changes: 167 additions & 0 deletions
167
app/scripts/components/common/map/style-generators/zarr-timeseries.tsx
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,167 @@ | ||
import { useEffect, useMemo, useState } from 'react'; | ||
import qs from 'qs'; | ||
import { RasterSource, RasterLayer } from 'mapbox-gl'; | ||
|
||
import { requestQuickCache } from '../utils'; | ||
import useMapStyle from '../hooks/use-map-style'; | ||
import useGeneratorParams from '../hooks/use-generator-params'; | ||
import { BaseGeneratorParams } from '../types'; | ||
|
||
import { ActionStatus, S_FAILED, S_LOADING, S_SUCCEEDED } from '$utils/status'; | ||
|
||
export interface ZarrTimeseriesProps extends BaseGeneratorParams { | ||
id: string; | ||
stacCol: string; | ||
date?: Date; | ||
sourceParams?: Record<string, any>; | ||
stacApiEndpoint?: string; | ||
tileApiEndpoint?: string; | ||
zoomExtent?: number[]; | ||
onStatusChange?: (result: { status: ActionStatus; id: string }) => void; | ||
} | ||
|
||
export function ZarrTimeseries(props: ZarrTimeseriesProps) { | ||
const { | ||
id, | ||
stacCol, | ||
stacApiEndpoint, | ||
tileApiEndpoint, | ||
date, | ||
sourceParams, | ||
zoomExtent, | ||
onStatusChange, | ||
hidden, | ||
opacity | ||
} = props; | ||
|
||
const { updateStyle } = useMapStyle(); | ||
const [assetUrl, setAssetUrl] = useState(''); | ||
|
||
const [minZoom] = zoomExtent ?? [0, 20]; | ||
|
||
const stacApiEndpointToUse = stacApiEndpoint ?? process.env.API_STAC_ENDPOINT; | ||
|
||
const generatorId = `zarr-timeseries-${id}`; | ||
|
||
// | ||
// Get the asset url | ||
// | ||
useEffect(() => { | ||
const controller = new AbortController(); | ||
|
||
async function load() { | ||
try { | ||
onStatusChange?.({ status: S_LOADING, id }); | ||
const data = await requestQuickCache({ | ||
url: `${stacApiEndpointToUse}/collections/${stacCol}`, | ||
method: 'GET', | ||
controller | ||
}); | ||
|
||
setAssetUrl(data.assets.zarr.href); | ||
onStatusChange?.({ status: S_SUCCEEDED, id }); | ||
} catch (error) { | ||
if (!controller.signal.aborted) { | ||
setAssetUrl(''); | ||
onStatusChange?.({ status: S_FAILED, id }); | ||
} | ||
return; | ||
} | ||
} | ||
|
||
load(); | ||
|
||
return () => { | ||
controller.abort(); | ||
}; | ||
}, [id, stacCol, stacApiEndpointToUse, date, onStatusChange]); | ||
|
||
// | ||
// Generate Mapbox GL layers and sources for raster timeseries | ||
// | ||
const haveSourceParamsChanged = useMemo( | ||
() => JSON.stringify(sourceParams), | ||
[sourceParams] | ||
); | ||
|
||
const generatorParams = useGeneratorParams(props); | ||
|
||
useEffect( | ||
() => { | ||
if (!tileApiEndpoint) return; | ||
|
||
const tileParams = qs.stringify({ | ||
url: assetUrl, | ||
time_slice: date, | ||
...sourceParams | ||
}); | ||
|
||
const zarrSource: RasterSource = { | ||
type: 'raster', | ||
url: `${tileApiEndpoint}?${tileParams}` | ||
}; | ||
|
||
const rasterOpacity = typeof opacity === 'number' ? opacity / 100 : 1; | ||
|
||
const zarrLayer: RasterLayer = { | ||
id: id, | ||
type: 'raster', | ||
source: id, | ||
paint: { | ||
'raster-opacity': hidden ? 0 : rasterOpacity, | ||
'raster-opacity-transition': { | ||
duration: 320 | ||
} | ||
}, | ||
minzoom: minZoom, | ||
metadata: { | ||
layerOrderPosition: 'raster' | ||
} | ||
}; | ||
|
||
const sources = { | ||
[id]: zarrSource | ||
}; | ||
const layers = [zarrLayer]; | ||
|
||
updateStyle({ | ||
generatorId, | ||
sources, | ||
layers, | ||
params: generatorParams | ||
}); | ||
}, | ||
// sourceParams not included, but using a stringified version of it to | ||
// detect changes (haveSourceParamsChanged) | ||
[ | ||
updateStyle, | ||
id, | ||
date, | ||
assetUrl, | ||
minZoom, | ||
tileApiEndpoint, | ||
haveSourceParamsChanged, | ||
generatorParams | ||
// generatorParams includes hidden and opacity | ||
// hidden, | ||
// opacity, | ||
// generatorId, // - dependent on id | ||
// sourceParams, // tracked by haveSourceParamsChanged | ||
] | ||
); | ||
|
||
// | ||
// Cleanup layers on unmount. | ||
// | ||
useEffect(() => { | ||
return () => { | ||
updateStyle({ | ||
generatorId, | ||
sources: {}, | ||
layers: [] | ||
}); | ||
}; | ||
}, [updateStyle, generatorId]); | ||
|
||
return null; | ||
} |
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