Class: ProximityHash
- Inherits:
-
Object
- Object
- ProximityHash
- Defined in:
- lib/proximityhash.rb
Constant Summary collapse
- MIN_PRECISION =
1- MAX_PRECISION =
12- EARTH_RADIUS =
6371000
Instance Method Summary collapse
- #calculate ⇒ Object
-
#initialize(latitude, longitude, radius, precision) ⇒ ProximityHash
constructor
A new instance of ProximityHash.
Constructor Details
#initialize(latitude, longitude, radius, precision) ⇒ ProximityHash
Returns a new instance of ProximityHash.
11 12 13 14 15 16 17 18 |
# File 'lib/proximityhash.rb', line 11 def initialize(latitude, longitude, radius, precision) @centre = [ latitude, longitude ] @radius = radius @precision = precision if ((precision > MAX_PRECISION) or (precision < MIN_PRECISION)) raise 'Precision out bounds' end end |
Instance Method Details
#calculate ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/proximityhash.rb', line 20 def calculate x = 0.0 y = 0.0 points = [] geohashes = {} grid_width = [ 5009400.0, 1252300.0, 156500.0, 39100.0, 4900.0, 1200.0, 152.9, 38.2, 4.8, 1.2, 0.149, 0.0370 ] grid_height = [ 4992600.0, 624100.0, 156000.0, 19500.0, 4900.0, 609.4, 152.4, 19.0, 4.8, 0.595, 0.149, 0.0199 ] height = (grid_height[@precision - 1]) / 2.0 width = (grid_width[@precision - 1]) / 2.0 latitude = @centre[0] longitude = @centre[1] lat_moves = (@radius / height).ceil lon_moves = (@radius / width).ceil (0 .. lat_moves).each do |i| temp_lat = y + (height * i) (0..lon_moves).each do |j| temp_lon = x + (width * j) if within_radius(temp_lat, temp_lon, y, x) == true x_cen, y_cen = get_centre(temp_lat, temp_lon, height, width) lat, lon = convert_to_latlon(y_cen, x_cen, latitude, longitude) points += [[lat, lon]] lat, lon = convert_to_latlon(-y_cen, x_cen, latitude, longitude) points += [[lat, lon]] lat, lon = convert_to_latlon(y_cen, -x_cen, latitude, longitude) points += [[lat, lon]] lat, lon = convert_to_latlon(-y_cen, -x_cen, latitude, longitude) points += [[lat, lon]] end end end points.each do |point| geohash = GeoHash.encode(point[0], point[1], @precision) geohashes[geohash] = Haversine.distance(point, @centre).to_meters end geohashes.sort_by { |key, value| value }.to_h end |