Skip to content

Commit 3bc320d

Browse files
committed
feat(datamart): use GFW Data API datamart
1 parent 030e4cb commit 3bc320d

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

pages/api/datamart/net-change/index.js

+36-28
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
import { dataRequest } from 'utils/request';
1+
import { dataMartRequest } from 'utils/request';
22

3-
// this PoC is only meant for net change widget (we chose net change for its simplicity)
4-
export default async (req, res) => {
5-
// example request:
6-
// localhost:3000/api/datamart/net-change/?type=global&adm0=MEX&adm1=9&adm2=3&download=true
7-
const { adm0 = '', adm1 = '', adm2 = '', download } = req.query;
3+
const get_net_tree_cover_change_results = async (iso, adm1, adm2) => {
4+
const paramStr = `?iso=${iso}${adm1 ? `&adm1=${adm1}` : ''}${
5+
adm2 ? `&adm2=${adm2}` : ''
6+
}`;
7+
const url = `/datamart/globalforestwatch/v1/net_tree_cover_change${paramStr}`;
8+
// console.info(`Data Mart Request: ${url}`);
9+
const response = await dataMartRequest.get(url);
810

9-
const isDownload = download === 'true';
11+
return [response?.data];
12+
};
1013

14+
const get_net_tree_cover_change_download_url = async (adm0, adm1, adm2) => {
1115
// checks
1216
if (adm1 === undefined && adm2 !== undefined) {
13-
return res.status(400).send({
14-
message: 'if adm2 is present, adm1 can not be empty',
15-
});
17+
throw new Error('if adm2 is present, adm1 can not be empty');
1618
}
1719

1820
if (!adm0 && (adm1 || adm2)) {
19-
return res.status(400).send({
20-
message: 'if adm1 or adm2 are present, adm0 can not be empty',
21-
});
21+
throw new Error('if adm1 or adm2 are present, adm0 can not be empty');
2222
}
2323

2424
let url = '/dataset';
@@ -71,22 +71,30 @@ export default async (req, res) => {
7171
fieldsList.push('adm2_name');
7272
}
7373

74-
url = `${url}/${dataset}/v202209/${
75-
isDownload ? 'download/csv' : 'query'
76-
}?sql=SELECT ${fieldsList} FROM data ${where}`;
74+
url = `${url}/${dataset}/v202209/download/csv?sql=SELECT ${fieldsList} FROM data ${where}`;
75+
// console.info(`Download url: ${url}`);
7776

78-
if (isDownload) {
79-
return res.status(200).json({
80-
data: {
81-
url,
82-
},
83-
});
84-
}
77+
return { url };
78+
};
8579

86-
// fetch
87-
const response = await dataRequest.get(url);
80+
// this PoC is only meant for net change widget (we chose net change for its simplicity)
81+
export default async (req, res) => {
82+
const { adm0 = '', adm1 = '', adm2 = '', download } = req.query;
83+
const isDownload = download === 'true';
84+
let response = null;
8885

89-
return res.status(200).json({
90-
data: response?.data,
91-
});
86+
try {
87+
if (isDownload) {
88+
response = await get_net_tree_cover_change_download_url(adm0, adm1, adm2);
89+
} else {
90+
response = await get_net_tree_cover_change_results(adm0, adm1, adm2);
91+
}
92+
93+
return res.status(200).json({ data: response });
94+
} catch (error) {
95+
// console.error('Error fetching net tree cover change data from the GFW data mart:', { message: error.message });
96+
return res.status(500).json({
97+
error: 'Failed to fetch net tree cover change data.',
98+
});
99+
}
92100
};

0 commit comments

Comments
 (0)