source: subversion/sites/rails_port/lib/geo_record.rb @ 8381

Last change on this file since 8381 was 8381, checked in by tomhughes, 12 years ago

Make the polymorphic relation association work properly.

File size: 1.2 KB
Line 
1module GeoRecord
2  def before_save
3    self.update_tile
4  end
5
6  # Is this node within -90 >= latitude >= 90 and -180 >= longitude >= 180
7  # * returns true/false
8  def in_world?
9    return false if self.lat < -90 or self.lat > 90
10    return false if self.lon < -180 or self.lon > 180
11    return true
12  end
13
14  def self.find_by_area(minlat, minlon, maxlat, maxlon, options)
15    self.with_scope(:find => {:conditions => OSM.sql_for_area(minlat, minlon, maxlat, maxlon)}) do
16      return self.find(:all, options)
17    end
18  end
19
20  def update_tile
21    self.tile = QuadTile.tile_for_point(lat, lon)
22  end
23
24  def lat=(l)
25    self.latitude = (l * 10000000).round
26  end
27
28  def lon=(l)
29    self.longitude = (l * 10000000).round
30  end
31
32  # Return WGS84 latitude
33  def lat
34    return self.latitude.to_f / 10000000
35  end
36
37  # Return WGS84 longitude
38  def lon
39    return self.longitude.to_f / 10000000
40  end
41
42  # Potlatch projections
43  def lon_potlatch(baselong,masterscale)
44    (self.lon-baselong)*masterscale
45  end
46
47  def lat_potlatch(basey,masterscale)
48    -(lat2y(self.lat)-basey)*masterscale
49  end
50 
51  private
52 
53  def lat2y(a)
54    180/Math::PI * Math.log(Math.tan(Math::PI/4+a*(Math::PI/180)/2))
55  end
56
57end
58
Note: See TracBrowser for help on using the repository browser.