Skip to content

Commit

Permalink
WIP: add fao years to options
Browse files Browse the repository at this point in the history
  • Loading branch information
wri7tno committed Nov 2, 2023
1 parent 54874a8 commit e5c92af
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 10 deletions.
24 changes: 19 additions & 5 deletions components/widgets/land-cover/fao-cover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,36 @@ export default {
},
settings: {
unit: 'ha',
faoYear: 2020,
},
refetchKeys: ['faoYear'],
sentences: {
globalInitial:
'According to the FAO, in {year}, {percent}% ({amountInHectares}) of the globe was covered by forest. Of this, {primaryPercent}%, was primary forest.',
'According to the FAO, in {year}, {percent} ({amountInHectares}) of the globe was covered by forest. Of this, {primaryPercent}, was primary forest.',
// globalInitial:
// 'FAO data from 2015 shows that there are {extent} of forest {location}, with primary forest occupying {primaryPercent} of the world.',
globalNoPrimary:
'FAO data from 2015 shows that there are {extent} of forest {location}, which occupies {primaryPercent} of the world.',
initial:
'According to the FAO, in {year}, {percent}% ({amountInHectares}) of {country} was covered by forest. Of this, {primaryPercent}%, was primary forest.',
'According to the FAO, in {year}, {percent} ({amountInHectares}) of {country} was covered by forest. Of this, {primaryPercent}, was primary forest.',
// initial:
// 'FAO data from 2015 shows that {location} contains {extent} of forest, with primary forest occupying {primaryPercent} of the country.',
noPrimary:
'FAO data from 2015 shows that {location} contains {extent} of forest, which occupies {primaryPercent} of the country.',
},
getData: (params) =>
all([getFAOExtent({ ...params }), getRanking({ ...params })]).then(
getSettingsConfig: () => {
return [
{
key: 'faoYear',
label: 'Period',
type: 'select',
clearable: false,
border: true,
},
];
},
getData: (params) => {
return all([getFAOExtent({ ...params }), getRanking({ ...params })]).then(
spread((getFAOResponse, getRankingResponse) => {
let data = {};
const fao = getFAOResponse.data.rows;
Expand All @@ -64,7 +77,8 @@ export default {
}
return data;
})
),
);
},
getDataURL: ({ params }) => [getFAOExtent({ ...params, download: true })],
getWidgetProps,
};
17 changes: 14 additions & 3 deletions components/widgets/land-cover/fao-cover/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const getLocationName = (state) => state.locationLabel;
const getColors = (state) => state.colors;
const getSentences = (state) => state.sentences;
const getTitle = (state) => state.title;
const getSettings = (state) => state.settings;

// get lists selected
export const parseData = createSelector(
Expand Down Expand Up @@ -60,19 +61,29 @@ export const parseData = createSelector(
);

export const parseSentence = createSelector(
[getData, getLocationName, getSentences],
(data, locationName, sentences) => {
[getData, getLocationName, getSentences, getSettings],
(data, locationName, sentences, settings) => {
if (isEmpty(data)) return null;
const { initial, noPrimary, globalInitial, globalNoPrimary } = sentences;
const { area_ha, extent, forest_primary } = data;
const { area_ha, extent, forest_primary, planted_forest } = data;
const { faoYear } = settings;
const primaryPercent =
forest_primary > 0
? (forest_primary / area_ha) * 100
: (extent / area_ha) * 100;
const percent = (planted_forest / area_ha) * 100;
const params = {
location: locationName === 'global' ? 'globally' : locationName,
extent: formatNumber({ num: extent, unit: 'ha', spaceUnit: true }),
primaryPercent: formatNumber({ num: primaryPercent, unit: '%' }),
year: faoYear,
percent: formatNumber({ num: percent, unit: '%' }),
amountInHectares: formatNumber({
num: area_ha,
unit: 'ha',
spaceUnit: true,
}),
country: locationName,
};
let sentence = forest_primary > 0 ? initial : noPrimary;
if (locationName === 'global') {
Expand Down
2 changes: 2 additions & 0 deletions components/widgets/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import confidence from 'data/confidence.json';
import bioTypes from 'data/biodiversity-int.json';
import ifl from 'data/ifl.json';
import source from 'data/sources.json';
import faoYear from 'data/fao-cover-years.json';

export default {
forestType: forestType.filter((f) => !f.hidden),
landCategory: landCategory.filter((l) => !l.hidden),
threshold,
decile,
firesThreshold,
faoYear,
unit,
gasesIncluded,
period,
Expand Down
18 changes: 18 additions & 0 deletions data/fao-cover-years.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"label": "2000",
"value": 2000
},
{
"label": "2010",
"value": 2010
},
{
"label": "2015",
"value": 2015
},
{
"label": "2020",
"value": 2020
}
]
4 changes: 2 additions & 2 deletions services/forest-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ const getLocationQuery = (adm0, adm1, adm2) =>
adm2 ? ` AND adm2 = ${adm2}` : ''
}`;

export const getFAOExtent = async ({ adm0, year = 2020, download }) => {
export const getFAOExtent = async ({ adm0, faoYear = 2020, download }) => {
const target = download ? 'csv' : 'json';

const url =
`/dataset/fao_forest_extent/v2020/query/${target}?sql=${NEW_SQL_QUERIES.faoExtent}`
.replace('{location}', adm0 ? `iso = '${adm0}'` : '1 = 1')
.replace('{year}', year);
.replace('{year}', faoYear);

if (download) {
return {
Expand Down

0 comments on commit e5c92af

Please sign in to comment.