Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FLAG-997] GADM coutry list #4891

Draft
wants to merge 4 commits into
base: epic/gadm
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pages/api/metadata/[...params].js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GFW_METADATA_API, GFW_STAGING_METADATA_API } from 'utils/apis';
import axios from 'axios';

const ENVIRONMENT = process.env.NEXT_PUBLIC_FEATURE_ENV;
const ENVIRONMENT = process.env.NEXT_PUBLIC_RW_FEATURE_ENV;
const GFW_METADATA_API_URL =
ENVIRONMENT === 'staging' ? GFW_STAGING_METADATA_API : GFW_METADATA_API;

Expand Down
5 changes: 2 additions & 3 deletions pages/dashboards/[[...location]].js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { parseStringWithVars } from 'utils/strings';
import { getLocationData } from 'services/location';
import { getPublishedNotifications } from 'services/notifications';
import {
// getCountriesProvider,
getRegionsProvider,
getSubRegionsProvider,
getCategorisedCountries,
Expand Down Expand Up @@ -183,7 +182,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 +196,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
20 changes: 2 additions & 18 deletions pages/embed/sentence/[[...location]].js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { parseGadm36Id } from 'utils/gadm';

import { getLocationData } from 'services/location';
import {
// getCountriesProvider,
getRegionsProvider,
getSubRegionsProvider,
getCategorisedCountries,
Expand Down Expand Up @@ -91,7 +90,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 +104,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 Expand Up @@ -151,21 +150,6 @@ export const getServerSideProps = async ({ params }) => {
};
}
};
//
// export const getStaticPaths = async () => {
// const countryData = await getCountriesProvider();
// const { rows: countries } = countryData?.data || {};
// const countryPaths = countries.map((c) => ({
// params: {
// location: ['country', c.iso],
// },
// }));
//
// return {
// paths: ['/embed/sentence/', ...countryPaths] || [],
// fallback: true,
// };
// };

const getSentenceClientSide = async (
locationNames = null,
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
2 changes: 1 addition & 1 deletion providers/datasets-provider/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const setDatasets = createAction('setDatasets');
export const setDatasetsWithMetadata = createAction('setDatasetsWithMetadata');

const handleFeatureEnvLock = (env) => {
const currEnv = process.env.NEXT_PUBLIC_FEATURE_ENV;
const currEnv = process.env.NEXT_PUBLIC_RW_FEATURE_ENV;
const MIXED_ENV = 'preproduction-staging';
if (env === MIXED_ENV && currEnv !== 'production') {
return true;
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,
};
})
);
2 changes: 1 addition & 1 deletion services/datasets.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { rwRequest, dataRequest } from 'utils/request';

const environmentString = () => {
const env = process.env.NEXT_PUBLIC_FEATURE_ENV;
const env = process.env.NEXT_PUBLIC_RW_FEATURE_ENV;
let envString = 'production';
if (env === 'preproduction') {
envString += ',preproduction';
Expand Down
4 changes: 3 additions & 1 deletion services/shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import request from 'utils/request';
import { GFW_API, GFW_STAGING_API } from 'utils/apis';

const GFW_API_URL =
process.env.NEXT_PUBLIC_FEATURE_ENV === 'staging' ? GFW_STAGING_API : GFW_API;
process.env.NEXT_PUBLIC_RW_FEATURE_ENV === 'staging'
? GFW_STAGING_API
: GFW_API;

const QUERIES = {
ogrConvert: '/v2/ogr/convert',
Expand Down