Class: Geonames::WebService

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

Class Method Summary collapse

Class Method Details

.country_code(lat, long) ⇒ Object


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/web_service.rb', line 224

def WebService.country_code ( lat, long )
   
    url = Geonames::GEONAMES_SERVER + "/countrycode?a=a"
    
    url = url + "&lat=" + lat.to_s
    url = url + "&lng=" + long.to_s

    uri = URI.parse(url)

    req = Net::HTTP::Get.new(uri.path + '?' + uri.query)

    res = Net::HTTP.start( uri.host, uri.port ) { |http|
        http.request( req )
    }

    doc = REXML::Document.new res.body

    return res.body.strip
  
end

.country_subdivision(lat, long) ⇒ Object


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/web_service.rb', line 195

def WebService.country_subdivision ( lat, long )
    country_subdivision = CountrySubdivision.new
   
    url = Geonames::GEONAMES_SERVER + "/countrySubdivision?a=a"
    
    url = url + "&lat=" + lat.to_s
    url = url + "&lng=" + long.to_s

    uri = URI.parse(url)

    req = Net::HTTP::Get.new(uri.path + '?' + uri.query)

    res = Net::HTTP.start( uri.host, uri.port ) { |http|
        http.request( req )
    }

    doc = REXML::Document.new res.body

    doc.elements.each("geonames/countrySubdivision") do |element|
        country_subdivision.country_code    = WebService::get_element_child_text( element, 'countryCode' )
        country_subdivision.country_name    = WebService::get_element_child_text( element, 'countryName' )
        country_subdivision.admin_code_1    = WebService::get_element_child_text( element, 'adminCode1' )
        country_subdivision.admin_name_1    = WebService::get_element_child_text( element, 'adminName1' )
    end

    return country_subdivision
  
end

.element_to_postal_code(element) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/web_service.rb', line 40

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

end

.element_to_toponym(element) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/web_service.rb', line 58

def WebService.element_to_toponym ( element )
    toponym = Toponym.new

    toponym.name                = WebService::get_element_child_text( element, 'name' )
    toponym.alternate_names     = WebService::get_element_child_text( element, 'alternateNames' )
    toponym.latitude            = WebService::get_element_child_float( element, 'lat' )
    toponym.longitude           = WebService::get_element_child_float( element, 'lng' )
    toponym.geoname_id          = WebService::get_element_child_text( element, 'geonameId' )
    toponym.country_code        = WebService::get_element_child_text( element, 'countryCode' )
    toponym.country_name        = WebService::get_element_child_text( element, 'countryName' )
    toponym.feature_class       = WebService::get_element_child_text( element, 'fcl' )
    toponym.feature_code        = WebService::get_element_child_text( element, 'fcode' )
    toponym.feature_class_name  = WebService::get_element_child_text( element, 'fclName' )
    toponym.feature_code_name   = WebService::get_element_child_text( element, 'fCodeName' )
    toponym.population          = WebService::get_element_child_int( element, 'population' )
    toponym.elevation           = WebService::get_element_child_text( element, 'elevation' )
    toponym.distance            = WebService::get_element_child_float( element, 'distance' )
    
    return toponym

end

.find_nearby_place_name(lat, long) ⇒ Object


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/web_service.rb', line 139

def WebService.find_nearby_place_name( lat, long )
    places = Array.new
    
    url = Geonames::GEONAMES_SERVER + "/findNearbyPlaceName?a=a"
    
    url = url + "&lat=" + lat.to_s
    url = url + "&lng=" + long.to_s

    uri = URI.parse(url)

    req = Net::HTTP::Get.new(uri.path + '?' + uri.query)

    res = Net::HTTP.start( uri.host, uri.port ) { |http|
        http.request( req )
    }

    doc = REXML::Document.new res.body
    
    doc.elements.each("geonames/geoname") do |element|

        places << WebService::element_to_toponym( element )
    
    end

    return places
  
end

.find_nearby_postal_codes(search_criteria) ⇒ Object


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/web_service.rb', line 114

def WebService.find_nearby_postal_codes( search_criteria )
    # postal codes to reutrn
    postal_codes = Array.new
    
    url = Geonames::GEONAMES_SERVER + "/findNearbyPostalCodes?a=a"
    url = url + search_criteria.to_query_params_string
    
    uri = URI.parse(url)

    req = Net::HTTP::Get.new(uri.path + '?' + uri.query)

    res = Net::HTTP.start( uri.host, uri.port ) { |http|
        http.request( req )
    }

    doc = REXML::Document.new res.body
    
    doc.elements.each("geonames/code") do |element|
        postal_codes << WebService::element_to_postal_code( element )
    end

    postal_codes
    
end

.get_element_child_float(element, child) ⇒ Object


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

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

.get_element_child_int(element, child) ⇒ Object


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

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

.get_element_child_text(element, child) ⇒ Object


22
23
24
25
26
# File 'lib/web_service.rb', line 22

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

.postal_code_search(search_criteria) ⇒ Object


80
81
82
83
84
85
86
87
# File 'lib/web_service.rb', line 80

def WebService.postal_code_search( postal_code, place_name, country_code )
    postal_code_sc = PostalCodeSearchCriteria.new
    postal_code_sc.postal_code = postal_code
    postal_code_sc.place_name = place_name
    postal_code_sc.coutry_code = country_code
    
    WebService.postal_code_search postal_code_sc
end

.search(search_criteria) ⇒ Object


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
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
321
322
323
324
# File 'lib/web_service.rb', line 245

def WebService.search( search_criteria )
    #toponym search results to return
    toponym_sr = ToponymSearchResult.new

    url = Geonames::GEONAMES_SERVER + "/search?a=a"

    if !search_criteria.q.nil? 
      url = url + "&q=" + CGI::escape( search_criteria.q )
    end

    if !search_criteria.name_equals.nil? 
      url = url + "&name_equals=" + CGI::escape( search_criteria.name_equals )
    end

    if !search_criteria.name_starts_with.nil? 
      url = url + "&name_startsWith=" + CGI::escape( search_criteria.name_starts_with )
    end

    if !search_criteria.name.nil? 
      url = url + "&name=" + CGI::escape( search_criteria.name )
    end

    if !search_criteria.tag.nil? 
      url = url + "&tag=" + CGI::escape( search_criteria.tag )
    end

    if !search_criteria.country_code.nil? 
      url = url + "&country=" + CGI::escape( search_criteria.country_code )
    end

    if !search_criteria.admin_code_1.nil? 
      url = url + "&adminCode1=" + CGI::escape( search_criteria.admin_code_1 )
    end

    if !search_criteria.language.nil? 
      url = url + "&lang=" + CGI::escape( search_criteria.language )
    end

    if !search_criteria.feature_class.nil? 
      url = url + "&featureClass=" + CGI::escape( search_criteria.feature_class )
    end

    if !search_criteria.feature_codes.nil?
      for feature_code in search_criteria.feature_codes
        url = url + "&fcode=" + CGI::escape( feature_code )
      end
    end

    if !search_criteria.max_rows.nil? 
      url = url + "&maxRows=" + CGI::escape( search_criteria.max_rows )
    end

    if !search_criteria.start_row.nil? 
      url = url + "&startRow=" + CGI::escape( search_criteria.start_row )
    end

    if !search_criteria.style.nil? 
      url = url + "&style=" + CGI::escape( search_criteria.style )
    end

    uri = URI.parse(url)

    req = Net::HTTP::Get.new(uri.path + '?' + uri.query)

    res = Net::HTTP.start( uri.host, uri.port ) { |http|
        http.request( req )
    }

    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 << WebService::element_to_toponym( element )
    
    end

    return toponym_sr
end

.timezone(lat, long) ⇒ Object


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/web_service.rb', line 167

def WebService.timezone( lat, long )
    timezone = Timezone.new
    
    url = Geonames::GEONAMES_SERVER + "/timezone?a=a"
    
    url = url + "&lat=" + lat.to_s
    url = url + "&lng=" + long.to_s

    uri = URI.parse(url)

    req = Net::HTTP::Get.new(uri.path + '?' + uri.query)

    res = Net::HTTP.start( uri.host, uri.port ) { |http|
        http.request( req )
    }

    doc = REXML::Document.new res.body
    
    doc.elements.each("geonames/timezone") do |element|
        timezone.timezone_id    = WebService::get_element_child_text( element, 'timezoneId' )
        timezone.gmt_offset     = WebService::get_element_child_float( element, 'gmtOffset' )
        timezone.dst_offset     = WebService::get_element_child_float( element, 'dstOffset' )
    end

    return timezone
  
end