Class: Location

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/ruby-opengeodb/location.rb

Overview

class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plz = nil) ⇒ Location

eine location ist (vorerst) durch eine bestimmte PLZ bestimmt

  • ..new(“12345”) # als PLZ

  • ..new(:plz => “12345”) # auch als PLZ

  • ..new(:ort => “Hintertupfingen”) als Ort



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ruby-opengeodb/location.rb', line 69

def initialize(plz=nil)
  if plz.class == Hash
    if plz[:plz]
      @plz = plz[:plz]
    end
  elsif plz.class == String
    @plz = plz
  end
  @lat = @lon = nil
  
  if plz
    @lat,@lon = get_lat_lon(@plz) if ! @lat 
  end
end

Instance Attribute Details

#plzObject (readonly)

Returns the value of attribute plz.



53
54
55
# File 'lib/ruby-opengeodb/location.rb', line 53

def plz
  @plz
end

Class Method Details

.by_plz(plz) ⇒ Object

Liefert ein Array von loc_id,PLZ,Ort



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ruby-opengeodb/location.rb', line 127

def Location.by_plz(plz)
  result = []
  recs=Textdata.find(:all, :conditions => "text_val = '#{plz}' AND text_type=500300000")
  recs.each { |rec|
    loc_id = rec['loc_id']
    rec2 = Textdata.find(:all, :conditions => "loc_id = '#{loc_id}' AND text_type=500100000")
    rec2.each { |ortsname_rec|
      ortsname = ortsname_rec.attributes['text_val']
      result << [loc_id, plz, ortsname]
    }
  }
  result
end

.db_connect(adapter = "mysql", host = "localhost", username = "root", password = nil, database = "opengeodb", socket = "/var/run/mysqld/mysqld.sock") ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/ruby-opengeodb/location.rb', line 55

def Location.db_connect(adapter="mysql",
                        host="localhost",
                        username="root",
                        password=nil,
                        database="opengeodb",
                        socket="/var/run/mysqld/mysqld.sock")
  activerecord_connect(adapter,host,username,password,database,socket)
  @connected = true
end

.like(pattern) ⇒ Object

Liefert ein Array von loc_id,PLZ,Ort



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ruby-opengeodb/location.rb', line 103

def Location.like(pattern)
  Location.db_connect if ! @connected
  orte = Textdata.find_by_sql("SELECT * FROM geodb_textdata WHERE text_val LIKE '#{pattern}' AND text_type=500100000")
  result = []
  orte.each {|ort|
    loc_id = ort.attributes['loc_id']
    # puts "loc_id: "+loc_id.to_s
    plzs = Textdata.find_by_sql("SELECT * FROM geodb_textdata WHERE loc_id = '#{loc_id}' AND text_type = 500300000")
    plzs.each {|plz_rec|
      plz = plz_rec.attributes['text_val']
      loc_id = plz_rec.attributes['loc_id']
      begin
      location = Location.new(plz)
      # result << location
      name = ort.attributes['text_val']
      result << [loc_id, plz, name]
      rescue
      end
    }
  }
  result
end

Instance Method Details

#distance(other) ⇒ Object

TODO: hier distance_lat_lon rufen?



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/ruby-opengeodb/location.rb', line 151

def distance(other)
  unless other.kind_of?(Location)
    raise ArgumentError, "Argument ist keine 'Location'"
  end
  result = Math.acos(
               (Math.sin(Location.to_rad(self.lat)) * Math.sin(Location.to_rad(other.lat))) + (Math.cos(Location.to_rad(self.lat)) * 
                   Math.cos(Location.to_rad(other.lat)) * 
                   Math.cos(Location.to_rad(self.lon) - Location.to_rad(other.lon)))) * 6371.0

  result
end

#distance_lat_lon(lat, lon) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/ruby-opengeodb/location.rb', line 141

def distance_lat_lon(lat,lon)
  result = Math.acos(
               (Math.sin(Location.to_rad(self.lat)) * Math.sin(Location.to_rad(lat))) + (Math.cos(Location.to_rad(self.lat)) * 
                   Math.cos(Location.to_rad(lat)) * 
                   Math.cos(Location.to_rad(self.lon) - Location.to_rad(lon)))) * 6371.0

  result
end

#latObject



88
89
90
91
# File 'lib/ruby-opengeodb/location.rb', line 88

def lat
  @lat,@lon = get_lat_lon(@plz) if ! @lat 
  @lat
end

#lonObject



93
94
95
96
# File 'lib/ruby-opengeodb/location.rb', line 93

def lon
  @lat,@lon = get_lat_lon(@plz) if ! @lon 
  @lon
end

#ortObject



163
164
165
166
167
168
169
170
# File 'lib/ruby-opengeodb/location.rb', line 163

def ort
  plz = @plz
  rec=Textdata.find(:all, :conditions => "text_val = '#{plz}' AND text_type=500300000")
  loc_id = rec.first['loc_id']
  rec=Textdata.find(:all, :conditions => "loc_id = '#{loc_id}' AND text_type=500100000")
  ortsname = rec.first['text_val']
  return ortsname
end

#to_sObject



84
85
86
# File 'lib/ruby-opengeodb/location.rb', line 84

def to_s
  "#{self.plz} #{self.ort} #{self.lat} #{self.lon}"
end

#XXXin_radius(radius = 10.0) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/ruby-opengeodb/location.rb', line 172

def XXXin_radius(radius=10.0)
  oo = Location.new
  plzs=Textdata.find(:all, :conditions => "text_type=500300000")
  result = []
  plzs.each {|plz|
    oo = Location.new(plz.text_val)
puts "neues Objekt mit PLZ "+oo.plz
puts "distance: "+oo.distance(self).to_s
    ans = gets

    #result << oo if oo.distance(self) <= radius
  }
  result
end