Module: DecisionAgent::Dsl::Helpers::GeospatialHelpers

Defined in:
lib/decision_agent/dsl/helpers/geospatial_helpers.rb

Overview

Geospatial helper methods for ConditionEvaluator

Class Method Summary collapse

Class Method Details

.haversine_distance(point1, point2) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/decision_agent/dsl/helpers/geospatial_helpers.rb', line 44

def self.haversine_distance(point1, point2)
  earth_radius_km = 6371.0

  lat1_rad = (point1[:lat] * Math::PI) / 180
  lat2_rad = (point2[:lat] * Math::PI) / 180
  delta_lat = ((point2[:lat] - point1[:lat]) * Math::PI) / 180
  delta_lon = ((point2[:lon] - point1[:lon]) * Math::PI) / 180

  haversine_a = (Math.sin(delta_lat / 2)**2) +
                (Math.cos(lat1_rad) * Math.cos(lat2_rad) *
                 (Math.sin(delta_lon / 2)**2))

  haversine_c = 2 * Math.atan2(Math.sqrt(haversine_a), Math.sqrt(1 - haversine_a))

  earth_radius_km * haversine_c
end

.parse_coordinates(value) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/decision_agent/dsl/helpers/geospatial_helpers.rb', line 8

def self.parse_coordinates(value)
  case value
  when Hash
    lat = value["lat"] || value[:lat] || value["latitude"] || value[:latitude]
    lon = value["lon"] || value[:lon] || value["lng"] || value[:lng] ||
          value["longitude"] || value[:longitude]
    return nil unless lat && lon

    { lat: lat.to_f, lon: lon.to_f }
  when Array
    return nil unless value.size == 2

    { lat: value[0].to_f, lon: value[1].to_f }
  end
end

.parse_polygon(value, parse_coordinates:) ⇒ Object



38
39
40
41
42
# File 'lib/decision_agent/dsl/helpers/geospatial_helpers.rb', line 38

def self.parse_polygon(value, parse_coordinates:)
  return nil unless value.is_a?(Array)

  value.map { |vertex| parse_coordinates.call(vertex) }.compact
end

.parse_radius_params(value, parse_coordinates:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/decision_agent/dsl/helpers/geospatial_helpers.rb', line 24

def self.parse_radius_params(value, parse_coordinates:)
  return nil unless value.is_a?(Hash)

  center_data = value["center"] || value[:center]
  radius = value["radius"] || value[:radius]

  return nil unless center_data && radius

  center = parse_coordinates.call(center_data)
  return nil unless center

  { center: center, radius: radius.to_f }
end

.point_in_polygon?(point, polygon) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/decision_agent/dsl/helpers/geospatial_helpers.rb', line 61

def self.point_in_polygon?(point, polygon)
  return false if polygon.size < 3

  inside = false
  j = polygon.size - 1

  (0...polygon.size).each do |i|
    xi = polygon[i][:lat]
    yi = polygon[i][:lon]
    xj = polygon[j][:lat]
    yj = polygon[j][:lon]

    intersect = ((yi > point[:lon]) != (yj > point[:lon])) &&
                (point[:lat] < ((xj - xi) * (point[:lon] - yi) / (yj - yi)) + xi)

    inside = !inside if intersect
    j = i
  end

  inside
end