curtilage 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Curtilage
4
+ # The lot side of grow_trim: which parking, garages and canopies are the
5
+ # site's, how much of a shared lot to claim, and the driveways to the
6
+ # street. Split from generator.rb only to keep files a readable size.
7
+ module Generator
8
+ module_function
9
+
10
+ # Mapped parking that touches the building or holds the pin is the
11
+ # site's lot.
12
+ # A lot touching shared_lot_buildings or more other buildings is a shared
13
+ # (shopping-centre) lot: splitting it between them leaves slivers, and it
14
+ # isn't this site's alone, so it's left out and flagged.
15
+ #
16
+ # Parking garages, carports and canopies next to the building count as
17
+ # lot too (LOT_TYPES).
18
+ def own_parking(features, target, opts, log)
19
+ structures = features.buildings.select do |b|
20
+ !b.equal?(target) && (LOT_TYPES.include?(b.category) || b.area < opts[:small_building_m2])
21
+ end
22
+ (features.parking + structures).select do |lot|
23
+ next false unless Geometry.contains?(lot.xy, [0.0, 0.0]) || touches?(lot, target, opts)
24
+
25
+ others = sharers(lot, features, target, opts)
26
+ if others >= opts[:shared_lot_buildings]
27
+ lot.instance_variable_set(:@shared, true)
28
+ log.note("parking #{lot.id} (#{lot.area.round} m2) is shared with #{others} other buildings: " \
29
+ "claimed the part nearer this building than the others")
30
+ next true
31
+ end
32
+ log.note("#{lot_label(lot)} (#{lot.area.round} m2) touches the building, so it's part of the lot")
33
+ true
34
+ end
35
+ end
36
+
37
+ # Other buildings (not garages or canopies) the lot also touches.
38
+ def sharers(lot, features, target, opts)
39
+ neighbours(features, target, opts).count do |other|
40
+ !LOT_TYPES.include?(other.category) && touches?(lot, other, opts)
41
+ end
42
+ end
43
+
44
+ def touches?(lot, building, opts)
45
+ return false unless lot.near?(building, opts[:parking_reach_m])
46
+
47
+ Geometry.overlap?(lot.xy, building.xy) ||
48
+ Geometry.closest_points(lot.xy, building.xy).first <= opts[:parking_reach_m]
49
+ end
50
+
51
+ # Claims the site's parking up to the kerb (at most parking_max_extent_m
52
+ # from the building), splitting a lot halfway where it also touches a
53
+ # neighbour, then keeps clear of neighbouring buildings.
54
+ #
55
+ # A shared (shopping-centre) lot is split halfway with each building on
56
+ # it, so the site gets the part nearer to it than to any other; the
57
+ # driveways linking the claimed lot to a public road are taken in.
58
+ def add_parking(poly, lots, site)
59
+ far = fence(site, site.opts[:parking_max_extent_m])
60
+ parts = lots.map { |lot| share_lot(Geometry.clip_convex(lot.xy, far), lot, site) }
61
+ grown = Geometry.counter_clockwise(Geometry.convex_hull(poly + parts.select { |p| p.size >= 3 }.flatten(1)))
62
+ grown = add_entrances(grown, far, site)
63
+ keep_off_neighbours(grown, site.features, site.target, site.opts, site.log)
64
+ end
65
+
66
+ # The box around the building, reach metres out, trimmed at roads.
67
+ def fence(site, reach)
68
+ rect = site.target.rectangle
69
+ box = Geometry.counter_clockwise(Geometry.oriented_box(rect[:corners], rect[:angle], reach))
70
+ trim_at_roads(box, site.features, site.target, site.opts, Log.new([], []))
71
+ end
72
+
73
+ # Driveways: service roads that run from the lot to a public road. The
74
+ # part within ENTRANCE_REACH_M of the lot joins the premises, up to the
75
+ # kerb: a short connector to the street gets in, a long service road
76
+ # running along the street (which would pull the polygon into a spike)
77
+ # doesn't.
78
+ def add_entrances(poly, fence, site)
79
+ points = site.features.roads.reject { |r| PUBLIC_HIGHWAYS.include?(r.highway) }.flat_map do |road|
80
+ pts = road.xy.each_cons(2).flat_map { |a, b| samples(a, b) }.select { |p| Geometry.contains?(fence, p) }
81
+ next [] unless pts.any? { |p| Geometry.contains?(poly, p) } && meets_public_road?(pts, site.features)
82
+
83
+ pts = pts.select { |p| Geometry.contains?(poly, p) || Geometry.distance_to_edges(poly, p) <= ENTRANCE_REACH_M }
84
+
85
+ site.log.note("took in the driveway #{road.id} to the street")
86
+ pts
87
+ end
88
+ return poly if points.empty?
89
+
90
+ Geometry.counter_clockwise(Geometry.convex_hull(poly + points))
91
+ end
92
+
93
+ def meets_public_road?(points, features)
94
+ street = points.filter_map do |p|
95
+ nearest_street_point(p, features)&.then do |q|
96
+ Math.hypot(q[0] - p[0], q[1] - p[1])
97
+ end
98
+ end
99
+ street.any? { |d| d <= ENTRANCE_MAX_M }
100
+ end
101
+
102
+ # A lot the site shares with one or two other buildings is still the
103
+ # site's (three or more is a shopping-centre lot, left out earlier): it is
104
+ # claimed whole, and keep_off_neighbours stays clear of their walls.
105
+ # Homes are the exception: a lot touching one is split halfway with it,
106
+ # so the claim stops short of the yard.
107
+ def share_lot(part, lot, site)
108
+ features, target, opts, log = site.to_a
109
+ shared = lot.instance_variable_get(:@shared)
110
+ others = neighbours(features, target, opts).select do |other|
111
+ touches?(lot, other, opts) && !Geometry.overlap?(other.hull, target.hull)
112
+ end
113
+ others.each do |other|
114
+ next unless (shared && !LOT_TYPES.include?(other.category)) || home?(other, features, opts)
115
+ next if part.size < 3
116
+
117
+ part = split_from_neighbour(part, target.hull, other.hull, target.rectangle[:angle])
118
+ end
119
+ log.note("parking #{lot.id} also touches building #{others.map(&:id).join(", ")}") if others.any?
120
+ part
121
+ end
122
+
123
+ # Stops 1 m short of any neighbouring building the lot reached.
124
+ def keep_off_neighbours(poly, features, target, opts, log)
125
+ area = Geometry.bounds(poly)
126
+ neighbours(features, target, opts).each do |other|
127
+ next unless Geometry.bounds_near?(area, other.bounds)
128
+ next if Geometry.overlap?(other.hull, target.hull) || !Geometry.overlap?(other.hull, poly)
129
+
130
+ trimmed = split_from_neighbour(poly, target.hull, other.hull, target.rectangle[:angle],
131
+ stop: ->(near, far) { [far - 1, near].max })
132
+ next if trimmed.size < 3
133
+
134
+ log.note("stopped 1 m short of building #{other.id}")
135
+ poly = trimmed
136
+ end
137
+ poly
138
+ end
139
+
140
+ # scope: :centre on a shared lot: the whole shopping centre, i.e. the
141
+ # shared lots plus every building on them, bounded by public roads.
142
+ def centre(shared, site)
143
+ features, target, opts, log = site.to_a
144
+ members = features.buildings.select do |b|
145
+ b.equal?(target) || (b.area >= opts[:neighbour_min_area_m2] && shared.any? { |lot| touches?(lot, b, opts) })
146
+ end
147
+ points = shared.flat_map(&:xy) + members.flat_map(&:hull)
148
+ poly = trim_at_roads(Geometry.counter_clockwise(Geometry.convex_hull(points)), features, target, opts, log)
149
+ log.flag(:shopping_centre, "mapped the whole shopping centre: #{members.size} buildings around " \
150
+ "#{shared.size} shared parking lot(s)")
151
+ poly
152
+ end
153
+
154
+ def lot_label(lot)
155
+ lot.kind == "building" ? "#{lot.category || "small"} structure #{lot.id}" : "parking #{lot.id}"
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Curtilage
4
+ # The store zone: the outline of the building the premises were made
5
+ # from, for an "in the store" area inside the premises.
6
+ module Generator
7
+ STORE_MARGIN_M = 0.05
8
+
9
+ module_function
10
+
11
+ # The building's OSM footprint, simplified to max_store_vertices.
12
+ # Footprints can be any shape (L, U, courtyards' outer edge); if
13
+ # simplifying would make the outline cross itself, its convex outline is
14
+ # used instead. It is then kept STORE_MARGIN_M inside the premises, so
15
+ # the store zone always sits within them, even after rounding.
16
+ def store_xy(target, premises, opts, log)
17
+ xy = Geometry.clean(Geometry.counter_clockwise(target.xy))
18
+ if xy.size > opts[:max_store_vertices]
19
+ simple = Geometry.simplify(xy, opts[:max_store_vertices])
20
+ xy = Geometry.self_intersects?(simple) ? Geometry.simplify(target.hull, opts[:max_store_vertices]) : simple
21
+ end
22
+ if target.area > opts[:large_building_note_m2]
23
+ log.flag(:store_may_be_unit, "the store outline is the whole #{target.area.round} m2 building: " \
24
+ "if the store is one unit of it, trim the outline to that unit")
25
+ end
26
+ inside = Geometry.clean(Geometry.clip_inside(xy, premises, STORE_MARGIN_M))
27
+ log.note("trimmed the store outline to fit inside the premises") if Geometry.area(inside) < Geometry.area(xy) - 1
28
+ inside
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,360 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Curtilage
4
+ # Plain-Ruby planar geometry. No gems: premises polygons are small (a
5
+ # building and its lot), so a flat projection around the site pin is
6
+ # accurate to well under a metre.
7
+ #
8
+ # Coordinates in and out are [lng, lat], matching GeoJSON. Projected points
9
+ # are [x, y] in metres east and north of an origin. Rings passed around
10
+ # projected are open (no repeated closing point).
11
+ module Geometry
12
+ METRES_PER_DEGREE_LAT = 110_540.0
13
+ METRES_PER_DEGREE_LNG_AT_EQUATOR = 111_320.0
14
+
15
+ module_function
16
+
17
+ # [lng, lat] points -> [x, y] metres around an origin [lng, lat].
18
+ def project(points, origin)
19
+ lng0, lat0 = origin
20
+ sx = metres_per_degree_lng(lat0)
21
+ points.map { |lng, lat| [(lng - lng0) * sx, (lat - lat0) * METRES_PER_DEGREE_LAT] }
22
+ end
23
+
24
+ # Inverse of project.
25
+ def unproject(xy, origin)
26
+ lng0, lat0 = origin
27
+ sx = metres_per_degree_lng(lat0)
28
+ xy.map { |x, y| [lng0 + (x / sx), lat0 + (y / METRES_PER_DEGREE_LAT)] }
29
+ end
30
+
31
+ def metres_per_degree_lng(lat)
32
+ Math.cos(lat * Math::PI / 180) * METRES_PER_DEGREE_LNG_AT_EQUATOR
33
+ end
34
+
35
+ def closed?(ring)
36
+ ring.size > 1 && ring.first == ring.last
37
+ end
38
+
39
+ # The ring without its closing point, whether or not it had one.
40
+ def open_ring(ring)
41
+ closed?(ring) ? ring[0...-1] : ring
42
+ end
43
+
44
+ def valid_coordinates?(ring)
45
+ ring.all? { |p| p.is_a?(Array) && p.size == 2 && p.all?(Numeric) && p[0].abs <= 180 && p[1].abs <= 90 }
46
+ end
47
+
48
+ # Shoelace area of [x, y] points (open ring), in square metres.
49
+ def area(xy)
50
+ sum = xy.each_index.sum { |i| (xy[i][0] * xy[(i + 1) % xy.size][1]) - (xy[(i + 1) % xy.size][0] * xy[i][1]) }
51
+ (sum / 2.0).abs
52
+ end
53
+
54
+ def centroid(xy)
55
+ [xy.sum { |p| p[0] } / xy.size, xy.sum { |p| p[1] } / xy.size]
56
+ end
57
+
58
+ # Ray casting. xy is an open ring.
59
+ def contains?(xy, point)
60
+ x, y = point
61
+ inside = false
62
+ xy.each_index do |i|
63
+ x1, y1 = xy[i]
64
+ x2, y2 = xy[(i + 1) % xy.size]
65
+ next unless (y1 > y) != (y2 > y)
66
+
67
+ inside = !inside if x < ((x2 - x1) * (y - y1) / (y2 - y1)) + x1
68
+ end
69
+ inside
70
+ end
71
+
72
+ # Shortest distance from a point to the ring's edges, in metres.
73
+ def distance_to_edges(xy, point)
74
+ xy.each_index.map { |i| segment_distance(point, xy[i], xy[(i + 1) % xy.size]) }.min
75
+ end
76
+
77
+ def segment_distance(point, a, b)
78
+ px, py = point
79
+ ax, ay = a
80
+ bx, by = b
81
+ dx = bx - ax
82
+ dy = by - ay
83
+ len2 = (dx * dx) + (dy * dy)
84
+ t = len2.zero? ? 0 : (((px - ax) * dx) + ((py - ay) * dy)) / len2
85
+ t = t.clamp(0.0, 1.0)
86
+ Math.hypot(px - (ax + (t * dx)), py - (ay + (t * dy)))
87
+ end
88
+
89
+ # True when any two non-adjacent edges cross. O(n^2), fine for premises.
90
+ def self_intersects?(xy)
91
+ n = xy.size
92
+ return false if n < 4
93
+
94
+ edges = xy.each_index.map { |i| [xy[i], xy[(i + 1) % n]] }
95
+ edges.each_index.any? do |i|
96
+ ((i + 2)...n).any? do |j|
97
+ next false if i.zero? && j == n - 1 # first and last edge share a vertex
98
+
99
+ segments_cross?(*edges[i], *edges[j])
100
+ end
101
+ end
102
+ end
103
+
104
+ def segments_cross?(p1, p2, p3, p4)
105
+ d1 = cross(p3, p4, p1)
106
+ d2 = cross(p3, p4, p2)
107
+ d3 = cross(p1, p2, p3)
108
+ d4 = cross(p1, p2, p4)
109
+ ((d1.positive? && d2.negative?) || (d1.negative? && d2.positive?)) &&
110
+ ((d3.positive? && d4.negative?) || (d3.negative? && d4.positive?))
111
+ end
112
+
113
+ def cross(o, a, b)
114
+ ((a[0] - o[0]) * (b[1] - o[1])) - ((a[1] - o[1]) * (b[0] - o[0]))
115
+ end
116
+
117
+ # Andrew's monotone chain. Returns the hull as an open ring.
118
+ def convex_hull(xy)
119
+ pts = xy.uniq.sort
120
+ return pts if pts.size < 3
121
+
122
+ lower = []
123
+ pts.each do |p|
124
+ lower.pop while lower.size >= 2 && cross(lower[-2], lower[-1], p) <= 0
125
+ lower << p
126
+ end
127
+ upper = []
128
+ pts.reverse_each do |p|
129
+ upper.pop while upper.size >= 2 && cross(upper[-2], upper[-1], p) <= 0
130
+ upper << p
131
+ end
132
+ lower[0...-1] + upper[0...-1]
133
+ end
134
+
135
+ # Minimum-area bounding rectangle, trying each hull edge direction.
136
+ # Returns { corners:, area:, angle: } with angle in radians, or nil when
137
+ # the points don't span an area.
138
+ def minimum_rectangle(xy)
139
+ hull = convex_hull(xy)
140
+ return nil if hull.size < 3
141
+
142
+ best = hull.each_index.map do |i|
143
+ a = hull[i]
144
+ b = hull[(i + 1) % hull.size]
145
+ angle = Math.atan2(b[1] - a[1], b[0] - a[0])
146
+ box = oriented_box(hull, angle)
147
+ { corners: box, area: area(box), angle: angle }
148
+ end
149
+ best.min_by { |r| r[:area] }
150
+ end
151
+
152
+ # [area_m2, orientation_degrees_0_to_180] of the minimum rectangle.
153
+ def min_rotated_rectangle(xy)
154
+ rect = minimum_rectangle(xy)
155
+ return [0.0, 0.0] unless rect
156
+
157
+ [rect[:area], (rect[:angle] * 180 / Math::PI) % 180]
158
+ end
159
+
160
+ # The box around the points with sides parallel and perpendicular to
161
+ # angle (radians), grown by margin metres on every side. Counter-clockwise.
162
+ def oriented_box(xy, angle, margin = 0)
163
+ local = rotate(xy, -angle)
164
+ xs = local.map(&:first)
165
+ ys = local.map(&:last)
166
+ x0 = xs.min - margin
167
+ x1 = xs.max + margin
168
+ y0 = ys.min - margin
169
+ y1 = ys.max + margin
170
+ rotate([[x0, y0], [x1, y0], [x1, y1], [x0, y1]], angle)
171
+ end
172
+
173
+ def rotate(xy, angle)
174
+ cos = Math.cos(angle)
175
+ sin = Math.sin(angle)
176
+ xy.map { |x, y| [(x * cos) - (y * sin), (x * sin) + (y * cos)] }
177
+ end
178
+
179
+ def signed_area(xy)
180
+ xy.each_index.sum { |i| (xy[i][0] * xy[(i + 1) % xy.size][1]) - (xy[(i + 1) % xy.size][0] * xy[i][1]) } / 2.0
181
+ end
182
+
183
+ def counter_clockwise(xy)
184
+ signed_area(xy).negative? ? xy.reverse : xy
185
+ end
186
+
187
+ # Centre of mass of the polygon's area (the vertex mean is biased toward
188
+ # densely sampled edges).
189
+ def area_centroid(xy)
190
+ a = signed_area(xy)
191
+ return centroid(xy) if a.abs < 1e-9
192
+
193
+ cx = 0.0
194
+ cy = 0.0
195
+ xy.each_index do |i|
196
+ x1, y1 = xy[i]
197
+ x2, y2 = xy[(i + 1) % xy.size]
198
+ f = (x1 * y2) - (x2 * y1)
199
+ cx += (x1 + x2) * f
200
+ cy += (y1 + y2) * f
201
+ end
202
+ [cx / (6 * a), cy / (6 * a)]
203
+ end
204
+
205
+ # Keeps the part of the polygon where (p - origin) . normal <= 0
206
+ # (Sutherland-Hodgman against one line). Works for any simple polygon;
207
+ # a non-convex input can come back with zero-width bridges, which don't
208
+ # change its area.
209
+ def clip_half_plane(xy, origin, normal)
210
+ sides = xy.map { |p| dot([p[0] - origin[0], p[1] - origin[1]], normal) }
211
+ xy.each_index.with_object([]) do |i, out|
212
+ j = (i + 1) % xy.size
213
+ out << xy[i] if sides[i] <= 0
214
+ out << lerp(xy[i], xy[j], sides[i].fdiv(sides[i] - sides[j])) if (sides[i] * sides[j]).negative?
215
+ end
216
+ end
217
+
218
+ def dot(a, b)
219
+ (a[0] * b[0]) + (a[1] * b[1])
220
+ end
221
+
222
+ def lerp(a, b, fraction)
223
+ [a[0] + (fraction * (b[0] - a[0])), a[1] + (fraction * (b[1] - a[1]))]
224
+ end
225
+
226
+ # The part of subject inside a convex clipper.
227
+ def clip_convex(subject, clipper)
228
+ clipper = counter_clockwise(clipper)
229
+ clipper.each_index.reduce(subject) do |poly, i|
230
+ break [] if poly.size < 3
231
+
232
+ a = clipper[i]
233
+ b = clipper[(i + 1) % clipper.size]
234
+ clip_half_plane(poly, a, [b[1] - a[1], a[0] - b[0]])
235
+ end
236
+ end
237
+
238
+ def closest_point_on_segment(point, a, b)
239
+ dx = b[0] - a[0]
240
+ dy = b[1] - a[1]
241
+ len2 = (dx * dx) + (dy * dy)
242
+ t = len2.zero? ? 0 : ((((point[0] - a[0]) * dx) + ((point[1] - a[1]) * dy)) / len2).clamp(0.0, 1.0)
243
+ [a[0] + (t * dx), a[1] + (t * dy)]
244
+ end
245
+
246
+ # The nearest pair of boundary points of two polygons: [distance, on_a, on_b].
247
+ # Assumes they don't overlap (check with overlap? first).
248
+ def closest_points(poly_a, poly_b)
249
+ candidates = []
250
+ [[poly_a, poly_b, false], [poly_b, poly_a, true]].each do |from, to, flipped|
251
+ from.each do |p|
252
+ to.each_index do |i|
253
+ q = closest_point_on_segment(p, to[i], to[(i + 1) % to.size])
254
+ candidates << (if flipped
255
+ [Math.hypot(p[0] - q[0], p[1] - q[1]), q,
256
+ p]
257
+ else
258
+ [Math.hypot(p[0] - q[0], p[1] - q[1]), p, q]
259
+ end)
260
+ end
261
+ end
262
+ end
263
+ candidates.min_by(&:first)
264
+ end
265
+
266
+ # [min_x, min_y, max_x, max_y].
267
+ def bounds(xy)
268
+ xs = xy.map(&:first)
269
+ ys = xy.map(&:last)
270
+ [xs.min, ys.min, xs.max, ys.max]
271
+ end
272
+
273
+ # Whether two bounds come within margin of each other: a cheap test
274
+ # before the exact ones.
275
+ def bounds_near?(a, b, margin = 0)
276
+ a[0] - margin <= b[2] && b[0] - margin <= a[2] && a[1] - margin <= b[3] && b[1] - margin <= a[3]
277
+ end
278
+
279
+ def overlap?(poly_a, poly_b)
280
+ return true if poly_a.any? { |p| contains?(poly_b, p) } || poly_b.any? { |p| contains?(poly_a, p) }
281
+
282
+ poly_a.each_index.any? do |i|
283
+ a1 = poly_a[i]
284
+ a2 = poly_a[(i + 1) % poly_a.size]
285
+ poly_b.each_index.any? { |j| segments_cross?(a1, a2, poly_b[j], poly_b[(j + 1) % poly_b.size]) }
286
+ end
287
+ end
288
+
289
+ # Drops the vertices that matter least (smallest triangle with their
290
+ # neighbours) until at most max remain. On a convex polygon this only
291
+ # cuts corners, so the result stays convex and inside the original.
292
+ def simplify(xy, max)
293
+ pts = xy.dup
294
+ while pts.size > max
295
+ n = pts.size
296
+ i = (0...n).min_by { |k| area([pts[(k - 1) % n], pts[k], pts[(k + 1) % n]]) }
297
+ pts.delete_at(i)
298
+ end
299
+ pts
300
+ end
301
+
302
+ # Like simplify, but only drops a vertex if the polygon still contains
303
+ # every point in keep (e.g. a building's corners). Stops early, above
304
+ # max, if no vertex can go.
305
+ def simplify_containing(xy, max, keep)
306
+ pts = xy.dup
307
+ while pts.size > max
308
+ n = pts.size
309
+ candidates = (0...n).sort_by { |k| area([pts[(k - 1) % n], pts[k], pts[(k + 1) % n]]) }
310
+ drop = candidates.find do |k|
311
+ trial = pts.dup.tap { |t| t.delete_at(k) }
312
+ keep.all? { |p| contains?(trial, p) || distance_to_edges(trial, p) < 0.01 }
313
+ end
314
+ break unless drop
315
+
316
+ pts.delete_at(drop)
317
+ end
318
+ pts
319
+ end
320
+
321
+ # The part of subject inside a convex clipper shrunk by margin metres.
322
+ def clip_inside(subject, clipper, margin)
323
+ clipper = counter_clockwise(clipper)
324
+ clipper.each_index.reduce(subject) do |poly, i|
325
+ break [] if poly.size < 3
326
+
327
+ a = clipper[i]
328
+ b = clipper[(i + 1) % clipper.size]
329
+ normal = [b[1] - a[1], a[0] - b[0]]
330
+ length = Math.hypot(*normal)
331
+ next poly if length < 1e-9
332
+
333
+ unit = normal.map { |v| v / length }
334
+ clip_half_plane(poly, [a[0] - (unit[0] * margin), a[1] - (unit[1] * margin)], unit)
335
+ end
336
+ end
337
+
338
+ # Removes repeated and collinear points (within tolerance metres).
339
+ def clean(xy, tolerance = 0.05)
340
+ pts = drop_repeats(xy, tolerance)
341
+ loop do
342
+ n = pts.size
343
+ break if n < 4
344
+
345
+ i = (0...n).find { |k| segment_distance(pts[k], pts[(k - 1) % n], pts[(k + 1) % n]) < tolerance }
346
+ break unless i
347
+
348
+ pts.delete_at(i)
349
+ end
350
+ pts
351
+ end
352
+
353
+ def drop_repeats(xy, tolerance)
354
+ near = ->(p, q) { Math.hypot(p[0] - q[0], p[1] - q[1]) < tolerance }
355
+ pts = xy.each_with_object([]) { |p, out| out << p unless out.last && near.call(p, out.last) }
356
+ pts.pop while pts.size > 1 && near.call(pts[0], pts[-1])
357
+ pts
358
+ end
359
+ end
360
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "open3"
5
+
6
+ module Curtilage
7
+ # Reads the features around pins from features Parquet files (built by
8
+ # Curtilage::Build) with the DuckDB CLI. Shelling out keeps the gem free of
9
+ # native dependencies; only the row groups near each pin are read.
10
+ #
11
+ # Files can be local paths or s3://, gs:// or https:// URLs (read in place
12
+ # with DuckDB's httpfs extension). For private buckets, pass setup_sql
13
+ # (e.g. a CREATE SECRET statement): it goes to DuckDB on stdin, never on
14
+ # the command line.
15
+ module Lookup
16
+ class Error < StandardError; end
17
+
18
+ # About 400 m north-south and 450 m east-west at US latitudes: room for
19
+ # the lot and its bounding roads.
20
+ DEFAULT_RADIUS_DEG = 0.004
21
+ MAX_OUTPUT_BYTES = 200_000_000
22
+ MAX_PINS = 10_000
23
+ REMOTE = %r{\A(s3|gs|gcs|https)://}
24
+
25
+ module_function
26
+
27
+ # Feature hashes for one pin. parquet: a path or URL, or a list of them.
28
+ def features(parquet:, pin:, **)
29
+ features_many(parquet: parquet, pins: [pin], **).first
30
+ end
31
+
32
+ # One DuckDB call for many pins; returns a list of feature lists in pin
33
+ # order.
34
+ #
35
+ # options: duckdb (CLI path), extension_directory (both default to
36
+ # Curtilage.config), radius_deg, setup_sql.
37
+ def features_many(parquet:, pins:, **options)
38
+ options = Curtilage.duckdb_options(options)
39
+ files = Array(parquet)
40
+ check_files(files)
41
+ raise ArgumentError, "between 1 and #{MAX_PINS} pins" unless pins.size.between?(1, MAX_PINS)
42
+
43
+ sql = sql(files, pins, options[:extension_directory], options.fetch(:radius_deg, DEFAULT_RADIUS_DEG),
44
+ options[:setup_sql])
45
+ out, err, status = Open3.capture3(options.fetch(:duckdb, "duckdb"), "-json", stdin_data: sql)
46
+ raise Error, "duckdb failed: #{err.strip[0, 500]}" unless status.success?
47
+ raise Error, "duckdb output too large" if out.bytesize > MAX_OUTPUT_BYTES
48
+
49
+ group(out, pins.size)
50
+ end
51
+
52
+ def check_files(files)
53
+ raise ArgumentError, "no parquet file given" if files.empty?
54
+
55
+ files.each do |f|
56
+ next if f.to_s.match?(REMOTE)
57
+ raise ArgumentError, "parquet file not found: #{f}" unless File.file?(f.to_s)
58
+ end
59
+ end
60
+
61
+ def group(out, count)
62
+ lists = Array.new(count) { [] }
63
+ rows = out.strip.empty? ? [] : JSON.parse(out)
64
+ rows.each do |row|
65
+ index = row.delete("pin_index")
66
+ lists[index] << row.merge("geometry" => JSON.parse(row["geometry"])) if index.is_a?(Integer)
67
+ end
68
+ lists
69
+ end
70
+
71
+ def sql(files, pins, extension_directory, radius_deg, setup_sql = nil)
72
+ r = Float(radius_deg)
73
+ raise ArgumentError, "radius out of range" unless r.positive? && r <= 0.05
74
+
75
+ [setup(files, extension_directory, setup_sql), query(files, pins, r)].join("\n")
76
+ end
77
+
78
+ def setup(files, extension_directory, setup_sql)
79
+ [("SET extension_directory = #{quote(File.expand_path(extension_directory))};" if extension_directory),
80
+ "INSTALL spatial; LOAD spatial;",
81
+ ("INSTALL httpfs; LOAD httpfs;" if files.any? { |f| f.to_s.match?(REMOTE) }),
82
+ setup_sql].compact.join("\n")
83
+ end
84
+
85
+ # One scan per pin with constant bounds, so DuckDB can skip every row
86
+ # group (and file) away from that pin; a join on a pins table can't.
87
+ def query(files, pins, radius)
88
+ list = files.map { |f| quote(f) }.join(", ")
89
+ scans = pins.each_with_index.map do |pin, i|
90
+ lng, lat = checked(pin)
91
+ "SELECT #{i} AS pin_index, id, feature, highway, name, ST_AsGeoJSON(wkb)::VARCHAR AS geometry " \
92
+ "FROM read_parquet([#{list}]) WHERE max_lng >= #{lng - radius} AND min_lng <= #{lng + radius} " \
93
+ "AND max_lat >= #{lat - radius} AND min_lat <= #{lat + radius}"
94
+ end
95
+ "#{scans.join(" UNION ALL ")};"
96
+ end
97
+
98
+ def checked(pin)
99
+ lng = Float(pin[0])
100
+ lat = Float(pin[1])
101
+ raise ArgumentError, "pin out of range: #{pin.inspect}" unless lng.abs <= 180 && lat.abs <= 90
102
+
103
+ [lng, lat]
104
+ end
105
+
106
+ def quote(text)
107
+ "'#{text.to_s.gsub("'", "''")}'"
108
+ end
109
+ end
110
+ end