-
Notifications
You must be signed in to change notification settings - Fork 9
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
Titiler-cmr layer #1001
Titiler-cmr layer #1001
Changes from 14 commits
973ce19
a4f4087
4b659fd
0242f19
495c88b
8653e48
0813bc7
c34b16a
402bf2c
f12bd6e
d705bf6
1b3bbd0
eb0cb4d
1ae67d9
a95e25b
b96b6b2
4791707
528b5a2
722793e
f793fe5
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 |
---|---|---|
|
@@ -11,7 +11,7 @@ interface ZarrResponseData { | |
zarr: { | ||
href: string | ||
} | ||
} | ||
}, | ||
} | ||
interface CMRResponseData { | ||
features: { | ||
|
@@ -23,8 +23,13 @@ interface CMRResponseData { | |
}[] | ||
} | ||
|
||
export function useZarr({ id, stacCol, stacApiEndpointToUse, date, onStatusChange }){ | ||
const [assetUrl, setAssetUrl] = useState(''); | ||
interface STACforCMRResponseData { | ||
collection_concept_id: string; | ||
renders: Record<string, any>; | ||
} | ||
|
||
export function useZarr({ id, stacCol, stacApiEndpointToUse, date, onStatusChange, sourceParams }){ | ||
const [tileParams, setTileParams] = useState({}); | ||
|
||
useEffect(() => { | ||
const controller = new AbortController(); | ||
|
@@ -38,11 +43,19 @@ export function useZarr({ id, stacCol, stacApiEndpointToUse, date, onStatusChang | |
controller | ||
}); | ||
|
||
setAssetUrl(data.assets.zarr.href); | ||
const tileParams = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔧 : This matches the local state variable's name, can we change this so it isn't confusing? Or i'd just set state directly with the object like so...
|
||
url: data.assets.zarr.href, | ||
time_slice: date, | ||
...sourceParams | ||
}; | ||
if (data.assets.zarr.href) { | ||
setTileParams(tileParams); | ||
} | ||
|
||
onStatusChange?.({ status: S_SUCCEEDED, id }); | ||
} catch (error) { | ||
if (!controller.signal.aborted) { | ||
setAssetUrl(''); | ||
setTileParams({}); | ||
onStatusChange?.({ status: S_FAILED, id }); | ||
} | ||
return; | ||
|
@@ -54,15 +67,15 @@ export function useZarr({ id, stacCol, stacApiEndpointToUse, date, onStatusChang | |
return () => { | ||
controller.abort(); | ||
}; | ||
}, [id, stacCol, stacApiEndpointToUse, date, onStatusChange]); | ||
}, [id, stacCol, stacApiEndpointToUse, date, onStatusChange, sourceParams]); | ||
|
||
return assetUrl; | ||
return tileParams; | ||
} | ||
|
||
|
||
|
||
export function useCMR({ id, stacCol, stacApiEndpointToUse, date, assetUrlReplacements, stacApiEndpoint, onStatusChange }){ | ||
const [assetUrl, setAssetUrl] = useState(''); | ||
export function useCMRSTAC({ id, stacCol, stacApiEndpointToUse, date, assetUrlReplacements, stacApiEndpoint, onStatusChange, sourceParams }){ | ||
const [tileParams, setTileParams] = useState({}); | ||
|
||
const replaceInAssetUrl = (url: string, replacement: AssetUrlReplacement) => { | ||
const {from, to } = replacement; | ||
|
@@ -90,11 +103,73 @@ export function useCMR({ id, stacCol, stacApiEndpointToUse, date, assetUrlReplac | |
}); | ||
|
||
const assetUrl = replaceInAssetUrl(data.features[0].assets.data.href, assetUrlReplacements); | ||
setAssetUrl(assetUrl); | ||
setTileParams({ | ||
url: assetUrl, | ||
time_slice: date, | ||
...sourceParams | ||
}); | ||
onStatusChange?.({ status: S_SUCCEEDED, id }); | ||
} catch (error) { | ||
if (!controller.signal.aborted) { | ||
setTileParams({}); | ||
onStatusChange?.({ status: S_FAILED, id }); | ||
} | ||
return; | ||
} | ||
} | ||
|
||
load(); | ||
|
||
return () => { | ||
controller.abort(); | ||
}; | ||
}, [id, stacCol, stacApiEndpointToUse, date, assetUrlReplacements, stacApiEndpoint, onStatusChange, sourceParams]); | ||
|
||
return tileParams; | ||
|
||
} | ||
|
||
|
||
export function useTitilerCMR({ id, stacCol, stacApiEndpointToUse, date, stacApiEndpoint, onStatusChange, sourceParams }){ | ||
const [tileParams, setTileParams] = useState({}); | ||
|
||
useEffect(() => { | ||
const controller = new AbortController(); | ||
|
||
async function load() { | ||
try { | ||
onStatusChange?.({ status: S_LOADING, id }); | ||
|
||
const data: STACforCMRResponseData = await requestQuickCache({ | ||
url: `${stacApiEndpointToUse}/collections/${stacCol}`, | ||
method: 'GET', | ||
controller | ||
}); | ||
|
||
let tileParams = { | ||
concept_id: data.collection_concept_id, | ||
datetime: date, | ||
...sourceParams | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔧 : This matches the naming of local state variable, can we change the name so it isn't confusing. |
||
}; | ||
|
||
// pick out the variable from the sourceParams and use it to get the renders params | ||
// see all ZarrReader Options: https://github.com/developmentseed/titiler-cmr/blob/develop/titiler/cmr/factory.py#L433-L452 | ||
const variable = sourceParams?.variable || null; | ||
if (variable != null) { | ||
tileParams.variable = variable; | ||
if (data.renders[variable]) { | ||
// what's in sourceParams will override what's in the renders object | ||
tileParams = { ...data.renders[variable], ...tileParams }; | ||
} | ||
} | ||
// if it's a COG collection we would want to use the bands parameter | ||
// see all Rasterio Reader Options: https://github.com/developmentseed/titiler-cmr/blob/develop/titiler/cmr/factory.py#L454-L498 | ||
|
||
setTileParams(tileParams); | ||
onStatusChange?.({ status: S_SUCCEEDED, id }); | ||
} catch (error) { | ||
if (!controller.signal.aborted) { | ||
setAssetUrl(''); | ||
setTileParams({}); | ||
onStatusChange?.({ status: S_FAILED, id }); | ||
} | ||
return; | ||
|
@@ -106,8 +181,8 @@ export function useCMR({ id, stacCol, stacApiEndpointToUse, date, assetUrlReplac | |
return () => { | ||
controller.abort(); | ||
}; | ||
}, [id, stacCol, stacApiEndpointToUse, date, assetUrlReplacements, stacApiEndpoint, onStatusChange]); | ||
}, [id, stacCol, stacApiEndpointToUse, date, stacApiEndpoint, onStatusChange, sourceParams]); | ||
|
||
return assetUrl; | ||
return tileParams; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,22 @@ | ||
import React from 'react'; | ||
import { Map as MapboxMap } from 'mapbox-gl'; | ||
|
||
import { ZarrPaintLayer } from './zarr-timeseries'; | ||
import { ActionStatus } from '$utils/status'; | ||
import { RasterPaintLayer } from './raster-paint-layer'; | ||
|
||
import { useCMR } from '$components/common/map/style-generators/hooks'; | ||
import { MapLayerRasterTimeseriesProps } from './raster-timeseries'; | ||
import { useCMRSTAC } from '$components/common/map/style-generators/hooks'; | ||
|
||
interface AssetUrlReplacement { | ||
from: string; | ||
to: string; | ||
} | ||
|
||
export interface MapLayerCMRTimeseriesProps { | ||
id: string; | ||
stacCol: string; | ||
date?: Date; | ||
mapInstance: MapboxMap; | ||
sourceParams?: Record<string, any>; | ||
stacApiEndpoint?: string; | ||
tileApiEndpoint?: string; | ||
assetUrlReplacements?: AssetUrlReplacement; | ||
zoomExtent?: number[]; | ||
onStatusChange?: (result: { status: ActionStatus; id: string }) => void; | ||
isHidden?: boolean; | ||
idSuffix?: string; | ||
} | ||
|
||
export function MapLayerCMRTimeseries(props:MapLayerCMRTimeseriesProps) { | ||
export function MapLayerCMRTimeseries(props:MapLayerRasterTimeseriesProps) { | ||
const { | ||
id, | ||
stacCol, | ||
stacApiEndpoint, | ||
date, | ||
assetUrlReplacements, | ||
onStatusChange, | ||
sourceParams, | ||
} = props; | ||
|
||
const stacApiEndpointToUse = stacApiEndpoint?? process.env.API_STAC_ENDPOINT; | ||
const assetUrl = useCMR({ id, stacCol, stacApiEndpointToUse, date, assetUrlReplacements, stacApiEndpoint, onStatusChange }); | ||
return <ZarrPaintLayer {...props} assetUrl={assetUrl} />; | ||
const tileParams = useCMRSTAC({ id, stacCol, stacApiEndpointToUse, date, assetUrlReplacements, stacApiEndpoint, onStatusChange, sourceParams }); | ||
return <RasterPaintLayer {...props} tileParams={tileParams} />; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder 🤔 if the backend team has json schemas we could generate into typescript interfaces to import, interested.. I'll follow up with them on this