curtilage 0.3.0 → 0.4.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +75 -0
- data/README.md +76 -22
- data/SECURITY.md +18 -3
- data/docs/reference.md +221 -27
- data/docs/schema/site-v1.json +362 -0
- data/docs/security.md +69 -0
- data/exe/curtilage +375 -82
- data/lib/curtilage/audit.rb +5 -5
- data/lib/curtilage/auto_data.rb +61 -11
- data/lib/curtilage/batch.rb +116 -0
- data/lib/curtilage/build.rb +117 -63
- data/lib/curtilage/candidates.rb +67 -0
- data/lib/curtilage/catalogue.rb +44 -16
- data/lib/curtilage/confidence.rb +55 -0
- data/lib/curtilage/config.rb +24 -2
- data/lib/curtilage/corrections.rb +57 -0
- data/lib/curtilage/diff.rb +90 -0
- data/lib/curtilage/doctor.rb +92 -0
- data/lib/curtilage/download.rb +42 -16
- data/lib/curtilage/duckdb.rb +170 -6
- data/lib/curtilage/error.rb +3 -0
- data/lib/curtilage/features.rb +54 -5
- data/lib/curtilage/formats.rb +271 -36
- data/lib/curtilage/generator.rb +145 -166
- data/lib/curtilage/generator_details.rb +22 -14
- data/lib/curtilage/generator_lot.rb +56 -32
- data/lib/curtilage/generator_pin.rb +74 -0
- data/lib/curtilage/generator_roads.rb +109 -0
- data/lib/curtilage/generator_steps.rb +54 -0
- data/lib/curtilage/generator_store.rb +18 -4
- data/lib/curtilage/generator_zones.rb +93 -0
- data/lib/curtilage/geometry.rb +48 -52
- data/lib/curtilage/lookup.rb +41 -21
- data/lib/curtilage/overture.rb +137 -17
- data/lib/curtilage/preview.rb +55 -16
- data/lib/curtilage/profiles.rb +62 -0
- data/lib/curtilage/regions.rb +4 -8
- data/lib/curtilage/result.rb +72 -7
- data/lib/curtilage/validation.rb +73 -9
- data/lib/curtilage/version.rb +1 -1
- data/lib/curtilage/zip_range.rb +81 -16
- data/lib/curtilage.rb +155 -17
- data/sql/build_from_shapefiles.sql +1 -1
- metadata +15 -2
data/lib/curtilage/duckdb.rb
CHANGED
|
@@ -1,24 +1,188 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "json"
|
|
3
4
|
require "open3"
|
|
4
5
|
|
|
5
6
|
module Curtilage
|
|
6
7
|
# The DuckDB CLI, which Lookup, Overture and Build run SQL through.
|
|
7
8
|
module DuckDB
|
|
9
|
+
MAX_ERROR_BYTES = 1_000_000
|
|
10
|
+
# How long a stopped DuckDB gets to exit before it's killed outright.
|
|
11
|
+
GRACE_SECONDS = 2
|
|
12
|
+
|
|
8
13
|
module_function
|
|
9
14
|
|
|
10
15
|
# [stdout, stderr, status] of the CLI run on sql. A missing binary is a
|
|
11
16
|
# Curtilage::Error saying what to install, not a bare Errno::ENOENT.
|
|
12
|
-
|
|
13
|
-
|
|
17
|
+
#
|
|
18
|
+
# DuckDB runs in its own process group: past timeout seconds, or once its
|
|
19
|
+
# output passes max_output bytes, the whole group is stopped and this
|
|
20
|
+
# raises, so a stuck or runaway query can't hang the caller or fill its
|
|
21
|
+
# memory. Output is read as it arrives, never more than the cap.
|
|
22
|
+
#
|
|
23
|
+
# DuckDB writes UTF-8; Ruby would otherwise label it with the locale, and
|
|
24
|
+
# under LANG=C a non-ASCII name in the JSON fails to parse.
|
|
25
|
+
# DUCKDB_AGENT_MODE=0: run from an AI coding tool (AI_AGENT, CLAUDECODE
|
|
26
|
+
# and the like), DuckDB 2.0 switches to an "agent mode" that changes its
|
|
27
|
+
# output and prints a notice on stderr. Older versions ignore it.
|
|
28
|
+
def capture(duckdb, sql, *args, timeout: Curtilage.config.duckdb_timeout, max_output: nil)
|
|
29
|
+
Open3.popen3({ "DUCKDB_AGENT_MODE" => "0" }, duckdb, *args, pgroup: true) do |stdin, stdout, stderr, wait|
|
|
30
|
+
feed, out, err = pipe_threads([stdin, stdout, stderr], sql, max_output, wait.pid)
|
|
31
|
+
finished = begin
|
|
32
|
+
wait.join(timeout)
|
|
33
|
+
rescue Exception # rubocop:disable Lint/RescueException
|
|
34
|
+
# Interrupted (Ctrl-C) or killed: DuckDB has its own process group,
|
|
35
|
+
# so the terminal's signal never reached it. Stop it before leaving.
|
|
36
|
+
stop(wait)
|
|
37
|
+
raise
|
|
38
|
+
end
|
|
39
|
+
stop(wait) unless finished
|
|
40
|
+
output = [feed, out, err].map(&:value)[1..]
|
|
41
|
+
raise Error, "DuckDB took over #{timeout} s and was stopped" unless finished
|
|
42
|
+
raise Error, "DuckDB's output passed #{max_output} bytes and it was stopped" if output[0] == :too_large
|
|
43
|
+
|
|
44
|
+
[output[0].force_encoding(Encoding::UTF_8), clean(output[1].to_s.force_encoding(Encoding::UTF_8)), wait.value]
|
|
45
|
+
end
|
|
14
46
|
rescue Errno::ENOENT
|
|
15
|
-
raise
|
|
16
|
-
|
|
47
|
+
raise DependencyError, "DuckDB CLI not found (#{duckdb}). Install DuckDB 1.4 or 2.0 " \
|
|
48
|
+
"(https://duckdb.org/docs/installation/) or set CURTILAGE_DUCKDB to its path; " \
|
|
49
|
+
"`curtilage doctor` checks."
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Threads that write the SQL and read both outputs; their values carry
|
|
53
|
+
# any error, so they don't report it themselves.
|
|
54
|
+
def pipe_threads(pipes, sql, max_output, pid)
|
|
55
|
+
stdin, stdout, stderr = pipes
|
|
56
|
+
[Thread.new { feed(stdin, sql) }, Thread.new { read_capped(stdout, max_output, pid) },
|
|
57
|
+
Thread.new { read_capped(stderr, MAX_ERROR_BYTES, pid) }].each { |t| t.report_on_exception = false }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def feed(stdin, sql)
|
|
61
|
+
stdin.write(sql)
|
|
62
|
+
rescue Errno::EPIPE, IOError
|
|
63
|
+
nil # DuckDB stopped reading (it exited or was stopped): its status says why
|
|
64
|
+
ensure
|
|
65
|
+
stdin.close unless stdin.closed?
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# All of stream, or :too_large once it passes cap (stopping the process).
|
|
69
|
+
# A stream closed under it (the run was abandoned: interrupted, timed
|
|
70
|
+
# out) ends the read quietly.
|
|
71
|
+
def read_capped(stream, cap, pid)
|
|
72
|
+
data = String.new
|
|
73
|
+
while (chunk = stream.read(65_536))
|
|
74
|
+
data << chunk
|
|
75
|
+
next unless cap && data.bytesize > cap
|
|
76
|
+
|
|
77
|
+
signal("TERM", pid)
|
|
78
|
+
return :too_large
|
|
79
|
+
end
|
|
80
|
+
data
|
|
81
|
+
rescue IOError
|
|
82
|
+
data
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# TERM to the whole process group, then KILL if it hasn't gone.
|
|
86
|
+
def stop(wait)
|
|
87
|
+
signal("TERM", wait.pid)
|
|
88
|
+
signal("KILL", wait.pid) unless wait.join(GRACE_SECONDS)
|
|
89
|
+
wait.join
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def signal(name, pid)
|
|
93
|
+
Process.kill(name, -pid)
|
|
94
|
+
rescue Errno::ESRCH, Errno::EPERM
|
|
95
|
+
nil # already gone
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# DuckDB's error without the query excerpt it quotes ("LINE 1: ..." and
|
|
99
|
+
# the "^" marker under it): the excerpt can hold the box around a pin,
|
|
100
|
+
# and error messages often travel further than the machine.
|
|
101
|
+
def clean(err)
|
|
102
|
+
err.lines.reject { |l| l.match?(/\A\s*LINE \d+:/) || l.match?(/\A\s*\^+\s*\z/) }.join
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Where the CLI actually is: duckdb as given if it names a path, else the
|
|
106
|
+
# first match on the PATH, or nil. Whatever this names runs with the
|
|
107
|
+
# user's rights, so doctor shows it.
|
|
108
|
+
def resolve(duckdb)
|
|
109
|
+
return File.expand_path(duckdb) if duckdb.include?(File::SEPARATOR)
|
|
110
|
+
|
|
111
|
+
ENV.fetch("PATH", "").split(File::PATH_SEPARATOR).map { |dir| File.join(dir, duckdb) }
|
|
112
|
+
.find { |file| File.executable?(file) && !File.directory?(file) }
|
|
17
113
|
end
|
|
18
114
|
|
|
19
|
-
#
|
|
115
|
+
# The SQL pointing DuckDB at an extension folder. DuckDB 2.0 renamed the
|
|
116
|
+
# setting, and warns about the old name on stdout, inside -json output.
|
|
117
|
+
# Without a duckdb path (SQL built on its own), the 1.x form.
|
|
118
|
+
def extension_sql(dir, duckdb = nil)
|
|
119
|
+
folder = quote(File.expand_path(dir))
|
|
120
|
+
return "SET extension_directories = [#{folder}];" if duckdb && major_version(duckdb) >= 2
|
|
121
|
+
|
|
122
|
+
"SET extension_directory = #{folder};"
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# The CLI's version line ("v2.0.1 (Cyanoptera) 8d78114e90"), or nil if
|
|
126
|
+
# it can't be run (the run that needs it reports that); asked once per
|
|
127
|
+
# binary.
|
|
128
|
+
def version(duckdb)
|
|
129
|
+
(@versions ||= {}).fetch(duckdb) do
|
|
130
|
+
out, _, status = capture(duckdb, "", "--version")
|
|
131
|
+
@versions[duckdb] = status.success? ? out.strip : nil
|
|
132
|
+
rescue Error
|
|
133
|
+
nil
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# The CLI's major version (1 if it can't tell).
|
|
138
|
+
def major_version(duckdb)
|
|
139
|
+
version(duckdb).to_s[/\Av?(\d+)\./, 1]&.then { |major| Integer(major) } || 1
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# { "spatial" => "v1.4.4", ... }: the versions of the extensions loaded
|
|
143
|
+
# after setup (SQL that loads them), for doctor and build metadata.
|
|
144
|
+
def extension_versions(duckdb, setup, names)
|
|
145
|
+
list = names.map { |n| quote(n) }.join(", ")
|
|
146
|
+
query = "SELECT extension_name AS name, extension_version AS version FROM duckdb_extensions() " \
|
|
147
|
+
"WHERE loaded AND extension_name IN (#{list});"
|
|
148
|
+
out, err, status = capture(duckdb, "#{setup}\n#{query}", "-json")
|
|
149
|
+
raise DependencyError, "DuckDB extensions (#{names.join(", ")}) could not be loaded: #{err.strip[0, 300]}" \
|
|
150
|
+
unless status.success?
|
|
151
|
+
|
|
152
|
+
JSON.parse(out).to_h { |row| [row["name"], row["version"]] }
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# SQL that makes the extensions available: installed if missing, then
|
|
156
|
+
# loaded. Offline, only loaded (from a pre-populated extension folder),
|
|
157
|
+
# with DuckDB's own automatic installs turned off. Signature checks are
|
|
158
|
+
# never relaxed (no allow_unsigned_extensions).
|
|
159
|
+
def load_sql(*names)
|
|
160
|
+
return (["SET autoinstall_known_extensions = false;"] + names.map { |n| "LOAD #{n};" }).join(" ") \
|
|
161
|
+
if Curtilage.offline?
|
|
162
|
+
|
|
163
|
+
names.map { |n| "INSTALL #{n}; LOAD #{n};" }.join(" ")
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# A SQL string literal. Line breaks and NUL bytes are refused: no real
|
|
167
|
+
# path or URL has them, and the CLI reads a line starting with "." as one
|
|
168
|
+
# of its own commands.
|
|
20
169
|
def quote(text)
|
|
21
|
-
|
|
170
|
+
text = text.to_s
|
|
171
|
+
raise ArgumentError, "text for DuckDB can't contain line breaks or NUL bytes: #{text.inspect}" \
|
|
172
|
+
if text.match?(/[\r\n\0]/)
|
|
173
|
+
|
|
174
|
+
"'#{text.gsub("'", "''")}'"
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# A DuckDB memory size ("400MB", "1.5 GiB"), checked rather than only
|
|
178
|
+
# quoted, so a mistake fails clearly instead of inside DuckDB.
|
|
179
|
+
MEMORY = /\A\d+(\.\d+)?\s?(KB|MB|GB|TB|KiB|MiB|GiB|TiB)\z/i
|
|
180
|
+
|
|
181
|
+
def memory_limit(value)
|
|
182
|
+
raise ArgumentError, "memory_limit looks like \"400MB\" or \"2GB\": #{value.inspect}" \
|
|
183
|
+
unless value.to_s.match?(MEMORY)
|
|
184
|
+
|
|
185
|
+
"SET memory_limit = #{quote(value)};"
|
|
22
186
|
end
|
|
23
187
|
end
|
|
24
188
|
end
|
data/lib/curtilage/error.rb
CHANGED
|
@@ -4,4 +4,7 @@ module Curtilage
|
|
|
4
4
|
# Every error the gem raises on purpose is one of these (input mistakes
|
|
5
5
|
# are ArgumentError).
|
|
6
6
|
class Error < StandardError; end
|
|
7
|
+
|
|
8
|
+
# DuckDB or one of its extensions is missing (the command line exits 3).
|
|
9
|
+
class DependencyError < Error; end
|
|
7
10
|
end
|
data/lib/curtilage/features.rb
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "digest"
|
|
4
|
+
require "json"
|
|
5
|
+
|
|
3
6
|
module Curtilage
|
|
4
7
|
# The OSM features around a pin, projected to metres around it.
|
|
5
8
|
#
|
|
@@ -14,7 +17,8 @@ module Curtilage
|
|
|
14
17
|
MAX_VERTICES = 5_000
|
|
15
18
|
|
|
16
19
|
# category: the building type from OSM (house, parking, roof, ...), or nil.
|
|
17
|
-
|
|
20
|
+
# source: where a non-OSM building came from (Overture's dataset name).
|
|
21
|
+
Polygon = Struct.new(:id, :kind, :name, :xy, :area, :category, :source, keyword_init: true) do
|
|
18
22
|
def hull
|
|
19
23
|
@hull ||= Geometry.convex_hull(xy)
|
|
20
24
|
end
|
|
@@ -34,7 +38,29 @@ module Curtilage
|
|
|
34
38
|
end
|
|
35
39
|
Line = Struct.new(:id, :highway, :name, :xy, keyword_init: true)
|
|
36
40
|
|
|
37
|
-
attr_reader :pin, :buildings, :ml_buildings, :parking, :roads, :railways, :dropped
|
|
41
|
+
attr_reader :pin, :buildings, :ml_buildings, :parking, :roads, :railways, :dropped, :sha256
|
|
42
|
+
|
|
43
|
+
# A SHA256 of the input that ignores the order of the features and the
|
|
44
|
+
# style of their keys, so two results can be shown to come from the same
|
|
45
|
+
# map data (Result#provenance).
|
|
46
|
+
def self.fingerprint(features)
|
|
47
|
+
Digest::SHA256.hexdigest(in_order(features).map(&:first).join("\n"))
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# [[canonical JSON, feature], ...] sorted by the JSON: one order whatever
|
|
51
|
+
# order the features came in (DuckDB promises none), so the rules see the
|
|
52
|
+
# same input every time.
|
|
53
|
+
def self.in_order(features)
|
|
54
|
+
Array(features).map { |f| [JSON.generate(canonical(f), allow_nan: true), f] }.sort_by(&:first)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def self.canonical(value)
|
|
58
|
+
case value
|
|
59
|
+
when Hash then value.to_h { |k, v| [k.to_s, canonical(v)] }.sort.to_h
|
|
60
|
+
when Array then value.map { |v| canonical(v) }
|
|
61
|
+
else value
|
|
62
|
+
end
|
|
63
|
+
end
|
|
38
64
|
|
|
39
65
|
def initialize(pin, features)
|
|
40
66
|
@pin = pin
|
|
@@ -45,8 +71,31 @@ module Curtilage
|
|
|
45
71
|
@railways = []
|
|
46
72
|
@dropped = 0
|
|
47
73
|
@seen = {}
|
|
48
|
-
|
|
49
|
-
@
|
|
74
|
+
ordered = self.class.in_order(features)
|
|
75
|
+
@sha256 = Digest::SHA256.hexdigest(ordered.map(&:first).join("\n"))
|
|
76
|
+
ordered.first(MAX_FEATURES).each { |pair| add(pair.last) }
|
|
77
|
+
@dropped += [ordered.size - MAX_FEATURES, 0].max
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Roads that bound a site (Generator::PUBLIC_HIGHWAYS), not service
|
|
81
|
+
# roads or driveways.
|
|
82
|
+
def public_roads
|
|
83
|
+
@public_roads ||= roads.select { |r| Generator::PUBLIC_HIGHWAYS.include?(r.highway) }
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# A copy centred on another point (metres, in this copy's frame): the
|
|
87
|
+
# pin moves there and every feature shifts with it. A flat shift, exact
|
|
88
|
+
# to well under a metre over the tens of metres a pin is snapped.
|
|
89
|
+
def moved_to(point)
|
|
90
|
+
dx, dy = point
|
|
91
|
+
copy = dup
|
|
92
|
+
copy.instance_variable_set(:@pin, Geometry.lnglat([point], pin, 9).first)
|
|
93
|
+
copy.instance_variable_set(:@public_roads, nil)
|
|
94
|
+
%i[@buildings @ml_buildings @parking @roads @railways].each do |name|
|
|
95
|
+
list = instance_variable_get(name).map { |f| f.class.new(**f.to_h, xy: f.xy.map { |x, y| [x - dx, y - dy] }) }
|
|
96
|
+
copy.instance_variable_set(name, list)
|
|
97
|
+
end
|
|
98
|
+
copy
|
|
50
99
|
end
|
|
51
100
|
|
|
52
101
|
# A copy whose buildings are the given list (see Generator buildings:).
|
|
@@ -98,7 +147,7 @@ module Curtilage
|
|
|
98
147
|
category = field(feature, :highway).to_s
|
|
99
148
|
polygon = Polygon.new(id: field(feature, :id), kind: kind, name: field(feature, :name),
|
|
100
149
|
xy: Geometry.counter_clockwise(ring), area: area,
|
|
101
|
-
category: category.empty? ? nil : category)
|
|
150
|
+
category: category.empty? ? nil : category, source: field(feature, :source))
|
|
102
151
|
case kind
|
|
103
152
|
when "building" then @buildings << polygon
|
|
104
153
|
when "ml_building" then @ml_buildings << polygon
|