1 # frozen_string_literal: true
 
   4   extend ActiveSupport::Concern
 
   6   # Ensure that when coordinates are printed that they are always in decimal degrees,
 
   8   # Unfortunately you can't extend Numeric classes directly (e.g. `Coord < Float`).
 
   9   class Coord < DelegateClass(Float)
 
  11       format("%<coord>.7f", :coord => self)
 
  15       format("%<coord>.7f", :coord => self).to_f
 
  19   # This scaling factor is used to convert between the float lat/lon that is
 
  20   # returned by the API, and the integer lat/lon equivalent that is stored in
 
  25     scope :bbox, ->(bbox) { where(OSM.sql_for_area(bbox, "#{table_name}.")) }
 
  26     before_save :update_tile
 
  29   # Is this node within -90 >= latitude >= 90 and -180 >= longitude >= 180
 
  30   # * returns true/false
 
  32     return false if lat < -90 || lat > 90
 
  33     return false if lon < -180 || lon > 180
 
  39     self.tile = QuadTile.tile_for_point(lat, lon)
 
  43     self.latitude = (l * SCALE).round
 
  47     self.longitude = (l * SCALE).round
 
  50   # Return WGS84 latitude
 
  52     Coord.new(latitude.to_f / SCALE)
 
  55   # Return WGS84 longitude
 
  57     Coord.new(longitude.to_f / SCALE)