Skip to content

Commit

Permalink
Merge pull request #3661 from Vizzuality/fix/plantations-calc
Browse files Browse the repository at this point in the history
Correct calculations for plantations
  • Loading branch information
edbrett committed Nov 29, 2018
2 parents 585d9cc + d30df37 commit 5b8242a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ export default ({ params }) =>
.then(
axios.spread((plantationsloss, gadmLoss) => {
let data = {};
const loss = plantationsloss.data && plantationsloss.data.data;
const lossPlantations =
plantationsloss.data && plantationsloss.data.data;
const totalLoss = gadmLoss.data && gadmLoss.data.data;
if (loss.length && totalLoss.length) {
if (
lossPlantations &&
totalLoss &&
lossPlantations.length &&
totalLoss.length
) {
data = {
loss,
lossPlantations,
totalLoss
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { getColorPalette } from 'utils/data';
import { biomassToCO2 } from 'utils/calculations';

// get list data
const getLoss = state => (state.data && state.data.loss) || null;
const getLossPlantations = state =>
(state.data && state.data.lossPlantations) || null;
const getTotalLoss = state => (state.data && state.data.totalLoss) || null;
const getSettings = state => state.settings || null;
const getLocationName = state => state.locationName || null;
Expand All @@ -17,20 +18,34 @@ const getSentence = state => state.config && state.config.sentence;

// get lists selected
export const parseData = createSelector(
[getLoss, getTotalLoss, getSettings],
(loss, totalLoss, settings) => {
if (!loss || !totalLoss) return null;
[getLossPlantations, getTotalLoss, getSettings],
(lossPlantations, totalLoss, settings) => {
if (!lossPlantations || !totalLoss) return null;
const { startYear, endYear } = settings;
const totalLossByYear = groupBy(totalLoss, 'year');
return uniqBy(
loss.filter(d => d.year >= startYear && d.year <= endYear).map(d => ({
...d,
outsideAreaLoss: totalLossByYear[d.year][0].area - d.area,
areaLoss: d.area || 0,
totalLoss: totalLossByYear[d.year][0].area || 0,
outsideCo2Loss: totalLossByYear[d.year][0].emissions - d.emissions,
co2Loss: d.emissions || 0
})),
lossPlantations
.filter(d => d.year >= startYear && d.year <= endYear)
.map(d => {
const groupedPlantations = groupBy(lossPlantations, 'year')[d.year];
const summedPlatationsLoss =
groupedPlantations && sumBy(groupedPlantations, 'area');
const summedPlatationsEmissions =
groupedPlantations && sumBy(groupedPlantations, 'emissions');
const totalLossForYear =
(totalLossByYear[d.year] && totalLossByYear[d.year][0]) || {};

const returnData = {
...d,
outsideAreaLoss: totalLossForYear.area - summedPlatationsLoss,
areaLoss: summedPlatationsLoss || 0,
totalLoss: totalLossForYear || 0,
outsideCo2Loss:
totalLossByYear[d.year][0].emissions - summedPlatationsEmissions,
co2Loss: summedPlatationsEmissions || 0
};
return returnData;
}),
'year'
);
}
Expand Down
52 changes: 40 additions & 12 deletions app/javascript/pages/dashboards/header/header-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,55 @@ export const getHeaderData = createThunkAction(
) => {
const extent = totalExtent.data.data;
const loss = totalLoss.data.data;
const plantationsExtent = totalPlantationsExtent.data.data;
const plantationsLoss = totalPlantationsLoss.data.data;
const plantationsExtent = totalPlantationsExtent.data.data;

// group over years
const groupedLoss = plantationsLoss && groupBy(loss, 'year');
const groupedPlantationsLoss =
plantationsLoss && groupBy(plantationsLoss, 'year');

const primaryLoss = totalPrimaryLoss.data.data;
const groupedLoss = loss && groupBy(loss, 'year');
const latestYear = max(Object.keys(groupedLoss));
const summedLoss = sumBy(groupedLoss[latestYear], 'area');
const summedEmissions = sumBy(groupedLoss[latestYear], 'emissions');

const latestPlantationLoss =
groupedPlantationsLoss[latestYear] || null;
const latestLoss = groupedLoss[latestYear] || null;

// sum over different bound1 within year
const summedPlantationsLoss =
latestPlantationLoss.length && latestPlantationLoss[0].area
? sumBy(latestPlantationLoss, 'area')
: 0;
const summedPlantationsEmissions =
latestPlantationLoss.length && latestPlantationLoss[0].emissions
? sumBy(latestPlantationLoss, 'emissions')
: 0;
const summedLoss =
latestLoss.length && latestLoss[0].area
? sumBy(latestLoss, 'area')
: 0;
const summedEmissions =
latestLoss.length && latestLoss[0].emissions
? sumBy(latestLoss, 'emissions')
: 0;

const data = {
totalArea: (extent[0] && extent[0].total_area) || 0,
extent: (extent[0] && extent[0].value) || 0,
plantationsExtent:
(plantationsExtent[0] && plantationsExtent[0].value) || 0,
plantationsExtent && plantationsExtent.length
? plantationsExtent[0].area
: 0,
totalLoss: {
area: summedLoss,
year: latestYear,
emissions: summedEmissions
area: summedLoss || 0,
year: latestYear || 0,
emissions: summedEmissions || 0
},
plantationsLoss: {
area: summedPlantationsLoss || 0,
emissions: summedPlantationsEmissions || 0
},
plantationsLoss:
plantationsLoss && plantationsLoss.length
? reverse(sortBy(plantationsLoss, 'year'))[0]
: {},
primaryLoss:
primaryLoss && primaryLoss.length
? reverse(sortBy(primaryLoss, 'year'))[0]
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/pages/dashboards/header/header-reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const initialState = {
globalInitial:
'In 2010, {location} had {extent} of tree cover, extending over {percentage} of its land area. In {year}, it lost {loss} of tree cover.',
withPlantationLoss:
'In 2010, {location} had {naturalForest} of natural forest, extending over {percentage} of its land area. In {year}, it lost {loss} of natural forest',
'In 2010, {location} had {naturalForest} of natural forest, extending over {percentage} of its land area. In {year}, it lost {naturalLoss} of natural forest',
indoInitial:
'In 2010, {location} had {naturalForest} of natural forest, extending over {percentageNatForest} of its land area. In {year}, it lost {loss} of tree cover, equivalent to {emissionsTreeCover} of CO₂ of emissions. {primaryLoss} of this loss occurred within intact and degraded primary forests and {naturalLoss} within natural forest.',
co2Emissions: ', equivalent to {emission} of CO\u2082 of emissions.',
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/pages/dashboards/header/header-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const getSentence = createSelector(
const percentageNatForest = format('.2r')(
(data.extent - data.plantationsExtent) / data.totalArea * 100
);
const lossWithOutPlantations = format('.3s')(
const lossWithoutPlantations = format('.3s')(
data.totalLoss.area - (data.plantationsLoss.area || 0)
);
const emissionsWithoutPlantations = format('.3s')(
Expand All @@ -138,7 +138,7 @@ export const getSentence = createSelector(
location: location || 'the world',
percentage: `${percentageCover}%`,
percentageNatForest: `${percentageNatForest}%`,
naturalLoss: `${lossWithOutPlantations}ha`,
naturalLoss: `${lossWithoutPlantations}ha`,
loss: `${loss}ha`,
emission: `${emissionsWithoutPlantations}t`,
emissionsTreeCover: `${emissions}t`,
Expand Down

0 comments on commit 5b8242a

Please sign in to comment.