Last active
August 29, 2015 14:02
-
-
Save k1ife/d2542b8c6ce28ea7458c to your computer and use it in GitHub Desktop.
getting from Geonames.org only needed cities with theirs own names and saveing them into my table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mysql2' | |
@country_id = 0 | |
@country_title = '' | |
@country_title_en = '' | |
@region_id = 0 | |
@region_title = '' | |
@region_title_en = '' | |
@region_title_alt = '' | |
@area_title = [] | |
@area_title_str = '' | |
@area_title_en = '' | |
@area_title_en_str = '' | |
@area_title_alt = [] | |
@area_title_alt_str = '' | |
@city_title = [] | |
@city_title_str = '' | |
@city_title_en = '' | |
@city_title_en_str = '' | |
@city_title_alt = '' | |
@city_title_alt_str = '' | |
@city_latitude = 0 | |
@city_longitude = 0 | |
@city_population = 0 | |
@admin1 = '' | |
@admin2 = '' | |
geonames = Mysql2::Client.new(:host => "localhost", :username => "root", :password => 'pass', :database => 'geonames') | |
geonames.query(" SELECT geonameId, iso_alpha2, name, languages FROM countryinfo").each do |country| | |
@country_name = country['name'] | |
@alpha2 = country['iso_alpha2'] | |
@lang = country['languages'].split(",")[0] | |
rsite = Mysql2::Client.new(:host => "localhost", :username => "root", :password => 'pass', :database => 'rsite_dev') | |
rsite.query(" SELECT id, title_en FROM countries WHERE title_en = '#{@country_name}' ").each do |couid| | |
@country_id = couid['id'] | |
@country_title = couid['title_en'] | |
@country_title_en = couid['title_en'] | |
end | |
geonames.query(" SELECT geonameid, name, alternatenames, latitude, longitude, admin1, admin2, population FROM geoname WHERE country = '#{@alpha2}' AND fcode LIKE 'PP%' ").each do |town| | |
@city_title = town['name'].gsub(/'/, "`").gsub(/’/, "`") | |
@city_title_en = town['name'].gsub(/'/, "`").gsub(/’/, "`") | |
@towngeoid = town['geonameid'] | |
@city_title_alt = @city_title + ", " + @city_title_en + ", " + town['alternatenames'].gsub(/'/, "`").gsub(/"/, "`").gsub(/’/, "`") | |
if @city_title_str == '' | |
@city_title_str = @city_title_en | |
end | |
@city_latitude = town['latitude'] | |
@city_longitude = town['longitude'] | |
@city_population = town['population'] | |
@admin1 = town['admin1'] | |
@admin2 = town['admin2'] | |
if @admin1 != '' | |
geonames.query(" SELECT code, name, geonameid FROM admin1CodesAscii WHERE code = '#{@alpha2}.#{@admin1}' ").each do |reg| | |
@reganame = reg['name'].gsub(/'/, "`").gsub(/’/, "`") | |
rsite.query(" SELECT id, title, title_en, title_alt FROM regions WHERE title_en = '#{@reganame}' ").each do |rsite_region| | |
@region_id = rsite_region['id'] | |
@region_title = rsite_region['title'] | |
@region_title_en = rsite_region['title_en'] | |
@region_title_alt = rsite_region['title_alt'] | |
end | |
end | |
end | |
rsite_write = Mysql2::Client.new(:host => "localhost", :username => "root", :password => 'pass', :database => 'rsite_dev') | |
rsite_write.query(" INSERT INTO cities SET country_id = '#{@country_id}', country = '#{@country_title}', country_en = '#{@country_title_en}', region_id = '#{@region_id}', region = '#{@region_title}', region_en = '#{@region_title_en}', region_alt = '#{@region_title_alt}', area = '#{@area_title_str}', area_en = '#{@area_title_en}', area_alt = '#{@area_title_alt_str}', title = '#{@city_title_str}', title_en = '#{@city_title_en}', title_alt = '#{@city_title_alt}', latitude = '#{@city_latitude}', longitude = '#{@city_longitude}', population = '#{@city_population}' ") | |
rsite_write.close | |
@region_id = 0 | |
@region_title = '' | |
@region_title_en = '' | |
@region_title_alt = '' | |
@area_title = [] | |
@area_title_str = '' | |
@area_title_en = '' | |
@area_title_en_str = '' | |
@area_title_alt = [] | |
@area_title_alt_str = '' | |
@city_title = [] | |
@city_title_str = '' | |
@city_title_en = '' | |
@city_title_en_str = '' | |
@city_title_alt = [] | |
@city_title_alt_str = '' | |
@city_latitude = 0 | |
@city_longitude = 0 | |
@city_population = 0 | |
@admin1 = '' | |
@admin2 = '' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment