Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

Commit

Permalink
Merge a06d457 into c3e1f9b
Browse files Browse the repository at this point in the history
  • Loading branch information
simaob committed Sep 14, 2018
2 parents c3e1f9b + a06d457 commit d7da248
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ define(
var scientific = d3.format('.3s')(data.value);
var displayName = data.id_1
? data.sub_nat_name
: data.boundary_id != 1 ? data.boundary_name : data.country_name;
: data.boundary_name != 'administrative boundary'
? data.boundary_name
: data.country_name;
return {
location_name: displayName,
valueString: data.value > 1000 ? scientific : shortened,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ define(
var aux = values[i.id][0];
var displayName = aux.id_1
? aux.sub_nat_name
: aux.boundary_id != 1 ? aux.boundary_name : aux.country_name;
: aux.boundary_name != 'administrative boundary' ? aux.boundary_name : aux.country_name;
i.location_name = displayName;
i.data = values[i.id];
return i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ define(
if (aux) {
displayName = aux.id_1
? aux.sub_nat_name // eslint-disable-line
: aux.boundary_id !== 1
: aux.boundary_name !== 'administrative boundary'
? aux.boundary_name
: aux.country_name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ define(

data['indicator_ids[]'] = model.indicators.join(',') || '';
return (
window.gfw.config.CLIMATE_API_HOST + '/api/downloads?' + $.param(data)
window.gfw.config.CLIMATE_API_HOST + '/downloads?' + $.param(data)
);
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
</div>
<div class="modal-footer">
<div class="m-btncontainer is-center">
<a href="http://opendata.arcgis.com/" target="_blank" class="">OPEN DATA PORTAL</a>
<a href="http://data.globalforestwatch.org/" target="_blank" class="">OPEN DATA PORTAL</a>
<button class="btn medium blue js-submit">Download</button>
</div>
</div>
Expand Down
11 changes: 6 additions & 5 deletions app/models/country.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ def jurisdictions_for iso

def areas_of_interest_for iso
sql = <<-SQL
SELECT DISTINCT boundary_id AS id, boundary_name AS name, boundary_code AS code
SELECT DISTINCT b.cartodb_id AS id, boundary_name AS name,
b.boundary_code AS code
FROM #{CDB_INDICATORS_VALUES_TABLE} i
INNER JOIN #{CDB_BOUNDARIES_TABLE} b ON
i.boundary_id = b.cartodb_id
WHERE UPPER(iso) = UPPER('#{iso}') AND
boundary <> 'admin'
ORDER BY boundary_id
i.boundary_code = b.boundary_code
WHERE UPPER(iso_and_sub_nat) = UPPER('#{iso}') AND
b.boundary_code <> 'admin'
ORDER BY b.boundary_code
SQL

get(base_countries_path+sql)['rows']
Expand Down
27 changes: 13 additions & 14 deletions app/models/download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def as_zip

def results_to_csv results
file_names = []
grouped = results.group_by{|t| t["indicator_name"]}
grouped = results.group_by{|t| t["indicator_id"]}
grouped.each do |id, data|
csv_file = Tempfile.new([id, ".csv"])
csv_file = Tempfile.new([id.to_s, ".csv"])
file_names << ["#{id}.csv", csv_file.path]
CSV.open(csv_file, "wb") do |csv|
csv << data.first.keys # adds the attributes name on the first line
Expand All @@ -70,18 +70,17 @@ def query_url

def select_query
<<-SQL
SELECT values.indicator_id, values.cartodb_id AS cartodb_id,
values.iso, values.sub_nat_id, values.boundary, values.boundary_id,
values.thresh, values.admin0_name AS country, values.year,
values.value, subnat.name_1 AS province,
boundaries.boundary_name, indicators.indicator_short AS indicator_name,
indicators.value_units AS units
FROM #{CDB_INDICATORS_VALUES_TABLE} AS values
INNER JOIN indicators ON values.indicator_id = indicators.indicator_id
LEFT JOIN #{CDB_SUBNAT_TABLE} AS subnat
SELECT values.indicator_id, values.cartodb_id AS cartodb_id, values.iso, values.sub_nat_id,
values.boundary_code AS boundary, values.boundary_code AS boundary_id,
values.thresh, values.country,
values.year, CAST(values.value AS double precision) AS value, subnat.name_1 AS province,
boundaries.boundary_name, indicators.units AS units
FROM gfw_climate_country_pages_indicator_values_2017_data_20180828 AS values
INNER JOIN gfw_climate_country_pages_indicator_info_2017_data AS indicators ON values.indicator_id = indicators.indicator_id
LEFT JOIN gadm28_adm1 AS subnat
ON values.sub_nat_id = subnat.id_1 AND values.iso = subnat.iso
LEFT JOIN #{CDB_BOUNDARIES_TABLE} AS boundaries
ON values.boundary_id = boundaries.cartodb_id
LEFT JOIN gfw_climate_country_pages_boundary_info_2017_data AS boundaries
ON values.boundary_code = boundaries.boundary_code
SQL
end

Expand All @@ -95,7 +94,7 @@ def where_clause
where += "AND thresh IN (#{@thresholds.join(",")})"
end

where += "AND values.boundary_id = #{@area.blank? ? ADMIN_BOUNDARY_ID : area}"
where += "AND values.boundary_code = #{@area.blank? ? "'#{ADMIN_BOUNDARY_ID}' " : area}"
where += "AND values.sub_nat_id #{@id_1.blank? ? 'IS NULL' : "= #{@id_1}"}"

if @start_year && @end_year
Expand Down
22 changes: 15 additions & 7 deletions app/models/indicator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def find_indicator(filter_params)

ids = "#{iso}_#{id_1}_#{area}"

puts url

timeouts do
item_caching(indicator_id, ids, nil, thresh_value) do
get(url)['rows'].blank? ? {} : get(url)['rows']
Expand All @@ -52,23 +54,29 @@ def index_query
def show_query(indicator_id, iso, id_1, area, thresh_value)
filter = <<-SQL
indicator_id = #{indicator_id}
AND (value IS NOT NULL OR text_value IS NOT NULL)
AND value IS NOT NULL
AND thresh = #{thresh_value}
SQL
filter += filter_location(iso, id_1, area) if iso.present?

sql = <<-SQL
SELECT indicator_id, values.cartodb_id AS cartodb_id,
values.iso, values.sub_nat_id, values.boundary, values.boundary_id,
values.thresh, values.the_geom, values.the_geom_webmercator,
values.admin0_name, values.year, values.value, values.text_value,
SELECT
indicator_id, values.cartodb_id AS cartodb_id,
values.iso, values.sub_nat_id,
values.boundary_code,
values.thresh, values.the_geom,
values.the_geom_webmercator,
values.country AS country_name,
values.country AS admin0_name,
values.year, CAST(values.value AS double precision) AS value,
values.value AS text_value,
subnat.name_1 AS sub_nat_name,
boundaries.boundary_name
FROM #{CDB_INDICATORS_VALUES_TABLE} AS values
LEFT JOIN #{CDB_SUBNAT_TABLE} AS subnat
ON values.sub_nat_id = subnat.id_1 AND values.iso = subnat.iso
LEFT JOIN #{CDB_BOUNDARIES_TABLE} AS boundaries
ON values.boundary_id = boundaries.cartodb_id
ON values.boundary_code = boundaries.boundary_code
WHERE #{filter}
SQL

Expand All @@ -79,7 +87,7 @@ def filter_location(iso, id_1, area)
<<-SQL
AND values.iso = UPPER('#{iso}')
AND values.sub_nat_id #{id_1.blank? ? 'IS NULL' : "= #{id_1}" }
AND values.boundary_id = #{area.blank? ? ADMIN_BOUNDARY_ID : area}
#{area.blank? ? "AND values.boundary_code = '#{ADMIN_BOUNDARY_ID}'" : "AND boundaries.cartodb_id = #{area}"}
SQL
end

Expand Down
8 changes: 4 additions & 4 deletions config/initializers/cartodb_constants.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ADMIN_BOUNDARY_ID = 1
CDB_INDICATORS_VALUES_TABLE = "indicators_values"
CDB_INDICATORS_TABLE = "indicators"
CDB_BOUNDARIES_TABLE="boundaries_table"
ADMIN_BOUNDARY_ID = "admin"
CDB_INDICATORS_VALUES_TABLE = "gfw_climate_country_pages_indicator_values_2017_data_20180913"
CDB_INDICATORS_TABLE = "gfw_climate_country_pages_indicator_info_2017_data"
CDB_BOUNDARIES_TABLE="gfw_climate_country_pages_boundary_info_2017_data"
CDB_COUNTRIES_TABLE = "gadm28_countries"
CDB_SUBNAT_TABLE = "gadm28_adm1"

0 comments on commit d7da248

Please sign in to comment.