Module: Geonames::WebService

Defined in:
lib/geonames/web_service.rb

Class Method Summary collapse

Class Method Details

.country_code(lat, long, radius = 0, maxRows = 1, *args) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/geonames/web_service.rb', line 410

def country_code(lat, long, radius=0, maxRows=1, *args)
  # maxRows is only implemented in the xml version:
  # http://groups.google.com/group/geonames/browse_thread/thread/f7f1bb2504ed216e
  # Therefore 'type=xml' is added:
  url = "/countrycode?a=a&type=xml"

  countries = []

  url << "&lat="     + lat.to_s
  url << "&lng="     + long.to_s
  url << "&maxRows=" + maxRows.to_s
  url << "&radius="  + radius.to_s

  res = make_request(url, args)

  doc = REXML::Document.new res.body

  doc.elements.each("geonames/country") do |element|
    countries << element_to_toponym(element)
  end

  countries
end

.country_info(country_code = nil, *args) ⇒ Object



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/geonames/web_service.rb', line 394

def country_info(country_code=nil, *args)
  url = "/countryInfo?a=a"
  url += "&country=#{country_code.to_s}" unless country_code.nil?
  res = make_request(url, args)

  doc = REXML::Document.new res.body

  countries = []

  doc.elements.each("geonames/country") do |element|
    countries << element_to_country_info(element)
  end

  countries.size > 1 ? countries : countries[0]
end

.country_subdivision(lat, long, radius = 0, maxRows = 1, *args) ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/geonames/web_service.rb', line 361

def country_subdivision(lat, long, radius=0, maxRows=1, *args)
  country_subdivisions = []

  # maxRows is only implemented in the xml version:
  # http://groups.google.com/group/geonames/browse_thread/thread/f7f1bb2504ed216e
  # Therefore 'type=xml' is added:
  url = "/countrySubdivision?a=a&type=xml"

  url << "&lat="     + lat.to_s
  url << "&lng="     + long.to_s
  url << "&maxRows=" + maxRows.to_s
  url << "&radius="  + radius.to_s

  res = make_request(url, args)

  doc = REXML::Document.new res.body

  doc.elements.each("geonames/countrySubdivision") do |element|
    country_subdivision = CountrySubdivision.new

    country_subdivision.country_code = get_element_child_text(element, 'countryCode')
    country_subdivision.country_name = get_element_child_text(element, 'countryName')
    country_subdivision.admin_code_1 = get_element_child_text(element, 'adminCode1')
    country_subdivision.admin_name_1 = get_element_child_text(element, 'adminName1')
    country_subdivision.code_fips    = get_element_child_text(element, 'code[@type="FIPS10-4"]')
    country_subdivision.code_iso     = get_element_child_text(element, 'code[@type="ISO3166-2"]')

    country_subdivisions << country_subdivision
  end

  country_subdivisions
end

.element_to_country_info(element) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/geonames/web_service.rb', line 129

def element_to_country_info(element)
  country_info = Geonames::CountryInfo.new

  country_info.country_code  = get_element_child_text(element,  'countryCode')
  country_info.country_name  = get_element_child_text(element,  'countryName')
  country_info.iso_numeric   = get_element_child_int(element,   'isoNumeric')
  country_info.iso_alpha_3   = get_element_child_text(element,  'isoAlpha3')
  country_info.fips_code     = get_element_child_text(element,  'fipsCode')
  country_info.continent     = get_element_child_text(element,  'continent')
  country_info.capital       = get_element_child_text(element,  'capital')
  country_info.area_sq_km    = get_element_child_float(element, 'areaInSqKm')
  country_info.population    = get_element_child_int(element,   'population')
  country_info.currency_code = get_element_child_text(element,  'currencyCode')
  country_info.languages     = get_element_child_text(element,  'languages').split(",")
  country_info.geoname_id    = get_element_child_int(element,   'geonameId')

  north = get_element_child_float(element, 'bBoxNorth')
  south = get_element_child_float(element, 'bBoxSouth')
  east  = get_element_child_float(element, 'bBoxEast')
  west  = get_element_child_float(element, 'bBoxWest')

  country_info.set_bounding_box(north, south, east, west)

  country_info
end

.element_to_intersection(element) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/geonames/web_service.rb', line 109

def element_to_intersection(element)
  intersection = Intersection.new

  intersection.street_1     = get_element_child_text(element,  'street1')
  intersection.street_2     = get_element_child_text(element,  'street2')
  intersection.admin_code_1 = get_element_child_text(element,  'adminCode1')
  intersection.admin_code_1 = get_element_child_text(element,  'adminCode1')
  intersection.admin_code_2 = get_element_child_text(element,  'adminCode2')
  intersection.admin_name_1 = get_element_child_text(element,  'adminName1')
  intersection.admin_name_2 = get_element_child_text(element,  'adminName2')
  intersection.country_code = get_element_child_text(element,  'countryCode')
  intersection.distance     = get_element_child_float(element, 'distance')
  intersection.longitude    = get_element_child_float(element, 'lat')
  intersection.latitude     = get_element_child_float(element, 'lng')
  intersection.place_name   = get_element_child_text(element,  'name')
  intersection.postal_code  = get_element_child_text(element,  'postalcode')

  intersection
end

.element_to_postal_code(element) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/geonames/web_service.rb', line 48

def element_to_postal_code(element)
  postal_code = PostalCode.new

  postal_code.admin_code_1 = get_element_child_text(element,  'adminCode1')
  postal_code.admin_code_2 = get_element_child_text(element,  'adminCode2')
  postal_code.admin_name_1 = get_element_child_text(element,  'adminName1')
  postal_code.admin_name_2 = get_element_child_text(element,  'adminName2')
  postal_code.country_code = get_element_child_text(element,  'countryCode')
  postal_code.distance     = get_element_child_float(element, 'distance')
  postal_code.longitude    = get_element_child_float(element, 'lng')
  postal_code.latitude     = get_element_child_float(element, 'lat')
  postal_code.place_name   = get_element_child_text(element,  'name')
  postal_code.postal_code  = get_element_child_text(element,  'postalcode')

  postal_code
end

.element_to_toponym(element) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/geonames/web_service.rb', line 83

def element_to_toponym(element)
  toponym = Toponym.new

  toponym.name               = get_element_child_text(element,  'name')
  toponym.alternate_names    = get_element_child_alternates(element, 'alternateName')
  toponym.latitude           = get_element_child_float(element, 'lat')
  toponym.longitude          = get_element_child_float(element, 'lng')
  toponym.geoname_id         = get_element_child_text(element,  'geonameId')
  toponym.country_code       = get_element_child_text(element,  'countryCode')
  toponym.country_name       = get_element_child_text(element,  'countryName')
  toponym.feature_class      = get_element_child_text(element,  'fcl')
  toponym.feature_code       = get_element_child_text(element,  'fcode')
  toponym.feature_class_name = get_element_child_text(element,  'fclName')
  toponym.feature_code_name  = get_element_child_text(element,  'fcodeName')
  toponym.population         = get_element_child_int(element,   'population')
  toponym.elevation          = get_element_child_text(element,  'elevation')
  toponym.distance           = get_element_child_float(element, 'distance')
  toponym.admin_code_1       = get_element_child_text(element,  'adminCode1')
  toponym.admin_code_2       = get_element_child_text(element,  'adminCode2')
  toponym.admin_name_1       = get_element_child_text(element,  'adminName1')
  toponym.admin_name_2       = get_element_child_text(element,  'adminName2')
  toponym.timezone           = get_element_child_text(element,  'timezone')

  toponym
end

.element_to_wikipedia_article(element) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/geonames/web_service.rb', line 65

def element_to_wikipedia_article(element)
  article = WikipediaArticle.new

  article.language      = get_element_child_text(element,  'lang')
  article.title         = get_element_child_text(element,  'title')
  article.summary       = get_element_child_text(element,  'summary')
  article.wikipedia_url = get_element_child_text(element,  'wikipediaUrl')
  article.feature       = get_element_child_text(element,  'feature')
  article.population    = get_element_child_text(element,  'population')
  article.elevation     = get_element_child_text(element,  'elevation')
  article.latitude      = get_element_child_float(element, 'lat')
  article.longitude     = get_element_child_float(element, 'lng')
  article.thumbnail_img = get_element_child_text(element,  'thumbnailImg')
  article.distance      = get_element_child_float(element, 'distance')

  article
end

.find_bounding_box_wikipedia(hashes, *args) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/geonames/web_service.rb', line 327

def find_bounding_box_wikipedia(hashes, *args)
  articles = []

  north      = hashes[:north]
  east       = hashes[:east]
  south      = hashes[:south]
  west       = hashes[:west]
  lang       = hashes[:lang]
  radius     = hashes[:radius]
  max_rows   = hashes[:max_rows]
  country    = hashes[:country]
  postalcode = hashes[:postalcode]
  q          = hashes[:q]

  url = "/wikipediaBoundingBox?a=a"

  url << "&north="    + north.to_s
  url << "&east="     + east.to_s
  url << "&south="    + south.to_s
  url << "&west="     + west.to_s
  url << "&radius="   + radius.to_s   unless radius.nil?
  url << "&max_rows=" + max_rows.to_s unless max_rows.nil?

  res = make_request(url, args)

  doc = REXML::Document.new res.body

  doc.elements.each("geonames/entry") do |element|
    articles << element_to_wikipedia_article(element)
  end

  articles
end

.find_nearby(lat, long, query_options = {}, *args) ⇒ Object



215
216
217
# File 'lib/geonames/web_service.rb', line 215

def find_nearby(lat, long, query_options = {}, *args)
  find_toponym("findNearby", lat, long, query_options, args)
end

.find_nearby_place_name(lat, long, query_options = {}, *args) ⇒ Object



211
212
213
# File 'lib/geonames/web_service.rb', line 211

def find_nearby_place_name(lat, long, query_options = {} , *args)
  find_toponym("findNearbyPlaceName", lat, long, query_options, args)
end

.find_nearby_postal_codes(search_criteria, *args) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/geonames/web_service.rb', line 173

def find_nearby_postal_codes(search_criteria, *args)
  # postal codes to reutrn
  postal_codes = []

  url = "/findNearbyPostalCodes?a=a"
  url << search_criteria.to_query_params_string

  res = make_request(url, args)

  doc = REXML::Document.new res.body

  doc.elements.each("geonames/code") do |element|
    postal_codes << element_to_postal_code(element)
  end

  postal_codes
end

.find_nearby_wikipedia(hashes, *args) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/geonames/web_service.rb', line 286

def find_nearby_wikipedia(hashes, *args)
  articles = []

  lat        = hashes[:lat]
  long       = hashes[:long]
  lang       = hashes[:lang]
  radius     = hashes[:radius]
  max_rows   = hashes[:max_rows]
  country    = hashes[:country]
  postalcode = hashes[:postalcode]
  q          = hashes[:q]

  url = "/findNearbyWikipedia?a=a"

  if !lat.nil? && !long.nil?
    url << "&lat="      + lat.to_s
    url << "&lng="      + long.to_s
    url << "&radius="   + radius.to_s   unless radius.nil?
    url << "&max_rows=" + max_rows.to_s unless max_rows.nil?
  elsif !q.nil?
    url << "&q="        + q
    url << "&radius="   + radius.to_s   unless radius.nil?
    url << "&max_rows=" + max_rows.to_s unless max_rows.nil?
  end

  res = make_request(url, args)

  doc = REXML::Document.new res.body

  doc.elements.each("geonames/entry") do |element|
    articles << element_to_wikipedia_article(element)
  end

  articles
end

.find_nearest_intersection(lat, long, *args) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/geonames/web_service.rb', line 219

def find_nearest_intersection(lat, long, *args)
  url = "/findNearestIntersection?a=a"

  url << "&lat=" + lat.to_s
  url << "&lng=" + long.to_s

  res = make_request(url, args)

  doc = REXML::Document.new res.body

  intersection = []

  doc.elements.each("geonames/intersection") do |element|
    intersection = element_to_intersection(element)
  end

  intersection
end

.find_toponym(action, lat, long, query_options, args) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/geonames/web_service.rb', line 191

def find_toponym(action, lat, long, query_options, args)
  places = []

  url = "/#{action}"

  url << "?lat=" + lat.to_s
  url << "&lng=" + long.to_s
  query_options.each { |k,v| url << "&#{k}=#{v}" }

  res = make_request(url, args)

  doc = REXML::Document.new res.body

  doc.elements.each("geonames/geoname") do |element|
    places << element_to_toponym(element)
  end

  places
end

.findBoundingBoxWikipedia(hashes) ⇒ Object



322
323
324
325
# File 'lib/geonames/web_service.rb', line 322

def findBoundingBoxWikipedia(hashes)
  # here for backwards compatibility
  find_bounding_box_wikipedia(hashes)
end

.findNearbyWikipedia(hashes) ⇒ Object



281
282
283
284
# File 'lib/geonames/web_service.rb', line 281

def findNearbyWikipedia(hashes)
  # here for backwards compatibility
  find_nearby_wikipedia(hashes)
end

.get_element_child_alternates(element, child) ⇒ Object



42
43
44
45
46
# File 'lib/geonames/web_service.rb', line 42

def get_element_child_alternates(element, child)
  alternates = {}
  element.each_element_with_attribute('lang',nil,0,child) { |e| alternates[e.attribute('lang').value] = e.text }
  alternates
end

.get_element_child_float(element, child) ⇒ Object



34
35
36
# File 'lib/geonames/web_service.rb', line 34

def get_element_child_float(element, child)
  element.elements[child][0].to_s.to_f unless element.elements[child].nil?
end

.get_element_child_int(element, child) ⇒ Object



38
39
40
# File 'lib/geonames/web_service.rb', line 38

def get_element_child_int(element, child)
  element.elements[child][0].to_s.to_i unless element.elements[child].nil?
end

.get_element_child_text(element, child) ⇒ Object



30
31
32
# File 'lib/geonames/web_service.rb', line 30

def get_element_child_text(element, child)
  element.elements[child][0].to_s unless element.elements[child].nil?
end

.hierarchy(geonameId, *args) ⇒ Object



238
239
240
241
242
243
244
245
# File 'lib/geonames/web_service.rb', line 238

def hierarchy(geonameId, *args)
  url = "/hierarchy?geonameId=#{geonameId.to_i}"
  res = make_request(url, args)
  doc = REXML::Document.new res.body
  doc.elements.collect("geonames/geoname") do |element|
    element_to_toponym(element)
  end
end

.make_request(path_and_query, *args) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/geonames/web_service.rb', line 262

def make_request(path_and_query, *args)
  url = Geonames.base_url + path_and_query
  url += "&username=#{Geonames.username}" if Geonames.username
  url += "&lang=#{Geonames.lang}"
  url += "&token=#{Geonames.token}"       if Geonames.token

  options = { :open_timeout => 60, :read_timeout => 60 }
  options.update(args.last.is_a?(Hash) ? args.pop : {})

  uri = URI.parse(url)
  req = Net::HTTP::Get.new("#{uri.path}?#{uri.query}", 'User-Agent' => USER_AGENT)

  Net::HTTP.start(uri.host, uri.port) do |http|
    http.read_timeout = options[:read_timeout]
    http.open_timeout = options[:open_timeout]
    http.request(req)
  end
end

.postal_code_search(search_criteria, *args) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/geonames/web_service.rb', line 155

def postal_code_search(search_criteria, *args)
  # postal codes to reutrn
  postal_codes = []

  url = "/postalCodeSearch?a=a"
  url << search_criteria.to_query_params_string

  res = make_request(url, args)
  
  doc = REXML::Document.new res.body

  doc.elements.each("geonames/code") do |element|
    postal_codes << element_to_postal_code(element)
  end

  postal_codes
end

.search(search_criteria, *args) ⇒ Object



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/geonames/web_service.rb', line 434

def search(search_criteria, *args)
  #toponym search results to return
  toponym_sr = ToponymSearchResult.new

  url = "/search?a=a"
  url << "&q="               + CGI.escape(search_criteria.q)                unless search_criteria.q.nil?
  url << "&name_equals="     + CGI.escape(search_criteria.name_equals)      unless search_criteria.name_equals.nil?
  url << "&name_startsWith=" + CGI.escape(search_criteria.name_starts_with) unless search_criteria.name_starts_with.nil?
  url << "&name="            + CGI.escape(search_criteria.name)             unless search_criteria.name.nil?
  url << "&tag="             + CGI.escape(search_criteria.tag)              unless search_criteria.tag.nil?
  url << "&country="         + CGI.escape(search_criteria.country_code)     unless search_criteria.country_code.nil?
  url << "&adminCode1="      + CGI.escape(search_criteria.admin_code_1)     unless search_criteria.admin_code_1.nil?
  url << "&lang="            + CGI.escape(search_criteria.language)         unless search_criteria.language.nil?
  url << "&featureClass="    + CGI.escape(search_criteria.feature_class)    unless search_criteria.feature_class.nil?
  url << "&maxRows="         + CGI.escape(search_criteria.max_rows)         unless search_criteria.max_rows.nil?
  url << "&startRow="        + CGI.escape(search_criteria.start_row)        unless search_criteria.start_row.nil?
  url << "&style="           + CGI.escape(search_criteria.style)            unless search_criteria.style.nil?
  url << "&isNameRequired="  + CGI.escape(search_criteria.is_name_required) unless search_criteria.is_name_required.nil?
  url << search_criteria.feature_codes.map {|fcode| "&fcode=" + CGI.escape(fcode) }.join unless search_criteria.feature_codes.nil?

  res = make_request(url, args)

  doc = REXML::Document.new res.body

  toponym_sr.total_results_count = doc.elements["geonames/totalResultsCount"].text

  doc.elements.each("geonames/geoname") do |element|
    toponym_sr.toponyms << element_to_toponym(element)
  end

  toponym_sr
end

.timezone(lat, long, *args) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/geonames/web_service.rb', line 247

def timezone(lat, long, *args)
  res = make_request("/timezone?lat=#{lat.to_s}&lng=#{long.to_s}", args)
  doc = REXML::Document.new res.body
  timezone = Timezone.new

  doc.elements.each("geonames/timezone") do |element|
    timezone.timezone_id = get_element_child_text(element,  'timezoneId')
    timezone.gmt_offset  = get_element_child_float(element, 'gmtOffset')
    timezone.dst_offset  = get_element_child_float(element, 'dstOffset')
    timezone.raw_offset  = get_element_child_float(element, 'rawOffset')
  end

  timezone
end