Skip to content

Commit

Permalink
BHBC 1251 -> Further enhancement and correction (#458)
Browse files Browse the repository at this point in the history
Fixing up map layers inferring functionality due to weirdness with layers
  • Loading branch information
sdevalapurkar authored Aug 17, 2021
1 parent 0963031 commit 08c3204
Show file tree
Hide file tree
Showing 3 changed files with 272 additions and 70 deletions.
123 changes: 53 additions & 70 deletions app/src/components/map/MapContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import 'leaflet-fullscreen/dist/leaflet.fullscreen.css';
import { throttle } from 'lodash-es';
import { ReProjector } from 'reproj-helper';
import { useBiohubApi } from 'hooks/useBioHubApi';
import {
determineMapGeometries,
getInferredLayersInfoByProjectedGeometry,
getInferredLayersInfoByWFSFeature,
getLayerTypesToSkipByProjectedGeometry
} from 'utils/mapLayersHelpers';

/*
Get leaflet icons working
Expand Down Expand Up @@ -69,6 +75,25 @@ const layerGeoFilterTypeMappings = {
'pub:WHSE_WILDLIFE_MANAGEMENT.WAA_WILDLIFE_MGMT_UNITS_SVW': 'GEOMETRY'
};

const layersToInfer = [
'pub:WHSE_TANTALIS.TA_PARK_ECORES_PA_SVW',
'pub:WHSE_ADMIN_BOUNDARIES.ADM_NR_REGIONS_SPG',
'pub:WHSE_ADMIN_BOUNDARIES.EADM_WLAP_REGION_BND_AREA_SVW',
'pub:WHSE_WILDLIFE_MANAGEMENT.WAA_WILDLIFE_MGMT_UNITS_SVW'
];

export const envToNrmRegionsMapping = {
'1- Vancouver Island': 'West Coast Natural Resource Region',
'2- Lower Mainland': 'South Coast Natural Resource Region',
'3- Thompson': 'Thompson-Okanagan Natural Resource Region',
'8- Okanagan': 'Thompson-Okanagan Natural Resource Region',
'4- Kootenay': 'Kootenay-Boundary Natural Resource Region',
'5- Cariboo': 'Cariboo Natural Resource Region',
'6- Skeena': 'Skeena Natural Resource Region',
'7- Omineca': 'Omineca Natural Resource Region',
'9- Peace': 'Northeast Natural Resource Region'
};

const layerContentHandlers = {
'pub:WHSE_WILDLIFE_MANAGEMENT.WAA_WILDLIFE_MGMT_UNITS_SVW': {
featureKeyHandler: (feature: Feature) => feature?.properties?.OBJECTID,
Expand Down Expand Up @@ -189,13 +214,6 @@ const MapContainer: React.FC<IMapContainerProps> = (props) => {

const [preDefinedGeometry, setPreDefinedGeometry] = useState<Feature>();

const layersToInfer = [
'pub:WHSE_TANTALIS.TA_PARK_ECORES_PA_SVW',
'pub:WHSE_ADMIN_BOUNDARIES.ADM_NR_REGIONS_SPG',
'pub:WHSE_ADMIN_BOUNDARIES.EADM_WLAP_REGION_BND_AREA_SVW',
'pub:WHSE_WILDLIFE_MANAGEMENT.WAA_WILDLIFE_MGMT_UNITS_SVW'
];

// Add a geometry defined from an existing overlay feature (via its popup)
useEffect(() => {
if (!preDefinedGeometry) {
Expand Down Expand Up @@ -300,61 +318,41 @@ const MapContainer: React.FC<IMapContainerProps> = (props) => {
return coordinatesString;
};

// TODO break this into smaller functions
/*
Function to get WFS feature details based on the existing map geometries
and layer types/filter criteria
*/
const throttledGetFeatureDetails = useCallback(
throttle(async (typeNames: string[], wfsParams?: IWFSParams) => {
let inferredLayersInfo;
const parksInfo: Set<string> = new Set(); // Parks and Eco-Reserves
const nrmInfo: Set<string> = new Set(); // NRM Regions
const envInfo: Set<string> = new Set(); // ENV Regions
const wmuInfo: Set<string> = new Set(); // Wildlife Management Units
let inferredLayersInfo = {
parksInfo,
nrmInfo,
envInfo,
wmuInfo
};

let drawnGeometries: any[] = [];

if (geometryState?.geometry.length) {
drawnGeometries = geometryState?.geometry;
} else if (nonEditableGeometries) {
drawnGeometries = nonEditableGeometries.map((geo: INonEditableGeometries) => geo.feature);
}
// Get map geometries based on whether boundary is non editable or drawn/uploaded
const mapGeometries: Feature[] = determineMapGeometries(geometryState?.geometry, nonEditableGeometries);

// Convert all geometries to BC Albers projection
const reprojectedGeometries = await Promise.all(changeProjections(drawnGeometries));
const reprojectedGeometries = await Promise.all(changeProjections(mapGeometries));

const wfsPromises: Promise<any>[] = [];

reprojectedGeometries.forEach((projectedGeo) => {
let filterCriteria = '';
const coordinatesString = generateCoordinatesString(projectedGeo.geometry);

filterCriteria = `${projectedGeo.geometry.type}${coordinatesString}`;
inferredLayersInfo = getInferredLayersInfoByProjectedGeometry(projectedGeo, inferredLayersInfo);
const layerTypesToSkip = getLayerTypesToSkipByProjectedGeometry(projectedGeo);

let equivalentType = '';
const geoId = projectedGeo.id as string;

if (geoId && geoId.includes('TA_PARK_ECORES_PA_SVW')) {
equivalentType = 'TA_PARK_ECORES_PA_SVW';
parksInfo.add(projectedGeo.properties?.PROTECTED_LANDS_NAME);
}

if (geoId && geoId.includes('ADM_NR_REGIONS_SPG')) {
equivalentType = 'ADM_NR_REGIONS_SPG';
nrmInfo.add(projectedGeo.properties?.REGION_NAME);
}

if (geoId && geoId.includes('EADM_WLAP_REGION_BND_AREA_SVW')) {
equivalentType = 'EADM_WLAP_REGION_BND_AREA_SVW';
envInfo.add(projectedGeo.properties?.REGION_NUMBER_NAME);
}

if (geoId && geoId.includes('WAA_WILDLIFE_MGMT_UNITS_SVW')) {
equivalentType = 'WAA_WILDLIFE_MGMT_UNITS_SVW';
wmuInfo.add(
`${projectedGeo.properties?.WILDLIFE_MGMT_UNIT_ID}, ${projectedGeo.properties?.GAME_MANAGEMENT_ZONE_ID}, ${projectedGeo.properties?.GAME_MANAGEMENT_ZONE_NAME}`
);
}

// Make Open Maps API call to retrieve intersecting features based on geometry and filter criteria
typeNames.forEach((typeName: string) => {
if (!equivalentType || !typeName.includes(equivalentType)) {
if (!layerTypesToSkip.includes(typeName)) {
const url = buildWFSURL(typeName, wfsParams);
const geoFilterType = layerGeoFilterTypeMappings[typeName];
const filterData = `INTERSECTS(${geoFilterType}, ${filterCriteria})`;
Expand All @@ -370,41 +368,26 @@ const MapContainer: React.FC<IMapContainerProps> = (props) => {
}
});
});

const wfsResult = await Promise.all(wfsPromises);

wfsResult.forEach((item: any) => {
item?.features?.forEach((feature: Feature) => {
const featureId = feature.id as string;

if (featureId.includes('TA_PARK_ECORES_PA_SVW')) {
parksInfo.add(feature.properties?.PROTECTED_LANDS_NAME);
}

if (featureId.includes('ADM_NR_REGIONS_SPG')) {
nrmInfo.add(feature.properties?.REGION_NAME);
}

if (featureId.includes('EADM_WLAP_REGION_BND_AREA_SVW')) {
envInfo.add(feature.properties?.REGION_NUMBER_NAME);
}

if (featureId.includes('WAA_WILDLIFE_MGMT_UNITS_SVW')) {
wmuInfo.add(
`${feature.properties?.WILDLIFE_MGMT_UNIT_ID}, ${feature.properties?.GAME_MANAGEMENT_ZONE_ID}, ${feature.properties?.GAME_MANAGEMENT_ZONE_NAME}`
);
}
inferredLayersInfo = getInferredLayersInfoByWFSFeature(feature, inferredLayersInfo);
});
});

inferredLayersInfo = {
parks: Array.from(parksInfo),
nrm: Array.from(nrmInfo),
env: Array.from(envInfo),
wmu: Array.from(wmuInfo)
if (!inferredLayersInfo) {
return;
}

const inferredLayers = {
parks: Array.from(inferredLayersInfo.parksInfo),
nrm: Array.from(inferredLayersInfo.nrmInfo),
env: Array.from(inferredLayersInfo.envInfo),
wmu: Array.from(inferredLayersInfo.wmuInfo)
};

setInferredLayersInfo && setInferredLayersInfo(inferredLayersInfo);
setInferredLayersInfo && setInferredLayersInfo(inferredLayers);
}, 300),
[geometryState?.geometry, nonEditableGeometries]
);
Expand Down
12 changes: 12 additions & 0 deletions app/src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,15 @@ export const getFormattedFileSize = (fileSize: number) => {
// gigabyte size
return `${(fileSize / 1000000000).toFixed(1)} GB`;
};

/**
* Function to get an object key by the value
* Ex: let obj = { 'role': 'admin' } -> getKeyByValue(obj, 'admin') will return 'role'
*
* @param {object} object
* @param {any} value
* @returns {any} key for the corresponding value
*/
export function getKeyByValue(object: any, value: any) {
return Object.keys(object).find((key) => object[key] === value);
}
Loading

0 comments on commit 08c3204

Please sign in to comment.