eugenebolshakov-geokit 1.5.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -353,19 +353,38 @@ module Geokit
353
353
  params << "&maxRows=10"
354
354
 
355
355
  self.do_call_geocoder_service(address, params) do |res, doc|
356
- # only take the first result
357
- res.lat=doc.elements['//geoname/lat'].text if doc.elements['//geoname/lat']
358
- res.lng=doc.elements['//geoname/lng'].text if doc.elements['//geoname/lng']
359
- res.name = doc.elements['//geoname/name'].text if doc.elements['//geoname/name']
360
- res.country_code=doc.elements['//geoname/countryCode'].text if doc.elements['//geoname/countryCode']
361
- res.provider='geonames'
362
- # if the location is a city or village
363
- if doc.elements['//geoname/fcl'].text == 'P'
364
- res.city=res.name
356
+ if(doc.elements['//geonames/totalResultsCount'].text.to_i > 0)
357
+ # only take the first result
358
+ res.lat=doc.elements['//geoname/lat'].text if doc.elements['//geoname/lat']
359
+ res.lng=doc.elements['//geoname/lng'].text if doc.elements['//geoname/lng']
360
+ res.name = doc.elements['//geoname/name'].text if doc.elements['//geoname/name']
361
+ res.country_code=doc.elements['//geoname/countryCode'].text if doc.elements['//geoname/countryCode']
362
+ res.provider='geonames'
363
+ # if the location is a city or village
364
+ if doc.elements['//geoname/fcl'].text == 'P'
365
+ res.city=res.name
366
+ end
367
+ res.state=doc.elements['//geoname/adminCode1'].text if doc.elements['//geoname/adminCode1']
368
+ res.timezone = doc.elements['//geoname/timezone'].text if doc.elements['//geoname/timezone']
369
+ res.success=true
370
+ end
371
+ end
372
+ end
373
+
374
+ def self.cities_in_bounds(bounds)
375
+ params = "/cities?north=#{bounds.ne.lat}&south=#{bounds.sw.lat}&east=#{bounds.ne.lng}&west=#{bounds.sw.lng}"
376
+
377
+ self.do_call_geocoder_service(bounds.to_s, params) do |res, doc|
378
+ if doc.elements['//geonames/geoname']
379
+ # only take the first result
380
+ first = doc.elements['//geonames/geoname']
381
+ res.lat = first.elements['lat'].text
382
+ res.lng = first.elements['lng'].text
383
+ res.city = res.name = first.elements['name'].text
384
+ res.country_code = first.elements['countryCode'].text if first.elements['countryCode']
385
+ res.provider='geonames'
386
+ res.success = true
365
387
  end
366
- res.state=doc.elements['//geoname/adminCode1'].text if doc.elements['//geoname/adminCode1']
367
- res.timezone = doc.elements['//geoname/timezone'].text if doc.elements['//geoname/timezone']
368
- res.success=true
369
388
  end
370
389
  end
371
390
 
@@ -376,15 +395,17 @@ module Geokit
376
395
  params = "/postalCodeSearch?placename=#{Geokit::Inflector::url_escape(address_string(address))}&maxRows=10"
377
396
 
378
397
  self.do_call_geocoder_service(address, params) do |res, doc|
379
- # only take the first result
380
- res.lat=doc.elements['//code/lat'].text if doc.elements['//code/lat']
381
- res.lng=doc.elements['//code/lng'].text if doc.elements['//code/lng']
382
- res.country_code=doc.elements['//code/countryCode'].text if doc.elements['//code/countryCode']
383
- res.provider='geonames'
384
- res.city=doc.elements['//code/name'].text if doc.elements['//code/name']
385
- res.state=doc.elements['//code/adminName1'].text if doc.elements['//code/adminName1']
386
- res.zip=doc.elements['//code/postalcode'].text if doc.elements['//code/postalcode']
387
- res.success=true
398
+ if(doc.elements['//geonames/totalResultsCount'].text.to_i > 0)
399
+ # only take the first result
400
+ res.lat=doc.elements['//code/lat'].text if doc.elements['//code/lat']
401
+ res.lng=doc.elements['//code/lng'].text if doc.elements['//code/lng']
402
+ res.country_code=doc.elements['//code/countryCode'].text if doc.elements['//code/countryCode']
403
+ res.provider='geonames'
404
+ res.city=doc.elements['//code/name'].text if doc.elements['//code/name']
405
+ res.state=doc.elements['//code/adminName1'].text if doc.elements['//code/adminName1']
406
+ res.zip=doc.elements['//code/postalcode'].text if doc.elements['//code/postalcode']
407
+ res.success=true
408
+ end
388
409
  end
389
410
  end
390
411
 
@@ -401,25 +422,24 @@ module Geokit
401
422
  end
402
423
  end
403
424
 
404
- def self.do_call_geocoder_service(address, params)
425
+ def self.do_call_geocoder_service(request, params)
405
426
  res = self.call_geocoder_service(url(params))
406
-
427
+
407
428
  raise GeocodeError.new("HTTP request failed: #{res.class}") if !res.is_a?(Net::HTTPSuccess)
408
-
429
+
409
430
  xml=res.body
410
- logger.debug "Geonames geocoding. Address: #{address}. Result: #{xml}"
431
+ logger.debug "Geonames gecoder. Request: #{request}. Result: #{xml}"
411
432
  doc=REXML::Document.new(xml)
412
-
413
- if(doc.elements['//geonames/totalResultsCount'].text.to_i > 0)
414
- res=GeoLoc.new
415
433
 
416
- yield res, doc
417
-
418
- return res
419
- else
420
- logger.info "Geonames was unable to geocode address: "+address
421
- return GeoLoc.new
434
+ res=GeoLoc.new
435
+
436
+ yield res, doc
437
+
438
+ if !res.success?
439
+ logger.info "Geonames was unable to process request: #{request}"
422
440
  end
441
+
442
+ res
423
443
  end
424
444
  end
425
445
 
@@ -0,0 +1,22 @@
1
+ require File.join(File.dirname(__FILE__), 'test_base_geocoder')
2
+
3
+ class GeonamesReverseGeocoderTest < BaseGeocoderTest #:nodoc: all
4
+ def test_cities_in_bounds
5
+ ne = GeoKit::LatLng.new(48.97, 2.47)
6
+ sw = GeoKit::LatLng.new(48.79, 2.21)
7
+ bounds = GeoKit::Bounds.new(sw, ne)
8
+
9
+ @response = MockSuccess.new
10
+ @response.expects(:body).returns(fixture('geonames/cities_in_bounds'))
11
+
12
+ url = "http://ws.geonames.org/cities?north=48.97&south=48.79&east=2.47&west=2.21"
13
+ Geokit::Geocoders::GeonamesGeocoder.expects(:call_geocoder_service).with(url).returns(@response)
14
+
15
+ res=Geokit::Geocoders::GeonamesGeocoder.cities_in_bounds(bounds)
16
+ assert_equal "Paris", res.city
17
+ assert_equal "48.85341,2.3488", res.ll
18
+ assert !res.is_us?
19
+ assert_equal "Paris, FR", res.full_address
20
+ assert_equal "geonames", res.provider
21
+ end
22
+ end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eugenebolshakov-geokit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 5
8
+ - 1
9
+ version: 1.5.1
5
10
  platform: ruby
6
11
  authors:
7
12
  - Andre Lewis and Bill Eisenhauer
@@ -40,6 +45,7 @@ files:
40
45
  - test/test_us_geocoder.rb
41
46
  - test/test_yahoo_geocoder.rb
42
47
  - test/test_geonames_geocoder.rb
48
+ - test/test_geonames_reverse_geocoder.rb
43
49
  has_rdoc: true
44
50
  homepage: http://geokit.rubyforge.org
45
51
  licenses: []
@@ -54,18 +60,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
54
60
  requirements:
55
61
  - - ">="
56
62
  - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
57
65
  version: "0"
58
- version:
59
66
  required_rubygems_version: !ruby/object:Gem::Requirement
60
67
  requirements:
61
68
  - - ">="
62
69
  - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
63
72
  version: "0"
64
- version:
65
73
  requirements: []
66
74
 
67
75
  rubyforge_project: geokit
68
- rubygems_version: 1.3.5
76
+ rubygems_version: 1.3.6
69
77
  signing_key:
70
78
  specification_version: 2
71
79
  summary: none
@@ -85,3 +93,4 @@ test_files:
85
93
  - test/test_us_geocoder.rb
86
94
  - test/test_yahoo_geocoder.rb
87
95
  - test/test_geonames_geocoder.rb
96
+ - test/test_geonames_reverse_geocoder.rb