class ReverseGeo
Public Class Methods
new(file = nil)
click to toggle source
# File lib/reverse_geo/reverse_geo.rb, line 7 def initialize(file = nil) file ||= File.join(File.dirname(__FILE__), @@default_shapefile) raise ArgumentError, 'Did not supply a valid geojson file.' unless File.exist?(file) @factory = RGeo::Geos.factory(srid: 4326) @countries = RGeo::GeoJSON.decode(File.open(file).read, :json_parser => :json, :geo_factory => @factory) end
Public Instance Methods
country(opts = {})
click to toggle source
# File lib/reverse_geo/reverse_geo.rb, line 19 def country(opts = {}) country_search('Country', opts) end
country_code(opts = {})
click to toggle source
# File lib/reverse_geo/reverse_geo.rb, line 15 def country_code(opts = {}) country_search('ISO_A3', opts) end
Private Instance Methods
country_search(property_name, opts)
click to toggle source
# File lib/reverse_geo/reverse_geo.rb, line 25 def country_search(property_name, opts) raise ArgumentError, "Did not supply a valid lat ([-90,90]) and lng ([-180,180]). You supplied lat: #{opts[:lat]}, lng: #{opts[:lng]}" unless opts[:lat] && opts[:lat].between?(-90, 90) && opts[:lng] && opts[:lng].between?(-180, 180) poi = @factory.point(opts[:lng], opts[:lat]) @countries.each do |country| return country.properties[property_name] if poi.within?(country.geometry) end nil#fallback if we didn't find anything end