ruby-opengeodb 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/License.txt +20 -0
- data/Manifest.txt +20 -0
- data/README.txt +3 -0
- data/Rakefile +125 -0
- data/examples/example1.rb +26 -0
- data/examples/example2.rb +20 -0
- data/lib/ruby-opengeodb.rb +6 -0
- data/lib/ruby-opengeodb/location.rb +115 -0
- data/lib/ruby-opengeodb/opengeodb_connect.rb +25 -0
- data/lib/ruby-opengeodb/version.rb +9 -0
- data/scripts/txt2html +67 -0
- data/setup.rb +1585 -0
- data/test/test_helper.rb +2 -0
- data/test/test_ruby-opengeodb.rb +82 -0
- data/website/index.html +168 -0
- data/website/index.txt +103 -0
- data/website/javascripts/rounded_corners_lite.inc.js +285 -0
- data/website/stylesheets/screen.css +138 -0
- data/website/template.rhtml +48 -0
- metadata +79 -0
data/History.txt
ADDED
data/License.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007 FIXME full name
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
History.txt
|
2
|
+
License.txt
|
3
|
+
Manifest.txt
|
4
|
+
README.txt
|
5
|
+
Rakefile
|
6
|
+
examples/example1.rb
|
7
|
+
examples/example2.rb
|
8
|
+
lib/ruby-opengeodb.rb
|
9
|
+
lib/ruby-opengeodb/version.rb
|
10
|
+
lib/ruby-opengeodb/location.rb
|
11
|
+
lib/ruby-opengeodb/opengeodb_connect.rb
|
12
|
+
scripts/txt2html
|
13
|
+
setup.rb
|
14
|
+
test/test_helper.rb
|
15
|
+
test/test_ruby-opengeodb.rb
|
16
|
+
website/index.html
|
17
|
+
website/index.txt
|
18
|
+
website/javascripts/rounded_corners_lite.inc.js
|
19
|
+
website/stylesheets/screen.css
|
20
|
+
website/template.rhtml
|
data/README.txt
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rake/packagetask'
|
6
|
+
require 'rake/gempackagetask'
|
7
|
+
require 'rake/rdoctask'
|
8
|
+
require 'rake/contrib/rubyforgepublisher'
|
9
|
+
require 'fileutils'
|
10
|
+
require 'hoe'
|
11
|
+
|
12
|
+
include FileUtils
|
13
|
+
require File.join(File.dirname(__FILE__), 'lib', 'ruby-opengeodb', 'version')
|
14
|
+
|
15
|
+
AUTHOR = 'Thomas Preymesser' # can also be an array of Authors
|
16
|
+
EMAIL = "thopre@gmail.com"
|
17
|
+
DESCRIPTION = "access to opengeodb Database"
|
18
|
+
GEM_NAME = 'ruby-opengeodb' # what ppl will type to install your gem
|
19
|
+
|
20
|
+
@config_file = "~/.rubyforge/user-config.yml"
|
21
|
+
@config = nil
|
22
|
+
def rubyforge_username
|
23
|
+
unless @config
|
24
|
+
begin
|
25
|
+
@config = YAML.load(File.read(File.expand_path(@config_file)))
|
26
|
+
rescue
|
27
|
+
puts <<-EOS
|
28
|
+
ERROR: No rubyforge config file found: #{@config_file}"
|
29
|
+
Run 'rubyforge setup' to prepare your env for access to Rubyforge
|
30
|
+
- See http://newgem.rubyforge.org/rubyforge.html for more details
|
31
|
+
EOS
|
32
|
+
exit
|
33
|
+
end
|
34
|
+
end
|
35
|
+
@rubyforge_username ||= @config["username"]
|
36
|
+
end
|
37
|
+
|
38
|
+
RUBYFORGE_PROJECT = 'ruby-opengeodb' # The unix name for your project
|
39
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
40
|
+
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
|
41
|
+
|
42
|
+
NAME = "ruby-opengeodb"
|
43
|
+
REV = nil
|
44
|
+
# UNCOMMENT IF REQUIRED:
|
45
|
+
# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
|
46
|
+
VERS = RubyOpengeodb::VERSION::STRING + (REV ? ".#{REV}" : "")
|
47
|
+
CLEAN.include ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store']
|
48
|
+
RDOC_OPTS = ['--quiet', '--title', 'ruby-opengeodb documentation',
|
49
|
+
"--opname", "index.html",
|
50
|
+
"--line-numbers",
|
51
|
+
"--main", "README",
|
52
|
+
"--inline-source"]
|
53
|
+
|
54
|
+
class Hoe
|
55
|
+
def extra_deps
|
56
|
+
@extra_deps.reject { |x| Array(x).first == 'hoe' }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Generate all the Rake tasks
|
61
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
62
|
+
hoe = Hoe.new(GEM_NAME, VERS) do |p|
|
63
|
+
p.author = AUTHOR
|
64
|
+
p.description = DESCRIPTION
|
65
|
+
p.email = EMAIL
|
66
|
+
p.summary = DESCRIPTION
|
67
|
+
p.url = HOMEPATH
|
68
|
+
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
|
69
|
+
p.test_globs = ["test/**/test_*.rb"]
|
70
|
+
p.clean_globs |= CLEAN #An array of file patterns to delete on clean.
|
71
|
+
|
72
|
+
# == Optional
|
73
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
74
|
+
p.extra_deps = [
|
75
|
+
['activerecord', '>= 1.0.0'],
|
76
|
+
] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
|
77
|
+
#p.spec_extras = {} # A hash of extra values to set in the gemspec.
|
78
|
+
end
|
79
|
+
|
80
|
+
CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\n\n")
|
81
|
+
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
|
82
|
+
hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
|
83
|
+
|
84
|
+
desc 'Generate website files'
|
85
|
+
task :website_generate do
|
86
|
+
Dir['website/**/*.txt'].each do |txt|
|
87
|
+
sh %{ ruby scripts/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
desc 'Upload website files to rubyforge'
|
92
|
+
task :website_upload do
|
93
|
+
host = "#{rubyforge_username}@rubyforge.org"
|
94
|
+
remote_dir = "/var/www/gforge-projects/#{PATH}/"
|
95
|
+
local_dir = 'website'
|
96
|
+
sh %{rsync -aCv #{local_dir}/ #{host}:#{remote_dir}}
|
97
|
+
end
|
98
|
+
|
99
|
+
desc 'Generate and upload website files'
|
100
|
+
task :website => [:website_generate, :website_upload, :publish_docs]
|
101
|
+
|
102
|
+
desc 'Release the website and new gem version'
|
103
|
+
task :deploy => [:check_version, :website, :release] do
|
104
|
+
puts "Remember to create SVN tag:"
|
105
|
+
puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
|
106
|
+
"svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
|
107
|
+
puts "Suggested comment:"
|
108
|
+
puts "Tagging release #{CHANGES}"
|
109
|
+
end
|
110
|
+
|
111
|
+
desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
|
112
|
+
task :local_deploy => [:website_generate, :install_gem]
|
113
|
+
|
114
|
+
task :check_version do
|
115
|
+
unless ENV['VERSION']
|
116
|
+
puts 'Must pass a VERSION=x.y.z release version'
|
117
|
+
exit
|
118
|
+
end
|
119
|
+
unless ENV['VERSION'] == VERS
|
120
|
+
puts "Please update your version.rb to match the release version, currently #{VERS}"
|
121
|
+
exit
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'ruby-opengeodb'
|
3
|
+
|
4
|
+
# Verbindung zur Datenbank aufbauen
|
5
|
+
Location.db_connect
|
6
|
+
|
7
|
+
# Achtung: die Laengen- und Breitengrade geben die ungefaehre Mitte des PLZ-Bereichs an und koennen leicht von den eigenen Koordinaten abweichen, wenn man in diesem PLZ-Gebiet wohnt.
|
8
|
+
|
9
|
+
locations = %w(13359 67059 90459 56068 69115)
|
10
|
+
# = Berlin, Ludwigshafen, Nuernberg, Koblenz, Heidelberg
|
11
|
+
|
12
|
+
locations.each { |plz|
|
13
|
+
location = Location.new(plz)
|
14
|
+
puts "PLZ: #{plz} Ort: #{location.ort} Lat: #{location.lat} Lon: #{location.lon}"
|
15
|
+
}
|
16
|
+
# Entfernung (Luftlinie) von Ludwigshafen -> Berlin
|
17
|
+
|
18
|
+
ludwigshafen = Location.new("67059")
|
19
|
+
berlin = Location.new("13359")
|
20
|
+
|
21
|
+
puts "Entfernung: " + ludwigshafen.distance(berlin).to_s + " km"
|
22
|
+
|
23
|
+
# zur Kontrolle, der umgekehrte Weg muss (zumindest in diesem Universum) gleich
|
24
|
+
# lang sein
|
25
|
+
|
26
|
+
puts "Entfernung: " + berlin.distance(ludwigshafen).to_s + " km"
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'ruby-opengeodb'
|
3
|
+
|
4
|
+
# Verbindung zur Datenbank aufbauen
|
5
|
+
Location.db_connect
|
6
|
+
|
7
|
+
# Achtung: die Laengen- und Breitengrade geben die ungefaehre Mitte des PLZ-Bereichs an und koennen leicht von den eigenen Koordinaten abweichen, wenn man in diesem PLZ-Gebiet wohnt.
|
8
|
+
|
9
|
+
nr=1
|
10
|
+
while nr<=99999
|
11
|
+
plz=sprintf("%05d",nr)
|
12
|
+
# puts plz
|
13
|
+
begin
|
14
|
+
location = Location.new(plz)
|
15
|
+
puts "PLZ: #{plz} Ort: #{location.ort} Lat: #{location.lat} Lon: #{location.lon}"
|
16
|
+
rescue
|
17
|
+
# keine PLZ in Datenbank vorhanden => ignorieren und weitermachen
|
18
|
+
end
|
19
|
+
nr += 1
|
20
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
|
2
|
+
class Textdata < ActiveRecord::Base
|
3
|
+
set_table_name "geodb_textdata"
|
4
|
+
end
|
5
|
+
|
6
|
+
class Coordinates < ActiveRecord::Base
|
7
|
+
set_table_name "geodb_coordinates"
|
8
|
+
end # class
|
9
|
+
|
10
|
+
class Changelog < ActiveRecord::Base
|
11
|
+
set_table_name "geodb_changelog"
|
12
|
+
|
13
|
+
def changelogs
|
14
|
+
@changelogs = Changelog.find(:all, :order => :datum)
|
15
|
+
@changelogs
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
"#{id}|#{datum}|#{beschreibung}|#{autor}|#{version}"
|
20
|
+
end
|
21
|
+
end # class
|
22
|
+
|
23
|
+
class Location
|
24
|
+
|
25
|
+
attr_reader :plz
|
26
|
+
|
27
|
+
def Location.db_connect(adapter="mysql",
|
28
|
+
host="localhost",
|
29
|
+
username="root",
|
30
|
+
password=nil,
|
31
|
+
database="opengeodb",
|
32
|
+
socket="/var/run/mysqld/mysqld.sock")
|
33
|
+
activerecord_connect(adapter,host,username,password,database,socket)
|
34
|
+
end
|
35
|
+
|
36
|
+
# eine location ist (vorerst) durch eine bestimmte PLZ bestimmt
|
37
|
+
def initialize(plz=nil)
|
38
|
+
@plz = plz
|
39
|
+
@lat = @lon = nil
|
40
|
+
#activerecord_connect
|
41
|
+
@lat,@lon = get_lat_lon(@plz) if ! @lat
|
42
|
+
end
|
43
|
+
|
44
|
+
def lat
|
45
|
+
@lat,@lon = get_lat_lon(@plz) if ! @lat
|
46
|
+
@lat
|
47
|
+
end
|
48
|
+
|
49
|
+
def lon
|
50
|
+
@lat,@lon = get_lat_lon(@plz) if ! @lon
|
51
|
+
@lon
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
def Location.to_deg(rad)
|
56
|
+
rad/0.0174532
|
57
|
+
end
|
58
|
+
|
59
|
+
def Location.to_rad(deg)
|
60
|
+
deg*0.0174532
|
61
|
+
end
|
62
|
+
|
63
|
+
public
|
64
|
+
def distance(other)
|
65
|
+
unless other.kind_of?(Location)
|
66
|
+
raise ArgumentError, "Argument ist keine 'Location'"
|
67
|
+
end
|
68
|
+
result = Math.acos(
|
69
|
+
(Math.sin(Location.to_rad(self.lat)) * Math.sin(Location.to_rad(other.lat))) + (Math.cos(Location.to_rad(self.lat)) *
|
70
|
+
Math.cos(Location.to_rad(other.lat)) *
|
71
|
+
Math.cos(Location.to_rad(self.lon) - Location.to_rad(other.lon)))) * 6371.0
|
72
|
+
|
73
|
+
result
|
74
|
+
end
|
75
|
+
private
|
76
|
+
|
77
|
+
def get_lat_lon(plz)
|
78
|
+
rec=Textdata.find(:all, :conditions => "text_val = '#{plz}' AND text_type=500100000")
|
79
|
+
loc_id = rec.first['loc_id']
|
80
|
+
rec=Coordinates.find(:all, :conditions => "loc_id = '#{loc_id}'")
|
81
|
+
lat = rec.first.attributes['lat']
|
82
|
+
lon = rec.first.attributes['lon']
|
83
|
+
return lat,lon
|
84
|
+
end
|
85
|
+
|
86
|
+
public
|
87
|
+
|
88
|
+
def ort
|
89
|
+
plz = @plz
|
90
|
+
rec=Textdata.find(:all, :conditions => "text_val = '#{plz}' AND text_type=500300000")
|
91
|
+
loc_id = rec.first['loc_id']
|
92
|
+
rec=Textdata.find(:all, :conditions => "loc_id = '#{loc_id}' AND text_type=500100000")
|
93
|
+
ortsname = rec.first['text_val']
|
94
|
+
return ortsname
|
95
|
+
end
|
96
|
+
|
97
|
+
def in_radius(radius=10.0)
|
98
|
+
oo = Location.new
|
99
|
+
plzs=Textdata.find(:all, :conditions => "text_type=500300000")
|
100
|
+
result = []
|
101
|
+
plzs.each {|plz|
|
102
|
+
p plz
|
103
|
+
oo = Location.new(plz.text_val)
|
104
|
+
puts "neues Objekt mit PLZ "+oo.plz
|
105
|
+
p oo
|
106
|
+
puts "distance: "+oo.distance(self).to_s
|
107
|
+
ans = gets
|
108
|
+
|
109
|
+
#result << oo if oo.distance(self) <= radius
|
110
|
+
}
|
111
|
+
result
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_record'
|
3
|
+
|
4
|
+
def activerecord_connect(adapter="mysql",
|
5
|
+
host="localhost",
|
6
|
+
username="root",
|
7
|
+
password=nil,
|
8
|
+
database="opengeodb",
|
9
|
+
socket="/var/run/mysqld/mysqld.sock")
|
10
|
+
if password
|
11
|
+
ActiveRecord::Base.establish_connection(:adapter => adapter,
|
12
|
+
:host => host,
|
13
|
+
:username => username,
|
14
|
+
:database => database,
|
15
|
+
:socket => socket)
|
16
|
+
else
|
17
|
+
ActiveRecord::Base.establish_connection(:adapter => adapter,
|
18
|
+
:host => host,
|
19
|
+
:username => username,
|
20
|
+
:password => password,
|
21
|
+
:database => database,
|
22
|
+
:socket => socket)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
data/scripts/txt2html
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'redcloth'
|
5
|
+
require 'syntax/convertors/html'
|
6
|
+
require 'erb'
|
7
|
+
require File.dirname(__FILE__) + '/../lib/ruby-opengeodb/version.rb'
|
8
|
+
|
9
|
+
version = RubyOpengeodb::VERSION::STRING
|
10
|
+
download = 'http://rubyforge.org/projects/ruby-opengeodb'
|
11
|
+
|
12
|
+
class Fixnum
|
13
|
+
def ordinal
|
14
|
+
# teens
|
15
|
+
return 'th' if (10..19).include?(self % 100)
|
16
|
+
# others
|
17
|
+
case self % 10
|
18
|
+
when 1: return 'st'
|
19
|
+
when 2: return 'nd'
|
20
|
+
when 3: return 'rd'
|
21
|
+
else return 'th'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Time
|
27
|
+
def pretty
|
28
|
+
return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def convert_syntax(syntax, source)
|
33
|
+
return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
|
34
|
+
end
|
35
|
+
|
36
|
+
if ARGV.length >= 1
|
37
|
+
src, template = ARGV
|
38
|
+
template ||= File.dirname(__FILE__) + '/../website/template.rhtml'
|
39
|
+
|
40
|
+
else
|
41
|
+
puts("Usage: #{File.split($0).last} source.txt [template.rhtml] > output.html")
|
42
|
+
exit!
|
43
|
+
end
|
44
|
+
|
45
|
+
template = ERB.new(File.open(template).read)
|
46
|
+
|
47
|
+
title = nil
|
48
|
+
body = nil
|
49
|
+
File.open(src) do |fsrc|
|
50
|
+
title_text = fsrc.readline
|
51
|
+
body_text = fsrc.read
|
52
|
+
syntax_items = []
|
53
|
+
body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</>!m){
|
54
|
+
ident = syntax_items.length
|
55
|
+
element, syntax, source = $1, $2, $3
|
56
|
+
syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
|
57
|
+
"syntax-temp-#{ident}"
|
58
|
+
}
|
59
|
+
title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
|
60
|
+
body = RedCloth.new(body_text).to_html
|
61
|
+
body.gsub!(%r!(?:<pre><code>)?syntax-temp-(d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
|
62
|
+
end
|
63
|
+
stat = File.stat(src)
|
64
|
+
created = stat.ctime
|
65
|
+
modified = stat.mtime
|
66
|
+
|
67
|
+
$stdout << template.result(binding)
|