]> git.openstreetmap.org Git - rails.git/blob - lib/geo_record.rb
Use options_for_select to set the selected items in the search form
[rails.git] / lib / geo_record.rb
1 require "delegate"
2
3 module GeoRecord
4   # Ensure that when coordinates are printed that they are always in decimal degrees,
5   # and not e.g. 4.0e-05
6   # Unfortunately you can't extend Numeric classes directly (e.g. `Coord < Float`).
7   class Coord < DelegateClass(Float)
8     def initialize(obj)
9       super(obj)
10     end
11
12     def to_s
13       format("%.7f", self)
14     end
15   end
16
17   # This scaling factor is used to convert between the float lat/lon that is
18   # returned by the API, and the integer lat/lon equivalent that is stored in
19   # the database.
20   SCALE = 10000000
21
22   def self.included(base)
23     base.scope :bbox, ->(bbox) { base.where(OSM.sql_for_area(bbox)) }
24     base.before_save :update_tile
25   end
26
27   # Is this node within -90 >= latitude >= 90 and -180 >= longitude >= 180
28   # * returns true/false
29   def in_world?
30     return false if lat < -90 || lat > 90
31     return false if lon < -180 || lon > 180
32     true
33   end
34
35   def update_tile
36     self.tile = QuadTile.tile_for_point(lat, lon)
37   end
38
39   def lat=(l)
40     self.latitude = (l * SCALE).round
41   end
42
43   def lon=(l)
44     self.longitude = (l * SCALE).round
45   end
46
47   # Return WGS84 latitude
48   def lat
49     Coord.new(latitude.to_f / SCALE)
50   end
51
52   # Return WGS84 longitude
53   def lon
54     Coord.new(longitude.to_f / SCALE)
55   end
56 end