Class: Geonames::WebService
- Inherits:
-
Object
- Object
- Geonames::WebService
- Defined in:
- lib/web_service.rb
Class Method Summary collapse
- .country_code(lat, long, radius = 0, maxRows = 1) ⇒ Object
- .country_info(country_code) ⇒ Object
- .country_subdivision(lat, long, radius = 0, maxRows = 1) ⇒ Object
- .element_to_country_info(element) ⇒ Object
- .element_to_intersection(element) ⇒ Object
- .element_to_postal_code(element) ⇒ Object
- .element_to_toponym(element) ⇒ Object
- .element_to_wikipedia_article(element) ⇒ Object
- .find_bounding_box_wikipedia(hashes) ⇒ Object
- .find_nearby_place_name(lat, long) ⇒ Object
- .find_nearby_postal_codes(search_criteria) ⇒ Object
- .find_nearby_wikipedia(hashes) ⇒ Object
- .find_nearest_intersection(lat, long) ⇒ Object
- .findBoundingBoxWikipedia(hashes) ⇒ Object
- .findNearbyWikipedia(hashes) ⇒ Object
- .get_element_child_float(element, child) ⇒ Object
- .get_element_child_int(element, child) ⇒ Object
- .get_element_child_text(element, child) ⇒ Object
- .make_request(path_and_query, *args) ⇒ Object
- .postal_code_search(search_criteria) ⇒ Object
- .search(search_criteria) ⇒ Object
- .timezone(lat, long, *args) ⇒ Object
Class Method Details
.country_code(lat, long, radius = 0, maxRows = 1) ⇒ Object
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/web_service.rb', line 398 def WebService.country_code ( lat, long, radius = 0, maxRows = 1 ) # 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 = Array.new url = url + "&lat=" + lat.to_s url = url + "&lng=" + long.to_s url = url + "&maxRows=" + maxRows.to_s url = url + "&radius=" + radius.to_s res = make_request(url) doc = REXML::Document.new res.body doc.elements.each("geonames/country") do |element| countries << WebService::element_to_toponym( element ) end return countries end |
.country_info(country_code) ⇒ Object
387 388 389 390 391 392 393 394 395 396 |
# File 'lib/web_service.rb', line 387 def WebService.country_info(country_code) url = "/countryInfo?a=a" url += "&country=#{country_code.to_s}" res = make_request(url) doc = REXML::Document.new res.body return WebService.element_to_country_info(doc.elements["geonames/country"]) end |
.country_subdivision(lat, long, radius = 0, maxRows = 1) ⇒ Object
350 351 352 353 354 355 356 357 358 359 360 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 |
# File 'lib/web_service.rb', line 350 def WebService.country_subdivision ( lat, long, radius = 0, maxRows = 1 ) country_subdivisions = Array.new # 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 = url + "&lat=" + lat.to_s url = url + "&lng=" + long.to_s url = url + "&maxRows=" + maxRows.to_s url = url + "&radius=" + radius.to_s res = make_request(url) doc = REXML::Document.new res.body doc.elements.each("geonames/countrySubdivision") do |element| country_subdivision = CountrySubdivision.new 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' ) country_subdivision.code_fips = WebService::get_element_child_text( element, 'code[@type="FIPS10-4"]') country_subdivision.code_iso = WebService::get_element_child_text( element, 'code[@type="ISO3166-2"]') country_subdivisions << country_subdivision end return country_subdivisions end |
.element_to_country_info(element) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/web_service.rb', line 120 def WebService.element_to_country_info(element) country_info = CountryInfo.new country_info.country_code = WebService.get_element_child_text(element, 'countryCode') country_info.country_name = WebService.get_element_child_text(element, 'countryName') country_info.iso_numeric = WebService.get_element_child_int(element, 'isoNumeric') country_info.iso_alpha_3 = WebService.get_element_child_text(element, 'isoAlpha3') country_info.fips_code = WebService.get_element_child_text(element, 'fipsCode') country_info.continent = WebService.get_element_child_text(element, 'continent') country_info.capital = WebService.get_element_child_text(element, 'capital') country_info.area_sq_km = WebService.get_element_child_float(element, 'areaInSqKm') country_info.population = WebService.get_element_child_int(element, 'population') country_info.currency_code = WebService.get_element_child_text(element, 'currencyCode') #actually an array of the available languages country_info.languages = WebService.get_element_child_text(element, 'languages').split(",") country_info.geoname_id = WebService.get_element_child_int(element, 'geonameId') north = WebService.get_element_child_float(element, 'bBoxNorth') south = WebService.get_element_child_float(element, 'bBoxSouth') east = WebService.get_element_child_float(element, 'bBoxEast') west = WebService.get_element_child_float(element, 'bBoxWest') country_info.set_bounding_box(north, south, east, west) return country_info end |
.element_to_intersection(element) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/web_service.rb', line 99 def WebService.element_to_intersection ( element ) intersection = Intersection.new intersection.street_1 = WebService::get_element_child_text( element, 'street1' ) intersection.street_2 = WebService::get_element_child_text( element, 'street2' ) intersection.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' ) intersection.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' ) intersection.admin_code_2 = WebService::get_element_child_text( element, 'adminCode2' ) intersection.admin_name_1 = WebService::get_element_child_text( element, 'adminName1' ) intersection.admin_name_2 = WebService::get_element_child_text( element, 'adminName2' ) intersection.country_code = WebService::get_element_child_text( element, 'countryCode' ) intersection.distance = WebService::get_element_child_float( element, 'distance' ) intersection.longitude = WebService::get_element_child_float( element, 'lat' ) intersection.latitude = WebService::get_element_child_float( element, 'lng' ) intersection.place_name = WebService::get_element_child_text( element, 'name' ) intersection.postal_code = WebService::get_element_child_text( element, 'postalcode' ) return intersection 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, 'lng' ) postal_code.latitude = WebService::get_element_child_float( element, 'lat' ) 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
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/web_service.rb', line 77 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 |
.element_to_wikipedia_article(element) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/web_service.rb', line 58 def WebService.element_to_wikipedia_article ( element ) article = WikipediaArticle.new article.language = WebService::get_element_child_text( element, 'lang' ) article.title = WebService::get_element_child_text( element, 'title' ) article.summary = WebService::get_element_child_text( element, 'summary' ) article.wikipedia_url = WebService::get_element_child_text( element, 'wikipediaUrl' ) article.feature = WebService::get_element_child_text( element, 'feature' ) article.population = WebService::get_element_child_text( element, 'population' ) article.elevation = WebService::get_element_child_text( element, 'elevation' ) article.latitude = WebService::get_element_child_float( element, 'lat' ) article.longitude = WebService::get_element_child_float( element, 'lng' ) article.thumbnail_img = WebService::get_element_child_text( element, 'thumbnailImg' ) article.distance = WebService::get_element_child_float( element, 'distance' ) return article end |
.find_bounding_box_wikipedia(hashes) ⇒ Object
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/web_service.rb', line 315 def WebService.find_bounding_box_wikipedia( hashes ) articles = Array.new 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 = url + "&north=" + north.to_s url = url + "&east=" + east.to_s url = url + "&south=" + south.to_s url = url + "&west=" + west.to_s url = url + "&radius=" + radius.to_s unless radius.nil? url = url + "&max_rows=" + max_rows.to_s unless max_rows.nil? res = make_request(url) doc = REXML::Document.new res.body doc.elements.each("geonames/entry") do |element| articles << WebService::element_to_wikipedia_article( element ) end return articles end |
.find_nearby_place_name(lat, long) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/web_service.rb', line 194 def WebService.find_nearby_place_name( lat, long ) places = Array.new url = "/findNearbyPlaceName?a=a" url = url + "&lat=" + lat.to_s url = url + "&lng=" + long.to_s res = make_request(url) 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
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/web_service.rb', line 175 def WebService.find_nearby_postal_codes( search_criteria ) # postal codes to reutrn postal_codes = Array.new url = "/findNearbyPostalCodes?a=a" url = url + search_criteria.to_query_params_string res = make_request(url) 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 |
.find_nearby_wikipedia(hashes) ⇒ Object
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 |
# File 'lib/web_service.rb', line 272 def WebService.find_nearby_wikipedia( hashes ) articles = Array.new 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 = url + "&lat=" + lat.to_s url = url + "&lng=" + long.to_s url = url + "&radius=" + radius.to_s unless radius.nil? url = url + "&max_rows=" + max_rows.to_s unless max_rows.nil? elsif !q.nil? url = url + "&q=" + q url = url + "&radius=" + radius.to_s unless radius.nil? url = url + "&max_rows=" + max_rows.to_s unless max_rows.nil? end res = make_request(url) doc = REXML::Document.new res.body doc.elements.each("geonames/entry") do |element| articles << WebService::element_to_wikipedia_article( element ) end return articles end |
.find_nearest_intersection(lat, long) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/web_service.rb', line 216 def WebService.find_nearest_intersection( lat, long ) url = "/findNearestIntersection?a=a" url = url + "&lat=" + lat.to_s url = url + "&lng=" + long.to_s res = make_request(url) doc = REXML::Document.new res.body intersection = [] doc.elements.each("geonames/intersection") do |element| intersection = WebService::element_to_intersection( element ) end return intersection end |
.findBoundingBoxWikipedia(hashes) ⇒ Object
310 311 312 313 |
# File 'lib/web_service.rb', line 310 def WebService.findBoundingBoxWikipedia( hashes ) # here for backwards compatibility WebService.find_bounding_box_wikipedia( hashes ) end |
.findNearbyWikipedia(hashes) ⇒ Object
267 268 269 270 |
# File 'lib/web_service.rb', line 267 def WebService.findNearbyWikipedia( hashes ) # here for backwards compatibility WebService.find_nearby_wikipedia( hashes ) 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 |
.make_request(path_and_query, *args) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/web_service.rb', line 250 def WebService.make_request(path_and_query, *args) url = Geonames::base_url + path_and_query url = url + "&username=#{Geonames::username}" if Geonames::username = { :open_timeout => 60, :read_timeout => 60 } .update(args.last.is_a?(::Hash) ? args.pop : {}) uri = URI.parse(url) req = Net::HTTP::Get.new(uri.path + '?' + uri.query) Net::HTTP.start(uri.host, uri.port) { |http| http.read_timeout = [:read_timeout] http.open_timeout = [:open_timeout] http.request(req) } end |
.postal_code_search(search_criteria) ⇒ Object
147 148 149 150 151 152 153 154 |
# File 'lib/web_service.rb', line 147 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.country_code = country_code WebService.postal_code_search postal_code_sc end |
.search(search_criteria) ⇒ Object
425 426 427 428 429 430 431 432 433 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 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/web_service.rb', line 425 def WebService.search( search_criteria ) #toponym search results to return toponym_sr = ToponymSearchResult.new url = "/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 res = make_request(url) 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, *args) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/web_service.rb', line 238 def WebService.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 = 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 timezone end |