ppe-ruby-geonames 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +72 -0
- data/lib/Rakefile.rb +21 -0
- data/lib/address.rb +25 -0
- data/lib/bounding_box.rb +18 -0
- data/lib/country_info.rb +36 -0
- data/lib/country_subdivision.rb +35 -0
- data/lib/geonames.rb +59 -0
- data/lib/intersection.rb +42 -0
- data/lib/main.rb +63 -0
- data/lib/postal_code.rb +40 -0
- data/lib/postal_code_search_criteria.rb +84 -0
- data/lib/tc_country_info.rb +56 -0
- data/lib/timezone.rb +29 -0
- data/lib/toponym.rb +45 -0
- data/lib/toponym_search_criteria.rb +44 -0
- data/lib/toponym_search_result.rb +33 -0
- data/lib/web_service.rb +504 -0
- data/lib/wikipedia_article.rb +42 -0
- metadata +72 -0
data/README.markdown
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# Geonames Ruby API
|
2
|
+
|
3
|
+
Ruby library for [Geonames Web Services](http://www.geonames.org/export/)
|
4
|
+
|
5
|
+
Created by [TouchBase Counsulting](http://www.tbcn.ca/geonames) to support GIS processes for [Carpool Connect](http://www.carpoolconnect.com/). Inspired by the Geonames [Java Client API library](http://www.geonames.org/source-code/).
|
6
|
+
|
7
|
+
## Installing ruby-geonames
|
8
|
+
|
9
|
+
Install from the command line:
|
10
|
+
|
11
|
+
sudo gem install elecnix-ruby-geonames
|
12
|
+
|
13
|
+
## Examples
|
14
|
+
|
15
|
+
The following exercises several of the Geonames web services, [full list of services](http://www.geonames.org/export/).
|
16
|
+
|
17
|
+
**Load the the geonames API**
|
18
|
+
|
19
|
+
require 'geonames'
|
20
|
+
|
21
|
+
**get list of places near by longitude/longitude location**
|
22
|
+
|
23
|
+
places_nearby = Geonames::WebService.find_nearby_place_name 43.900120387, -78.882869834
|
24
|
+
p places_nearby
|
25
|
+
|
26
|
+
outputs:
|
27
|
+
|
28
|
+
`[#<Geonames::Toponym:0x6c8c98 @population=nil, @geoname_id='6094578', @longitude=-78.849568878, @feature_class_name=nil, @country_name='Canada', @latitude=43.900120387, @feature_class='P', @country_code='CA', @name='Oshawa', @feature_code_name=nil, @elevation=nil, @distance=2.6679930307932, @alternate_names=nil, @feature_code='PPL'>]`
|
29
|
+
|
30
|
+
**get timezone for longitude/longitude location**
|
31
|
+
|
32
|
+
timezone = Geonames::WebService.timezone 43.900120387, -78.882869834
|
33
|
+
p timezone
|
34
|
+
|
35
|
+
**get country code for longitude/longitude location**
|
36
|
+
|
37
|
+
country_code = Geonames::WebService.country_code 43.900120387, -78.882869834
|
38
|
+
p country_code
|
39
|
+
|
40
|
+
**get country sub-division info for longitude/longitude location**
|
41
|
+
|
42
|
+
country_subdivision = Geonames::WebService.country_subdivision 43.900120387, -78.882869834
|
43
|
+
p country_subdivision
|
44
|
+
|
45
|
+
**get postal codes for a given location**
|
46
|
+
|
47
|
+
postal_code_sc = Geonames::PostalCodeSearchCriteria.new
|
48
|
+
postal_code_sc.place_name = "Oshawa"
|
49
|
+
postal_codes = Geonames::WebService.postal_code_search postal_code_sc
|
50
|
+
p postal_codes
|
51
|
+
|
52
|
+
**get nearby postal codes for a place name**
|
53
|
+
|
54
|
+
postal_code_sc = Geonames::PostalCodeSearchCriteria.new
|
55
|
+
postal_code_sc.place_name = "Oshawa"
|
56
|
+
postal_codes_nearby = Geonames::WebService.find_nearby_postal_codes postal_code_sc
|
57
|
+
p postal_codes_nearby
|
58
|
+
|
59
|
+
## Commercial Service Support
|
60
|
+
|
61
|
+
If you use the commercial service, you should put something like this in your configuration:
|
62
|
+
|
63
|
+
Geonames::username = 'username'
|
64
|
+
Geonames::base_url = 'http://ws.geonames.net'
|
65
|
+
|
66
|
+
In a Rails application, this would go in the `config/environment.rb` file.
|
67
|
+
|
68
|
+
# Contributors
|
69
|
+
|
70
|
+
1. Adam Wisniewski
|
71
|
+
2. Nicolas Marchildon (elecnix)
|
72
|
+
|
data/lib/Rakefile.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#=============================================================================
|
2
|
+
#
|
3
|
+
# Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
6
|
+
# use this file except in compliance with the License. You may obtain a copy of
|
7
|
+
# the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
13
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
14
|
+
# License for the specific language governing permissions and limitations under
|
15
|
+
# the License.
|
16
|
+
#
|
17
|
+
#=============================================================================
|
18
|
+
|
19
|
+
require 'rake'
|
20
|
+
require 'rake/testtask'
|
21
|
+
require 'rake/rdoctask'
|
data/lib/address.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
#=============================================================================
|
2
|
+
#
|
3
|
+
# Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
6
|
+
# use this file except in compliance with the License. You may obtain a copy of
|
7
|
+
# the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
13
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
14
|
+
# License for the specific language governing permissions and limitations under
|
15
|
+
# the License.
|
16
|
+
#
|
17
|
+
#=============================================================================
|
18
|
+
|
19
|
+
module Geonames
|
20
|
+
class Address
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
data/lib/bounding_box.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Geonames
|
2
|
+
class BoundingBox
|
3
|
+
attr_reader :north_point, :south_point, :east_point, :west_point
|
4
|
+
attr_writer :north_point, :south_point, :east_point, :west_point
|
5
|
+
|
6
|
+
def initialize(north=0.0, south=0.0, east=0.0, west=0.0)
|
7
|
+
self.north_point = north
|
8
|
+
self.south_point = south
|
9
|
+
self.east_point = east
|
10
|
+
self.west_point = west
|
11
|
+
end
|
12
|
+
|
13
|
+
def contains?(point)
|
14
|
+
#todo implement me
|
15
|
+
return false
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/country_info.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Geonames
|
2
|
+
class CountryInfo
|
3
|
+
attr_reader :country_code, :country_name, :iso_numeric, :iso_alpha_3
|
4
|
+
attr_reader :fips_code, :continent, :capital, :area_sq_km, :population
|
5
|
+
attr_reader :currency_code, :geoname_id
|
6
|
+
|
7
|
+
attr_writer :country_code, :country_name, :iso_numeric, :iso_alpha_3
|
8
|
+
attr_writer :fips_code, :continent, :capital, :area_sq_km, :population
|
9
|
+
attr_writer :currency_code, :geoname_id
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@langs = []
|
13
|
+
@bounding_box = BoundingBox.new()
|
14
|
+
end
|
15
|
+
|
16
|
+
def bounding_box
|
17
|
+
return @bounding_box
|
18
|
+
end
|
19
|
+
|
20
|
+
def bounding_box=(new_bb)
|
21
|
+
@bounding_box = new_bb
|
22
|
+
end
|
23
|
+
|
24
|
+
def set_bounding_box(north, south, east, west)
|
25
|
+
@bounding_box = BoundingBox.new(north, south, east, west)
|
26
|
+
end
|
27
|
+
|
28
|
+
def languages
|
29
|
+
return @langs
|
30
|
+
end
|
31
|
+
|
32
|
+
def languages=(new_langs)
|
33
|
+
@langs = new_langs
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
#=============================================================================
|
2
|
+
#
|
3
|
+
# Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
6
|
+
# use this file except in compliance with the License. You may obtain a copy of
|
7
|
+
# the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
13
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
14
|
+
# License for the specific language governing permissions and limitations under
|
15
|
+
# the License.
|
16
|
+
#
|
17
|
+
#=============================================================================
|
18
|
+
|
19
|
+
module Geonames
|
20
|
+
class CountrySubdivision
|
21
|
+
attr :country_code
|
22
|
+
attr :country_name
|
23
|
+
attr :admin_code_1
|
24
|
+
attr :admin_name_1
|
25
|
+
attr :code_fips
|
26
|
+
attr :code_iso
|
27
|
+
|
28
|
+
attr_writer :country_code, :country_name
|
29
|
+
attr_writer :admin_name_1, :admin_code_1
|
30
|
+
attr_writer :code_fips, :code_iso
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
data/lib/geonames.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
#=============================================================================
|
2
|
+
#
|
3
|
+
# Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
6
|
+
# use this file except in compliance with the License. You may obtain a copy of
|
7
|
+
# the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
13
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
14
|
+
# License for the specific language governing permissions and limitations under
|
15
|
+
# the License.
|
16
|
+
#
|
17
|
+
#=============================================================================
|
18
|
+
|
19
|
+
require 'cgi'
|
20
|
+
require 'net/http'
|
21
|
+
require 'rexml/document'
|
22
|
+
|
23
|
+
require 'web_service'
|
24
|
+
require 'toponym'
|
25
|
+
require 'toponym_search_result'
|
26
|
+
require 'toponym_search_criteria'
|
27
|
+
require 'postal_code'
|
28
|
+
require 'postal_code_search_criteria'
|
29
|
+
require 'timezone'
|
30
|
+
require 'country_subdivision'
|
31
|
+
require 'wikipedia_article'
|
32
|
+
require 'intersection'
|
33
|
+
|
34
|
+
module Geonames
|
35
|
+
|
36
|
+
GEONAMES_SERVER = "http://ws.geonames.org"
|
37
|
+
USER_AGENT = "geonames ruby webservice client 0.1"
|
38
|
+
|
39
|
+
@@username = nil
|
40
|
+
@@base_url = "http://ws.geonames.org"
|
41
|
+
|
42
|
+
def self.username
|
43
|
+
@@username
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.username=(username)
|
47
|
+
@@username = username
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.base_url
|
51
|
+
@@base_url
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.base_url=(base_url)
|
55
|
+
@@base_url = base_url
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
data/lib/intersection.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
#=============================================================================
|
2
|
+
#
|
3
|
+
# Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
6
|
+
# use this file except in compliance with the License. You may obtain a copy of
|
7
|
+
# the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
13
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
14
|
+
# License for the specific language governing permissions and limitations under
|
15
|
+
# the License.
|
16
|
+
#
|
17
|
+
#=============================================================================
|
18
|
+
|
19
|
+
module Geonames
|
20
|
+
class Intersection
|
21
|
+
attr :street_1
|
22
|
+
attr :street_2
|
23
|
+
attr :latitude
|
24
|
+
attr :longitude
|
25
|
+
attr :distance
|
26
|
+
attr :postal_code
|
27
|
+
attr :place_name
|
28
|
+
attr :country_code
|
29
|
+
attr :admin_code_1
|
30
|
+
attr :admin_name_2
|
31
|
+
attr :admin_code_2
|
32
|
+
attr :admin_name_1
|
33
|
+
|
34
|
+
attr_writer :street_1, :street_2
|
35
|
+
attr_writer :postal_code, :place_name, :country_code
|
36
|
+
attr_writer :latitude, :longitude, :admin_name_1
|
37
|
+
attr_writer :admin_code_1, :admin_name_2, :admin_code_2
|
38
|
+
attr_writer :distance
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
|
data/lib/main.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
#=============================================================================
|
2
|
+
#
|
3
|
+
# Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
|
4
|
+
# Contributions by Andrew Turner, High Earth Orbit
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
7
|
+
# use this file except in compliance with the License. You may obtain a copy of
|
8
|
+
# the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
14
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
15
|
+
# License for the specific language governing permissions and limitations under
|
16
|
+
# the License.
|
17
|
+
#
|
18
|
+
#=============================================================================
|
19
|
+
|
20
|
+
require 'geonames'
|
21
|
+
require 'pp'
|
22
|
+
|
23
|
+
# get the nearest intersection
|
24
|
+
intersection = Geonames::WebService.find_nearest_intersection 40.7574053333333, -73.9734773333333
|
25
|
+
puts intersection.street_1 #=> Park Ave
|
26
|
+
puts intersection.street_2 #=> E 51st St
|
27
|
+
|
28
|
+
# get wikipedia articles by lat / long
|
29
|
+
articles_nearby = Geonames::WebService.find_nearby_wikipedia :lat => 43.900120387, :long => -78.882869834
|
30
|
+
p articles_nearby
|
31
|
+
|
32
|
+
# get wikipedia articles by bounding box
|
33
|
+
articles_nearby = Geonames::WebService.find_bounding_box_wikipedia :north => 43.900120387, :east => -78.882869834, :south => 43.82, :west => 79.0
|
34
|
+
p articles_nearby
|
35
|
+
|
36
|
+
# get list of places near by longitude/longitude location
|
37
|
+
places_nearby = Geonames::WebService.find_nearby_place_name 43.900120387, -78.882869834
|
38
|
+
p places_nearby
|
39
|
+
|
40
|
+
# get timezone for longitude/longitude location
|
41
|
+
timezone = Geonames::WebService.timezone 43.900120387, -78.882869834
|
42
|
+
p timezone
|
43
|
+
|
44
|
+
# get country code for longitude/longitude location
|
45
|
+
country_code = Geonames::WebService.country_code 43.900120387, -78.882869834
|
46
|
+
p country_code
|
47
|
+
|
48
|
+
# get country sub-division info for longitude/longitude location
|
49
|
+
country_subdivision = Geonames::WebService.country_subdivision 43.900120387, -78.882869834
|
50
|
+
p country_subdivision
|
51
|
+
|
52
|
+
# get postal codes for a given location
|
53
|
+
postal_code_sc = Geonames::PostalCodeSearchCriteria.new
|
54
|
+
postal_code_sc.place_name = "Oshawa"
|
55
|
+
postal_codes = Geonames::WebService.postal_code_search postal_code_sc
|
56
|
+
p postal_codes
|
57
|
+
|
58
|
+
# get nearby postal codes for a place name
|
59
|
+
postal_code_sc = Geonames::PostalCodeSearchCriteria.new
|
60
|
+
postal_code_sc.place_name = "Oshawa"
|
61
|
+
postal_codes_nearby = Geonames::WebService.find_nearby_postal_codes postal_code_sc
|
62
|
+
p postal_codes_nearby
|
63
|
+
|
data/lib/postal_code.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
#=============================================================================
|
2
|
+
#
|
3
|
+
# Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
6
|
+
# use this file except in compliance with the License. You may obtain a copy of
|
7
|
+
# the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
13
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
14
|
+
# License for the specific language governing permissions and limitations under
|
15
|
+
# the License.
|
16
|
+
#
|
17
|
+
#=============================================================================
|
18
|
+
|
19
|
+
module Geonames
|
20
|
+
class PostalCode
|
21
|
+
attr :postal_code
|
22
|
+
attr :place_name
|
23
|
+
attr :country_code
|
24
|
+
attr :latitude
|
25
|
+
attr :longitude
|
26
|
+
attr :admin_name_1
|
27
|
+
attr :admin_code_1
|
28
|
+
attr :admin_name_2
|
29
|
+
attr :admin_code_2
|
30
|
+
attr :distance
|
31
|
+
|
32
|
+
attr_writer :postal_code, :place_name, :country_code
|
33
|
+
attr_writer :latitude, :longitude, :admin_name_1
|
34
|
+
attr_writer :admin_code_1, :admin_name_2, :admin_code_2
|
35
|
+
attr_writer :distance
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
@@ -0,0 +1,84 @@
|
|
1
|
+
#=============================================================================
|
2
|
+
#
|
3
|
+
# Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
6
|
+
# use this file except in compliance with the License. You may obtain a copy of
|
7
|
+
# the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
13
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
14
|
+
# License for the specific language governing permissions and limitations under
|
15
|
+
# the License.
|
16
|
+
#
|
17
|
+
#=============================================================================
|
18
|
+
|
19
|
+
module Geonames
|
20
|
+
class PostalCodeSearchCriteria
|
21
|
+
|
22
|
+
attr :postal_code
|
23
|
+
attr :place_name
|
24
|
+
attr :country_code
|
25
|
+
attr :latitude
|
26
|
+
attr :longitude
|
27
|
+
attr :style
|
28
|
+
attr :max_rows
|
29
|
+
attr :is_or_operator
|
30
|
+
attr :radius
|
31
|
+
|
32
|
+
attr_writer :postal_code, :place_name, :country_code
|
33
|
+
attr_writer :latitude, :longitude, :style
|
34
|
+
attr_writer :max_rows, :is_or_operator, :radius
|
35
|
+
|
36
|
+
def initialize
|
37
|
+
@is_or_operator = false
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_query_params_string
|
41
|
+
url = ''
|
42
|
+
|
43
|
+
if !@postal_code.nil?
|
44
|
+
url = url + "&postalcode=" + CGI::escape( @postal_code )
|
45
|
+
end
|
46
|
+
|
47
|
+
if !@place_name.nil?
|
48
|
+
url = url + "&placename=" + CGI::escape( @place_name )
|
49
|
+
end
|
50
|
+
|
51
|
+
if !@latitude.nil?
|
52
|
+
url = url + "&lat" + CGI::escape( @latitude.to_s )
|
53
|
+
end
|
54
|
+
|
55
|
+
if !@longitude.nil?
|
56
|
+
url = url + "&lng" + CGI::escape( @longitude.to_s )
|
57
|
+
end
|
58
|
+
|
59
|
+
if !@style.nil?
|
60
|
+
url = url + "&style" + CGI::escape( @style )
|
61
|
+
end
|
62
|
+
|
63
|
+
if !@country_code.nil?
|
64
|
+
url = url + "&country=" + CGI::escape( @country_code )
|
65
|
+
end
|
66
|
+
|
67
|
+
if !@max_rows.nil?
|
68
|
+
url = url + "&maxRows=" + CGI::escape( @max_rows )
|
69
|
+
end
|
70
|
+
|
71
|
+
if !@radius.nil?
|
72
|
+
url = url + "&radius=" + CGI::escape( @radius.to_s )
|
73
|
+
end
|
74
|
+
|
75
|
+
if @is_or_operator
|
76
|
+
url = url + "&operator=OR"
|
77
|
+
end
|
78
|
+
|
79
|
+
url
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
|
@@ -0,0 +1,56 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
|
3
|
+
require 'geonames'
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
class TestCountryInfo < Test::Unit::TestCase
|
7
|
+
def setup
|
8
|
+
@test_code = "TH"
|
9
|
+
|
10
|
+
@expected =
|
11
|
+
{:country_code => 'TH', :country_name => 'Thailand', :iso_numeric => 764,
|
12
|
+
:iso_alpha_3 => 'THA', :fips_code => 'TH', :continent => 'AS',
|
13
|
+
:capital => 'Bangkok', :area_sq_km => 514000.0, :population => 65493000,
|
14
|
+
:currency_code => 'THB', :geonameId => 1605651,
|
15
|
+
:languages => ['th', 'en'],
|
16
|
+
:box_north => 20.4631977081299, :box_south => 5.60999917984009,
|
17
|
+
:box_east => 105.63939666748, :box_west => 97.3456268310547}
|
18
|
+
end
|
19
|
+
|
20
|
+
def teardown
|
21
|
+
#nothing here really
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_simple_values
|
25
|
+
info = Geonames::WebService.country_info(@test_code)
|
26
|
+
|
27
|
+
assert_equal(@expected[:country_code], info.country_code)
|
28
|
+
assert_equal(@expected[:country_name], info.country_name)
|
29
|
+
assert_equal(@expected[:iso_numeric], info.iso_numeric)
|
30
|
+
assert_equal(@expected[:iso_alpha_3], info.iso_alpha_3)
|
31
|
+
assert_equal(@expected[:fips_code], info.fips_code)
|
32
|
+
assert_equal(@expected[:continent], info.continent)
|
33
|
+
assert_equal(@expected[:capital], info.capital)
|
34
|
+
assert_equal(@expected[:area_sq_km], info.area_sq_km)
|
35
|
+
assert_equal(@expected[:population], info.population)
|
36
|
+
assert_equal(@expected[:currency_code], info.currency_code)
|
37
|
+
assert_equal(@expected[:geonameId], info.geoname_id )
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_languages
|
41
|
+
info = Geonames::WebService.country_info(@test_code)
|
42
|
+
|
43
|
+
assert_equal(2, info.languages.size)
|
44
|
+
assert_equal(@expected[:languages][0], info.languages[0])
|
45
|
+
assert_equal(@expected[:languages][1], info.languages[1])
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_bounding_box
|
49
|
+
info = Geonames::WebService.country_info(@test_code)
|
50
|
+
|
51
|
+
assert_equal(@expected[:box_north], info.bounding_box.north_point)
|
52
|
+
assert_equal(@expected[:box_south], info.bounding_box.south_point)
|
53
|
+
assert_equal(@expected[:box_east], info.bounding_box.east_point)
|
54
|
+
assert_equal(@expected[:box_west], info.bounding_box.west_point)
|
55
|
+
end
|
56
|
+
end
|
data/lib/timezone.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#=============================================================================
|
2
|
+
#
|
3
|
+
# Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
6
|
+
# use this file except in compliance with the License. You may obtain a copy of
|
7
|
+
# the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
13
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
14
|
+
# License for the specific language governing permissions and limitations under
|
15
|
+
# the License.
|
16
|
+
#
|
17
|
+
#=============================================================================
|
18
|
+
|
19
|
+
module Geonames
|
20
|
+
class Timezone
|
21
|
+
attr :timezone_id
|
22
|
+
attr :gmt_offset
|
23
|
+
attr :dst_offset
|
24
|
+
|
25
|
+
attr_writer :timezone_id, :gmt_offset, :dst_offset
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
data/lib/toponym.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
#=============================================================================
|
2
|
+
#
|
3
|
+
# Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
6
|
+
# use this file except in compliance with the License. You may obtain a copy of
|
7
|
+
# the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
13
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
14
|
+
# License for the specific language governing permissions and limitations under
|
15
|
+
# the License.
|
16
|
+
#
|
17
|
+
#=============================================================================
|
18
|
+
|
19
|
+
module Geonames
|
20
|
+
class Toponym
|
21
|
+
|
22
|
+
attr :geoname_id
|
23
|
+
attr :name
|
24
|
+
attr :alternate_names
|
25
|
+
attr :country_code
|
26
|
+
attr :country_name
|
27
|
+
attr :population
|
28
|
+
attr :elevation
|
29
|
+
attr :feature_class
|
30
|
+
attr :feature_class_name
|
31
|
+
attr :feature_code
|
32
|
+
attr :feature_code_name
|
33
|
+
attr :latitude
|
34
|
+
attr :longitude
|
35
|
+
attr :distance
|
36
|
+
|
37
|
+
attr_writer :geoname_id, :name, :alternate_names, :country_code
|
38
|
+
attr_writer :country_name, :population, :elevation, :feature_class
|
39
|
+
attr_writer :feature_class_name, :feature_code,:feature_code_name
|
40
|
+
attr_writer :latitude, :longitude, :distance
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
#=============================================================================
|
2
|
+
#
|
3
|
+
# Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
6
|
+
# use this file except in compliance with the License. You may obtain a copy of
|
7
|
+
# the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
13
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
14
|
+
# License for the specific language governing permissions and limitations under
|
15
|
+
# the License.
|
16
|
+
#
|
17
|
+
#=============================================================================
|
18
|
+
|
19
|
+
module Geonames
|
20
|
+
class ToponymSearchCriteria
|
21
|
+
|
22
|
+
attr :q
|
23
|
+
attr :country_code
|
24
|
+
attr :name
|
25
|
+
attr :name_equals
|
26
|
+
attr :name_starts_with
|
27
|
+
attr :tag
|
28
|
+
attr :language
|
29
|
+
attr :style
|
30
|
+
attr :feature_class
|
31
|
+
attr :feature_codes
|
32
|
+
attr :admin_code_1
|
33
|
+
attr :max_rows
|
34
|
+
attr :start_row
|
35
|
+
|
36
|
+
attr_writer :q, :country_code, :name, :name_equals
|
37
|
+
attr_writer :name_starts_with, :tag, :language, :style
|
38
|
+
attr_writer :feature_class, :feature_codes, :admin_code_1
|
39
|
+
attr_writer :max_rows, :start_row
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
#=============================================================================
|
2
|
+
#
|
3
|
+
# Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
6
|
+
# use this file except in compliance with the License. You may obtain a copy of
|
7
|
+
# the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
13
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
14
|
+
# License for the specific language governing permissions and limitations under
|
15
|
+
# the License.
|
16
|
+
#
|
17
|
+
#=============================================================================
|
18
|
+
|
19
|
+
module Geonames
|
20
|
+
class ToponymSearchResult
|
21
|
+
|
22
|
+
attr :total_results_count
|
23
|
+
attr :toponyms
|
24
|
+
|
25
|
+
attr_writer :total_results_count, :toponyms
|
26
|
+
|
27
|
+
def initialize
|
28
|
+
@toponyms = Array.new
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
data/lib/web_service.rb
ADDED
@@ -0,0 +1,504 @@
|
|
1
|
+
#=============================================================================
|
2
|
+
#
|
3
|
+
# Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
|
4
|
+
# Contributions by Andrew Turner, High Earth Orbit
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
7
|
+
# use this file except in compliance with the License. You may obtain a copy of
|
8
|
+
# the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
14
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
15
|
+
# License for the specific language governing permissions and limitations under
|
16
|
+
# the License.
|
17
|
+
#
|
18
|
+
#=============================================================================
|
19
|
+
|
20
|
+
module Geonames
|
21
|
+
class WebService
|
22
|
+
def WebService.get_element_child_text( element, child )
|
23
|
+
if !element.elements[child].nil?
|
24
|
+
element.elements[child][0].to_s
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def WebService.get_element_child_float( element, child )
|
29
|
+
if !element.elements[child].nil?
|
30
|
+
element.elements[child][0].to_s.to_f
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def WebService.get_element_child_int( element, child )
|
35
|
+
if !element.elements[child].nil?
|
36
|
+
element.elements[child][0].to_s.to_i
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def WebService.element_to_postal_code ( element )
|
41
|
+
postal_code = PostalCode.new
|
42
|
+
|
43
|
+
postal_code.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' )
|
44
|
+
postal_code.admin_code_2 = WebService::get_element_child_text( element, 'adminCode2' )
|
45
|
+
postal_code.admin_name_1 = WebService::get_element_child_text( element, 'adminName1' )
|
46
|
+
postal_code.admin_name_2 = WebService::get_element_child_text( element, 'adminName2' )
|
47
|
+
postal_code.country_code = WebService::get_element_child_text( element, 'countryCode' )
|
48
|
+
postal_code.distance = WebService::get_element_child_float( element, 'distance' )
|
49
|
+
postal_code.longitude = WebService::get_element_child_float( element, 'lng' )
|
50
|
+
postal_code.latitude = WebService::get_element_child_float( element, 'lat' )
|
51
|
+
postal_code.place_name = WebService::get_element_child_text( element, 'name' )
|
52
|
+
postal_code.postal_code = WebService::get_element_child_text( element, 'postalcode' )
|
53
|
+
|
54
|
+
return postal_code
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
def WebService.element_to_wikipedia_article ( element )
|
59
|
+
article = WikipediaArticle.new
|
60
|
+
|
61
|
+
article.language = WebService::get_element_child_text( element, 'lang' )
|
62
|
+
article.title = WebService::get_element_child_text( element, 'title' )
|
63
|
+
article.summary = WebService::get_element_child_text( element, 'summary' )
|
64
|
+
article.wikipedia_url = WebService::get_element_child_text( element, 'wikipediaUrl' )
|
65
|
+
article.feature = WebService::get_element_child_text( element, 'feature' )
|
66
|
+
article.population = WebService::get_element_child_text( element, 'population' )
|
67
|
+
article.elevation = WebService::get_element_child_text( element, 'elevation' )
|
68
|
+
article.latitude = WebService::get_element_child_float( element, 'lat' )
|
69
|
+
article.longitude = WebService::get_element_child_float( element, 'lng' )
|
70
|
+
article.thumbnail_img = WebService::get_element_child_text( element, 'thumbnailImg' )
|
71
|
+
article.distance = WebService::get_element_child_float( element, 'distance' )
|
72
|
+
|
73
|
+
return article
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
def WebService.element_to_toponym ( element )
|
78
|
+
toponym = Toponym.new
|
79
|
+
|
80
|
+
toponym.name = WebService::get_element_child_text( element, 'name' )
|
81
|
+
toponym.alternate_names = WebService::get_element_child_text( element, 'alternateNames' )
|
82
|
+
toponym.latitude = WebService::get_element_child_float( element, 'lat' )
|
83
|
+
toponym.longitude = WebService::get_element_child_float( element, 'lng' )
|
84
|
+
toponym.geoname_id = WebService::get_element_child_text( element, 'geonameId' )
|
85
|
+
toponym.country_code = WebService::get_element_child_text( element, 'countryCode' )
|
86
|
+
toponym.country_name = WebService::get_element_child_text( element, 'countryName' )
|
87
|
+
toponym.feature_class = WebService::get_element_child_text( element, 'fcl' )
|
88
|
+
toponym.feature_code = WebService::get_element_child_text( element, 'fcode' )
|
89
|
+
toponym.feature_class_name = WebService::get_element_child_text( element, 'fclName' )
|
90
|
+
toponym.feature_code_name = WebService::get_element_child_text( element, 'fCodeName' )
|
91
|
+
toponym.population = WebService::get_element_child_int( element, 'population' )
|
92
|
+
toponym.elevation = WebService::get_element_child_text( element, 'elevation' )
|
93
|
+
toponym.distance = WebService::get_element_child_float( element, 'distance' )
|
94
|
+
|
95
|
+
return toponym
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
def WebService.element_to_intersection ( element )
|
100
|
+
intersection = Intersection.new
|
101
|
+
|
102
|
+
intersection.street_1 = WebService::get_element_child_text( element, 'street1' )
|
103
|
+
intersection.street_2 = WebService::get_element_child_text( element, 'street2' )
|
104
|
+
intersection.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' )
|
105
|
+
intersection.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' )
|
106
|
+
intersection.admin_code_2 = WebService::get_element_child_text( element, 'adminCode2' )
|
107
|
+
intersection.admin_name_1 = WebService::get_element_child_text( element, 'adminName1' )
|
108
|
+
intersection.admin_name_2 = WebService::get_element_child_text( element, 'adminName2' )
|
109
|
+
intersection.country_code = WebService::get_element_child_text( element, 'countryCode' )
|
110
|
+
intersection.distance = WebService::get_element_child_float( element, 'distance' )
|
111
|
+
intersection.longitude = WebService::get_element_child_float( element, 'lat' )
|
112
|
+
intersection.latitude = WebService::get_element_child_float( element, 'lng' )
|
113
|
+
intersection.place_name = WebService::get_element_child_text( element, 'name' )
|
114
|
+
intersection.postal_code = WebService::get_element_child_text( element, 'postalcode' )
|
115
|
+
|
116
|
+
return intersection
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
def WebService.element_to_country_info(element)
|
121
|
+
country_info = CountryInfo.new
|
122
|
+
|
123
|
+
country_info.country_code = WebService.get_element_child_text(element, 'countryCode')
|
124
|
+
country_info.country_name = WebService.get_element_child_text(element, 'countryName')
|
125
|
+
country_info.iso_numeric = WebService.get_element_child_int(element, 'isoNumeric')
|
126
|
+
country_info.iso_alpha_3 = WebService.get_element_child_text(element, 'isoAlpha3')
|
127
|
+
country_info.fips_code = WebService.get_element_child_text(element, 'fipsCode')
|
128
|
+
country_info.continent = WebService.get_element_child_text(element, 'continent')
|
129
|
+
country_info.capital = WebService.get_element_child_text(element, 'capital')
|
130
|
+
country_info.area_sq_km = WebService.get_element_child_float(element, 'areaInSqKm')
|
131
|
+
country_info.population = WebService.get_element_child_int(element, 'population')
|
132
|
+
country_info.currency_code = WebService.get_element_child_text(element, 'currencyCode')
|
133
|
+
#actually an array of the available languages
|
134
|
+
country_info.languages = WebService.get_element_child_text(element, 'languages').split(",")
|
135
|
+
country_info.geoname_id = WebService.get_element_child_int(element, 'geonameId')
|
136
|
+
|
137
|
+
north = WebService.get_element_child_float(element, 'bBoxNorth')
|
138
|
+
south = WebService.get_element_child_float(element, 'bBoxSouth')
|
139
|
+
east = WebService.get_element_child_float(element, 'bBoxEast')
|
140
|
+
west = WebService.get_element_child_float(element, 'bBoxWest')
|
141
|
+
|
142
|
+
country_info.set_bounding_box(north, south, east, west)
|
143
|
+
|
144
|
+
return country_info
|
145
|
+
end
|
146
|
+
|
147
|
+
def WebService.postal_code_search( postal_code, place_name, country_code )
|
148
|
+
postal_code_sc = PostalCodeSearchCriteria.new
|
149
|
+
postal_code_sc.postal_code = postal_code
|
150
|
+
postal_code_sc.place_name = place_name
|
151
|
+
postal_code_sc.country_code = country_code
|
152
|
+
|
153
|
+
WebService.postal_code_search postal_code_sc
|
154
|
+
end
|
155
|
+
|
156
|
+
def WebService.postal_code_search( search_criteria )
|
157
|
+
# postal codes to reutrn
|
158
|
+
postal_codes = Array.new
|
159
|
+
|
160
|
+
url = "/postalCodeSearch?a=a"
|
161
|
+
url = url + search_criteria.to_query_params_string
|
162
|
+
|
163
|
+
res = make_request(url)
|
164
|
+
|
165
|
+
doc = REXML::Document.new res.body
|
166
|
+
|
167
|
+
doc.elements.each("geonames/code") do |element|
|
168
|
+
postal_codes << WebService::element_to_postal_code( element )
|
169
|
+
end
|
170
|
+
|
171
|
+
postal_codes
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
def WebService.find_nearby_postal_codes( search_criteria )
|
176
|
+
# postal codes to reutrn
|
177
|
+
postal_codes = Array.new
|
178
|
+
|
179
|
+
url = "/findNearbyPostalCodes?a=a"
|
180
|
+
url = url + search_criteria.to_query_params_string
|
181
|
+
|
182
|
+
res = make_request(url)
|
183
|
+
|
184
|
+
doc = REXML::Document.new res.body
|
185
|
+
|
186
|
+
doc.elements.each("geonames/code") do |element|
|
187
|
+
postal_codes << WebService::element_to_postal_code( element )
|
188
|
+
end
|
189
|
+
|
190
|
+
postal_codes
|
191
|
+
|
192
|
+
end
|
193
|
+
|
194
|
+
def WebService.find_nearby_place_name( lat, long )
|
195
|
+
places = Array.new
|
196
|
+
|
197
|
+
url = "/findNearbyPlaceName?a=a"
|
198
|
+
|
199
|
+
url = url + "&lat=" + lat.to_s
|
200
|
+
url = url + "&lng=" + long.to_s
|
201
|
+
|
202
|
+
res = make_request(url)
|
203
|
+
|
204
|
+
doc = REXML::Document.new res.body
|
205
|
+
|
206
|
+
doc.elements.each("geonames/geoname") do |element|
|
207
|
+
|
208
|
+
places << WebService::element_to_toponym( element )
|
209
|
+
|
210
|
+
end
|
211
|
+
|
212
|
+
return places
|
213
|
+
|
214
|
+
end
|
215
|
+
|
216
|
+
def WebService.find_nearest_intersection( lat, long )
|
217
|
+
|
218
|
+
url = "/findNearestIntersection?a=a"
|
219
|
+
|
220
|
+
url = url + "&lat=" + lat.to_s
|
221
|
+
url = url + "&lng=" + long.to_s
|
222
|
+
|
223
|
+
res = make_request(url)
|
224
|
+
|
225
|
+
doc = REXML::Document.new res.body
|
226
|
+
|
227
|
+
intersection = []
|
228
|
+
doc.elements.each("geonames/intersection") do |element|
|
229
|
+
|
230
|
+
intersection = WebService::element_to_intersection( element )
|
231
|
+
|
232
|
+
end
|
233
|
+
|
234
|
+
return intersection
|
235
|
+
|
236
|
+
end
|
237
|
+
|
238
|
+
def WebService.timezone( lat, long, *args )
|
239
|
+
res = make_request("/timezone?lat=#{lat.to_s}&lng=#{long.to_s}", args)
|
240
|
+
doc = REXML::Document.new res.body
|
241
|
+
timezone = Timezone.new
|
242
|
+
doc.elements.each("geonames/timezone") do |element|
|
243
|
+
timezone.timezone_id = WebService::get_element_child_text( element, 'timezoneId' )
|
244
|
+
timezone.gmt_offset = WebService::get_element_child_float( element, 'gmtOffset' )
|
245
|
+
timezone.dst_offset = WebService::get_element_child_float( element, 'dstOffset' )
|
246
|
+
end
|
247
|
+
timezone
|
248
|
+
end
|
249
|
+
|
250
|
+
def WebService.make_request(path_and_query, *args)
|
251
|
+
url = Geonames::base_url + path_and_query
|
252
|
+
url = url + "&username=#{Geonames::username}" if Geonames::username
|
253
|
+
options = {
|
254
|
+
:open_timeout => 60,
|
255
|
+
:read_timeout => 60
|
256
|
+
}
|
257
|
+
options.update(args.last.is_a?(::Hash) ? args.pop : {})
|
258
|
+
uri = URI.parse(url)
|
259
|
+
req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
|
260
|
+
Net::HTTP.start(uri.host, uri.port) { |http|
|
261
|
+
http.read_timeout = options[:read_timeout]
|
262
|
+
http.open_timeout = options[:open_timeout]
|
263
|
+
http.request(req)
|
264
|
+
}
|
265
|
+
end
|
266
|
+
|
267
|
+
def WebService.findNearbyWikipedia( hashes )
|
268
|
+
# here for backwards compatibility
|
269
|
+
WebService.find_nearby_wikipedia( hashes )
|
270
|
+
end
|
271
|
+
|
272
|
+
def WebService.find_nearby_wikipedia( hashes )
|
273
|
+
articles = Array.new
|
274
|
+
|
275
|
+
lat = hashes[:lat]
|
276
|
+
long = hashes[:long]
|
277
|
+
lang = hashes[:lang]
|
278
|
+
radius = hashes[:radius]
|
279
|
+
max_rows = hashes[:max_rows]
|
280
|
+
country = hashes[:country]
|
281
|
+
postalcode = hashes[:postalcode]
|
282
|
+
q = hashes[:q]
|
283
|
+
|
284
|
+
url = "/findNearbyWikipedia?a=a"
|
285
|
+
|
286
|
+
if !lat.nil? && !long.nil?
|
287
|
+
url = url + "&lat=" + lat.to_s
|
288
|
+
url = url + "&lng=" + long.to_s
|
289
|
+
url = url + "&radius=" + radius.to_s unless radius.nil?
|
290
|
+
url = url + "&max_rows=" + max_rows.to_s unless max_rows.nil?
|
291
|
+
|
292
|
+
elsif !q.nil?
|
293
|
+
url = url + "&q=" + q
|
294
|
+
url = url + "&radius=" + radius.to_s unless radius.nil?
|
295
|
+
url = url + "&max_rows=" + max_rows.to_s unless max_rows.nil?
|
296
|
+
end
|
297
|
+
|
298
|
+
res = make_request(url)
|
299
|
+
|
300
|
+
doc = REXML::Document.new res.body
|
301
|
+
|
302
|
+
doc.elements.each("geonames/entry") do |element|
|
303
|
+
articles << WebService::element_to_wikipedia_article( element )
|
304
|
+
end
|
305
|
+
|
306
|
+
return articles
|
307
|
+
|
308
|
+
end
|
309
|
+
|
310
|
+
def WebService.findBoundingBoxWikipedia( hashes )
|
311
|
+
# here for backwards compatibility
|
312
|
+
WebService.find_bounding_box_wikipedia( hashes )
|
313
|
+
end
|
314
|
+
|
315
|
+
def WebService.find_bounding_box_wikipedia( hashes )
|
316
|
+
articles = Array.new
|
317
|
+
|
318
|
+
north = hashes[:north]
|
319
|
+
east = hashes[:east]
|
320
|
+
south = hashes[:south]
|
321
|
+
west = hashes[:west]
|
322
|
+
lang = hashes[:lang]
|
323
|
+
radius = hashes[:radius]
|
324
|
+
max_rows = hashes[:max_rows]
|
325
|
+
country = hashes[:country]
|
326
|
+
postalcode = hashes[:postalcode]
|
327
|
+
q = hashes[:q]
|
328
|
+
|
329
|
+
url = "/wikipediaBoundingBox?a=a"
|
330
|
+
|
331
|
+
url = url + "&north=" + north.to_s
|
332
|
+
url = url + "&east=" + east.to_s
|
333
|
+
url = url + "&south=" + south.to_s
|
334
|
+
url = url + "&west=" + west.to_s
|
335
|
+
url = url + "&radius=" + radius.to_s unless radius.nil?
|
336
|
+
url = url + "&max_rows=" + max_rows.to_s unless max_rows.nil?
|
337
|
+
|
338
|
+
res = make_request(url)
|
339
|
+
|
340
|
+
doc = REXML::Document.new res.body
|
341
|
+
|
342
|
+
doc.elements.each("geonames/entry") do |element|
|
343
|
+
articles << WebService::element_to_wikipedia_article( element )
|
344
|
+
end
|
345
|
+
|
346
|
+
return articles
|
347
|
+
|
348
|
+
end
|
349
|
+
|
350
|
+
def WebService.country_subdivision ( lat, long, radius = 0, maxRows = 1 )
|
351
|
+
|
352
|
+
country_subdivisions = Array.new
|
353
|
+
|
354
|
+
# maxRows is only implemented in the xml version:
|
355
|
+
# http://groups.google.com/group/geonames/browse_thread/thread/f7f1bb2504ed216e
|
356
|
+
# Therefore 'type=xml' is added:
|
357
|
+
url = "/countrySubdivision?a=a&type=xml"
|
358
|
+
|
359
|
+
url = url + "&lat=" + lat.to_s
|
360
|
+
url = url + "&lng=" + long.to_s
|
361
|
+
url = url + "&maxRows=" + maxRows.to_s
|
362
|
+
url = url + "&radius=" + radius.to_s
|
363
|
+
|
364
|
+
res = make_request(url)
|
365
|
+
|
366
|
+
doc = REXML::Document.new res.body
|
367
|
+
|
368
|
+
doc.elements.each("geonames/countrySubdivision") do |element|
|
369
|
+
|
370
|
+
country_subdivision = CountrySubdivision.new
|
371
|
+
|
372
|
+
country_subdivision.country_code = WebService::get_element_child_text( element, 'countryCode' )
|
373
|
+
country_subdivision.country_name = WebService::get_element_child_text( element, 'countryName' )
|
374
|
+
country_subdivision.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' )
|
375
|
+
country_subdivision.admin_name_1 = WebService::get_element_child_text( element, 'adminName1' )
|
376
|
+
country_subdivision.code_fips = WebService::get_element_child_text( element, 'code[@type="FIPS10-4"]')
|
377
|
+
country_subdivision.code_iso = WebService::get_element_child_text( element, 'code[@type="ISO3166-2"]')
|
378
|
+
|
379
|
+
country_subdivisions << country_subdivision
|
380
|
+
|
381
|
+
end
|
382
|
+
|
383
|
+
return country_subdivisions
|
384
|
+
|
385
|
+
end
|
386
|
+
|
387
|
+
def WebService.country_info(country_code)
|
388
|
+
url = "/countryInfo?a=a"
|
389
|
+
|
390
|
+
url += "&country=#{country_code.to_s}"
|
391
|
+
res = make_request(url)
|
392
|
+
|
393
|
+
doc = REXML::Document.new res.body
|
394
|
+
|
395
|
+
return WebService.element_to_country_info(doc.elements["geonames/country"])
|
396
|
+
end
|
397
|
+
|
398
|
+
def WebService.country_code ( lat, long, radius = 0, maxRows = 1 )
|
399
|
+
# maxRows is only implemented in the xml version:
|
400
|
+
# http://groups.google.com/group/geonames/browse_thread/thread/f7f1bb2504ed216e
|
401
|
+
# Therefore 'type=xml' is added:
|
402
|
+
url = "/countrycode?a=a&type=xml"
|
403
|
+
|
404
|
+
countries = Array.new
|
405
|
+
|
406
|
+
url = url + "&lat=" + lat.to_s
|
407
|
+
url = url + "&lng=" + long.to_s
|
408
|
+
url = url + "&maxRows=" + maxRows.to_s
|
409
|
+
url = url + "&radius=" + radius.to_s
|
410
|
+
|
411
|
+
res = make_request(url)
|
412
|
+
|
413
|
+
doc = REXML::Document.new res.body
|
414
|
+
|
415
|
+
doc.elements.each("geonames/country") do |element|
|
416
|
+
|
417
|
+
countries << WebService::element_to_toponym( element )
|
418
|
+
|
419
|
+
end
|
420
|
+
|
421
|
+
return countries
|
422
|
+
|
423
|
+
end
|
424
|
+
|
425
|
+
def WebService.search( search_criteria )
|
426
|
+
#toponym search results to return
|
427
|
+
toponym_sr = ToponymSearchResult.new
|
428
|
+
|
429
|
+
url = "/search?a=a"
|
430
|
+
|
431
|
+
if !search_criteria.q.nil?
|
432
|
+
url = url + "&q=" + CGI::escape( search_criteria.q )
|
433
|
+
end
|
434
|
+
|
435
|
+
if !search_criteria.name_equals.nil?
|
436
|
+
url = url + "&name_equals=" + CGI::escape( search_criteria.name_equals )
|
437
|
+
end
|
438
|
+
|
439
|
+
if !search_criteria.name_starts_with.nil?
|
440
|
+
url = url + "&name_startsWith=" + CGI::escape( search_criteria.name_starts_with )
|
441
|
+
end
|
442
|
+
|
443
|
+
if !search_criteria.name.nil?
|
444
|
+
url = url + "&name=" + CGI::escape( search_criteria.name )
|
445
|
+
end
|
446
|
+
|
447
|
+
if !search_criteria.tag.nil?
|
448
|
+
url = url + "&tag=" + CGI::escape( search_criteria.tag )
|
449
|
+
end
|
450
|
+
|
451
|
+
if !search_criteria.country_code.nil?
|
452
|
+
url = url + "&country=" + CGI::escape( search_criteria.country_code )
|
453
|
+
end
|
454
|
+
|
455
|
+
if !search_criteria.admin_code_1.nil?
|
456
|
+
url = url + "&adminCode1=" + CGI::escape( search_criteria.admin_code_1 )
|
457
|
+
end
|
458
|
+
|
459
|
+
if !search_criteria.language.nil?
|
460
|
+
url = url + "&lang=" + CGI::escape( search_criteria.language )
|
461
|
+
end
|
462
|
+
|
463
|
+
if !search_criteria.feature_class.nil?
|
464
|
+
url = url + "&featureClass=" + CGI::escape( search_criteria.feature_class )
|
465
|
+
end
|
466
|
+
|
467
|
+
if !search_criteria.feature_codes.nil?
|
468
|
+
for feature_code in search_criteria.feature_codes
|
469
|
+
url = url + "&fcode=" + CGI::escape( feature_code )
|
470
|
+
end
|
471
|
+
end
|
472
|
+
|
473
|
+
if !search_criteria.max_rows.nil?
|
474
|
+
url = url + "&maxRows=" + CGI::escape( search_criteria.max_rows )
|
475
|
+
end
|
476
|
+
|
477
|
+
if !search_criteria.start_row.nil?
|
478
|
+
url = url + "&startRow=" + CGI::escape( search_criteria.start_row )
|
479
|
+
end
|
480
|
+
|
481
|
+
if !search_criteria.style.nil?
|
482
|
+
url = url + "&style=" + CGI::escape( search_criteria.style )
|
483
|
+
end
|
484
|
+
|
485
|
+
res = make_request(url)
|
486
|
+
|
487
|
+
doc = REXML::Document.new res.body
|
488
|
+
|
489
|
+
toponym_sr.total_results_count = doc.elements["geonames/totalResultsCount"].text
|
490
|
+
|
491
|
+
doc.elements.each("geonames/geoname") do |element|
|
492
|
+
|
493
|
+
toponym_sr.toponyms << WebService::element_to_toponym( element )
|
494
|
+
|
495
|
+
end
|
496
|
+
|
497
|
+
return toponym_sr
|
498
|
+
end
|
499
|
+
|
500
|
+
end
|
501
|
+
end
|
502
|
+
|
503
|
+
#alias WebService.find_nearby_wikipedia findNearbyWikipedia
|
504
|
+
#alias find_bounding_box_wikipedia findBoundingBoxWikipedia
|
@@ -0,0 +1,42 @@
|
|
1
|
+
#=============================================================================
|
2
|
+
#
|
3
|
+
# Copyright 2007 Adam Wisniewski <adamw@tbcn.ca>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
6
|
+
# use this file except in compliance with the License. You may obtain a copy of
|
7
|
+
# the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
13
|
+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
14
|
+
# License for the specific language governing permissions and limitations under
|
15
|
+
# the License.
|
16
|
+
#
|
17
|
+
#=============================================================================
|
18
|
+
|
19
|
+
module Geonames
|
20
|
+
class WikipediaArticle
|
21
|
+
|
22
|
+
attr :language
|
23
|
+
attr :title
|
24
|
+
attr :summary
|
25
|
+
attr :wikipedia_url
|
26
|
+
attr :feature
|
27
|
+
attr :population
|
28
|
+
attr :elevation
|
29
|
+
attr :latitude
|
30
|
+
attr :longitude
|
31
|
+
attr :thumbnail_img
|
32
|
+
attr :distance
|
33
|
+
|
34
|
+
attr_writer :language, :title, :summary
|
35
|
+
attr_writer :wikipedia_url, :feature, :population
|
36
|
+
attr_writer :elevation, :latitude, :longitude
|
37
|
+
attr_writer :thumbnail_img, :distance
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ppe-ruby-geonames
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.7
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Wisniewski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-07 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: adamw@tbcn.ca
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.markdown
|
24
|
+
files:
|
25
|
+
- README.markdown
|
26
|
+
- lib/timezone.rb
|
27
|
+
- lib/wikipedia_article.rb
|
28
|
+
- lib/geonames.rb
|
29
|
+
- lib/postal_code_search_criteria.rb
|
30
|
+
- lib/intersection.rb
|
31
|
+
- lib/country_subdivision.rb
|
32
|
+
- lib/toponym.rb
|
33
|
+
- lib/toponym_search_result.rb
|
34
|
+
- lib/bounding_box.rb
|
35
|
+
- lib/web_service.rb
|
36
|
+
- lib/postal_code.rb
|
37
|
+
- lib/toponym_search_criteria.rb
|
38
|
+
- lib/tc_country_info.rb
|
39
|
+
- lib/main.rb
|
40
|
+
- lib/Rakefile.rb
|
41
|
+
- lib/address.rb
|
42
|
+
- lib/country_info.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/elecnix/ruby-geonames
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.3.5
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Ruby library for Geonames Web Services (http://www.geonames.org/export/)
|
71
|
+
test_files: []
|
72
|
+
|