Class: Geogov::Geonames

Inherits:
Object
  • Object
show all
Defined in:
lib/geogov/providers/geonames.rb

Instance Method Summary collapse

Constructor Details

#initialize(username = "username", url = "http://api.geonames.org") ⇒ Geonames

Returns a new instance of Geonames.



3
4
5
6
# File 'lib/geogov/providers/geonames.rb', line 3

def initialize(username = "username", url = "http://api.geonames.org")
    @url = url
    @username = username
end

Instance Method Details

#centre_of_country(country_code) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/geogov/providers/geonames.rb', line 23

def centre_of_country(country_code)
  params = { "country" => country_code, "type" => "JSON" }
  results = query("countryInfo",params)
  if results && results["geonames"] && results["geonames"][0]
    country = results["geonames"][0]
    bbe, bbw = country["bBoxEast"],country["bBoxWest"]
    bbn, bbs = country["bBoxNorth"],country["bBoxSouth"]
    lon,lat = (bbe.to_f+bbw.to_f)/2,(bbn.to_f+bbs.to_f)/2
    return { "lat" => lat, "lon" => lon }
  else
    return nil
  end
end

#lat_lon_to_country(lat, lon) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/geogov/providers/geonames.rb', line 37

def lat_lon_to_country(lat,lon)
  params = { "lat" => lat, "lng" => lon, 'type'=>"JSON"}
  results = query("countryCode",params)
  if results && results["countryCode"]
    return results["countryCode"]
  else
    return nil
  end
end

#nearest_place_name(lat, lon) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/geogov/providers/geonames.rb', line 13

def nearest_place_name(lat,lon)
  params = { "lat" => lat, "lng" => lon}
  results = query("findNearbyPlaceNameJSON",params)
  if results && results["geonames"]
    return results["geonames"][0]["name"]
  else
    return nil
  end
end

#query(method, params) ⇒ Object



8
9
10
11
# File 'lib/geogov/providers/geonames.rb', line 8

def query(method,params)
  params = {"username"=>@username}.merge(params)
  Geogov.get_json("#{@url}/#{method}?"+Geogov.hash_to_params(params))
end