ruby-opengeodb 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +5 -0
- data/examples/where_is_the_geocache.rb +2 -4
- data/lib/ruby-opengeodb/location.rb +78 -27
- data/lib/ruby-opengeodb/version.rb +2 -2
- data/test/test_ruby-opengeodb.rb +54 -3
- data/website/index.html +24 -6
- data/website/index.txt +21 -3
- metadata +2 -2
data/History.txt
CHANGED
@@ -9,8 +9,8 @@
|
|
9
9
|
require 'rubygems'
|
10
10
|
require 'ruby-opengeodb'
|
11
11
|
|
12
|
-
PLZ_START =
|
13
|
-
PLZ_END =
|
12
|
+
PLZ_START = 00000
|
13
|
+
PLZ_END = 99999
|
14
14
|
|
15
15
|
Location.db_connect
|
16
16
|
|
@@ -80,5 +80,3 @@ while true do
|
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
|
-
print "RETURN druecken zum Beenden"
|
84
|
-
ans=gets
|
@@ -33,6 +33,8 @@ class Typename < ActiveRecord::Base
|
|
33
33
|
typename = Typename.find(:first, :conditions => "name = 'KFZ-Kennzeichen' AND type_locale = 'de'")
|
34
34
|
when :kanton
|
35
35
|
typename = Typename.find(:first, :conditions => "name = 'Kanton' AND type_locale = 'de_CH'")
|
36
|
+
when :ortsname
|
37
|
+
typename = Typename.find(:first, :conditions => "name = 'Name' AND type_locale = 'de'")
|
36
38
|
else
|
37
39
|
raise 'invalid type: '+type.to_s
|
38
40
|
end
|
@@ -57,14 +59,30 @@ class Location < ActiveRecord::Base
|
|
57
59
|
database="opengeodb",
|
58
60
|
socket="/var/run/mysqld/mysqld.sock")
|
59
61
|
activerecord_connect(adapter,host,username,password,database,socket)
|
62
|
+
@connected = true
|
60
63
|
end
|
61
64
|
|
62
65
|
# eine location ist (vorerst) durch eine bestimmte PLZ bestimmt
|
66
|
+
# - ..new("12345") # als PLZ
|
67
|
+
# - ..new(:plz => "12345") # auch als PLZ
|
68
|
+
# - ..new(:ort => "Hintertupfingen") als Ort
|
63
69
|
def initialize(plz=nil)
|
64
|
-
|
70
|
+
if plz.class == Hash
|
71
|
+
if plz[:plz]
|
72
|
+
@plz = plz[:plz]
|
73
|
+
end
|
74
|
+
elsif plz.class == String
|
75
|
+
@plz = plz
|
76
|
+
end
|
65
77
|
@lat = @lon = nil
|
66
|
-
|
67
|
-
|
78
|
+
|
79
|
+
if plz
|
80
|
+
@lat,@lon = get_lat_lon(@plz) if ! @lat
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def to_s
|
85
|
+
"#{self.plz} #{self.ort} #{self.lat} #{self.lon}"
|
68
86
|
end
|
69
87
|
|
70
88
|
def lat
|
@@ -77,20 +95,49 @@ class Location < ActiveRecord::Base
|
|
77
95
|
@lon
|
78
96
|
end
|
79
97
|
|
80
|
-
def Location.all
|
81
|
-
Location.find(:all)
|
82
|
-
end
|
98
|
+
#def Location.all
|
99
|
+
#Location.find(:all)
|
100
|
+
#end
|
83
101
|
|
84
|
-
|
85
|
-
def Location.
|
86
|
-
|
102
|
+
# Liefert ein Array von loc_id,PLZ,Ort
|
103
|
+
def Location.like(pattern)
|
104
|
+
Location.db_connect if ! @connected
|
105
|
+
orte = Textdata.find_by_sql("SELECT * FROM geodb_textdata WHERE text_val LIKE '#{pattern}' AND text_type=500100000")
|
106
|
+
result = []
|
107
|
+
orte.each {|ort|
|
108
|
+
loc_id = ort.attributes['loc_id']
|
109
|
+
# puts "loc_id: "+loc_id.to_s
|
110
|
+
plzs = Textdata.find_by_sql("SELECT * FROM geodb_textdata WHERE loc_id = '#{loc_id}' AND text_type = 500300000")
|
111
|
+
plzs.each {|plz_rec|
|
112
|
+
plz = plz_rec.attributes['text_val']
|
113
|
+
loc_id = plz_rec.attributes['loc_id']
|
114
|
+
begin
|
115
|
+
location = Location.new(plz)
|
116
|
+
# result << location
|
117
|
+
name = ort.attributes['text_val']
|
118
|
+
result << [loc_id, plz, name]
|
119
|
+
rescue
|
120
|
+
end
|
121
|
+
}
|
122
|
+
}
|
123
|
+
result
|
87
124
|
end
|
88
125
|
|
89
|
-
|
90
|
-
|
126
|
+
# Liefert ein Array von loc_id,PLZ,Ort
|
127
|
+
def Location.by_plz(plz)
|
128
|
+
result = []
|
129
|
+
recs=Textdata.find(:all, :conditions => "text_val = '#{plz}' AND text_type=500300000")
|
130
|
+
recs.each { |rec|
|
131
|
+
loc_id = rec['loc_id']
|
132
|
+
rec2 = Textdata.find(:all, :conditions => "loc_id = '#{loc_id}' AND text_type=500100000")
|
133
|
+
rec2.each { |ortsname_rec|
|
134
|
+
ortsname = ortsname_rec.attributes['text_val']
|
135
|
+
result << [loc_id, plz, ortsname]
|
136
|
+
}
|
137
|
+
}
|
138
|
+
result
|
91
139
|
end
|
92
140
|
|
93
|
-
public
|
94
141
|
def distance_lat_lon(lat,lon)
|
95
142
|
result = Math.acos(
|
96
143
|
(Math.sin(Location.to_rad(self.lat)) * Math.sin(Location.to_rad(lat))) + (Math.cos(Location.to_rad(self.lat)) *
|
@@ -112,18 +159,6 @@ public
|
|
112
159
|
|
113
160
|
result
|
114
161
|
end
|
115
|
-
private
|
116
|
-
|
117
|
-
def get_lat_lon(plz)
|
118
|
-
rec=Textdata.find(:all, :conditions => "text_val = '#{plz}' AND text_type=500100000")
|
119
|
-
loc_id = rec.first['loc_id']
|
120
|
-
rec=Coordinates.find(:all, :conditions => "loc_id = '#{loc_id}'")
|
121
|
-
lat = rec.first.attributes['lat']
|
122
|
-
lon = rec.first.attributes['lon']
|
123
|
-
return lat,lon
|
124
|
-
end
|
125
|
-
|
126
|
-
public
|
127
162
|
|
128
163
|
def ort
|
129
164
|
plz = @plz
|
@@ -134,15 +169,13 @@ public
|
|
134
169
|
return ortsname
|
135
170
|
end
|
136
171
|
|
137
|
-
def
|
172
|
+
def XXXin_radius(radius=10.0)
|
138
173
|
oo = Location.new
|
139
174
|
plzs=Textdata.find(:all, :conditions => "text_type=500300000")
|
140
175
|
result = []
|
141
176
|
plzs.each {|plz|
|
142
|
-
p plz
|
143
177
|
oo = Location.new(plz.text_val)
|
144
178
|
puts "neues Objekt mit PLZ "+oo.plz
|
145
|
-
p oo
|
146
179
|
puts "distance: "+oo.distance(self).to_s
|
147
180
|
ans = gets
|
148
181
|
|
@@ -151,5 +184,23 @@ puts "distance: "+oo.distance(self).to_s
|
|
151
184
|
result
|
152
185
|
end
|
153
186
|
|
187
|
+
private
|
188
|
+
|
189
|
+
def Location.to_deg(rad)
|
190
|
+
rad/0.0174532
|
191
|
+
end
|
192
|
+
|
193
|
+
def Location.to_rad(deg)
|
194
|
+
deg*0.0174532
|
195
|
+
end
|
196
|
+
|
197
|
+
def get_lat_lon(plz)
|
198
|
+
rec=Textdata.find(:all, :conditions => "text_val = '#{plz}' AND text_type=500100000")
|
199
|
+
loc_id = rec.first['loc_id']
|
200
|
+
rec=Coordinates.find(:all, :conditions => "loc_id = '#{loc_id}'")
|
201
|
+
lat = rec.first.attributes['lat']
|
202
|
+
lon = rec.first.attributes['lon']
|
203
|
+
return lat,lon
|
204
|
+
end
|
154
205
|
end
|
155
206
|
|
data/test/test_ruby-opengeodb.rb
CHANGED
@@ -21,7 +21,7 @@ class TestRubyOpengeodb < Test::Unit::TestCase
|
|
21
21
|
#end
|
22
22
|
|
23
23
|
def test_ludwigshafen
|
24
|
-
assert_equal "Ludwigshafen am Rhein", Location.new("67059").ort, "Ludwigshafen am Rhein nicht erhalten"
|
24
|
+
assert_equal "Ludwigshafen am Rhein", Location.new(:plz=>"67059").ort, "Ludwigshafen am Rhein nicht erhalten"
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_berlin_plz
|
@@ -86,6 +86,7 @@ class TestRubyOpengeodb < Test::Unit::TestCase
|
|
86
86
|
def test_typenames
|
87
87
|
typenames = Typename.new
|
88
88
|
assert_equal 500300000, typenames.type_id(:postleitzahl)
|
89
|
+
assert_equal 500100000, typenames.type_id(:ortsname)
|
89
90
|
assert_equal 500300000, typenames.type_id(:plz)
|
90
91
|
assert_raise (RuntimeError) {
|
91
92
|
typenames.type_id(:unbekannt)
|
@@ -115,8 +116,58 @@ class TestRubyOpengeodb < Test::Unit::TestCase
|
|
115
116
|
assert_equal 29949, count # TODO: Anzahl kann sich natuerlich bei neueren Versionen der Datenbank aendert
|
116
117
|
end
|
117
118
|
|
118
|
-
def
|
119
|
-
|
119
|
+
def test_namenssuche
|
120
|
+
locs = Location.like("Holle%")
|
121
|
+
assert_equal [[18580, "31188", "Holle"],
|
122
|
+
[18581, "06179", "Holleben"],
|
123
|
+
[18582, "27616", "Hollen"],
|
124
|
+
[18583, "86568", "Hollenbach"],
|
125
|
+
[18584, "23883", "Hollenbek"],
|
126
|
+
[18585, "21279", "Hollenstedt"],
|
127
|
+
[18586, "56412", "Holler"],
|
128
|
+
[18587, "21723", "Hollern-Twielenfleth"]], locs
|
129
|
+
assert_equal [[19268, "93348", "Kirchdorf"],
|
130
|
+
[19269, "83527", "Kirchdorf"],
|
131
|
+
[19270, "18519", "Kirchdorf"],
|
132
|
+
[19271, "27245", "Kirchdorf"]], Location.like("Kirchdorf")
|
133
|
+
assert_equal [], Location.like("Gibbsnet")
|
134
|
+
assert_equal [[13836,"25927","Aventoft"]] , Location.like("Aventoft")
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_plzsuche
|
138
|
+
locs = Location.by_plz("27616")
|
139
|
+
assert_equal [[14443, "27616", "Beverstedt"],
|
140
|
+
[16659, "27616", "Frelsdorf"],
|
141
|
+
[18041, "27616", "Heerstedt"],
|
142
|
+
[19318, "27616", "Kirchwistedt"],
|
143
|
+
[14726, "27616", "Bokel"],
|
144
|
+
[20499, "27616", "Lunestedt"],
|
145
|
+
[13709, "27616", "Appeln"],
|
146
|
+
[18582, "27616", "Hollen"],
|
147
|
+
[24701, "27616", "Stubben"]], locs
|
148
|
+
assert_equal 9, locs.size
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_berlin_set
|
152
|
+
locs = Location.by_plz("13359")
|
153
|
+
assert_equal [[14356,"13359","Berlin"]], locs
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_empty_plz_set
|
157
|
+
locs = Location.by_plz("AAAAA")
|
158
|
+
assert_equal [], locs
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_plzsuche_lat_lon
|
162
|
+
# das geht nicht, weil ich beim Anlegen eines Objekt keine mehrdeutigen
|
163
|
+
# PLZ verwende:
|
164
|
+
#locs = Location.by_plz("27616")
|
165
|
+
#locs.each {|loc|
|
166
|
+
# l = Location.new(loc[1])
|
167
|
+
# puts l.to_s
|
168
|
+
# assert_in_delta 53.4430232824391, l.lat, 0.0001
|
169
|
+
# assert_in_delta 8.82614916550224, l.lon, 0.0001
|
170
|
+
#}
|
120
171
|
end
|
121
172
|
|
122
173
|
end
|
data/website/index.html
CHANGED
@@ -33,7 +33,7 @@
|
|
33
33
|
<h1>ruby opengeodb</h1>
|
34
34
|
<div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/ruby-opengeodb"; return false'>
|
35
35
|
<p>Get Version</p>
|
36
|
-
<a href="http://rubyforge.org/projects/ruby-opengeodb" class="numbers">0.0
|
36
|
+
<a href="http://rubyforge.org/projects/ruby-opengeodb" class="numbers">0.1.0</a>
|
37
37
|
</div>
|
38
38
|
<h1>→ ‘ruby-opengeodb’</h1>
|
39
39
|
|
@@ -64,6 +64,16 @@ Probleme: <a href="http://www.google.com/search?q=opengeodb&ie=utf-8&oe=
|
|
64
64
|
|
65
65
|
<pre syntax="ruby">sudo gem install ruby-opengeodb</pre>
|
66
66
|
|
67
|
+
<p>Um das Gem verwenden zu können, muß die OpenGeoDB Datenbank installiert sein – entweder lokal oder auf einem anderen System auf dem Zugriff auf die
|
68
|
+
Datenbank moeglich ist.</p>
|
69
|
+
|
70
|
+
|
71
|
+
<p>Ich habe mit einer MySQL-Datenbank getestet. Andere Datenbanken sollten auch
|
72
|
+
problemlos möglich sein, solange das Gem Active-Record damit umgehen kann.
|
73
|
+
Ein Ändern der Connect-Parameter sollte eigentlich ausreichen, um eine andere
|
74
|
+
Datenbank zu verwenden. Falls nicht, schreibt mir eine eMail mit der Problembeschreibung oder sogar einer Lösung dafür!</p>
|
75
|
+
|
76
|
+
|
67
77
|
<h2>The basics</h2>
|
68
78
|
|
69
79
|
|
@@ -117,6 +127,17 @@ puts "Entfernung: " + berlin.distance(ludwigshafen).to_s + " km"
|
|
117
127
|
# Eine Distanz kann auch zu einer definierten Koordinate berechnet werden
|
118
128
|
|
119
129
|
puts "Entfernung: " + berlin.distance_lat_lon(50.0, 10.0).to_s + " km"
|
130
|
+
|
131
|
+
# liefert ein Array aller passenden Orte einer PLZ
|
132
|
+
|
133
|
+
liste = Location.by_plz("27616") # mehrere Orte haben diese PLZ
|
134
|
+
# liefert ein Array mit [loc_id, plz, ort]
|
135
|
+
# 'loc_id' ist ein interner Key bei OpenGeoDB
|
136
|
+
|
137
|
+
liste = Location.like("Holle%") # alle Orte, die mit Holle beginnen
|
138
|
+
# liefert ein Array mit [loc_id, plz, ort]
|
139
|
+
# s. o.
|
140
|
+
|
120
141
|
</code>
|
121
142
|
</pre>
|
122
143
|
|
@@ -132,15 +153,12 @@ methode im eigenlichen Sinne, kann aber ganz nützlich sein, wenn man wissen
|
|
132
153
|
<p>Was wollt ihr unbedingt noch haben – schreibt mir an die untenstehende eMail-Adresse!</p>
|
133
154
|
|
134
155
|
|
135
|
-
<h2>
|
156
|
+
<h2>Mailinglist</h2>
|
136
157
|
|
137
158
|
|
138
159
|
<p><a href="http://groups.google.com/group/ruby-opengeodb">http://groups.google.com/group/ruby-opengeodb</a></p>
|
139
160
|
|
140
161
|
|
141
|
-
<p><span class="caps">TODO</span> – create Google Group – ruby-opengeodb</p>
|
142
|
-
|
143
|
-
|
144
162
|
<h2>How to submit patches</h2>
|
145
163
|
|
146
164
|
|
@@ -161,7 +179,7 @@ methode im eigenlichen Sinne, kann aber ganz nützlich sein, wenn man wissen
|
|
161
179
|
|
162
180
|
<p>Comments are welcome. Send an email to <a href="mailto:thopre@gmail.com">Thomas Preymesser</a>.</p>
|
163
181
|
<p class="coda">
|
164
|
-
<a href="mailto:drnicwilliams@gmail.com">Dr Nic</a>,
|
182
|
+
<a href="mailto:drnicwilliams@gmail.com">Dr Nic</a>, 30th July 2007<br>
|
165
183
|
Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
|
166
184
|
</p>
|
167
185
|
</div>
|
data/website/index.txt
CHANGED
@@ -23,6 +23,14 @@ h2. Installing
|
|
23
23
|
|
24
24
|
<pre syntax="ruby">sudo gem install ruby-opengeodb</pre>
|
25
25
|
|
26
|
+
Um das Gem verwenden zu können, muß die OpenGeoDB Datenbank installiert sein - entweder lokal oder auf einem anderen System auf dem Zugriff auf die
|
27
|
+
Datenbank moeglich ist.
|
28
|
+
|
29
|
+
Ich habe mit einer MySQL-Datenbank getestet. Andere Datenbanken sollten auch
|
30
|
+
problemlos möglich sein, solange das Gem Active-Record damit umgehen kann.
|
31
|
+
Ein Ändern der Connect-Parameter sollte eigentlich ausreichen, um eine andere
|
32
|
+
Datenbank zu verwenden. Falls nicht, schreibt mir eine eMail mit der Problembeschreibung oder sogar einer Lösung dafür!
|
33
|
+
|
26
34
|
h2. The basics
|
27
35
|
|
28
36
|
Da ich mit Test-Driven-Development (TDD) arbeite, sollten für jede Funktion
|
@@ -75,6 +83,18 @@ puts "Entfernung: " + berlin.distance(ludwigshafen).to_s + " km"
|
|
75
83
|
|
76
84
|
|
77
85
|
puts "Entfernung: " + berlin.distance_lat_lon(50.0, 10.0).to_s + " km"
|
86
|
+
|
87
|
+
# liefert ein Array aller passenden Orte einer PLZ
|
88
|
+
|
89
|
+
liste = Location.by_plz("27616") # mehrere Orte haben diese PLZ
|
90
|
+
# liefert ein Array mit [loc_id, plz, ort]
|
91
|
+
# 'loc_id' ist ein interner Key bei OpenGeoDB
|
92
|
+
|
93
|
+
|
94
|
+
liste = Location.like("Holle%") # alle Orte, die mit Holle beginnen
|
95
|
+
# liefert ein Array mit [loc_id, plz, ort]
|
96
|
+
# s. o.
|
97
|
+
|
78
98
|
</code>
|
79
99
|
</pre>
|
80
100
|
|
@@ -87,12 +107,10 @@ methode im eigenlichen Sinne, kann aber ganz nützlich sein, wenn man wissen
|
|
87
107
|
|
88
108
|
Was wollt ihr unbedingt noch haben - schreibt mir an die untenstehende eMail-Adresse!
|
89
109
|
|
90
|
-
h2.
|
110
|
+
h2. Mailinglist
|
91
111
|
|
92
112
|
"http://groups.google.com/group/ruby-opengeodb":http://groups.google.com/group/ruby-opengeodb
|
93
113
|
|
94
|
-
TODO - create Google Group - ruby-opengeodb
|
95
|
-
|
96
114
|
h2. How to submit patches
|
97
115
|
|
98
116
|
Read the "8 steps for fixing other people's code":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/ and for section "8b: Submit patch to Google Groups":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/#8b-google-groups, use the Google Group above.
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: ruby-opengeodb
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0
|
7
|
-
date: 2007-07-
|
6
|
+
version: 0.1.0
|
7
|
+
date: 2007-07-30 00:00:00 +02:00
|
8
8
|
summary: access to opengeodb Database
|
9
9
|
require_paths:
|
10
10
|
- lib
|