Class: ReverseGeo

Inherits:
Object
  • Object
show all
Defined in:
lib/reverse_geo/reverse_geo.rb

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ ReverseGeo

Returns a new instance of ReverseGeo.

Raises:



7
8
9
10
11
12
13
# 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

Instance Method Details

#country(opts = {}) ⇒ Object

Raises:



15
16
17
18
19
20
21
22
23
24
# File 'lib/reverse_geo/reverse_geo.rb', line 15

def country(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['ISO_A3'] if poi.within?(country.geometry)
  end

  return nil#fallback if we didn't find anything
end