Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct calculations for plantations #3661

Merged
merged 5 commits into from
Nov 29, 2018
Merged

Correct calculations for plantations #3661

merged 5 commits into from
Nov 29, 2018

Conversation

01painadam
Copy link
Contributor

@01painadam 01painadam commented Nov 26, 2018

Overview

Plantations loss / extent / emissions were not being summed over all types in the returned data, instead just taking the first element in the array. Note that plantations are returned with a bound1 or bound2 key.

(I suggest that we review this across all widgets that use forestType === 'plantations')

I have updated the header and loss in plantations widget to handle this. In all cases where there are plantations (and we therefor quote natural forest) we should have the figure in the header:

screen shot 2018-11-26 at 17 17 32

5.07kha in this case ... equal to the tooltip figure here:

screen shot 2018-11-26 at 17 17 41

natural forest: 5.07kha

Copy link
Contributor

@edbrett edbrett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates. No comments on your new code but some on the old. Maybe you can update quickly?

const returnData = {
...d,
outsideAreaLoss:
totalLossByYear[d.year][0].area - summedPlatationsLoss,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this was there before but it concerns me. As it is reused a few times can we declare totalLossByYear[d.year][0] as a variable above and then access the keys here. Something like this:

const totalLossForYear = (totalLossByYear[d.year] && totalLossByYear[d.year][0]) || {};

sumBy(groupedPlantationsLoss[latestYear], 'emissions');
const summedPlantationsExtent =
groupedPlantationsExtent &&
sumBy(groupedPlantationsExtent[latestYear], 'area');
const summedLoss = sumBy(groupedLoss[latestYear], 'area');
const summedEmissions = sumBy(groupedLoss[latestYear], 'emissions');
const data = {
totalArea: (extent[0] && extent[0].total_area) || 0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again here we aren't checking if extent is an array or even if it exists. Can we improve this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs addressing. What is the extent isnt an array? This will crash.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plantationsExtent is always a length = 1 array so i've gone with:

plantationsExtent && plantationsExtent.length ? plantationsExtent[0].area : 0,

const totalLoss = gadmLoss.data && gadmLoss.data.data;
if (loss.length && totalLoss.length) {
if (lossPlantations.length && totalLoss.length) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if lossPlantations is undefined then this will crash also. You need to check for it first before doing an array method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (lossPlantations && totalLoss && lossPlantations.length && totalLoss.length) { ?

@edbrett edbrett merged commit 5b8242a into develop Nov 29, 2018
@edbrett edbrett deleted the fix/plantations-calc branch November 29, 2018 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants