diff --git a/components/widgets/land-cover/fao-cover/index.js b/components/widgets/land-cover/fao-cover/index.js index af9dd7e3b1..aa4f4ecb8b 100644 --- a/components/widgets/land-cover/fao-cover/index.js +++ b/components/widgets/land-cover/fao-cover/index.js @@ -11,7 +11,7 @@ export default { widget: 'faoCover', title: { initial: 'FAO forest cover in {location}', - global: 'Global FAO forest cover' + global: 'Global FAO forest cover', }, chartType: 'pieChart', categories: ['land-cover'], @@ -21,47 +21,50 @@ export default { dataType: 'fao', metaKey: 'widget_forest_cover_fao', sortOrder: { - landCover: 5 + landCover: 5, }, settings: { - unit: 'ha' + unit: 'ha', }, sentences: { globalInitial: - 'FAO data from 2015 shows that there are {extent} of forest {location}, with primary forest occupying {primaryPercent} of the world.', + '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: - 'FAO data from 2015 shows that {location} contains {extent} of forest, with primary forest occupying {primaryPercent} of the country.', + '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.' + 'FAO data from 2015 shows that {location} contains {extent} of forest, which occupies {primaryPercent} of the country.', }, - getData: params => + getData: (params) => all([getFAOExtent({ ...params }), getRanking({ ...params })]).then( spread((getFAOResponse, getRankingResponse) => { let data = {}; const fao = getFAOResponse.data.rows; const ranking = getRankingResponse.data.rows; + if (fao.length && ranking.length) { - const faoTotal = fao.map(f => ({ - ...f, - area_ha: parseFloat(f.area_ha.replace(',', '')) * 1000 - })); - let faoData = faoTotal[0]; + let faoData = fao[0]; + if (fao.length > 1) { faoData = {}; - Object.keys(omit(faoTotal[0], ['iso', 'name'])).forEach(k => { - faoData[k] = sumBy(faoTotal, k) || 0; + Object.keys(omit(fao[0], ['iso', 'country'])).forEach((k) => { + faoData[k] = sumBy(fao, k) || 0; }); } + data = { ...faoData, - rank: ranking[0].rank || 0 + rank: ranking[0].rank || 0, }; } return data; }) ), getDataURL: ({ params }) => [getFAOExtent({ ...params, download: true })], - getWidgetProps + getWidgetProps, }; diff --git a/services/forest-data.js b/services/forest-data.js index 610301a5e7..1ddf20e533 100644 --- a/services/forest-data.js +++ b/services/forest-data.js @@ -1,4 +1,4 @@ -import { cartoRequest } from 'utils/request'; +import { cartoRequest, dataRequest, DATA_API_URL } from 'utils/request'; import globalLandCoverCategories from 'data/global-land-cover-categories.json'; @@ -6,7 +6,7 @@ import { CARTO_API } from 'utils/apis'; const NEW_SQL_QUERIES = { faoExtent: - 'SELECT country AS iso, name, plantfor * 1000 AS planted_forest__ha, primfor * 1000 AS primary_forest__ha, natregfor * 1000 AS regenerated_forest__ha, forest * 1000 AS fao_treecover__ha, totarea as area_ha FROM table_1_forest_area_and_characteristics WHERE {location} AND year = 2015', + 'SELECT iso, country, "planted forest (ha)" AS planted_forest__ha, "primary (ha)" AS primary_forest__ha, "naturally regenerating forest (ha)" AS regenerated_forest__ha, "forest (ha)" AS fao_treecover__ha, "total land area (ha)" as area_ha FROM data WHERE {location} AND year = {year}', faoReforest: 'SELECT country AS iso, name, year, reforest * 1000 AS reforestation__rate, forest*1000 AS fao_treecover_reforest__ha FROM table_1_forest_area_and_characteristics as fao WHERE fao.year = {period} AND reforest > 0 ORDER BY reforestation__rate DESC', faoDeforest: @@ -44,23 +44,26 @@ const getLocationQuery = (adm0, adm1, adm2) => adm2 ? ` AND adm2 = ${adm2}` : '' }`; -export const getFAOExtent = ({ adm0, download }) => { - const url = `/sql?q=${NEW_SQL_QUERIES.faoExtent}`.replace( - '{location}', - adm0 ? `country = '${adm0}'` : '1 = 1' - ); +export const getFAOExtent = async ({ adm0, year = 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); if (download) { return { name: 'fao_treecover_extent__ha', - url: `${CARTO_API}${url}&format=csv`, + url: `${DATA_API_URL}${url}`, }; } - return cartoRequest.get(url).then((response) => ({ - ...response, + const response = await dataRequest.get(url); + + const widgetData = { data: { - rows: response.data.rows.map((o) => { + rows: response.data.map((o) => { // delete old key, replace it with new // delete Object.assign(o, {[newKey]: o[oldKey] })[oldKey] delete Object.assign(o, { planted_forest: o.planted_forest__ha }) @@ -75,7 +78,9 @@ export const getFAOExtent = ({ adm0, download }) => { return o; }), }, - })); + }; + + return widgetData; }; export const getFAOReforest = ({ period, download }) => { diff --git a/utils/request.js b/utils/request.js index 049c165777..380ecd75e2 100644 --- a/utils/request.js +++ b/utils/request.js @@ -19,7 +19,7 @@ const ENVIRONMENT = process.env.NEXT_PUBLIC_FEATURE_ENV; const GFW_API_URL = ENVIRONMENT === 'staging' ? GFW_STAGING_API : GFW_API; const GFW_METADATA_API_URL = ENVIRONMENT === 'staging' ? GFW_STAGING_METADATA_API : GFW_METADATA_API; -const DATA_API_URL = +export const DATA_API_URL = ENVIRONMENT === 'staging' ? GFW_STAGING_DATA_API : GFW_DATA_API; // We never use the `staging-api.resourcewatch.org`.