15
16
17
18
19
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/odin/transform/verbs/geo_verbs.rb', line 15
def register(registry)
dv = Types::DynValue
registry["distance"] = ->(args, ctx) {
lat1 = NumericVerbs.to_double(args[0])
lon1 = NumericVerbs.to_double(args[1])
lat2 = NumericVerbs.to_double(args[2])
lon2 = NumericVerbs.to_double(args[3])
unit = args[4]&.to_string || "km"
return dv.of_null if lat1.nil? || lon1.nil? || lat2.nil? || lon2.nil?
valid_units = %w[km mi miles nm]
unless valid_units.include?(unit)
ctx.errors << TransformEngine.incompatible_conversion_error(
"distance", "unknown unit '#{unit}' (expected 'km', 'mi', or 'miles')"
)
return dv.of_null
end
dlat = (lat2 - lat1) * DEG_TO_RAD
dlon = (lon2 - lon1) * DEG_TO_RAD
a = Math.sin(dlat / 2)**2 +
Math.cos(lat1 * DEG_TO_RAD) * Math.cos(lat2 * DEG_TO_RAD) *
Math.sin(dlon / 2)**2
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
dist_km = EARTH_RADIUS_KM * c
result = case unit
when "mi", "miles" then dist_km * KM_TO_MI
when "nm" then dist_km * KM_TO_NM
else dist_km
end
return dv.of_null if result.nan? || result.infinite?
dv.of_float(result)
}
registry["inBoundingBox"] = ->(args, _ctx) {
lat = NumericVerbs.to_double(args[0])
lon = NumericVerbs.to_double(args[1])
min_lat = NumericVerbs.to_double(args[2])
min_lon = NumericVerbs.to_double(args[3])
max_lat = NumericVerbs.to_double(args[4])
max_lon = NumericVerbs.to_double(args[5])
return dv.of_null if [lat, lon, min_lat, min_lon, max_lat, max_lon].any?(&:nil?)
dv.of_bool(lat >= min_lat && lat <= max_lat && lon >= min_lon && lon <= max_lon)
}
registry["toRadians"] = ->(args, _ctx) {
v = NumericVerbs.to_double(args[0])
return dv.of_null if v.nil?
dv.of_float(v * DEG_TO_RAD)
}
registry["toDegrees"] = ->(args, _ctx) {
v = NumericVerbs.to_double(args[0])
return dv.of_null if v.nil?
dv.of_float(v * RAD_TO_DEG)
}
registry["bearing"] = ->(args, _ctx) {
lat1 = NumericVerbs.to_double(args[0])
lon1 = NumericVerbs.to_double(args[1])
lat2 = NumericVerbs.to_double(args[2])
lon2 = NumericVerbs.to_double(args[3])
return dv.of_null if lat1.nil? || lon1.nil? || lat2.nil? || lon2.nil?
lat1r = lat1 * DEG_TO_RAD
lat2r = lat2 * DEG_TO_RAD
dlon = (lon2 - lon1) * DEG_TO_RAD
y = Math.sin(dlon) * Math.cos(lat2r)
x = Math.cos(lat1r) * Math.sin(lat2r) - Math.sin(lat1r) * Math.cos(lat2r) * Math.cos(dlon)
bearing = Math.atan2(y, x) * RAD_TO_DEG
bearing = (bearing + 360) % 360
return dv.of_null if bearing.nan? || bearing.infinite?
dv.of_float(bearing)
}
registry["midpoint"] = ->(args, _ctx) {
lat1 = NumericVerbs.to_double(args[0])
lon1 = NumericVerbs.to_double(args[1])
lat2 = NumericVerbs.to_double(args[2])
lon2 = NumericVerbs.to_double(args[3])
return dv.of_null if lat1.nil? || lon1.nil? || lat2.nil? || lon2.nil?
lat1r = lat1 * DEG_TO_RAD
lat2r = lat2 * DEG_TO_RAD
lon1r = lon1 * DEG_TO_RAD
dlon = (lon2 - lon1) * DEG_TO_RAD
bx = Math.cos(lat2r) * Math.cos(dlon)
by = Math.cos(lat2r) * Math.sin(dlon)
mid_lat = Math.atan2(
Math.sin(lat1r) + Math.sin(lat2r),
Math.sqrt((Math.cos(lat1r) + bx)**2 + by**2)
)
mid_lon = lon1r + Math.atan2(by, Math.cos(lat1r) + bx)
mid_lat_deg = mid_lat * RAD_TO_DEG
mid_lon_deg = mid_lon * RAD_TO_DEG
dv.of_object({
"lat" => dv.of_float(mid_lat_deg),
"lon" => dv.of_float(mid_lon_deg)
})
}
end
|