-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create new raster paint layer module and factor out BaseTimeseriesPro…
…pos (#1105) **Related Ticket:** #1055 ### Description of Changes Simply put, this PR is a small refactoring to create a basic `RasterPaintLayer` available, which both ZarrTimeseries and CmrTimeseries use. * Both CmrTimeseires and ZarrTimeseries were using `ZarrPaintLayer`. It seems appropriate then to rename `ZarrPaintLayer` to `RasterPaintLayer` and put it in a separate file. * Merged common attributes of `Raster`, `Zarr`, and `CmrTimeseriesPropos` into `BaseTimeseriesProps ### Notes & Questions About Changes 1. Should I create a new issue to remove the Zarr and Cmr layers from the app/scripts/common/components/mapbox/layers? 2. I noticed that a request is made for tiles even when the selected date falls outside of the datasets temporal range. Is this the expected behavior? I raised an issue in titiler-xarray (developmentseed/titiler-xarray#62) that the API should not respond with tiles in this case, but also wondering if a request should be made at all 🤔 * _This is probably something we need to fix in useZarr and useCmr, so the question is, should we do that in this PR or a new PR? I would propose a new PR to keep changes as minimal as possible._ ### Validation / Testing I loaded the layers in the deploy preview, used the show/hide and time slider to make sure all that functionality was working as expected. I came across the issue described in (2) under Notes & Questions About Changes
- Loading branch information
Showing
5 changed files
with
163 additions
and
171 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
122 changes: 122 additions & 0 deletions
122
app/scripts/components/common/map/style-generators/raster-paint-layer.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,122 @@ | ||
import { useEffect, useMemo } from 'react'; | ||
import qs from 'qs'; | ||
import { RasterSource, RasterLayer } from 'mapbox-gl'; | ||
|
||
import { BaseGeneratorParams } from '../types'; | ||
import useMapStyle from '../hooks/use-map-style'; | ||
import useGeneratorParams from '../hooks/use-generator-params'; | ||
|
||
interface RasterPaintLayerProps extends BaseGeneratorParams { | ||
id: string; | ||
date?: Date; | ||
sourceParams?: Record<string, any>; | ||
tileApiEndpoint?: string; | ||
zoomExtent?: number[]; | ||
assetUrl: string; | ||
} | ||
|
||
export function RasterPaintLayer(props: RasterPaintLayerProps) { | ||
const { | ||
id, | ||
tileApiEndpoint, | ||
date, | ||
sourceParams, | ||
zoomExtent, | ||
assetUrl, | ||
hidden, | ||
opacity | ||
} = props; | ||
|
||
const { updateStyle } = useMapStyle(); | ||
const [minZoom] = zoomExtent ?? [0, 20]; | ||
const generatorId = `zarr-timeseries-${id}`; | ||
|
||
// | ||
// Generate Mapbox GL layers and sources for raster timeseries | ||
// | ||
const haveSourceParamsChanged = useMemo( | ||
() => JSON.stringify(sourceParams), | ||
[sourceParams] | ||
); | ||
|
||
const generatorParams = useGeneratorParams(props); | ||
|
||
useEffect( | ||
() => { | ||
if (!assetUrl) 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
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