From eb74b2502c906f94d199baa45bd7cd4a4762e76c Mon Sep 17 00:00:00 2001 From: Gary Tempus Jr Date: Fri, 6 Sep 2024 16:27:10 -0400 Subject: [PATCH] feat(datamart): use GFW Data API datamart --- pages/api/datamart/net-change/index.js | 64 +++++++++++++++----------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/pages/api/datamart/net-change/index.js b/pages/api/datamart/net-change/index.js index 7de9bf0e36..8245a1e9ea 100644 --- a/pages/api/datamart/net-change/index.js +++ b/pages/api/datamart/net-change/index.js @@ -1,24 +1,24 @@ -import { dataRequest } from 'utils/request'; +import { dataMartRequest } from 'utils/request'; -// this PoC is only meant for net change widget (we chose net change for its simplicity) -export default async (req, res) => { - // example request: - // localhost:3000/api/datamart/net-change/?type=global&adm0=MEX&adm1=9&adm2=3&download=true - const { adm0 = '', adm1 = '', adm2 = '', download } = req.query; +const get_net_tree_cover_change_results = async (iso, adm1, adm2) => { + const paramStr = `?iso=${iso}${adm1 ? `&adm1=${adm1}` : ''}${ + adm2 ? `&adm2=${adm2}` : '' + }`; + const url = `/datamart/analysis/forest_change/tree_cover_change/net_tree_cover_change${paramStr}`; + // console.info(`Data Mart Request: ${url}`); + const response = await dataMartRequest.get(url); - const isDownload = download === 'true'; + return [response?.data]; +}; +const get_net_tree_cover_change_download_url = async (adm0, adm1, adm2) => { // checks if (adm1 === undefined && adm2 !== undefined) { - return res.status(400).send({ - message: 'if adm2 is present, adm1 can not be empty', - }); + throw new Error('if adm2 is present, adm1 can not be empty'); } if (!adm0 && (adm1 || adm2)) { - return res.status(400).send({ - message: 'if adm1 or adm2 are present, adm0 can not be empty', - }); + throw new Error('if adm1 or adm2 are present, adm0 can not be empty'); } let url = '/dataset'; @@ -71,22 +71,30 @@ export default async (req, res) => { fieldsList.push('adm2_name'); } - url = `${url}/${dataset}/v202209/${ - isDownload ? 'download/csv' : 'query' - }?sql=SELECT ${fieldsList} FROM data ${where}`; + url = `${url}/${dataset}/v202209/download/csv?sql=SELECT ${fieldsList} FROM data ${where}`; + // console.info(`Download url: ${url}`); - if (isDownload) { - return res.status(200).json({ - data: { - url, - }, - }); - } + return { url }; +}; - // fetch - const response = await dataRequest.get(url); +// this PoC is only meant for net change widget (we chose net change for its simplicity) +export default async (req, res) => { + const { adm0 = '', adm1 = '', adm2 = '', download } = req.query; + const isDownload = download === 'true'; + let response = null; - return res.status(200).json({ - data: response?.data, - }); + try { + if (isDownload) { + response = await get_net_tree_cover_change_download_url(adm0, adm1, adm2); + } else { + response = await get_net_tree_cover_change_results(adm0, adm1, adm2); + } + + return res.status(200).json({ data: response }); + } catch (error) { + // console.error('Error fetching net tree cover change data from the GFW data mart:', { message: error.message }); + return res.status(500).json({ + error: 'Failed to fetch net tree cover change data.', + }); + } };