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

FAO Globalisation, Tree-Cover Tab #3399

Merged
merged 1 commit into from
May 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import axios from 'axios';
import sumBy from 'lodash/sumBy';
import omit from 'lodash/omit';

import { getFAO } from 'services/forest-data';
import { getRanking } from 'services/country';
Expand All @@ -12,8 +14,18 @@ export const getData = ({ params, dispatch, setWidgetData, widget }) => {
const fao = getFAOResponse.data.rows;
const ranking = getRankingResponse.data.rows;
if (fao.length && ranking.length) {
fao.forEach(
(fao.area_ha = parseFloat(fao.area_ha.replace(',', '')) * 1000)
);
let faoData = fao[0];
if (fao.length > 1) {
faoData = {};
Object.keys(omit(fao[0], ['iso', 'name'])).forEach(k => {
faoData[k] = sumBy(fao, k);
});
}
data = {
...fao[0],
...faoData,
rank: ranking[0].rank || 0
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export const initialState = {
},
colors: 'extent',
sentences: {
globalInitial:
'FAO data from 2015 shows that there was {extent} of forest {location}, with Primary forest occupying {primaryPercent} of land area.',
globalNoPrimary:
'FAO data from 2015 shows that there was {extent} of forest {location}, which occupies {primaryPercent} of the country.',
initial:
'FAO data from 2015 shows that {location} contains {extent} of forest, with Primary forest occupying {primaryPercent} of the country.',
noPrimary:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,39 @@ const getSentences = state => state.config && state.config.sentences;
export const parseData = createSelector(
[getData, getCurrentLocation, getColors],
(data, currentLabel, colors) => {
if (isEmpty(data) || !currentLabel) return null;
if (isEmpty(data)) return null;
const {
area_ha,
extent,
forest_planted,
planted_forest,
forest_primary,
forest_regenerated
} = data;
const colorRange = getColorPalette(colors.ramp, 3);
const naturallyRegenerated = extent / 100 * forest_regenerated;
const primaryForest = forest_primary ? extent / 100 * forest_primary : 0;
const plantedForest = extent / 100 * forest_planted;
const nonForest =
area_ha - (naturallyRegenerated + primaryForest + plantedForest);
const total =
naturallyRegenerated + primaryForest + plantedForest + area_ha;
area_ha - (forest_regenerated + forest_primary + planted_forest);
return [
{
label: 'Naturally regenerated Forest',
value: naturallyRegenerated,
percentage: naturallyRegenerated / total * 100,
value: forest_regenerated,
percentage: forest_regenerated / area_ha * 100,
color: colorRange[0]
},
{
label: 'Primary Forest',
value: primaryForest,
percentage: primaryForest / total * 100,
value: forest_primary,
percentage: forest_primary / area_ha * 100,
color: colorRange[1]
},
{
label: 'Planted Forest',
value: plantedForest,
percentage: plantedForest / total * 100,
value: planted_forest,
percentage: planted_forest / area_ha * 100,
color: colorRange[2]
},
{
label: 'Non-Forest',
value: nonForest,
percentage: nonForest / total * 100,
percentage: nonForest / area_ha * 100,
color: colors.nonForest
}
];
Expand All @@ -61,19 +55,20 @@ export const parseData = createSelector(
export const getSentence = createSelector(
[getData, getCurrentLocation, getSentences],
(data, currentLabel, sentences) => {
if (isEmpty(data) || !currentLabel) return null;
const { initial, noPrimary } = sentences;
if (isEmpty(data)) return null;
const { initial, noPrimary, globalInitial, globalNoPrimary } = sentences;
const { area_ha, extent, forest_primary } = data;
const primaryForest = extent / 100 * forest_primary;
const sentence = primaryForest > 0 ? initial : noPrimary;

const params = {
location: currentLabel,
location: currentLabel || 'globally',
extent: `${format('.3s')(extent)}ha`,
primaryPercent:
primaryForest > 0
? `${format('.0f')(primaryForest / area_ha * 100)}%`
forest_primary > 0
? `${format('.0f')(forest_primary / area_ha * 100)}%`
: `${format('.0f')(extent / area_ha * 100)}%`
};
let sentence = forest_primary > 0 ? initial : noPrimary;
if (!currentLabel) { sentence = forest_primary > 0 ? globalInitial : globalNoPrimary; }
return {
sentence,
params
Expand Down
6 changes: 3 additions & 3 deletions app/javascript/services/forest-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const SQL_QUERIES = {
lossRanked:
"SELECT polyname, year_data.year as year, SUM(year_data.area_loss) as loss, SUM({extent_year}) as extent, FROM data WHERE polyname = '{polyname}' AND thresh={threshold} GROUP BY polyname, iso, nested(year_data.year)",
fao:
"SELECT fao.iso, fao.name, forest_planted, forest_primary, forest_regenerated, fao.forest_primary, fao.extent, a.land as area_ha FROM gfw2_countries as fao INNER JOIN umd_nat_staging as a ON fao.iso = a.iso WHERE fao.iso = '{country}' AND a.year = 2001 AND a.thresh = 30",
'SELECT country AS iso, name, plantfor * 1000 AS planted_forest, primfor * 1000 AS forest_primary, natregfor * 1000 AS forest_regenerated, forest * 1000 AS extent, totarea as area_ha FROM table_1_forest_area_and_characteristics WHERE {location} year = 2015',
faoExtent:
'SELECT country AS iso, name, year, reforest * 1000 AS rate, forest*1000 AS extent FROM table_1_forest_area_and_characteristics as fao WHERE fao.year = {period} AND reforest > 0 ORDER BY rate DESC',
faoDeforest:
Expand Down Expand Up @@ -185,8 +185,8 @@ export const getLoss = ({

export const getFAO = ({ country }) => {
const url = `${CARTO_REQUEST_URL}${SQL_QUERIES.fao}`.replace(
'{country}',
country
'{location}',
country ? `country = '${country}' AND` : ''
);
return axios.get(url);
};
Expand Down