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

update dashboard blacklists to use the new gid codes #3493

Merged
merged 2 commits into from
Jun 22, 2018
Merged
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
4 changes: 2 additions & 2 deletions app/assets/javascripts/services/CountryService.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ define(['Class', 'uri', 'bluebird', 'map/services/DataService'], function(
showCountry:
"/query/{countriesDataset}?sql=SELECT name_engli as name, iso, ST_AsGeoJSON(the_geom) AS topojson FROM {countriesTable} WHERE iso='{iso}'",
getRegionsList:
"/query/{regionsDataset}?sql=SELECT cartodb_id, iso, bbox as bounds, gid_1, name_1 FROM {regionsTable} WHERE iso='{iso}' ORDER BY name_1",
"/query/{regionsDataset}?sql=SELECT cartodb_id, iso, bbox as bounds, gid_1, name_1 FROM {regionsTable} WHERE iso='{iso}' AND iso != 'XCA' AND iso != 'TWN' ORDER BY name_1",
showRegion:
"/query/{regionsDataset}?sql=SELECT gid_1, name_1, ST_AsGeoJSON(the_geom) AS geojson FROM {regionsTable} WHERE iso='{iso}' AND gid_1='{region}' ORDER BY name_1",
getSubRegionsList:
"/sql?q=SELECT gid_2 as id, name_2 as name FROM gadm36_adm2 WHERE iso = '{iso}' AND gid_1 = '{region}' ORDER BY name"
"/sql?q=SELECT gid_2 as id, name_2 as name FROM gadm36_adm2 WHERE iso = '{iso}' AND iso != 'XCA' AND iso != 'TWN' AND gid_1 = '{region}' AND type_2 not in ('Waterbody') ORDER BY name"
};

var CountriesService = Class.extend({
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/dashboards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def check_location
@location = Dashboards.find_adm2_by_iso_id(params[:iso], params[:region], params[:sub_region])
elsif params[:region]
@location = Dashboards.find_adm1_by_iso_id(params[:iso], params[:region])
elsif params[:iso]
else
@location = Dashboards.find_country_by_iso(params[:iso])
end
if !@location
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
getSubRegionsProvider,
getCountryLinksProvider
} from 'services/country';
import { getWaterBodiesBlacklistProvider } from 'services/whitelists';

import { getGeostoreProvider } from 'services/geostore';
import BOUNDS from 'data/bounds.json';
Expand Down Expand Up @@ -81,29 +80,19 @@ export const getSubRegions = createThunkAction(
(country, region) => (dispatch, state) => {
if (!state().countryData.isSubRegionsLoading) {
dispatch(setSubRegionsLoading(true));
axios
.all([
getSubRegionsProvider(country, region),
getWaterBodiesBlacklistProvider(country, region)
])
.then(
axios.spread((subRegions, blacklistResponse) => {
const { rows } = subRegions.data;
const parsedResponse = [];
uniqBy(rows).forEach(row => {
parsedResponse.push({
id: parseGadm36Id(row.id).adm2,
name: row.name
});
getSubRegionsProvider(country, region)
.then(subRegions => {
const { rows } = subRegions.data;
const parsedResponse = [];
uniqBy(rows).forEach(row => {
parsedResponse.push({
id: parseGadm36Id(row.id).adm2,
name: row.name
});
const blackList = blacklistResponse.data.rows.map(i => i.adm2);
const subRegionList =
parsedResponse &&
parsedResponse.filter(r => blackList.indexOf(r.id) === -1);
dispatch(setSubRegions(uniqBy(subRegionList, 'id')));
dispatch(setSubRegionsLoading(false));
})
)
});
dispatch(setSubRegions(uniqBy(parsedResponse, 'id')));
dispatch(setSubRegionsLoading(false));
})
.catch(error => {
dispatch(setSubRegionsLoading(false));
console.info(error);
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/services/country.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const SQL_QUERIES = {
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}' ORDER BY name",
"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 Down
13 changes: 1 addition & 12 deletions app/javascript/services/whitelists.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import request from 'utils/request';
import { buildGadm36Id } from 'utils/format';

const DATASET = process.env.COUNTRIES_PAGE_DATASET;
const REQUEST_URL = `${process.env.GFW_API}/query/${DATASET}?sql=`;
const CARTO_REQUEST_URL = `${process.env.CARTO_API}/sql?q=`;

const SQL_QUERIES = {
getCountryWhitelist:
"SELECT polyname, SUM(area_extent_2000) as total_extent_2000, SUM(area_extent) as total_extent_2010, SUM(area_gain) as total_gain, SUM(year_data.area_loss) as total_loss FROM data WHERE thresh = 0 AND iso = '{iso}' GROUP BY polyname",
getRegionWhitelist:
'SELECT polyname, SUM(area_extent_2000) as total_extent_2000, SUM(area_extent) as total_extent_2010, SUM(area_gain) as total_gain, SUM(year_data.area_loss) as total_loss FROM data WHERE thresh = 0 AND {location} GROUP BY polyname',
getWaterBodiesWhitelist:
"SELECT iso, gid_1, gid_2 from water_bodies_gadm36 WHERE iso = '{adm0}' AND gid_1 = '{adm1}'"
'SELECT polyname, SUM(area_extent_2000) as total_extent_2000, SUM(area_extent) as total_extent_2010, SUM(area_gain) as total_gain, SUM(year_data.area_loss) as total_loss FROM data WHERE thresh = 0 AND {location} GROUP BY polyname'
};

const getLocationQuery = (adm0, adm1, adm2) =>
Expand All @@ -34,10 +30,3 @@ export const getRegionWhitelistProvider = (adm0, adm1, adm2) => {
);
return request.get(url);
};

export const getWaterBodiesBlacklistProvider = (adm0, adm1) => {
const url = `${CARTO_REQUEST_URL}${SQL_QUERIES.getWaterBodiesWhitelist}`
.replace('{adm0}', buildGadm36Id(adm0))
.replace('{adm1}', buildGadm36Id(adm0, adm1));
return request.get(url);
};
23 changes: 4 additions & 19 deletions app/models/dashboards.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class Dashboards
class << self
def find_all_countries
url = "#{ENV['CARTO_API']}/sql?q=SELECT%20DISTINCT%20iso,%20name_engli%20as%20name%20FROM%20gadm36_countries%20WHERE%20iso%20!=%20'XCA'%20AND%20iso%20!=%20'TWN'"
# CACHE: &bust=true if you want to flush the cache
response = Typhoeus.get(url, headers: {"Accept" => "application/json"})
if response.success? and (response.body.length > 0)
JSON.parse(response.body)["rows"]
Expand All @@ -13,8 +12,7 @@ def find_all_countries

def find_country_by_iso(iso)
return nil unless iso
url = "#{ENV['CARTO_API']}/sql?q=SELECT%20iso,%20name_engli%20as%20name%20FROM%20gadm36_countries%20WHERE%20iso%20=%20'#{iso}'%20AND%20iso%20!=%20'XCA'%20AND%20iso%20!=%20'TWN'%20LIMIT%201"
# CACHE: &bust=true if you want to flush the cache
url = "#{ENV['CARTO_API']}/sql?q=SELECT%20iso%2C%20name_engli%20as%20name%20FROM%20gadm36_countries%20WHERE%20iso%20%3D%20%27#{iso}%27%20AND%20iso%20!%3D%20%27XCA%27%20AND%20iso%20!%3D%20%27TWN%27"
response = Typhoeus.get(url, headers: {"Accept" => "application/json"})
if response.success? and (response.body.length > 0)
JSON.parse(response.body)["rows"][0]
Expand All @@ -25,8 +23,7 @@ def find_country_by_iso(iso)

def find_adm1_by_iso_id(iso, adm1)
return nil unless adm1
url = "#{ENV['CARTO_API']}/sql?q=SELECT%20iso%2C%20id_1%20as%20id%2C%20name_0%20as%20name%2C%20name_1%20as%20adm1%20FROM%20gadm28_adm1%20WHERE%20iso%20%3D%20%27#{iso}%27%20AND%20id_1%20%3D%20#{adm1}"
# CACHE: &bust=true if you want to flush the cache
url = "#{ENV['CARTO_API']}/sql?q=SELECT%20iso%2C%20gid_1%20as%20id%2C%20name_0%20as%20name%2C%20name_1%20as%20adm1%20FROM%20gadm36_adm1%20WHERE%20gid_1%20%3D%20'#{iso}.#{adm1}_1'%20AND%20iso%20!%3D%20%27XCA%27%20AND%20iso%20!%3D%20%27TWN%27"
response = Typhoeus.get(url, headers: {"Accept" => "application/json"})
if response.success? and (response.body.length > 0)
JSON.parse(response.body)["rows"][0]
Expand All @@ -36,9 +33,8 @@ def find_adm1_by_iso_id(iso, adm1)
end

def find_adm2_by_iso_id(iso, adm1, adm2)
return nil if check_adm2_waterbody(iso, adm1)
url = "#{ENV['CARTO_API']}/sql?q=SELECT%20iso%2C%20id_1%2C%20id_2%2C%20name_0%20as%20name%2C%20name_1%20as%20adm1%2C%20name_2%20as%20adm2%20FROM%20gadm28_adm2%20WHERE%20iso%20%3D%20%27#{iso}%27%20AND%20id_1%20%3D%20#{adm1}%20AND%20id_2%20%3D%20#{adm2}"
# CACHE: &bust=true if you want to flush the cache
return nil unless adm2
url = "#{ENV['CARTO_API']}/sql?q=SELECT%20gid_2%2C%20name_0%20as%20name%2C%20name_1%20as%20adm1%2C%20name_2%20as%20adm2%20FROM%20gadm36_adm2%20WHERE%20gid_2%20%3D%20'#{iso}.#{adm1}.#{adm2}_1'%20AND%20iso%20!%3D%20%27XCA%27%20AND%20iso%20!%3D%20%27TWN%27%20AND%20type_2%20NOT%20IN%20('Waterbody')"
response = Typhoeus.get(url, headers: {"Accept" => "application/json"})
if response.success? and (response.body.length > 0)
JSON.parse(response.body)["rows"][0]
Expand All @@ -47,16 +43,5 @@ def find_adm2_by_iso_id(iso, adm1, adm2)
end
end

def check_adm2_waterbody(iso, adm1)
return nil unless adm1
url = "#{ENV['CARTO_API']}/sql?q=SELECT%20iso%2C%20adm1%2C%20adm2%20FROM%20water_bodies_gadm28%20WHERE%20iso%20%3D%20%27#{iso}%27%20AND%20adm1%20%3D%20#{adm1}"
# CACHE: &bust=true if you want to flush the cache
response = Typhoeus.get(url, headers: {"Accept" => "application/json"})
if response.success? and (response.body.length > 0)
JSON.parse(response.body)["rows"][0]
else
nil
end
end
end
end