Skip to content

Commit df9e614

Browse files
authored
fix: Consolidate RasterPaintLayer (#1607)
**Related Ticket:** #1545 ### Description of Changes - [x] I need to confirm how we can get wmts tile url from non raster timeseries data. ### Validation / Testing Load all types of datasets (raster, CMR ([HLS data](https://deploy-preview-837--visex.netlify.app/exploration?datasets=%5B%7B%22id%22%3A%22HLSL30_2.0_false_color%22%2C%22settings%22%3A%7B%22isVisible%22%3Atrue%2C%22opacity%22%3A100%2C%22analysisMetrics%22%3A%5B%7B%22id%22%3A%22mean%22%2C%22label%22%3A%22Average%22%2C%22chartLabel%22%3A%22Average%22%2C%22themeColor%22%3A%22infographicB%22%7D%2C%7B%22id%22%3A%22std%22%2C%22label%22%3A%22St+Deviation%22%2C%22chartLabel%22%3A%22St+Deviation%22%2C%22themeColor%22%3A%22infographicD%22%7D%5D%7D%7D%2C%7B%22id%22%3A%22HLSL30_2.0_true_color%22%2C%22settings%22%3A%7B%22isVisible%22%3Afalse%2C%22opacity%22%3A100%2C%22analysisMetrics%22%3A%5B%7B%22id%22%3A%22mean%22%2C%22label%22%3A%22Average%22%2C%22chartLabel%22%3A%22Average%22%2C%22themeColor%22%3A%22infographicB%22%7D%2C%7B%22id%22%3A%22std%22%2C%22label%22%3A%22St+Deviation%22%2C%22chartLabel%22%3A%22St+Deviation%22%2C%22themeColor%22%3A%22infographicD%22%7D%5D%7D%7D%2C%7B%22id%22%3A%22HLSS30_2.0_false_color%22%2C%22settings%22%3A%7B%22isVisible%22%3Atrue%2C%22opacity%22%3A100%2C%22analysisMetrics%22%3A%5B%7B%22id%22%3A%22mean%22%2C%22label%22%3A%22Average%22%2C%22chartLabel%22%3A%22Average%22%2C%22themeColor%22%3A%22infographicB%22%7D%2C%7B%22id%22%3A%22std%22%2C%22label%22%3A%22St+Deviation%22%2C%22chartLabel%22%3A%22St+Deviation%22%2C%22themeColor%22%3A%22infographicD%22%7D%5D%7D%7D%5D&taxonomy=%7B%7D&search=&date=2024-08-30T15%3A00%3A00.000Z&aois=%5B%5D&dateRange=) - you will need to zoom to minnesota to see the data), WMS (any ESRI data that @Slesa added: https://deploy-preview-1607--veda-ui.netlify.app/exploration?search=arcgis&datasets=%5B%5D&taxonomy=%7B%7D ) - I believe we no longer have a Zarr dataset on production). Ensure they are working as expected. 'Expectation' includes tile loading, zooming to the dataset for raster timeseries data, datetime change, and 'Load into GIS' functionality. @aboydnw - Can I ask to test if all the layers work as expected? I created a preview on veda config in case this helps: NASA-IMPACT/veda-config#837 @slesaad - Can you check if I am not missing anything from wms layer?
1 parent 1dfb599 commit df9e614

File tree

9 files changed

+331
-265
lines changed

9 files changed

+331
-265
lines changed

app/scripts/components/common/map/hooks/use-map-style.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { useContext } from "react";
2-
import { StylesContext, StylesContextType } from "../styles";
3-
import useCustomMarker from "./use-custom-marker";
4-
import useMaps from "./use-maps";
1+
import { useContext } from 'react';
2+
import { StylesContext, StylesContextType } from '../styles';
3+
import useCustomMarker from './use-custom-marker';
4+
import useMaps from './use-maps';
55

66
export function useStylesContext(): StylesContextType {
77
return useContext(StylesContext);

app/scripts/components/common/map/style-generators/cmr-timeseries.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ import { useRequestStatus } from './hooks';
88
import { userTzDate2utcString } from '$utils/date';
99

1010
export function CMRTimeseries(props: BaseTimeseriesProps) {
11-
const { id, date, sourceParams, onStatusChange } = props;
11+
const {
12+
id,
13+
date,
14+
sourceParams,
15+
onStatusChange,
16+
hidden,
17+
opacity,
18+
generatorOrder
19+
} = props;
1220
const start_datetime = userTzDate2utcString(startOfDay(date));
1321
const end_datetime = userTzDate2utcString(endOfDay(date));
1422
const tileParams = {
@@ -28,6 +36,14 @@ export function CMRTimeseries(props: BaseTimeseriesProps) {
2836
tileParams={tileParams}
2937
generatorPrefix='cmr-timeseries'
3038
onStatusChange={changeStatus}
39+
hidden={hidden}
40+
opacity={opacity}
41+
generatorOrder={generatorOrder}
42+
metadataFormatter={(tilejsonData) => {
43+
return {
44+
xyzTileUrl: tilejsonData?.tiles[0]
45+
};
46+
}}
3147
/>
3248
);
3349
}

app/scripts/components/common/map/style-generators/raster-paint-layer.tsx

Lines changed: 81 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { useEffect, useMemo } from 'react';
2-
import qs from 'qs';
32
import { RasterSourceSpecification, RasterLayerSpecification } from 'mapbox-gl';
4-
3+
import { requestQuickCache } from '../utils';
54
import { BaseGeneratorParams } from '../types';
65
import useMapStyle from '../hooks/use-map-style';
76
import useGeneratorParams from '../hooks/use-generator-params';
87
import { STATUS_KEY } from './hooks';
98

9+
import { formatTitilerParameter } from './utils';
10+
import { TileJSON } from '$types/veda';
1011
import { ActionStatus, S_SUCCEEDED } from '$utils/status';
1112

1213
interface RasterPaintLayerProps extends BaseGeneratorParams {
@@ -17,6 +18,11 @@ interface RasterPaintLayerProps extends BaseGeneratorParams {
1718
tileParams: Record<string, any>;
1819
generatorPrefix?: string;
1920
reScale?: { min: number; max: number };
21+
metadataFormatter?: (
22+
tileJsonData: TileJSON | null,
23+
tileParamsAsString: string
24+
) => Record<string, any>;
25+
sourceParamFormatter?: (tileUrl: string) => Record<string, any>;
2026
onStatusChange?: (params: {
2127
status: ActionStatus;
2228
context: STATUS_KEY;
@@ -34,6 +40,8 @@ export function RasterPaintLayer(props: RasterPaintLayerProps) {
3440
colorMap,
3541
reScale,
3642
generatorPrefix = 'raster',
43+
metadataFormatter,
44+
sourceParamFormatter = (tileUrl) => ({ url: tileUrl }),
3745
onStatusChange
3846
} = props;
3947
const { updateStyle } = useMapStyle();
@@ -60,88 +68,82 @@ export function RasterPaintLayer(props: RasterPaintLayerProps) {
6068

6169
useEffect(
6270
() => {
63-
// Create a modified version of the parameters
64-
const processedParams = { ...updatedTileParams } as {
65-
reScale?: number[];
66-
colormap_name?: string;
67-
bands?: string[];
68-
assets?: string[];
69-
[key: string]: any;
70-
};
71-
72-
// bands and assets need to be sent as repeat query params
73-
const { bands, assets, bbox, ...regularParams } = processedParams;
74-
75-
const repeatParams: Record<string, string[] | undefined> = {};
76-
if (Array.isArray(bands)) repeatParams.bands = bands;
77-
if (Array.isArray(assets)) repeatParams.assets = assets;
78-
79-
const regularParamsString = qs.stringify(regularParams, {
80-
arrayFormat: 'comma'
81-
});
71+
if (!tileApiEndpoint) return;
72+
const controller = new AbortController();
73+
async function run() {
74+
// Create a modified version of the parameters
75+
const tileParamsAsString = formatTitilerParameter(updatedTileParams);
76+
const tileUrl = `${tileApiEndpoint}?${tileParamsAsString}`;
77+
78+
try {
79+
let tileUrlMetadata;
80+
if (!generatorPrefix.includes('wms')) {
81+
// wms data doesn't have an endpoint for tlejson
82+
const tileJsonData = await requestQuickCache<any>({
83+
url: tileUrl,
84+
method: 'GET',
85+
payload: null,
86+
controller
87+
});
88+
89+
tileUrlMetadata =
90+
metadataFormatter &&
91+
metadataFormatter(tileJsonData, tileParamsAsString);
92+
} else {
93+
tileUrlMetadata =
94+
metadataFormatter && metadataFormatter(null, tileParamsAsString);
95+
}
8296

83-
const repeatParamsString = qs.stringify(repeatParams, {
84-
arrayFormat: 'repeat'
85-
});
97+
const mapSourceParams = sourceParamFormatter(tileUrl);
8698

87-
// Need to use raw query params for bbox, because it's a mapbox template query param
88-
const bboxString = bbox ? `bbox=${bbox}` : '';
89-
90-
const tileParamsAsString = [
91-
regularParamsString,
92-
repeatParamsString,
93-
bboxString
94-
]
95-
.filter(Boolean) // Remove empty strings
96-
.join('&');
97-
98-
// Use url only if the request is being made to a tilejson endpoint (eg. raster)
99-
// otherwise use tiles array (eg. wms)
100-
const mapSourceParams = tileApiEndpoint?.includes('tilejson')
101-
? {
102-
url: `${tileApiEndpoint}?${tileParamsAsString}`
103-
}
104-
: {
105-
tiles: [`${tileApiEndpoint}?${tileParamsAsString}`],
106-
tileSize: 256
99+
const rasterSource: RasterSourceSpecification = {
100+
type: 'raster',
101+
...mapSourceParams
107102
};
108103

109-
const zarrSource: RasterSourceSpecification = {
110-
type: 'raster',
111-
...mapSourceParams
112-
};
113-
114-
const rasterOpacity = typeof opacity === 'number' ? opacity / 100 : 1;
115-
116-
const zarrLayer: RasterLayerSpecification = {
117-
id: id,
118-
type: 'raster',
119-
source: id,
120-
paint: {
121-
'raster-opacity': hidden ? 0 : rasterOpacity,
122-
'raster-opacity-transition': {
123-
duration: 320
124-
}
125-
},
126-
minzoom: minZoom,
127-
metadata: {
128-
layerOrderPosition: 'raster'
129-
}
130-
};
131-
132-
const sources = {
133-
[id]: zarrSource
134-
};
135-
const layers = [zarrLayer];
104+
const rasterOpacity = typeof opacity === 'number' ? opacity / 100 : 1;
105+
106+
const rasterLayer: RasterLayerSpecification = {
107+
id: id,
108+
type: 'raster',
109+
source: id,
110+
paint: {
111+
'raster-opacity': hidden ? 0 : rasterOpacity,
112+
'raster-opacity-transition': {
113+
duration: 320
114+
}
115+
},
116+
minzoom: minZoom,
117+
metadata: {
118+
id,
119+
layerOrderPosition: 'raster',
120+
...tileUrlMetadata
121+
}
122+
};
136123

137-
updateStyle({
138-
generatorId,
139-
sources,
140-
layers,
141-
params: generatorParams
142-
});
143-
if (onStatusChange)
144-
onStatusChange({ status: S_SUCCEEDED, context: STATUS_KEY.Layer });
124+
const sources = {
125+
[id]: rasterSource
126+
};
127+
const layers = [rasterLayer];
128+
129+
updateStyle({
130+
generatorId,
131+
sources,
132+
layers,
133+
params: generatorParams
134+
});
135+
if (onStatusChange)
136+
onStatusChange({
137+
status: S_SUCCEEDED,
138+
context: STATUS_KEY.Layer
139+
});
140+
} catch (e) {
141+
// eslint-disable-next-line no-console
142+
console.error(e);
143+
throw e;
144+
}
145+
}
146+
run();
145147
},
146148
// sourceParams not included, but using a stringified version of it to
147149
// detect changes (haveSourceParamsChanged)

0 commit comments

Comments
 (0)