Skip to content

Commit

Permalink
Add a fetch countries geocoding task to update bounding boxes for all…
Browse files Browse the repository at this point in the history
… countries.
  • Loading branch information
rposborne committed Sep 27, 2016
1 parent 8389221 commit c72327e
Show file tree
Hide file tree
Showing 254 changed files with 3,297 additions and 1,549 deletions.
51 changes: 2 additions & 49 deletions Rakefile
Expand Up @@ -3,6 +3,8 @@ require 'bundler/gem_tasks'

require 'rake'
require 'rspec/core/rake_task'
ISO3166_ROOT_PATH = File.dirname(__FILE__)
Dir.glob('lib/countries/tasks/*.rake').each { |r| load r }

desc 'Run all examples'
RSpec::Core::RakeTask.new(:spec) do |t|
Expand Down Expand Up @@ -78,52 +80,3 @@ task :update_cache do

File.open(File.join(File.dirname(__FILE__), 'lib', 'countries', 'cache', 'countries.json'), 'wb') { |f| f.write(data.to_json) }
end

require 'geocoder'
require 'retryable'
# raise on geocoding errors such as query limit exceeded
Geocoder.configure(always_raise: :all)
# Try to geocode a given query, on exceptions it retries up to 3 times then gives up.
# @param [String] query string to geocode
# @return [Hash] first valid result or nil
def geocode(query)
Retryable.retryable(tries: 3, sleep: ->(n) { 2**n }) do
Geocoder.search(query).first
end
rescue => e
warn "Attempts exceeded for query #{query}, last error was #{e.message}"
nil
end

desc 'Retrieve and store subdivisions coordinates'
task :fetch_subdivisions do
require 'countries'
# Iterate all countries with subdivisions
ISO3166::Country.all.select(&:subdivisions?).each do |c|
# Iterate subdivisions
state_data = c.subdivisions.dup
state_data.reject { |_, data| data['latitude'] }.each do |code, data|
location = "#{data['name']}, #{c.name}"

# Handle special geocoding cases where Google defaults to well known
# cities, instead of the states.
if c.alpha2 == 'US' && %w(NY WA OK).include?(code)
location = "#{data['name']} State, United States"
end

next unless (result = geocode(location))
geometry = result.geometry
if geometry['location']
state_data[code]['geo']['latitude'] = geometry['location']['lat']
state_data[code]['geo']['longitude'] = geometry['location']['lng']
end
next unless geometry['bounds']
state_data[code]['geo']['min_latitude'] = geometry['bounds']['southwest']['lat']
state_data[code]['geo']['min_longitude'] = geometry['bounds']['southwest']['lng']
state_data[code]['geo']['max_latitude'] = geometry['bounds']['northeast']['lat']
state_data[code]['geo']['max_longitude'] = geometry['bounds']['northeast']['lng']
end
# Write updated YAML for current country
File.open(File.join(File.dirname(__FILE__), 'lib', 'countries', 'data', 'subdivisions', "#{c.alpha2}.yaml"), 'w+') { |f| f.write state_data.to_yaml }
end
end
2 changes: 1 addition & 1 deletion lib/countries/cache/countries.json

Large diffs are not rendered by default.

19 changes: 13 additions & 6 deletions lib/countries/data/countries/AD.yaml
Expand Up @@ -32,12 +32,19 @@ AD:
languages_spoken:
- ca
geo:
latitude: 42 30 N
latitude: 42.506285
latitude_dec: '42.5506591796875'
longitude: 1 30 E
longitude: 1.521801
longitude_dec: '1.5762332677841187'
max_latitude: '42.65'
max_longitude: '1.783333'
min_latitude: '42.433333'
min_longitude: '1.416667'
max_latitude: 42.6557909
max_longitude: 1.786639
min_latitude: 42.4287488
min_longitude: 1.4087051
bounds:
northeast:
lat: 42.6557909
lng: 1.786639
southwest:
lat: 42.4287488
lng: 1.4087051
currency_code: EUR
19 changes: 13 additions & 6 deletions lib/countries/data/countries/AE.yaml
Expand Up @@ -40,12 +40,19 @@ AE:
languages_spoken:
- ar
geo:
latitude: 24 00 N
latitude: 23.424076
latitude_dec: '23.684776306152344'
longitude: 54 00 E
longitude: 53.847818
longitude_dec: '54.536643981933594'
max_latitude: '26.133333'
max_longitude: '58'
min_latitude: '22.166667'
min_longitude: '45'
max_latitude: 26.069654
max_longitude: 56.3816785
min_latitude: 22.6315137
min_longitude: 51.497705
bounds:
northeast:
lat: 26.069654
lng: 56.3816785
southwest:
lat: 22.6315137
lng: 51.497705
currency_code: AED
19 changes: 13 additions & 6 deletions lib/countries/data/countries/AF.yaml
Expand Up @@ -34,12 +34,19 @@ AF:
- uz
- tk
geo:
latitude: 33 00 N
latitude: 33.93911
latitude_dec: '33.833248138427734'
longitude: 65 00 E
longitude: 67.709953
longitude_dec: '66.02528381347656'
max_latitude: '38.483611'
max_longitude: '74.8868713067'
min_latitude: '29.383333'
min_longitude: '60.566667'
max_latitude: 38.4908766
max_longitude: 74.889862
min_latitude: 29.3772
min_longitude: 60.5170004
bounds:
northeast:
lat: 38.4908766
lng: 74.889862
southwest:
lat: 29.3772
lng: 60.5170004
currency_code: AFN
19 changes: 13 additions & 6 deletions lib/countries/data/countries/AG.yaml
Expand Up @@ -32,12 +32,19 @@ AG:
languages_spoken:
- en
geo:
latitude: 17 03 N
latitude: 17.060816
latitude_dec: '17.09273910522461'
longitude: 61 48 W
longitude: -61.796428
longitude_dec: "-61.81040954589844"
max_latitude: '17.733333'
max_longitude: "-61.666667"
min_latitude: '16.916667'
min_longitude: "-62.333333"
max_latitude: 17.7291867
max_longitude: -61.6571681
min_latitude: 16.932532
min_longitude: -62.3476571
bounds:
northeast:
lat: 17.7291867
lng: -61.6571681
southwest:
lat: 16.932532
lng: -62.3476571
currency_code: XCD
19 changes: 13 additions & 6 deletions lib/countries/data/countries/AI.yaml
Expand Up @@ -28,12 +28,19 @@ AI:
languages_spoken:
- en
geo:
latitude: 18 15 N
latitude: 18.220554
latitude_dec: '18.22646713256836'
longitude: 63 10 W
longitude: -63.06861499999999
longitude_dec: "-63.0473518371582"
max_latitude: '18.6'
max_longitude: "-62.916667"
min_latitude: '18.15'
min_longitude: "-63.433333"
max_latitude: 18.595088
max_longitude: -62.92710330000001
min_latitude: 18.1499464
min_longitude: -63.42898210000001
bounds:
northeast:
lat: 18.595088
lng: -62.92710330000001
southwest:
lat: 18.1499464
lng: -63.42898210000001
currency_code: XCD
19 changes: 13 additions & 6 deletions lib/countries/data/countries/AL.yaml
Expand Up @@ -33,12 +33,19 @@ AL:
languages_spoken:
- sq
geo:
latitude: 41 00 N
latitude: 41.153332
latitude_dec: '41.11113357543945'
longitude: 20 00 E
longitude: 20.168331
longitude_dec: '20.02745246887207'
max_latitude: '42.659167'
max_longitude: '21.05'
min_latitude: '39.65'
min_longitude: '19.266667'
max_latitude: 42.6610819
max_longitude: 21.0572394
min_latitude: 39.6447296
min_longitude: 19.2639363
bounds:
northeast:
lat: 42.6610819
lng: 21.0572394
southwest:
lat: 39.6447296
lng: 19.2639363
currency_code: ALL
19 changes: 13 additions & 6 deletions lib/countries/data/countries/AM.yaml
Expand Up @@ -33,12 +33,19 @@ AM:
- hy
- ru
geo:
latitude: 40 00 N
latitude: 40.069099
latitude_dec: '40.29266357421875'
longitude: 45 00 E
longitude: 45.038189
longitude_dec: '44.93947219848633'
max_latitude: '41.3'
max_longitude: '46.560556'
min_latitude: '38.894167'
min_longitude: '43.4425'
max_latitude: 41.300993
max_longitude: 46.634222
min_latitude: 38.8402439
min_longitude: 43.44726
bounds:
northeast:
lat: 41.300993
lng: 46.634222
southwest:
lat: 38.8402439
lng: 43.44726
currency_code: AMD
19 changes: 13 additions & 6 deletions lib/countries/data/countries/AO.yaml
Expand Up @@ -28,12 +28,19 @@ AO:
languages_spoken:
- pt
geo:
latitude: 12 30 S
latitude: -11.202692
latitude_dec: "-12.333555221557617"
longitude: 18 30 E
longitude: 17.873887
longitude_dec: '17.539464950561523'
max_latitude: "-4.4"
max_longitude: '23.983333'
min_latitude: "-32"
min_longitude: '10'
max_latitude: -4.387944000000001
max_longitude: 24.0878855
min_latitude: -18.039104
min_longitude: 11.6695619
bounds:
northeast:
lat: -4.387944000000001
lng: 24.0878855
southwest:
lat: -18.039104
lng: 11.6695619
currency_code: AOA
19 changes: 13 additions & 6 deletions lib/countries/data/countries/AQ.yaml
Expand Up @@ -27,12 +27,19 @@ AQ:
languages_official: []
languages_spoken: []
geo:
latitude: 90 00 S
latitude: -75.250973
latitude_dec: "-82.862752"
longitude: 0 00 E
longitude: -0.071389
longitude_dec: "-135.0"
max_latitude: "-60.0"
max_longitude: '180.0'
min_latitude: "-90.0"
min_longitude: "-180.0"
max_latitude: -60.1086999
max_longitude: 180
min_latitude: -90
min_longitude: -180
bounds:
northeast:
lat: -60.1086999
lng: 180
southwest:
lat: -90
lng: -180
currency_code: USD
19 changes: 13 additions & 6 deletions lib/countries/data/countries/AR.yaml
Expand Up @@ -40,12 +40,19 @@ AR:
- es
- gn
geo:
latitude: 34 00 S
latitude: -38.416097
latitude_dec: "-37.071964263916016"
longitude: 64 00 W
longitude: -63.61667199999999
longitude_dec: "-64.85450744628906"
max_latitude: "-21.783333"
max_longitude: "-53.65"
min_latitude: "-58.116667"
min_longitude: "-73.533333"
max_latitude: -21.7810459
max_longitude: -53.6374811
min_latitude: -55.05727899999999
min_longitude: -73.56036019999999
bounds:
northeast:
lat: -21.7810459
lng: -53.6374811
southwest:
lat: -55.05727899999999
lng: -73.56036019999999
currency_code: ARS
19 changes: 13 additions & 6 deletions lib/countries/data/countries/AS.yaml
Expand Up @@ -34,12 +34,19 @@ AS:
- en
- sm
geo:
latitude: 14 20 S
latitude: -14.3060204
latitude_dec: "-14.31956672668457"
longitude: 170 00 W
longitude: -170.6961815
longitude_dec: "-170.7403564453125"
max_latitude: "-11.049690"
max_longitude: "-169.416077"
min_latitude: "-14.382470"
min_longitude: "-171.091873"
max_latitude: -14.1551045
max_longitude: -169.4186269
min_latitude: -14.3824778
min_longitude: -170.846781
bounds:
northeast:
lat: -14.1551045
lng: -169.4186269
southwest:
lat: -14.3824778
lng: -170.846781
currency_code: USD
19 changes: 13 additions & 6 deletions lib/countries/data/countries/AT.yaml
Expand Up @@ -52,12 +52,19 @@ AT:
languages_spoken:
- de
geo:
latitude: 47 20 N
latitude: 47.516231
latitude_dec: '47.58843994140625'
longitude: 13 20 E
longitude: 14.550072
longitude_dec: '14.14021110534668'
max_latitude: '49.016667'
max_longitude: '19'
min_latitude: '46.377222'
min_longitude: '1.2'
max_latitude: 49.0206081
max_longitude: 17.1606861
min_latitude: 46.3723357
min_longitude: 9.530783399999999
bounds:
northeast:
lat: 49.0206081
lng: 17.1606861
southwest:
lat: 46.3723357
lng: 9.530783399999999
currency_code: EUR
19 changes: 13 additions & 6 deletions lib/countries/data/countries/AU.yaml
Expand Up @@ -36,12 +36,19 @@ AU:
languages_spoken:
- en
geo:
latitude: 27 00 S
latitude: -25.274398
latitude_dec: "-25.585241317749023"
longitude: 133 00 E
longitude: 133.775136
longitude_dec: '134.50411987304688'
max_latitude: "-10.68"
max_longitude: '153.63'
min_latitude: "-43.63"
min_longitude: '113.15'
max_latitude: -9.2198214
max_longitude: 159.2557541
min_latitude: -54.7772185
min_longitude: 112.9215625
bounds:
northeast:
lat: -9.2198214
lng: 159.2557541
southwest:
lat: -54.7772185
lng: 112.9215625
currency_code: AUD

0 comments on commit c72327e

Please sign in to comment.