openstreetmap-website/lib/country.rb
ardean80 552a471188
Update country.rb
Without adding "Rails.root.join(...)" (or something else returning a complete path to the countries.xml file) this error occurred: 
"No such file or directory @ rb_sysopen - config/countries.xml" 
I think it happened because it could not find that file through a relative path.
2018-02-16 14:43:53 +01:00

36 lines
883 B
Ruby

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(code)
countries[code]
end
def self.countries
@countries ||= load_countries
end
def self.load_countries
countries = {}
xml = REXML::Document.new(File.read(Rails.root.join("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