Skip to content

Commit

Permalink
Pass layer data to layer components instead of specifying
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfdsilva committed May 17, 2023
1 parent 4cd8a80 commit f8b15c1
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 110 deletions.
16 changes: 6 additions & 10 deletions app/scripts/components/common/blocks/scrollytelling/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -378,16 +378,14 @@ function Scrollytelling(props) {
<LayerCmp
key={runtimeData.id}
id={runtimeData.id}
mapInstance={mapRef.current}
stacCol={layer.stacCol}
mapInstance={mapRef.current!}
layerData={layer}
date={runtimeData.datetime}
sourceParams={layer.sourceParams}
zoomExtent={layer.zoomExtent}
onStatusChange={onLayerLoadSuccess}
isHidden={
!activeChapterLayerId ||
activeChapterLayerId !== runtimeData.id ||
activeChapter.showBaseMap
!!activeChapter.showBaseMap
}
/>
);
Expand Down Expand Up @@ -468,16 +466,14 @@ function Scrollytelling(props) {
<LayerCmp
key={runtimeData.id}
id={runtimeData.id}
mapInstance={mapRef.current}
stacCol={layer.stacCol}
mapInstance={mapRef.current!}
date={runtimeData.datetime}
sourceParams={layer.sourceParams}
zoomExtent={layer.zoomExtent}
layerData={layer}
onStatusChange={onLayerLoadSuccess}
isHidden={
!activeChapterLayerId ||
activeChapterLayerId !== runtimeData.id ||
activeChapter.showBaseMap
!!activeChapter.showBaseMap
}
/>
);
Expand Down
15 changes: 5 additions & 10 deletions app/scripts/components/common/mapbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,9 @@ function MapboxMapComponent(props: MapboxMapProps, ref) {
{isMapLoaded && baseLayerResolvedData && BaseLayerComponent && (
<BaseLayerComponent
id={`base-${baseLayerResolvedData.id}`}
stacCol={baseLayerResolvedData.stacCol}
assetUrl={baseLayerResolvedData.assetUrl}
mapInstance={mapRef.current}
layerData={baseLayerResolvedData}
mapInstance={mapRef.current!}
date={date}
sourceParams={baseLayerResolvedData.sourceParams}
zoomExtent={baseLayerResolvedData.zoomExtent}
onStatusChange={onBaseLayerStatusChange}
/>
)}
Expand Down Expand Up @@ -448,11 +445,9 @@ function MapboxMapComponent(props: MapboxMapProps, ref) {
CompareLayerComponent && (
<CompareLayerComponent
id={`compare-${compareLayerResolvedData.id}`}
stacCol={compareLayerResolvedData.stacCol}
mapInstance={mapCompareRef.current}
date={compareToDate}
sourceParams={compareLayerResolvedData.sourceParams}
zoomExtent={compareLayerResolvedData.zoomExtent}
layerData={compareLayerResolvedData}
mapInstance={mapCompareRef.current!}
date={compareToDate!}
onStatusChange={onCompareLayerStatusChange}
/>
)}
Expand Down
24 changes: 9 additions & 15 deletions app/scripts/components/common/mapbox/layers/raster-timeseries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ const LOG = true;

const FIT_BOUNDS_PADDING = 32;

interface MapLayerRasterTimeseriesProps {
export interface MapLayerRasterTimeseriesProps {
id: string;
stacCol: string;
date?: Date;
mapInstance: MapboxMap;
sourceParams: object;
zoomExtent?: [number, number];
onStatusChange?: (result: { status: ActionStatus; id: string }) => void;
isHidden: boolean;
isHidden?: boolean;
layerData: {
sourceParams?: object;
zoomExtent?: number[];
stacCol: string;
};
}

export interface StacFeature {
Expand All @@ -64,16 +66,8 @@ interface Statuses {
}

export function MapLayerRasterTimeseries(props: MapLayerRasterTimeseriesProps) {
const {
id,
stacCol,
date,
mapInstance,
sourceParams,
zoomExtent,
onStatusChange,
isHidden
} = props;
const { id, date, mapInstance, onStatusChange, isHidden } = props;
const { sourceParams, zoomExtent, stacCol } = props.layerData;

const theme = useTheme();
const { updateStyle } = useMapStyle();
Expand Down
23 changes: 18 additions & 5 deletions app/scripts/components/common/mapbox/layers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@ import {
DatasetDatumReturnType,
DatasetLayerCompareNormalized
} from 'veda';
import { MapLayerRasterTimeseries, StacFeature } from './raster-timeseries';
import { MapLayerVectorTimeseries } from './vector-timeseries';
import { MapLayerZarrTimeseries } from './zarr-timeseries';
import {
MapLayerRasterTimeseries,
MapLayerRasterTimeseriesProps,
StacFeature
} from './raster-timeseries';
import {
MapLayerVectorTimeseries,
MapLayerVectorTimeseriesProps
} from './vector-timeseries';
import {
MapLayerZarrTimeseries,
MapLayerZarrTimeseriesProps
} from './zarr-timeseries';

import { userTzDate2utcString, utcString2userTzDate } from '$utils/date';
import { AsyncDatasetLayer } from '$context/layer-data';
Expand All @@ -31,7 +41,11 @@ import { HintedError } from '$utils/hinted-error';
export const getLayerComponent = (
isTimeseries: boolean,
layerType: 'raster' | 'vector' | 'zarr'
): FunctionComponent<any> | null => {
): FunctionComponent<
| MapLayerRasterTimeseriesProps
| MapLayerVectorTimeseriesProps
| MapLayerZarrTimeseriesProps
> | null => {
if (isTimeseries) {
if (layerType === 'raster') return MapLayerRasterTimeseries;
if (layerType === 'vector') return MapLayerVectorTimeseries;
Expand Down Expand Up @@ -71,7 +85,6 @@ export const getCompareLayerData = (
id: stacCol,
stacCol,
type: type || layerData.type,
assetUrl: layerData.assetUrl,
zoomExtent: zoomExtent || layerData.zoomExtent,
sourceParams: defaultsDeep({}, sourceParams, layerData.sourceParams),
...passThroughProps
Expand Down
27 changes: 10 additions & 17 deletions app/scripts/components/common/mapbox/layers/vector-timeseries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,22 @@ import { useCustomMarker } from './custom-marker';
import { ActionStatus, S_FAILED, S_LOADING, S_SUCCEEDED } from '$utils/status';
import { userTzDate2utcString } from '$utils/date';

interface MapLayerVectorTimeseriesProps {
export interface MapLayerVectorTimeseriesProps {
id: string;
stacCol: string;
date?: Date;
mapInstance: MapboxMap;
sourceParams: object;
zoomExtent?: [number, number];
onStatusChange?: (result: { status: ActionStatus; id: string }) => void;
isHidden: boolean;
isHidden?: boolean;
layerData: {
sourceParams?: object;
zoomExtent?: number[];
stacCol: string;
};
}

export function MapLayerVectorTimeseries(props: MapLayerVectorTimeseriesProps) {
const {
id,
stacCol,
date,
mapInstance,
sourceParams,
zoomExtent,
onStatusChange,
isHidden
} = props;
const { id, date, mapInstance, onStatusChange, isHidden } = props;
const { sourceParams, zoomExtent, stacCol } = props.layerData;

const theme = useTheme();
const { updateStyle } = useMapStyle();
Expand Down Expand Up @@ -81,7 +75,6 @@ export function MapLayerVectorTimeseries(props: MapLayerVectorTimeseriesProps) {
};
}, [mapInstance, id, stacCol, date, onStatusChange]);


const markerLayout = useCustomMarker(mapInstance);

//
Expand Down Expand Up @@ -188,7 +181,7 @@ export function MapLayerVectorTimeseries(props: MapLayerVectorTimeseriesProps) {
'source-layer': 'default',
layout: {
...(markerLayout as any),
visibility: isHidden ? 'none' : 'visible',
visibility: isHidden ? 'none' : 'visible'
},
paint: {
'icon-color': theme.color?.infographicB,
Expand Down
55 changes: 23 additions & 32 deletions app/scripts/components/common/mapbox/layers/zarr-timeseries.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,52 @@
import { useEffect, useMemo } from 'react';
import qs from 'qs';
import {
AnyLayer,
AnySourceImpl,
RasterLayer,
RasterSource,
} from 'mapbox-gl';
import { AnyLayer, AnySourceImpl, RasterLayer, RasterSource } from 'mapbox-gl';

import { useMapStyle } from './styles';

interface MapLayerZarrTimeseriesProps {
export interface MapLayerZarrTimeseriesProps {
id: string;
date?: Date;
sourceParams: object;
zoomExtent?: [number, number];
assetUrl?: string;
isHidden: boolean;
isHidden?: boolean;
layerData: {
sourceParams?: object;
zoomExtent?: number[];
assetUrl: string;
};
}

export function MapLayerZarrTimeseries(props: MapLayerZarrTimeseriesProps) {
const {
id,
date,
sourceParams,
zoomExtent,
assetUrl,
isHidden
} = props;
const { id, date, isHidden } = props;
const { sourceParams = {}, zoomExtent, assetUrl } = props.layerData;

const { updateStyle } = useMapStyle();

const minZoom = zoomExtent?.[0] ?? 0;
const tilesUrl = process.env.API_XARRAY_ENDPOINT;
const tilerUrl = process.env.API_XARRAY_ENDPOINT;

//
// Generate Mapbox GL layers and sources for raster timeseries
//
const haveSourceParamsChanged = useMemo(
() => JSON.stringify(sourceParams),
[sourceParams]
);

useEffect(
() => {
let layers: AnyLayer[] = [];
let sources: Record<string, AnySourceImpl> = {};

if (tilesUrl) {
const tileParams = qs.stringify(
{
url: assetUrl,
time_slice: date,
...sourceParams
}
);
if (tilerUrl) {
const tileParams = qs.stringify({
url: assetUrl,
time_slice: date,
...sourceParams
});

const zarrSource: RasterSource = {
type: 'raster',
url: `${tilesUrl}?${tileParams}`
url: `${tilerUrl}?${tileParams}`
};

const zarrLayer: RasterLayer = {
Expand All @@ -78,10 +69,9 @@ export function MapLayerZarrTimeseries(props: MapLayerZarrTimeseriesProps) {
};

sources = {
...sources,
[id]: zarrSource
};
layers = [...layers, zarrLayer];
layers = [zarrLayer];
}

updateStyle({
Expand All @@ -95,7 +85,8 @@ export function MapLayerZarrTimeseries(props: MapLayerZarrTimeseriesProps) {
updateStyle,
id,
date,
tilesUrl,
assetUrl,
tilerUrl,
minZoom,
haveSourceParamsChanged,
isHidden
Expand Down
34 changes: 16 additions & 18 deletions app/scripts/context/layer-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ const fetchLayerById = async (
isPeriodic: data['dashboard:is_periodic'],
timeDensity: data['dashboard:time_density']
};
// TODO: Normalize API data structure
// For the time being the vector and raster sources have different api
// endpoints, and different properties to get data from.

if (type === 'vector') {
const featuresApiEndpoint = data.links.find((l) => l.rel === 'child').href;
const { data: featuresApiData } = await axios.get(featuresApiEndpoint);
Expand All @@ -55,23 +53,23 @@ const fetchLayerById = async (
domain: featuresApiData.extent.temporal.interval[0]
}
};
}

const defaultData = {
timeseries: {
...commonTimeseriesParams,
domain: data.summaries ? data.summaries.datetime : data.extent.temporal.interval[0]
}
};

if (type === 'zarr') {
return {
...defaultData,
assetUrl: data.assets.zarr.href
} else {
const defaultData = {
timeseries: {
...commonTimeseriesParams,
domain: data.summaries ? data.summaries.datetime : data.extent.temporal.interval[0]
}
};

if (type === 'zarr') {
return {
...defaultData,
assetUrl: data.assets.zarr.href
};
} else {
return defaultData;
}
}

return defaultData;
};

// Create a query object for react query.
Expand Down
3 changes: 0 additions & 3 deletions parcel-resolver-veda/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ declare module 'veda' {
initialDatetime?: 'newest' | 'oldest' | string;
projection?: ProjectionOptions;
type: DatasetLayerType;
assetUrl?: string;
stacApiOverride?: string;
compare: DatasetLayerCompareSTAC | DatasetLayerCompareInternal | null;
legend?: LayerLegendCategorical | LayerLegendGradient;
}
Expand All @@ -68,7 +66,6 @@ declare module 'veda' {
extends DatasetLayerCommonCompareProps {
id: string;
stacCol: string;
assetUrl?: string;
type: DatasetLayerType;
}

Expand Down

0 comments on commit f8b15c1

Please sign in to comment.