svlt-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 +87 -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 +60 -0
- data/lib/intersection.rb +42 -0
- data/lib/main.rb +75 -0
- data/lib/postal_code.rb +40 -0
- data/lib/postal_code_search_criteria.rb +84 -0
- data/lib/search_criteria.rb +37 -0
- data/lib/tc_country_info.rb +56 -0
- data/lib/timezone.rb +29 -0
- data/lib/toponym.rb +52 -0
- data/lib/toponym_search_criteria.rb +44 -0
- data/lib/toponym_search_result.rb +33 -0
- data/lib/web_service.rb +510 -0
- data/lib/wikipedia_article.rb +42 -0
- metadata +80 -0
data/lib/toponym.rb
ADDED
@@ -0,0 +1,52 @@
|
|
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
|
+
attr :continent_code
|
37
|
+
attr :admin_code_1
|
38
|
+
attr :admin_code_2
|
39
|
+
attr :admin_code_3
|
40
|
+
attr :timezone
|
41
|
+
attr :score
|
42
|
+
|
43
|
+
attr_writer :geoname_id, :name, :alternate_names, :country_code
|
44
|
+
attr_writer :country_name, :population, :elevation, :feature_class
|
45
|
+
attr_writer :feature_class_name, :feature_code,:feature_code_name
|
46
|
+
attr_writer :latitude, :longitude, :distance
|
47
|
+
attr_writer :continent_code, :admin_code_1, :admin_code_2, :admin_code_3, :timezone, :score
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
@@ -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,510 @@
|
|
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.latitude = WebService::get_element_child_float( element, 'lat' )
|
82
|
+
toponym.longitude = WebService::get_element_child_float( element, 'lng' )
|
83
|
+
toponym.geoname_id = WebService::get_element_child_text( element, 'geonameId' )
|
84
|
+
toponym.country_code = WebService::get_element_child_text( element, 'countryCode' )
|
85
|
+
toponym.country_name = WebService::get_element_child_text( element, 'countryName' )
|
86
|
+
toponym.feature_class = WebService::get_element_child_text( element, 'fcl' )
|
87
|
+
toponym.feature_code = WebService::get_element_child_text( element, 'fcode' )
|
88
|
+
toponym.feature_class_name = WebService::get_element_child_text( element, 'fclName' )
|
89
|
+
toponym.feature_code_name = WebService::get_element_child_text( element, 'fCodeName' )
|
90
|
+
toponym.population = WebService::get_element_child_int( element, 'population' )
|
91
|
+
toponym.alternate_names = WebService::get_element_child_text( element, 'alternateNames' )
|
92
|
+
toponym.elevation = WebService::get_element_child_text( element, 'elevation' )
|
93
|
+
toponym.continent_code = WebService::get_element_child_text( element, 'continentCode' )
|
94
|
+
|
95
|
+
toponym.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' )
|
96
|
+
toponym.admin_code_2 = WebService::get_element_child_text( element, 'adminCode2' )
|
97
|
+
toponym.admin_code_3 = WebService::get_element_child_text( element, 'adminCode3' )
|
98
|
+
toponym.timezone = WebService::get_element_child_text( element, 'timezone' )
|
99
|
+
toponym.score = WebService::get_element_child_float( element, 'score' )
|
100
|
+
|
101
|
+
return toponym
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
def WebService.element_to_intersection ( element )
|
106
|
+
intersection = Intersection.new
|
107
|
+
|
108
|
+
intersection.street_1 = WebService::get_element_child_text( element, 'street1' )
|
109
|
+
intersection.street_2 = WebService::get_element_child_text( element, 'street2' )
|
110
|
+
intersection.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' )
|
111
|
+
intersection.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' )
|
112
|
+
intersection.admin_code_2 = WebService::get_element_child_text( element, 'adminCode2' )
|
113
|
+
intersection.admin_name_1 = WebService::get_element_child_text( element, 'adminName1' )
|
114
|
+
intersection.admin_name_2 = WebService::get_element_child_text( element, 'adminName2' )
|
115
|
+
intersection.country_code = WebService::get_element_child_text( element, 'countryCode' )
|
116
|
+
intersection.distance = WebService::get_element_child_float( element, 'distance' )
|
117
|
+
intersection.longitude = WebService::get_element_child_float( element, 'lat' )
|
118
|
+
intersection.latitude = WebService::get_element_child_float( element, 'lng' )
|
119
|
+
intersection.place_name = WebService::get_element_child_text( element, 'name' )
|
120
|
+
intersection.postal_code = WebService::get_element_child_text( element, 'postalcode' )
|
121
|
+
|
122
|
+
return intersection
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
def WebService.element_to_country_info(element)
|
127
|
+
country_info = CountryInfo.new
|
128
|
+
|
129
|
+
country_info.country_code = WebService.get_element_child_text(element, 'countryCode')
|
130
|
+
country_info.country_name = WebService.get_element_child_text(element, 'countryName')
|
131
|
+
country_info.iso_numeric = WebService.get_element_child_int(element, 'isoNumeric')
|
132
|
+
country_info.iso_alpha_3 = WebService.get_element_child_text(element, 'isoAlpha3')
|
133
|
+
country_info.fips_code = WebService.get_element_child_text(element, 'fipsCode')
|
134
|
+
country_info.continent = WebService.get_element_child_text(element, 'continent')
|
135
|
+
country_info.capital = WebService.get_element_child_text(element, 'capital')
|
136
|
+
country_info.area_sq_km = WebService.get_element_child_float(element, 'areaInSqKm')
|
137
|
+
country_info.population = WebService.get_element_child_int(element, 'population')
|
138
|
+
country_info.currency_code = WebService.get_element_child_text(element, 'currencyCode')
|
139
|
+
#actually an array of the available languages
|
140
|
+
country_info.languages = WebService.get_element_child_text(element, 'languages').split(",")
|
141
|
+
country_info.geoname_id = WebService.get_element_child_int(element, 'geonameId')
|
142
|
+
|
143
|
+
north = WebService.get_element_child_float(element, 'bBoxNorth')
|
144
|
+
south = WebService.get_element_child_float(element, 'bBoxSouth')
|
145
|
+
east = WebService.get_element_child_float(element, 'bBoxEast')
|
146
|
+
west = WebService.get_element_child_float(element, 'bBoxWest')
|
147
|
+
|
148
|
+
country_info.set_bounding_box(north, south, east, west)
|
149
|
+
|
150
|
+
return country_info
|
151
|
+
end
|
152
|
+
|
153
|
+
def WebService.postal_code_search( postal_code, place_name, country_code )
|
154
|
+
postal_code_sc = PostalCodeSearchCriteria.new
|
155
|
+
postal_code_sc.postal_code = postal_code
|
156
|
+
postal_code_sc.place_name = place_name
|
157
|
+
postal_code_sc.country_code = country_code
|
158
|
+
|
159
|
+
WebService.postal_code_search postal_code_sc
|
160
|
+
end
|
161
|
+
|
162
|
+
def WebService.postal_code_search( search_criteria )
|
163
|
+
# postal codes to reutrn
|
164
|
+
postal_codes = Array.new
|
165
|
+
|
166
|
+
url = "/postalCodeSearch?a=a"
|
167
|
+
url = url + search_criteria.to_query_params_string
|
168
|
+
|
169
|
+
res = make_request(url)
|
170
|
+
|
171
|
+
doc = REXML::Document.new res.body
|
172
|
+
|
173
|
+
doc.elements.each("geonames/code") do |element|
|
174
|
+
postal_codes << WebService::element_to_postal_code( element )
|
175
|
+
end
|
176
|
+
|
177
|
+
postal_codes
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
def WebService.find_nearby_postal_codes( search_criteria )
|
182
|
+
# postal codes to reutrn
|
183
|
+
postal_codes = Array.new
|
184
|
+
|
185
|
+
url = "/findNearbyPostalCodes?a=a"
|
186
|
+
url = url + search_criteria.to_query_params_string
|
187
|
+
|
188
|
+
res = make_request(url)
|
189
|
+
|
190
|
+
doc = REXML::Document.new res.body
|
191
|
+
|
192
|
+
doc.elements.each("geonames/code") do |element|
|
193
|
+
postal_codes << WebService::element_to_postal_code( element )
|
194
|
+
end
|
195
|
+
|
196
|
+
postal_codes
|
197
|
+
|
198
|
+
end
|
199
|
+
|
200
|
+
def WebService.find_nearby_place_name( lat, long )
|
201
|
+
places = Array.new
|
202
|
+
|
203
|
+
url = "/findNearbyPlaceName?a=a"
|
204
|
+
|
205
|
+
url = url + "&lat=" + lat.to_s
|
206
|
+
url = url + "&lng=" + long.to_s
|
207
|
+
|
208
|
+
res = make_request(url)
|
209
|
+
|
210
|
+
doc = REXML::Document.new res.body
|
211
|
+
|
212
|
+
doc.elements.each("geonames/geoname") do |element|
|
213
|
+
|
214
|
+
places << WebService::element_to_toponym( element )
|
215
|
+
|
216
|
+
end
|
217
|
+
|
218
|
+
return places
|
219
|
+
|
220
|
+
end
|
221
|
+
|
222
|
+
def WebService.find_nearest_intersection( lat, long )
|
223
|
+
|
224
|
+
url = "/findNearestIntersection?a=a"
|
225
|
+
|
226
|
+
url = url + "&lat=" + lat.to_s
|
227
|
+
url = url + "&lng=" + long.to_s
|
228
|
+
|
229
|
+
res = make_request(url)
|
230
|
+
|
231
|
+
doc = REXML::Document.new res.body
|
232
|
+
|
233
|
+
intersection = []
|
234
|
+
doc.elements.each("geonames/intersection") do |element|
|
235
|
+
|
236
|
+
intersection = WebService::element_to_intersection( element )
|
237
|
+
|
238
|
+
end
|
239
|
+
|
240
|
+
return intersection
|
241
|
+
|
242
|
+
end
|
243
|
+
|
244
|
+
def WebService.timezone( lat, long, *args )
|
245
|
+
res = make_request("/timezone?lat=#{lat.to_s}&lng=#{long.to_s}", args)
|
246
|
+
doc = REXML::Document.new res.body
|
247
|
+
timezone = Timezone.new
|
248
|
+
doc.elements.each("geonames/timezone") do |element|
|
249
|
+
timezone.timezone_id = WebService::get_element_child_text( element, 'timezoneId' )
|
250
|
+
timezone.gmt_offset = WebService::get_element_child_float( element, 'gmtOffset' )
|
251
|
+
timezone.dst_offset = WebService::get_element_child_float( element, 'dstOffset' )
|
252
|
+
end
|
253
|
+
timezone
|
254
|
+
end
|
255
|
+
|
256
|
+
def WebService.make_request(path_and_query, *args)
|
257
|
+
url = Geonames::base_url + path_and_query
|
258
|
+
url = url + "&username=#{Geonames::username}" if Geonames::username
|
259
|
+
options = {
|
260
|
+
:open_timeout => 60,
|
261
|
+
:read_timeout => 60
|
262
|
+
}
|
263
|
+
options.update(args.last.is_a?(::Hash) ? args.pop : {})
|
264
|
+
uri = URI.parse(url)
|
265
|
+
req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
|
266
|
+
Net::HTTP.start(uri.host, uri.port) { |http|
|
267
|
+
http.read_timeout = options[:read_timeout]
|
268
|
+
http.open_timeout = options[:open_timeout]
|
269
|
+
http.request(req)
|
270
|
+
}
|
271
|
+
end
|
272
|
+
|
273
|
+
def WebService.findNearbyWikipedia( hashes )
|
274
|
+
# here for backwards compatibility
|
275
|
+
WebService.find_nearby_wikipedia( hashes )
|
276
|
+
end
|
277
|
+
|
278
|
+
def WebService.find_nearby_wikipedia( hashes )
|
279
|
+
articles = Array.new
|
280
|
+
|
281
|
+
lat = hashes[:lat]
|
282
|
+
long = hashes[:long]
|
283
|
+
lang = hashes[:lang]
|
284
|
+
radius = hashes[:radius]
|
285
|
+
max_rows = hashes[:max_rows]
|
286
|
+
country = hashes[:country]
|
287
|
+
postalcode = hashes[:postalcode]
|
288
|
+
q = hashes[:q]
|
289
|
+
|
290
|
+
url = "/findNearbyWikipedia?a=a"
|
291
|
+
|
292
|
+
if !lat.nil? && !long.nil?
|
293
|
+
url = url + "&lat=" + lat.to_s
|
294
|
+
url = url + "&lng=" + long.to_s
|
295
|
+
url = url + "&radius=" + radius.to_s unless radius.nil?
|
296
|
+
url = url + "&max_rows=" + max_rows.to_s unless max_rows.nil?
|
297
|
+
|
298
|
+
elsif !q.nil?
|
299
|
+
url = url + "&q=" + q
|
300
|
+
url = url + "&radius=" + radius.to_s unless radius.nil?
|
301
|
+
url = url + "&max_rows=" + max_rows.to_s unless max_rows.nil?
|
302
|
+
end
|
303
|
+
|
304
|
+
res = make_request(url)
|
305
|
+
|
306
|
+
doc = REXML::Document.new res.body
|
307
|
+
|
308
|
+
doc.elements.each("geonames/entry") do |element|
|
309
|
+
articles << WebService::element_to_wikipedia_article( element )
|
310
|
+
end
|
311
|
+
|
312
|
+
return articles
|
313
|
+
|
314
|
+
end
|
315
|
+
|
316
|
+
def WebService.findBoundingBoxWikipedia( hashes )
|
317
|
+
# here for backwards compatibility
|
318
|
+
WebService.find_bounding_box_wikipedia( hashes )
|
319
|
+
end
|
320
|
+
|
321
|
+
def WebService.find_bounding_box_wikipedia( hashes )
|
322
|
+
articles = Array.new
|
323
|
+
|
324
|
+
north = hashes[:north]
|
325
|
+
east = hashes[:east]
|
326
|
+
south = hashes[:south]
|
327
|
+
west = hashes[:west]
|
328
|
+
lang = hashes[:lang]
|
329
|
+
radius = hashes[:radius]
|
330
|
+
max_rows = hashes[:max_rows]
|
331
|
+
country = hashes[:country]
|
332
|
+
postalcode = hashes[:postalcode]
|
333
|
+
q = hashes[:q]
|
334
|
+
|
335
|
+
url = "/wikipediaBoundingBox?a=a"
|
336
|
+
|
337
|
+
url = url + "&north=" + north.to_s
|
338
|
+
url = url + "&east=" + east.to_s
|
339
|
+
url = url + "&south=" + south.to_s
|
340
|
+
url = url + "&west=" + west.to_s
|
341
|
+
url = url + "&radius=" + radius.to_s unless radius.nil?
|
342
|
+
url = url + "&max_rows=" + max_rows.to_s unless max_rows.nil?
|
343
|
+
|
344
|
+
res = make_request(url)
|
345
|
+
|
346
|
+
doc = REXML::Document.new res.body
|
347
|
+
|
348
|
+
doc.elements.each("geonames/entry") do |element|
|
349
|
+
articles << WebService::element_to_wikipedia_article( element )
|
350
|
+
end
|
351
|
+
|
352
|
+
return articles
|
353
|
+
|
354
|
+
end
|
355
|
+
|
356
|
+
def WebService.country_subdivision ( lat, long, radius = 0, maxRows = 1 )
|
357
|
+
|
358
|
+
country_subdivisions = Array.new
|
359
|
+
|
360
|
+
# maxRows is only implemented in the xml version:
|
361
|
+
# http://groups.google.com/group/geonames/browse_thread/thread/f7f1bb2504ed216e
|
362
|
+
# Therefore 'type=xml' is added:
|
363
|
+
url = "/countrySubdivision?a=a&type=xml"
|
364
|
+
|
365
|
+
url = url + "&lat=" + lat.to_s
|
366
|
+
url = url + "&lng=" + long.to_s
|
367
|
+
url = url + "&maxRows=" + maxRows.to_s
|
368
|
+
url = url + "&radius=" + radius.to_s
|
369
|
+
|
370
|
+
res = make_request(url)
|
371
|
+
|
372
|
+
doc = REXML::Document.new res.body
|
373
|
+
|
374
|
+
doc.elements.each("geonames/countrySubdivision") do |element|
|
375
|
+
|
376
|
+
country_subdivision = CountrySubdivision.new
|
377
|
+
|
378
|
+
country_subdivision.country_code = WebService::get_element_child_text( element, 'countryCode' )
|
379
|
+
country_subdivision.country_name = WebService::get_element_child_text( element, 'countryName' )
|
380
|
+
country_subdivision.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' )
|
381
|
+
country_subdivision.admin_name_1 = WebService::get_element_child_text( element, 'adminName1' )
|
382
|
+
country_subdivision.code_fips = WebService::get_element_child_text( element, 'code[@type="FIPS10-4"]')
|
383
|
+
country_subdivision.code_iso = WebService::get_element_child_text( element, 'code[@type="ISO3166-2"]')
|
384
|
+
|
385
|
+
country_subdivisions << country_subdivision
|
386
|
+
|
387
|
+
end
|
388
|
+
|
389
|
+
return country_subdivisions
|
390
|
+
|
391
|
+
end
|
392
|
+
|
393
|
+
def WebService.country_info(country_code)
|
394
|
+
url = "/countryInfo?a=a"
|
395
|
+
|
396
|
+
url += "&country=#{country_code.to_s}"
|
397
|
+
res = make_request(url)
|
398
|
+
|
399
|
+
doc = REXML::Document.new res.body
|
400
|
+
|
401
|
+
return WebService.element_to_country_info(doc.elements["geonames/country"])
|
402
|
+
end
|
403
|
+
|
404
|
+
def WebService.country_code ( lat, long, radius = 0, maxRows = 1 )
|
405
|
+
# maxRows is only implemented in the xml version:
|
406
|
+
# http://groups.google.com/group/geonames/browse_thread/thread/f7f1bb2504ed216e
|
407
|
+
# Therefore 'type=xml' is added:
|
408
|
+
url = "/countrycode?a=a&type=xml"
|
409
|
+
|
410
|
+
countries = Array.new
|
411
|
+
|
412
|
+
url = url + "&lat=" + lat.to_s
|
413
|
+
url = url + "&lng=" + long.to_s
|
414
|
+
url = url + "&maxRows=" + maxRows.to_s
|
415
|
+
url = url + "&radius=" + radius.to_s
|
416
|
+
|
417
|
+
res = make_request(url)
|
418
|
+
|
419
|
+
doc = REXML::Document.new res.body
|
420
|
+
|
421
|
+
doc.elements.each("geonames/country") do |element|
|
422
|
+
|
423
|
+
countries << WebService::element_to_toponym( element )
|
424
|
+
|
425
|
+
end
|
426
|
+
|
427
|
+
return countries
|
428
|
+
|
429
|
+
end
|
430
|
+
|
431
|
+
def WebService.search( search_criteria )
|
432
|
+
#toponym search results to return
|
433
|
+
toponym_sr = ToponymSearchResult.new
|
434
|
+
|
435
|
+
url = "/search?a=a"
|
436
|
+
|
437
|
+
if !search_criteria.q.nil?
|
438
|
+
url = url + "&q=" + CGI::escape( search_criteria.q )
|
439
|
+
end
|
440
|
+
|
441
|
+
if !search_criteria.name_equals.nil?
|
442
|
+
url = url + "&name_equals=" + CGI::escape( search_criteria.name_equals )
|
443
|
+
end
|
444
|
+
|
445
|
+
if !search_criteria.name_starts_with.nil?
|
446
|
+
url = url + "&name_startsWith=" + CGI::escape( search_criteria.name_starts_with )
|
447
|
+
end
|
448
|
+
|
449
|
+
if !search_criteria.name.nil?
|
450
|
+
url = url + "&name=" + CGI::escape( search_criteria.name )
|
451
|
+
end
|
452
|
+
|
453
|
+
if !search_criteria.tag.nil?
|
454
|
+
url = url + "&tag=" + CGI::escape( search_criteria.tag )
|
455
|
+
end
|
456
|
+
|
457
|
+
if !search_criteria.country.nil?
|
458
|
+
url = url + "&country=" + CGI::escape( search_criteria.country )
|
459
|
+
end
|
460
|
+
|
461
|
+
if !search_criteria.admin_code_1.nil?
|
462
|
+
url = url + "&adminCode1=" + CGI::escape( search_criteria.admin_code_1 )
|
463
|
+
end
|
464
|
+
|
465
|
+
if !search_criteria.language.nil?
|
466
|
+
url = url + "&lang=" + CGI::escape( search_criteria.language )
|
467
|
+
end
|
468
|
+
|
469
|
+
if !search_criteria.feature_class.nil?
|
470
|
+
url = url + "&featureClass=" + CGI::escape( search_criteria.feature_class )
|
471
|
+
end
|
472
|
+
|
473
|
+
if !search_criteria.feature_codes.nil?
|
474
|
+
for feature_code in search_criteria.feature_codes
|
475
|
+
url = url + "&fcode=" + CGI::escape( feature_code )
|
476
|
+
end
|
477
|
+
end
|
478
|
+
|
479
|
+
if !search_criteria.max_rows.nil?
|
480
|
+
url = url + "&maxRows=" + CGI::escape( search_criteria.max_rows )
|
481
|
+
end
|
482
|
+
|
483
|
+
if !search_criteria.start_row.nil?
|
484
|
+
url = url + "&startRow=" + CGI::escape( search_criteria.start_row )
|
485
|
+
end
|
486
|
+
|
487
|
+
if !search_criteria.style.nil?
|
488
|
+
url = url + "&style=" + CGI::escape( search_criteria.style )
|
489
|
+
end
|
490
|
+
|
491
|
+
res = make_request(url)
|
492
|
+
|
493
|
+
doc = REXML::Document.new res.body
|
494
|
+
|
495
|
+
toponym_sr.total_results_count = doc.elements["geonames/totalResultsCount"].text
|
496
|
+
|
497
|
+
doc.elements.each("geonames/geoname") do |element|
|
498
|
+
|
499
|
+
toponym_sr.toponyms << WebService::element_to_toponym( element )
|
500
|
+
|
501
|
+
end
|
502
|
+
|
503
|
+
return toponym_sr
|
504
|
+
end
|
505
|
+
|
506
|
+
end
|
507
|
+
end
|
508
|
+
|
509
|
+
#alias WebService.find_nearby_wikipedia findNearbyWikipedia
|
510
|
+
#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
|
+
|