Replace the country table with a static XML countries file

Storing the countries in the database is overkill, so just use a
copy of the geonames countryInfo data that is loaded into a hash
the first time it is used.
This commit is contained in:
Tom Hughes 2014-01-17 19:25:58 +00:00
parent 933b091330
commit 42b329ed82
6 changed files with 4798 additions and 66 deletions

View file

@ -1,2 +0,0 @@
class Country < ActiveRecord::Base
end

4753
config/countries.xml Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,5 @@
class DropCountries < ActiveRecord::Migration
def change
drop_table :countries
end
end

View file

@ -274,39 +274,6 @@ CREATE SEQUENCE client_applications_id_seq
ALTER SEQUENCE client_applications_id_seq OWNED BY client_applications.id;
--
-- Name: countries; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE countries (
id integer NOT NULL,
code character varying(2) NOT NULL,
min_lat double precision NOT NULL,
max_lat double precision NOT NULL,
min_lon double precision NOT NULL,
max_lon double precision NOT NULL
);
--
-- Name: countries_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE countries_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: countries_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE countries_id_seq OWNED BY countries.id;
--
-- Name: current_node_tags; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
@ -1187,13 +1154,6 @@ ALTER TABLE ONLY changesets ALTER COLUMN id SET DEFAULT nextval('changesets_id_s
ALTER TABLE ONLY client_applications ALTER COLUMN id SET DEFAULT nextval('client_applications_id_seq'::regclass);
--
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY countries ALTER COLUMN id SET DEFAULT nextval('countries_id_seq'::regclass);
--
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
--
@ -1344,14 +1304,6 @@ ALTER TABLE ONLY client_applications
ADD CONSTRAINT client_applications_pkey PRIMARY KEY (id);
--
-- Name: countries_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
--
ALTER TABLE ONLY countries
ADD CONSTRAINT countries_pkey PRIMARY KEY (id);
--
-- Name: current_node_tags_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
--
@ -1665,13 +1617,6 @@ CREATE INDEX changesets_user_id_created_at_idx ON changesets USING btree (user_i
CREATE INDEX changesets_user_id_id_idx ON changesets USING btree (user_id, id);
--
-- Name: countries_code_idx; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE UNIQUE INDEX countries_code_idx ON countries USING btree (code);
--
-- Name: current_nodes_tile_idx; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
@ -2476,6 +2421,8 @@ INSERT INTO schema_migrations (version) VALUES ('20131212124700');
INSERT INTO schema_migrations (version) VALUES ('20140115192822');
INSERT INTO schema_migrations (version) VALUES ('20140117185510');
INSERT INTO schema_migrations (version) VALUES ('21');
INSERT INTO schema_migrations (version) VALUES ('22');

38
lib/country.rb Normal file
View file

@ -0,0 +1,38 @@
class Country
attr_reader :code, :min_lat, :max_lat, :min_lon, :max_lon
def initialize(code, min_lat, max_lat, min_lon, max_lon)
@code = code
@min_lat = min_lat
@max_lat = max_lat
@min_lon = min_lon
@max_lon = max_lon
end
def self.find_by_code(code)
countries[code]
end
private
def self.countries
@@countries ||= load_countries
end
def self.load_countries
countries = Hash.new
xml = REXML::Document.new(File.read("config/countries.xml"))
xml.elements.each("geonames/country") do |ele|
code = ele.get_text("countryCode").to_s
minlon = ele.get_text("west").to_s
minlat = ele.get_text("south").to_s
maxlon = ele.get_text("east").to_s
maxlat = ele.get_text("north").to_s
countries[code] = Country.new(code, minlat.to_f, maxlat.to_f, minlon.to_f, maxlon.to_f)
end
countries
end
end

View file

@ -1,9 +0,0 @@
require File.dirname(__FILE__) + '/../test_helper'
class CountryTest < ActiveSupport::TestCase
fixtures :countries
test "country count" do
assert_equal 2, Country.count
end
end