Skip to content

Commit

Permalink
fix: filter location by altitudinalZoneForestEcoregion, aspect and slope
Browse files Browse the repository at this point in the history
  • Loading branch information
friedjoff committed Sep 9, 2020
1 parent c1c613d commit b4b0111
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"dependencies": {
"lodash.difference": "^4.5.0",
"lodash.get": "^4.4.2",
"lodash.intersection": "^4.4.0",
"lodash.union": "^4.6.0",
"lodash.xor": "^4.5.0"
Expand Down
136 changes: 124 additions & 12 deletions lib/src/locate.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import get from 'lodash.get';
import intersection from 'lodash.intersection';

import ecograms from '../data/ecograms.json';
Expand All @@ -7,6 +8,103 @@ import types from '../data/types.json';
const by12 = (value) => [1, 2].includes(value);
const by129 = (value) => [1, 2, 9].includes(value);

const altitudinalZoneForestEcoregionLookup = {
J: {
20: '0.0',
40: '0.1',
50: '0.2',
60: '0.3',
80: '0.4',
},
M: {
20: '1.0',
40: '1.1',
50: '1.2',
60: '1.3',
80: '1.4',
},
1: {
20: '2.0',
40: '2.1',
50: '2.2',
60: '2.3',
80: '2.4',
90: '2.5',
100: '2.6',
},
'2a': {
20: '3.0',
40: '3.1',
50: '3.2',
60: '3.3',
80: '3.4',
90: '3.5',
100: '3.6',
},
'2b': {
20: '4.0',
80: '4.1',
90: '4.4',
100: '4.5',
},
3: {
80: '5.0',
90: '5.1',
100: '5.2',
},
4: {
40: '6.0',
80: '6.1',
90: '6.2',
100: '6.3',
},
'5a': {
10: '7.0',
20: '7.1',
30: '7.2',
70: '7.3',
80: '7.4',
90: '7.5',
100: '7.6',
},
'5b': {
10: '8.0',
30: '8.1',
70: '8.2',
90: '8.3',
},
Me: {
10: '9.0',
30: '9.1',
},
};
const aspectLookup = [
'001-025',
'026-050',
'051-075',
'076-100',
'101-125',
'126-150',
'151-175',
'176-200',
'201-225',
'226-250',
'251-275',
'276-300',
'301-325',
'326-350',
'351-375',
'376-400',
];
const slopeLookup = [
'000-010',
'010-025',
'025-050',
'050-075',
'075-100',
'100',
];

/**
* This function tries to locate the forest type for a given location.
*
Expand Down Expand Up @@ -35,14 +133,14 @@ function locate(location = {}) {
}

let forestTypes = types.forestType;
if (location.forestEcoregion && location.altitudinalZone) {
forestTypes = forestTypes.filter(
(ft) =>
ft.altitudinalZoneForestEcoregion &&
ft.altitudinalZoneForestEcoregion.filter(
([az, fe]) => az === altitudinalZone && fe === forestEcoregion,
).length > 0,
);
if (forestEcoregion && altitudinalZone) {
forestTypes = forestTypes.filter((ft) => {
const altitudinalZoneForestEcoregion = get(
ft.altitudinalZoneForestEcoregion,
altitudinalZoneForestEcoregionLookup[forestEcoregion][altitudinalZone],
);
return by12(altitudinalZoneForestEcoregion);
});
}
if (location.indicators && location.indicators.length > 0) {
const indicatorsForestTypes = location.indicators.map(
Expand Down Expand Up @@ -113,13 +211,27 @@ function locate(location = {}) {
forestTypes = forestTypes.filter((ft) => by12(ft.reliefType[4]));
}
if (location.aspects) {
forestTypes = forestTypes.filter(
(ft) => intersection(ft.aspect, location.aspects).length > 0,
forestTypes = forestTypes.filter((ft) =>
location.aspects.some((la) =>
by12(
get(
ft.aspect,
aspectLookup.findIndex((al) => al == la),
),
),
),
);
}
if (location.slopes) {
forestTypes = forestTypes.filter(
(ft) => intersection(ft.slope, location.slopes).length > 0,
forestTypes = forestTypes.filter((ft) =>
location.slopes.some((ls) =>
by12(
get(
ft.slope,
slopeLookup.findIndex((sl) => sl == ls),
),
),
),
);
}
if (location.groups) {
Expand Down

0 comments on commit b4b0111

Please sign in to comment.