Skip to content

Commit

Permalink
feat(countries): migrate queries from gadm 3.6 to 4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
willian-viana committed Dec 18, 2024
1 parent da0c405 commit 72fd5cc
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 40 deletions.
4 changes: 2 additions & 2 deletions pages/dashboards/[[...location]].js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export const getServerSideProps = async ({ params, query, req }) => {
const countryLinks = await getCountryLinksSerialized();
countryData = {
...countryData,
regions: uniqBy(regions.data.rows).map((row) => ({
regions: uniqBy(regions.data).map((row) => ({
id: parseGadm36Id(row.id).adm1,
value: parseGadm36Id(row.id).adm1,
label: row.name,
Expand All @@ -197,7 +197,7 @@ export const getServerSideProps = async ({ params, query, req }) => {
const subRegions = await getSubRegionsProvider(adm0, adm1);
countryData = {
...countryData,
subRegions: uniqBy(subRegions.data.rows).map((row) => ({
subRegions: uniqBy(subRegions.data).map((row) => ({
id: parseGadm36Id(row.id).adm2,
value: parseGadm36Id(row.id).adm2,
label: row.name,
Expand Down
4 changes: 2 additions & 2 deletions pages/embed/sentence/[[...location]].js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const getServerSideProps = async ({ params }) => {
const countryLinks = await getCountryLinksSerialized();
countryData = {
...countryData,
regions: uniqBy(regions.data.rows).map((row) => ({
regions: uniqBy(regions.data).map((row) => ({
id: parseGadm36Id(row.id).adm1,
value: parseGadm36Id(row.id).adm1,
label: row.name,
Expand All @@ -105,7 +105,7 @@ export const getServerSideProps = async ({ params }) => {
const subRegions = await getSubRegionsProvider(adm0, adm1);
countryData = {
...countryData,
subRegions: uniqBy(subRegions.data.rows).map((row) => ({
subRegions: uniqBy(subRegions.data).map((row) => ({
id: parseGadm36Id(row.id).adm2,
value: parseGadm36Id(row.id).adm2,
label: row.name,
Expand Down
37 changes: 19 additions & 18 deletions providers/country-data-provider/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const getRegions = createThunkAction(
getRegionsProvider(country)
.then((response) => {
const parsedResponse = [];
uniqBy(response.data.rows).forEach((row) => {
uniqBy(response.data).forEach((row) => {
parsedResponse.push({
id: parseGadm36Id(row.id).adm1,
name: row.name,
Expand All @@ -64,25 +64,26 @@ export const getRegions = createThunkAction(

export const getSubRegions = createThunkAction(
'getSubRegions',
({ adm0, adm1, token }) => (dispatch) => {
dispatch(setSubRegionsLoading(true));
getSubRegionsProvider(adm0, adm1, token)
.then((subRegions) => {
const { rows } = subRegions.data;
const parsedResponse = [];
uniqBy(rows).forEach((row) => {
parsedResponse.push({
id: parseGadm36Id(row.id).adm2,
name: row.name,
({ adm0, adm1, token }) =>
(dispatch) => {
dispatch(setSubRegionsLoading(true));
getSubRegionsProvider(adm0, adm1, token)
.then((subRegions) => {
const { rows } = subRegions.data;
const parsedResponse = [];
uniqBy(rows).forEach((row) => {
parsedResponse.push({
id: parseGadm36Id(row.id).adm2,
name: row.name,
});
});
dispatch(setSubRegions(uniqBy(parsedResponse, 'id')));
dispatch(setSubRegionsLoading(false));
})
.catch(() => {
dispatch(setSubRegionsLoading(false));
});
dispatch(setSubRegions(uniqBy(parsedResponse, 'id')));
dispatch(setSubRegionsLoading(false));
})
.catch(() => {
dispatch(setSubRegionsLoading(false));
});
}
}
);

export const getCountryLinks = createThunkAction(
Expand Down
44 changes: 26 additions & 18 deletions services/country.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { all, spread } from 'axios';

import { cartoRequest } from 'utils/request';
import { cartoRequest, dataRequest } from 'utils/request';
import { getGadm36Id } from 'utils/gadm';

const GADM_DATASET = '/dataset/gadm_administrative_boundaries/v4.1/query';

const SQL_QUERIES = {
getCountries:
"SELECT iso, name_engli as name FROM gadm36_countries WHERE iso != 'TWN' AND iso != 'XCA' ORDER BY name",
getGADMCountries:
"SELECT country AS name, gid_0 AS iso FROM gadm_administrative_boundaries WHERE adm_level = '0' ORDER BY country",
getGADMRegions:
"SELECT name_1 AS name, gid_1 AS id FROM gadm_administrative_boundaries WHERE adm_level='1' AND gid_0 = '{iso}' ORDER BY name ",
getGADMSubRegions:
"SELECT gid_2 as id, name_2 as name FROM gadm_administrative_boundaries WHERE gid_0 = '{iso}' AND gid_1 = '{adm1}' AND adm_level='2' AND type_2 NOT IN ('Waterbody', 'Water body', 'Water Body') ORDER BY name",
getFAOCountries:
'SELECT DISTINCT country AS iso, name FROM table_1_forest_area_and_characteristics',
getRegions:
"SELECT gid_1 as id, name_1 as name FROM gadm36_adm1 WHERE iso = '{iso}' ORDER BY name ",
getSubRegions:
"SELECT gid_2 as id, name_2 as name FROM gadm36_adm2 WHERE iso = '{iso}' AND gid_1 = '{adm1}' AND type_2 NOT IN ('Waterbody', 'Water body', 'Water Body') ORDER BY name",
getCountryLinks:
'SELECT iso, external_links FROM external_links_gfw WHERE forest_atlas is true',
getRanking:
Expand All @@ -22,8 +24,9 @@ const convertToOptions = (countries) =>
countries.map((c) => ({ label: c.name, value: c.iso }));

export const getCountriesProvider = () => {
const url = `/sql?q=${SQL_QUERIES.getCountries}`;
return cartoRequest.get(url);
const url = `${GADM_DATASET}?sql=${SQL_QUERIES.getGADMCountries}`;

return dataRequest.get(url);
};

export const getFAOCountriesProvider = () => {
Expand All @@ -32,15 +35,20 @@ export const getFAOCountriesProvider = () => {
};

export const getRegionsProvider = ({ adm0, token }) => {
const url = `/sql?q=${SQL_QUERIES.getRegions}`.replace('{iso}', adm0);
return cartoRequest.get(url, { cancelToken: token });
const url = `${GADM_DATASET}?sql=${SQL_QUERIES.getGADMRegions}`.replace(
'{iso}',
adm0
);

return dataRequest.get(url, { cancelToken: token });
};

export const getSubRegionsProvider = (adm0, adm1, token) => {
const url = `/sql?q=${SQL_QUERIES.getSubRegions}`
const url = `${GADM_DATASET}?sql=${SQL_QUERIES.getGADMSubRegions}`
.replace('{iso}', adm0)
.replace('{adm1}', getGadm36Id(adm0, adm1));
return cartoRequest.get(url, { cancelToken: token });

return dataRequest.get(url, { cancelToken: token });
};

export const getCountryLinksProvider = (token) => {
Expand All @@ -67,17 +75,17 @@ export const getCountryLinksSerialized = async () => {

export const getCategorisedCountries = (asOptions = false) =>
all([getCountriesProvider(), getFAOCountriesProvider()]).then(
spread((gadm36Countries, faoCountries) => {
spread((gadm41Countries, faoCountries) => {
return {
gadmCountries: asOptions
? convertToOptions(gadm36Countries.data.rows)
: gadm36Countries.data.rows,
? convertToOptions(gadm41Countries.data)
: gadm41Countries.data,
faoCountries: asOptions
? convertToOptions(faoCountries.data.rows)
: faoCountries.data.rows,
countries: asOptions
? convertToOptions(gadm36Countries.data.rows)
: gadm36Countries.data.rows,
? convertToOptions(gadm41Countries.data)
: gadm41Countries.data,
};
})
);

0 comments on commit 72fd5cc

Please sign in to comment.