Skip to content

Commit

Permalink
Include data parsing for wms getfeatureinfo request
Browse files Browse the repository at this point in the history
  • Loading branch information
JorgeMartinezG committed Apr 24, 2021
1 parent 447082d commit 7c4018a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/components/MapView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const getActiveFeatureInfoLayers = (map: Map): WMSLayerProps[] | undefined => {
}

const featureInfoLayers = Object.values(LayerDefinitions).filter(
l => layerIds.includes(l.id) && l.type === 'wms' && l.featureinfoProps,
l => layerIds.includes(l.id) && l.type === 'wms' && l.featureInfoProps,
);

if (featureInfoLayers.length === 0) {
Expand Down
11 changes: 10 additions & 1 deletion src/config/mozambique/layers.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,16 @@
"main": false
},
"base_url": "https://geonode.wfp.org/geoserver",
"featureinfo_props": ["event_name", "pub_date"]
"feature_info_props": {
"event_name": {
"type": "text",
"label": "Event name"
},
"pub_date": {
"type": "date",
"label": "Publication date"
}
}
},
"adamts_nodes": {
"title": "Tropical Storms - Nodes",
Expand Down
13 changes: 12 additions & 1 deletion src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ export class BoundaryLayerProps extends CommonLayerProps {
adminLevelLocalNames: string[]; // Same as above, local to country
}

export enum labelType {
Date = 'date',
Text = 'text',
Number = 'number',
}

interface featureInfoProps {
type: labelType;
label: string;
}

export class WMSLayerProps extends CommonLayerProps {
type: 'wms';
baseUrl: string;
Expand All @@ -187,7 +198,7 @@ export class WMSLayerProps extends CommonLayerProps {
wcsConfig?: RawDataConfiguration;

@optional
featureinfoProps?: string[];
featureInfoProps?: { [key: string]: featureInfoProps };
}

export class NSOLayerProps extends CommonLayerProps {
Expand Down
24 changes: 18 additions & 6 deletions src/utils/server-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ImpactLayerProps,
WMSLayerProps,
FeatureInfoType,
labelType,
} from '../config/types';

// Note: PRISM's date picker is designed to work with dates in the UTC timezone
Expand Down Expand Up @@ -252,6 +253,14 @@ export async function getLayersAvailableDates(): Promise<AvailableDates> {
return merge({}, ...layerDates);
}

function parseValue(value: string, type: labelType): string {
if (type === labelType.Date) {
return `${moment(value).utc().format('MMMM Do YYYY, h:mm:ss')} UTC`;
}

return value;
}

function fetchFeatureInfo(
layers: WMSLayerProps[],
url: string,
Expand Down Expand Up @@ -283,11 +292,11 @@ function fetchFeatureInfo(
// Get fields from layer configuration.
const [layerId] = (feature?.id as string).split('.');

const searchProps =
(layers &&
layers.find(l => l.serverLayerName === layerId)
?.featureinfoProps) ||
[];
const featureInfoProps =
layers?.find(l => l.serverLayerName === layerId)?.featureInfoProps ||
{};

const searchProps = Object.keys(featureInfoProps);

const properties = feature.properties ?? {};

Expand All @@ -296,7 +305,10 @@ function fetchFeatureInfo(
.reduce(
(obj, key) => ({
...obj,
[key]: properties[key],
[featureInfoProps[key].label]: parseValue(
properties[key],
featureInfoProps[key].type,
),
}),
{},
);
Expand Down

0 comments on commit 7c4018a

Please sign in to comment.