Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:Vizzuality/gfw
Browse files Browse the repository at this point in the history
  • Loading branch information
edbrett committed Sep 10, 2020
2 parents fdbd0b5 + 375e0cf commit 48feb5c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 46 deletions.
99 changes: 53 additions & 46 deletions components/widgets/forest-change/tree-loss-pct/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import sumBy from 'lodash/sumBy';
import { formatNumber } from 'utils/format';
import {
yearTicksFormatter,
zeroFillYears
zeroFillYears,
} from 'components/widgets/utils/data';

// get list data
const getLoss = state => state.data && state.data.loss;
const getExtent = state => state.data && state.data.extent;
const getPrimaryLoss = state => state.data && state.data.primaryLoss;
const getAdminLoss = state => state.data && state.data.adminLoss;
const getSettings = state => state.settings;
const getLocationLabel = state => state.locationLabel;
const getIndicator = state => state.indicator;
const getColors = state => state.colors;
const getSentence = state => state && state.sentence;
const getTitle = state => state.title;
const getLoss = (state) => state.data && state.data.loss;
const getExtent = (state) => state.data && state.data.extent;
const getPrimaryLoss = (state) => state.data && state.data.primaryLoss;
const getAdminLoss = (state) => state.data && state.data.adminLoss;
const getSettings = (state) => state.settings;
const getLocationLabel = (state) => state.locationLabel;
const getIndicator = (state) => state.indicator;
const getColors = (state) => state.colors;
const getSentence = (state) => state && state.sentence;
const getTitle = (state) => state.title;

const parseData = createSelector(
[getAdminLoss, getPrimaryLoss, getLoss, getExtent, getSettings],
Expand All @@ -34,21 +34,21 @@ const parseData = createSelector(
return null;
}
const { startYear, endYear, yearsRange } = settings;
const years = yearsRange && yearsRange.map(yearObj => yearObj.value);
const years = yearsRange && yearsRange.map((yearObj) => yearObj.value);
const fillObj = {
area: 0,
biomassLoss: 0,
bound1: null,
emissions: 0,
percentage: 0
percentage: 0,
};
const initalLossArr = primaryLoss.find(d => d.year === 2002);
const initalLossArr = primaryLoss.find((d) => d.year === 2002);
const initalLoss = initalLossArr
? initalLossArr.umd_tree_cover_loss__ha
: 0;
const totalAdminLoss =
sumBy(
adminLoss.filter(d => d.year >= startYear && d.year <= endYear),
adminLoss.filter((d) => d.year >= startYear && d.year <= endYear),
'area'
) || 0;

Expand All @@ -63,7 +63,7 @@ const parseData = createSelector(
fillObj
);

const parsedData = zeroFilledData.map(d => {
const parsedData = zeroFilledData.map((d) => {
if (d.year !== 2001) initalExtent -= d.area;
const yearData = {
...d,
Expand All @@ -72,7 +72,7 @@ const parseData = createSelector(
area: d.area || 0,
emissions: d.emissions || 0,
extentRemainingHa: initalExtent,
extentRemaining: 100 * initalExtent / initalExtent2001
extentRemaining: (100 * initalExtent) / initalExtent2001,
};
return yearData;
});
Expand All @@ -87,60 +87,60 @@ const filterData = createSelector(
return null;
}
const { startYear, endYear } = settings;
return parsedData.filter(d => d.year >= startYear && d.year <= endYear);
return parsedData.filter((d) => d.year >= startYear && d.year <= endYear);
}
);

const parseConfig = createSelector([getColors], colors => ({
const parseConfig = createSelector([getColors], (colors) => ({
height: 250,
xKey: 'year',
yKeys: {
bars: {
area: {
fill: colors.primaryForestLoss,
background: false,
yAxisId: 'area'
}
yAxisId: 'area',
},
},
lines: {
extentRemaining: {
stroke: colors.primaryForestExtent,
yAxisId: 'extentRemaining',
strokeDasharray: '3 4'
}
}
strokeDasharray: '3 4',
},
},
},
xAxis: {
tickFormatter: yearTicksFormatter
tickFormatter: yearTicksFormatter,
},
yAxis: {
yAxisId: 'area'
yAxisId: 'area',
},
rightYAxis: {
yAxisId: 'extentRemaining',
unit: '%',
maxYValue: 100
maxYValue: 100,
},
unit: 'ha',
tooltip: [
{
key: 'year'
key: 'year',
},
{
key: 'extentRemaining',
unitFormat: value =>
unitFormat: (value) =>
formatNumber({ num: value, unit: '%', precision: 3 }),
label: 'Primary forest extent remaining',
color: colors.primaryForestExtent,
dashline: true
dashline: true,
},
{
key: 'area',
unitFormat: value => formatNumber({ num: value, unit: 'ha' }),
unitFormat: (value) => formatNumber({ num: value, unit: 'ha' }),
label: 'Primary forest loss',
color: colors.primaryForestLoss
}
]
color: colors.primaryForestLoss,
},
],
}));

export const parseTitle = createSelector(
Expand All @@ -161,7 +161,7 @@ const parseSentence = createSelector(
getSettings,
getLocationLabel,
getIndicator,
getSentence
getSentence,
],
(data, extent, settings, locationLabel, indicator, sentences) => {
if (!data) return null;
Expand All @@ -171,19 +171,26 @@ const parseSentence = createSelector(
noLoss,
noLossWithIndicator,
globalInitial,
globalWithIndicator
globalWithIndicator,
} = sentences;
const { startYear, endYear } = settings;
const totalLossPrimary = (data && data.length && sumBy(data, 'area')) || 0;
const totalLoss = (data && data.length && data[0].totalLoss) || 0;

const filteredData =
data && data.length ? data.filter((y) => y.year > 2001) : [];
const totalLossPrimary = filteredData.length
? sumBy(filteredData, 'area')
: 0;
const totalLoss = filteredData.length ? filteredData[0].totalLoss : 0;
const percentageLoss =
(totalLoss && extent && totalLossPrimary / totalLoss * 100) || 0;
(totalLoss && extent && (totalLossPrimary / totalLoss) * 100) || 0;

const initialExtentData = data.find(d => d.year === startYear - 1);
const initialExtentData = filteredData.find(
(d) => d.year === startYear - 1
);
const initialExtent =
(initialExtentData && initialExtentData.extentRemaining) || 0;

const finalExtentData = data.find(d => d.year === endYear);
const finalExtentData = filteredData.find((d) => d.year === endYear);
const finalExtent =
(finalExtentData && finalExtentData.extentRemaining) || 0;

Expand All @@ -201,21 +208,21 @@ const parseSentence = createSelector(
endYear,
extentDelta: formatNumber({
num: Math.abs(initialExtent - finalExtent),
unit: '%'
unit: '%',
}),
loss: formatNumber({ num: totalLossPrimary, unit: 'ha' }),
percent: formatNumber({ num: percentageLoss, unit: '%' }),
component: {
key: 'total tree cover loss',
fine: true,
tooltip:
'Total tree cover loss includes loss in dry and non-tropical primary forests, secondary forests, and tree plantations in addition to humid primary forest loss.'
}
'Total tree cover loss includes loss in dry and non-tropical primary forests, secondary forests, and tree plantations in addition to humid primary forest loss.',
},
};

return {
sentence,
params
params,
};
}
);
Expand All @@ -224,5 +231,5 @@ export default createStructuredSelector({
data: filterData,
config: parseConfig,
sentence: parseSentence,
title: parseTitle
title: parseTitle,
});
16 changes: 16 additions & 0 deletions providers/datasets-provider/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ const decodes = {
alpha = 0.;
}
`,
staticRemap: `
float red = color.r;
float green = color.g;
float blue = color.b;
if (red == 0. && green == 0. && blue == 0.) {
alpha = 0.;
} else {
alpha = 1.;
}
color.r = red;
color.g = green;
color.b = blue;
`,
biomassLoss: `
float countBuckets = 5.; // buckets length / 3: three bands
float year = 2000.0 + (color.r * 255.);
Expand Down Expand Up @@ -581,6 +596,7 @@ export default {
treeLossByDriver: decodes.treeLossByDriver,
GLADs: decodes.GLADs,
RADDs: decodes.RADDs,
staticRemap: decodes.staticRemap,
biomassLoss: decodes.biomassLoss,
woodyBiomass: decodes.woodyBiomass,
terrai: decodes.terrai,
Expand Down

0 comments on commit 48feb5c

Please sign in to comment.