geonames 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
File without changes
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'date'
3
+
4
+ SPEC = Gem::Specification.new do |s|
5
+ s.name = "geonames"
6
+ s.version = "0.1.0"
7
+ s.author = "Adam Wisniewski"
8
+ s.email = "adamw@tbcn.ca"
9
+ s.date = s.date = Date.today.to_s
10
+ s.homepage = "http://www.tbcn.ca/geonames"
11
+ s.platform = Gem::Platform::RUBY
12
+ s.summary = "Ruby library for Geonames Web Services (http://www.geonames.org/export/)"
13
+ candidates = Dir.glob("{bin,docs,lib,test}/**/*")
14
+ s.files = Dir.glob('**/*')
15
+ s.require_path = "lib"
16
+ s.has_rdoc = true
17
+ s.extra_rdoc_files = ["README"]
18
+ end
@@ -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'
@@ -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
+
@@ -0,0 +1,32 @@
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
+
26
+ attr_writer :country_code, :country_name
27
+ attr_writer :admin_name_1, :admin_code_1
28
+
29
+ end
30
+ end
31
+
32
+
@@ -0,0 +1,38 @@
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
+
32
+ module Geonames
33
+
34
+ GEONAMES_SERVER = "http://ws.geonames.org"
35
+ USER_AGENT = "geonames ruby webservice client 0.1"
36
+
37
+ end
38
+
@@ -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 Intersection
21
+
22
+ end
23
+ end
24
+
25
+
@@ -0,0 +1,48 @@
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 'geonames'
20
+
21
+ # get list of places near by longitude/longitude location
22
+ places_nearby = Geonames::WebService.find_nearby_place_name 43.900120387, -78.882869834
23
+ p places_nearby
24
+
25
+ # get timezone for longitude/longitude location
26
+ timezone = Geonames::WebService.timezone 43.900120387, -78.882869834
27
+ p timezone
28
+
29
+ # get country code for longitude/longitude location
30
+ country_code = Geonames::WebService.country_code 43.900120387, -78.882869834
31
+ p country_code
32
+
33
+ # get country sub-division info for longitude/longitude location
34
+ country_subdivision = Geonames::WebService.country_subdivision 43.900120387, -78.882869834
35
+ p country_subdivision
36
+
37
+ # get postal codes for a given location
38
+ postal_code_sc = Geonames::PostalCodeSearchCriteria.new
39
+ postal_code_sc.place_name = "Oshawa"
40
+ postal_codes = Geonames::WebService.postal_code_search postal_code_sc
41
+ p postal_codes
42
+
43
+ # get nearby postal codes for a place name
44
+ postal_code_sc = Geonames::PostalCodeSearchCriteria.new
45
+ postal_code_sc.place_name = "Oshawa"
46
+ postal_codes_nearby = Geonames::WebService.find_nearby_postal_codes postal_code_sc
47
+ p postal_codes_nearby
48
+
@@ -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,79 @@
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
+
31
+ attr_writer :postal_code, :place_name, :country_code
32
+ attr_writer :latitude, :longitude, :style
33
+ attr_writer :max_rows, :is_or_operator
34
+
35
+ def initialize
36
+ @is_or_operator = false
37
+ end
38
+
39
+ def to_query_params_string
40
+ url = ''
41
+
42
+ if !@postal_code.nil?
43
+ url = url + "&postalcode=" + CGI::escape( @postal_code )
44
+ end
45
+
46
+ if !@place_name.nil?
47
+ url = url + "&placename=" + CGI::escape( @place_name )
48
+ end
49
+
50
+ if !@latitude.nil?
51
+ url = url + "&lat" + CGI::escape( @latitude )
52
+ end
53
+
54
+ if !@longitude.nil?
55
+ url = url + "&lng" + CGI::escape( @longitude )
56
+ end
57
+
58
+ if !@style.nil?
59
+ url = url + "&style" + CGI::escape( @style )
60
+ end
61
+
62
+ if !@country_code.nil?
63
+ url = url + "&country=" + CGI::escape( @country_code )
64
+ end
65
+
66
+ if !@max_rows.nil?
67
+ url = url + "&maxRows=" + CGI::escape( @max_rows )
68
+ end
69
+
70
+ if @is_or_operator
71
+ url = url + "&operator=OR"
72
+ end
73
+
74
+ url
75
+ end
76
+ end
77
+ end
78
+
79
+
@@ -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
+
@@ -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
+
@@ -0,0 +1,330 @@
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
+
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, 'lat' )
50
+ postal_code.latitude = WebService::get_element_child_float( element, 'lng' )
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_toponym ( element )
59
+ toponym = Toponym.new
60
+
61
+ toponym.name = WebService::get_element_child_text( element, 'name' )
62
+ toponym.alternate_names = WebService::get_element_child_text( element, 'alternateNames' )
63
+ toponym.latitude = WebService::get_element_child_float( element, 'lat' )
64
+ toponym.longitude = WebService::get_element_child_float( element, 'lng' )
65
+ toponym.geoname_id = WebService::get_element_child_text( element, 'geonameId' )
66
+ toponym.country_code = WebService::get_element_child_text( element, 'countryCode' )
67
+ toponym.country_name = WebService::get_element_child_text( element, 'countryName' )
68
+ toponym.feature_class = WebService::get_element_child_text( element, 'fcl' )
69
+ toponym.feature_code = WebService::get_element_child_text( element, 'fcode' )
70
+ toponym.feature_class_name = WebService::get_element_child_text( element, 'fclName' )
71
+ toponym.feature_code_name = WebService::get_element_child_text( element, 'fCodeName' )
72
+ toponym.population = WebService::get_element_child_int( element, 'population' )
73
+ toponym.elevation = WebService::get_element_child_text( element, 'elevation' )
74
+ toponym.distance = WebService::get_element_child_float( element, 'distance' )
75
+
76
+ return toponym
77
+
78
+ end
79
+
80
+ def WebService.postal_code_search( postal_code, place_name, country_code )
81
+ postal_code_sc = PostalCodeSearchCriteria.new
82
+ postal_code_sc.postal_code = postal_code
83
+ postal_code_sc.place_name = place_name
84
+ postal_code_sc.coutry_code = country_code
85
+
86
+ WebService.postal_code_search postal_code_sc
87
+ end
88
+
89
+ def WebService.postal_code_search( search_criteria )
90
+ # postal codes to reutrn
91
+ postal_codes = Array.new
92
+
93
+ url = Geonames::GEONAMES_SERVER + "/postalCodeSearch?a=a"
94
+ url = url + search_criteria.to_query_params_string
95
+
96
+ uri = URI.parse(url)
97
+
98
+ req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
99
+
100
+ res = Net::HTTP.start( uri.host, uri.port ) { |http|
101
+ http.request( req )
102
+ }
103
+
104
+ doc = REXML::Document.new res.body
105
+
106
+ doc.elements.each("geonames/code") do |element|
107
+ postal_codes << WebService::element_to_postal_code( element )
108
+ end
109
+
110
+ postal_codes
111
+
112
+ end
113
+
114
+ def WebService.find_nearby_postal_codes( search_criteria )
115
+ # postal codes to reutrn
116
+ postal_codes = Array.new
117
+
118
+ url = Geonames::GEONAMES_SERVER + "/findNearbyPostalCodes?a=a"
119
+ url = url + search_criteria.to_query_params_string
120
+
121
+ uri = URI.parse(url)
122
+
123
+ req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
124
+
125
+ res = Net::HTTP.start( uri.host, uri.port ) { |http|
126
+ http.request( req )
127
+ }
128
+
129
+ doc = REXML::Document.new res.body
130
+
131
+ doc.elements.each("geonames/code") do |element|
132
+ postal_codes << WebService::element_to_postal_code( element )
133
+ end
134
+
135
+ postal_codes
136
+
137
+ end
138
+
139
+ def WebService.find_nearby_place_name( lat, long )
140
+ places = Array.new
141
+
142
+ url = Geonames::GEONAMES_SERVER + "/findNearbyPlaceName?a=a"
143
+
144
+ url = url + "&lat=" + lat.to_s
145
+ url = url + "&lng=" + long.to_s
146
+
147
+ uri = URI.parse(url)
148
+
149
+ req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
150
+
151
+ res = Net::HTTP.start( uri.host, uri.port ) { |http|
152
+ http.request( req )
153
+ }
154
+
155
+ doc = REXML::Document.new res.body
156
+
157
+ doc.elements.each("geonames/geoname") do |element|
158
+
159
+ places << WebService::element_to_toponym( element )
160
+
161
+ end
162
+
163
+ return places
164
+
165
+ end
166
+
167
+ def WebService.timezone( lat, long )
168
+ timezone = Timezone.new
169
+
170
+ url = Geonames::GEONAMES_SERVER + "/timezone?a=a"
171
+
172
+ url = url + "&lat=" + lat.to_s
173
+ url = url + "&lng=" + long.to_s
174
+
175
+ uri = URI.parse(url)
176
+
177
+ req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
178
+
179
+ res = Net::HTTP.start( uri.host, uri.port ) { |http|
180
+ http.request( req )
181
+ }
182
+
183
+ doc = REXML::Document.new res.body
184
+
185
+ doc.elements.each("geonames/timezone") do |element|
186
+ timezone.timezone_id = WebService::get_element_child_text( element, 'timezoneId' )
187
+ timezone.gmt_offset = WebService::get_element_child_float( element, 'gmtOffset' )
188
+ timezone.dst_offset = WebService::get_element_child_float( element, 'dstOffset' )
189
+ end
190
+
191
+ return timezone
192
+
193
+ end
194
+
195
+ def WebService.country_subdivision ( lat, long )
196
+ country_subdivision = CountrySubdivision.new
197
+
198
+ url = Geonames::GEONAMES_SERVER + "/countrySubdivision?a=a"
199
+
200
+ url = url + "&lat=" + lat.to_s
201
+ url = url + "&lng=" + long.to_s
202
+
203
+ uri = URI.parse(url)
204
+
205
+ req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
206
+
207
+ res = Net::HTTP.start( uri.host, uri.port ) { |http|
208
+ http.request( req )
209
+ }
210
+
211
+ doc = REXML::Document.new res.body
212
+
213
+ doc.elements.each("geonames/countrySubdivision") do |element|
214
+ country_subdivision.country_code = WebService::get_element_child_text( element, 'countryCode' )
215
+ country_subdivision.country_name = WebService::get_element_child_text( element, 'countryName' )
216
+ country_subdivision.admin_code_1 = WebService::get_element_child_text( element, 'adminCode1' )
217
+ country_subdivision.admin_name_1 = WebService::get_element_child_text( element, 'adminName1' )
218
+ end
219
+
220
+ return country_subdivision
221
+
222
+ end
223
+
224
+ def WebService.country_code ( lat, long )
225
+
226
+ url = Geonames::GEONAMES_SERVER + "/countrycode?a=a"
227
+
228
+ url = url + "&lat=" + lat.to_s
229
+ url = url + "&lng=" + long.to_s
230
+
231
+ uri = URI.parse(url)
232
+
233
+ req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
234
+
235
+ res = Net::HTTP.start( uri.host, uri.port ) { |http|
236
+ http.request( req )
237
+ }
238
+
239
+ doc = REXML::Document.new res.body
240
+
241
+ return res.body.strip
242
+
243
+ end
244
+
245
+ def WebService.search( search_criteria )
246
+ #toponym search results to return
247
+ toponym_sr = ToponymSearchResult.new
248
+
249
+ url = Geonames::GEONAMES_SERVER + "/search?a=a"
250
+
251
+ if !search_criteria.q.nil?
252
+ url = url + "&q=" + CGI::escape( search_criteria.q )
253
+ end
254
+
255
+ if !search_criteria.name_equals.nil?
256
+ url = url + "&name_equals=" + CGI::escape( search_criteria.name_equals )
257
+ end
258
+
259
+ if !search_criteria.name_starts_with.nil?
260
+ url = url + "&name_startsWith=" + CGI::escape( search_criteria.name_starts_with )
261
+ end
262
+
263
+ if !search_criteria.name.nil?
264
+ url = url + "&name=" + CGI::escape( search_criteria.name )
265
+ end
266
+
267
+ if !search_criteria.tag.nil?
268
+ url = url + "&tag=" + CGI::escape( search_criteria.tag )
269
+ end
270
+
271
+ if !search_criteria.country_code.nil?
272
+ url = url + "&country=" + CGI::escape( search_criteria.country_code )
273
+ end
274
+
275
+ if !search_criteria.admin_code_1.nil?
276
+ url = url + "&adminCode1=" + CGI::escape( search_criteria.admin_code_1 )
277
+ end
278
+
279
+ if !search_criteria.language.nil?
280
+ url = url + "&lang=" + CGI::escape( search_criteria.language )
281
+ end
282
+
283
+ if !search_criteria.feature_class.nil?
284
+ url = url + "&featureClass=" + CGI::escape( search_criteria.feature_class )
285
+ end
286
+
287
+ if !search_criteria.feature_codes.nil?
288
+ for feature_code in search_criteria.feature_codes
289
+ url = url + "&fcode=" + CGI::escape( feature_code )
290
+ end
291
+ end
292
+
293
+ if !search_criteria.max_rows.nil?
294
+ url = url + "&maxRows=" + CGI::escape( search_criteria.max_rows )
295
+ end
296
+
297
+ if !search_criteria.start_row.nil?
298
+ url = url + "&startRow=" + CGI::escape( search_criteria.start_row )
299
+ end
300
+
301
+ if !search_criteria.style.nil?
302
+ url = url + "&style=" + CGI::escape( search_criteria.style )
303
+ end
304
+
305
+ uri = URI.parse(url)
306
+
307
+ req = Net::HTTP::Get.new(uri.path + '?' + uri.query)
308
+
309
+ res = Net::HTTP.start( uri.host, uri.port ) { |http|
310
+ http.request( req )
311
+ }
312
+
313
+ doc = REXML::Document.new res.body
314
+
315
+ toponym_sr.total_results_count = doc.elements["geonames/totalResultsCount"].text
316
+
317
+ doc.elements.each("geonames/geoname") do |element|
318
+
319
+ toponym_sr.toponyms << WebService::element_to_toponym( element )
320
+
321
+ end
322
+
323
+ return toponym_sr
324
+ end
325
+
326
+
327
+ end
328
+ end
329
+
330
+
@@ -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 WikipediaArticle
21
+
22
+ end
23
+ end
24
+
25
+
File without changes
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project-private xmlns="http://www.netbeans.org/ns/project-private/1">
3
+ <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1"/>
4
+ </project-private>
@@ -0,0 +1,3 @@
1
+ main.file=main.rb
2
+ src.dir=lib
3
+ test.src.dir=test
@@ -0,0 +1,15 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project xmlns="http://www.netbeans.org/ns/project/1">
3
+ <type>org.netbeans.modules.ruby.rubyproject</type>
4
+ <configuration>
5
+ <data xmlns="http://www.netbeans.org/ns/ruby-project/1">
6
+ <name>geonames</name>
7
+ <source-roots>
8
+ <root id="src.dir"/>
9
+ </source-roots>
10
+ <test-roots>
11
+ <root id="test.src.dir"/>
12
+ </test-roots>
13
+ </data>
14
+ </configuration>
15
+ </project>
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: geonames
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2007-02-25 00:00:00 -05:00
8
+ summary: Ruby library for Geonames Web Services (http://www.geonames.org/export/)
9
+ require_paths:
10
+ - lib
11
+ email: adamw@tbcn.ca
12
+ homepage: http://www.tbcn.ca/geonames
13
+ rubyforge_project:
14
+ description:
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Adam Wisniewski
31
+ files:
32
+ - geonames.gemspec
33
+ - lib
34
+ - nbproject
35
+ - README
36
+ - test
37
+ - lib/address.rb
38
+ - lib/country_subdivision.rb
39
+ - lib/geonames.rb
40
+ - lib/intersection.rb
41
+ - lib/main.rb
42
+ - lib/postal_code.rb
43
+ - lib/postal_code_search_criteria.rb
44
+ - lib/Rakefile.rb
45
+ - lib/timezone.rb
46
+ - lib/toponym.rb
47
+ - lib/toponym_search_criteria.rb
48
+ - lib/toponym_search_result.rb
49
+ - lib/web_service.rb
50
+ - lib/wikipedia_article.rb
51
+ - nbproject/private
52
+ - nbproject/project.properties
53
+ - nbproject/project.xml
54
+ - nbproject/private/private.properties
55
+ - nbproject/private/private.xml
56
+ test_files: []
57
+
58
+ rdoc_options: []
59
+
60
+ extra_rdoc_files:
61
+ - README
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ requirements: []
67
+
68
+ dependencies: []
69
+