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.
data/lib/curtilage.rb ADDED
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "curtilage/version"
4
+ require_relative "curtilage/config"
5
+ require_relative "curtilage/geometry"
6
+ require_relative "curtilage/validation"
7
+ require_relative "curtilage/features"
8
+ require_relative "curtilage/generator"
9
+ require_relative "curtilage/generator_lot"
10
+ require_relative "curtilage/generator_store"
11
+ require_relative "curtilage/lookup"
12
+ require_relative "curtilage/download"
13
+ require_relative "curtilage/zip_range"
14
+ require_relative "curtilage/footprints"
15
+ require_relative "curtilage/build"
16
+ require_relative "curtilage/catalogue"
17
+ require_relative "curtilage/regions"
18
+ require_relative "curtilage/auto_data"
19
+ require_relative "curtilage/overture"
20
+ require_relative "curtilage/audit"
21
+
22
+ # Premises generation from OpenStreetMap data. Coordinates are [lng, lat]
23
+ # throughout, matching GeoJSON.
24
+ module Curtilage
25
+ # Named styles; any option passed alongside a preset overrides it.
26
+ PRESETS = {
27
+ # The building and its whole lot: parking up to the kerb, railways and a
28
+ # strip short of neighbours. The default.
29
+ whole_lot: {},
30
+ # The building plus a small margin, still trimmed at roads.
31
+ tight: { method: :building, building_margin_m: 8 },
32
+ # Like whole_lot, but a store in a shopping centre gets the whole centre.
33
+ centre: { scope: :centre },
34
+ }.freeze
35
+
36
+ # Premises for one pin, fetching its map data as configured (see
37
+ # generate_many for the options). The simplest way in:
38
+ #
39
+ # result = Curtilage.premises([-80.0, 35.0])
40
+ # result.ok? && result.ring
41
+ def self.premises(pin, **)
42
+ generate_many([pin], **).first
43
+ end
44
+
45
+ # Premises for one pin from features you already have (Lookup.features).
46
+ # With no method or preset, the recommended method with its fallback
47
+ # (Generator.propose).
48
+ def self.generate(pin, features, method: nil, preset: nil, **options)
49
+ opts = preset_options(preset).merge(options)
50
+ method ||= opts.delete(:method)
51
+ opts.delete(:method)
52
+ method ? Generator.generate(method, pin, features, **opts) : Generator.propose(pin, features, **opts)
53
+ end
54
+
55
+ # Results for many pins, in pin order, looking features up once per group
56
+ # of pins that share data files.
57
+ #
58
+ # Where the data comes from:
59
+ # - source: :geofabrik (the default, or Curtilage.config.source): built on demand from Geofabrik for
60
+ # just the regions the pins are in (AutoData), in cache_dir if given
61
+ # (reused for a month), else in a temporary folder deleted afterwards.
62
+ # Minutes per region the first time; nothing to host.
63
+ # - source: :overture: read in place from Overture Maps' public release,
64
+ # seconds per batch, nothing downloaded or hosted (Overture).
65
+ # - catalogue: a Catalogue of regions built ahead of time.
66
+ # - parquet: a path or URL, or a list of them.
67
+ #
68
+ # lookup: options for Lookup and the build (duckdb:, extension_directory:,
69
+ # setup_sql:). data: options for AutoData (max_age_days:, ml_footprints:).
70
+ # Other options go to generate.
71
+ def self.generate_many(pins, source: nil, catalogue: nil, parquet: nil, cache_dir: nil, lookup: {}, data: {},
72
+ **options)
73
+ source = (source || config.source).to_sym
74
+ cache_dir ||= config.cache_dir
75
+ lookup = duckdb_options(lookup)
76
+ return from_overture(pins, lookup, data, options) if source == :overture && !(catalogue || parquet)
77
+ raise ArgumentError, "unknown source #{source.inspect} (have: geofabrik, overture)" unless source == :geofabrik
78
+
79
+ auto = nil
80
+ unless catalogue || parquet
81
+ auto = AutoData.new(cache_dir: cache_dir, **lookup.slice(:duckdb, :extension_directory), **data)
82
+ catalogue = auto.catalogue_for(pins)
83
+ end
84
+ run_many(pins, catalogue, parquet, lookup, options)
85
+ ensure
86
+ auto&.cleanup
87
+ end
88
+
89
+ def self.from_overture(pins, lookup, data, options)
90
+ lists = Overture.features_many(pins, **lookup.slice(:duckdb, :extension_directory, :radius_deg),
91
+ **data.slice(:release))
92
+ pins.zip(lists).map do |pin, features|
93
+ generate(pin, features, **options).tap { |r| r.attribution = Overture::ATTRIBUTION }
94
+ end
95
+ end
96
+
97
+ def self.run_many(pins, catalogue, parquet, lookup, options)
98
+ results = Array.new(pins.size)
99
+ groups = pins.each_index.group_by { |i| catalogue ? catalogue.files_for(pins[i]) : Array(parquet) }
100
+ groups.each do |files, indexes|
101
+ lists = if files.empty?
102
+ indexes.map { [] }
103
+ else
104
+ Lookup.features_many(parquet: files, pins: pins.values_at(*indexes), **lookup)
105
+ end
106
+ indexes.zip(lists).each do |i, features|
107
+ results[i] = generate(pins[i], features, **options)
108
+ no_data(results[i]) if files.empty?
109
+ end
110
+ end
111
+ results
112
+ end
113
+
114
+ def self.preset_options(preset)
115
+ return {} if preset.nil?
116
+
117
+ PRESETS.fetch(preset) do
118
+ raise ArgumentError, "unknown preset #{preset.inspect} (have: #{PRESETS.keys.join(", ")})"
119
+ end.dup
120
+ end
121
+
122
+ def self.no_data(result)
123
+ result.flags << :no_data
124
+ result.explanation.unshift("no map data covers this pin")
125
+ end
126
+ end
@@ -0,0 +1,52 @@
1
+ -- Build one region's features file (Parquet) from Geofabrik's free
2
+ -- shapefiles. The geometry is already assembled (multipolygons included), so
3
+ -- this is read, filter, write. Run it with `curtilage build <state>`
4
+ -- (Curtilage::Build), which sets everything below up.
5
+ -- Output columns: id, feature, highway, name, min_lng, min_lat, max_lng, max_lat, wkb.
6
+ -- feature is building, ml_building, parking, road or railway. highway holds
7
+ -- the class: the road or railway class (Geofabrik's fclass), or the building
8
+ -- type (house, parking, roof, ...; empty when OSM only says building=yes). ML footprints have no OSM
9
+ -- id, so they get negative ids, unique within the file.
10
+ --
11
+ -- The caller defines, before this runs:
12
+ -- views buildings_a_src, traffic_a_src, roads_src and railways_src, each
13
+ -- reading every part of that layer (large layers are split into _1, _2, ...)
14
+ -- view ml_buildings_src (geom): machine-learned footprints, empty unless asked for
15
+ -- SET VARIABLE out_path = '<features>.parquet';
16
+
17
+
18
+ COPY (
19
+ WITH features AS (
20
+ SELECT osm_id::BIGINT AS id, 'building' AS feature, type AS highway, name, geom
21
+ FROM buildings_a_src
22
+ UNION ALL
23
+ SELECT osm_id::BIGINT, 'parking', NULL, name, geom
24
+ FROM traffic_a_src
25
+ WHERE fclass = 'parking'
26
+ UNION ALL
27
+ -- Drivable roads and service roads at ground level: a tunnel under a lot
28
+ -- or a flyover above it doesn't bound the premises.
29
+ SELECT osm_id::BIGINT, 'road', fclass, name, geom
30
+ FROM roads_src
31
+ WHERE fclass IN ('motorway', 'trunk', 'primary', 'secondary', 'tertiary', 'unclassified', 'residential',
32
+ 'service', 'motorway_link', 'trunk_link', 'primary_link', 'secondary_link', 'tertiary_link')
33
+ AND tunnel = 'F' AND bridge = 'F'
34
+ UNION ALL
35
+ -- Railways at ground level bound premises like public roads. Trams run
36
+ -- in the street, which is already a road.
37
+ SELECT osm_id::BIGINT, 'railway', fclass, name, geom
38
+ FROM railways_src
39
+ WHERE fclass IN ('rail', 'light_rail', 'subway', 'narrow_gauge')
40
+ AND tunnel = 'F' AND bridge = 'F'
41
+ UNION ALL
42
+ SELECT -row_number() OVER (), 'ml_building', NULL, NULL, geom
43
+ FROM ml_buildings_src
44
+ )
45
+ SELECT id, feature, highway, name,
46
+ ST_XMin(geom) AS min_lng, ST_YMin(geom) AS min_lat,
47
+ ST_XMax(geom) AS max_lng, ST_YMax(geom) AS max_lat,
48
+ ST_AsWKB(geom) AS wkb
49
+ FROM features
50
+ WHERE geom IS NOT NULL AND NOT ST_IsEmpty(geom) -- a few shapefile records have no geometry
51
+ ORDER BY ST_Hilbert(geom, {'min_x': -180, 'min_y': -90, 'max_x': 180, 'max_y': 90}::BOX_2D)
52
+ ) TO (getvariable('out_path')) (FORMAT parquet, COMPRESSION zstd, ROW_GROUP_SIZE 20000);
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: curtilage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Emil M
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: |
13
+ Proposes a premises polygon for a location (the building and its customer-facing lot,
14
+ bounded by roads, railways and neighbours) plus the building's own outline, with review
15
+ flags, validation and an explanation of each step. Map data is free and needs no hosting:
16
+ OpenStreetMap regions are built on demand from Geofabrik, or read in place from Overture
17
+ Maps. Rule-based; machine-learned footprints are opt-in.
18
+ email:
19
+ - curtilage.cohesive702@8shield.net
20
+ executables:
21
+ - curtilage
22
+ extensions: []
23
+ extra_rdoc_files: []
24
+ files:
25
+ - CHANGELOG.md
26
+ - LICENSE.txt
27
+ - README.md
28
+ - SECURITY.md
29
+ - docs/reference.md
30
+ - exe/curtilage
31
+ - lib/curtilage.rb
32
+ - lib/curtilage/audit.rb
33
+ - lib/curtilage/auto_data.rb
34
+ - lib/curtilage/build.rb
35
+ - lib/curtilage/catalogue.rb
36
+ - lib/curtilage/config.rb
37
+ - lib/curtilage/download.rb
38
+ - lib/curtilage/features.rb
39
+ - lib/curtilage/footprints.rb
40
+ - lib/curtilage/generator.rb
41
+ - lib/curtilage/generator_lot.rb
42
+ - lib/curtilage/generator_store.rb
43
+ - lib/curtilage/geometry.rb
44
+ - lib/curtilage/lookup.rb
45
+ - lib/curtilage/overture.rb
46
+ - lib/curtilage/regions.rb
47
+ - lib/curtilage/validation.rb
48
+ - lib/curtilage/version.rb
49
+ - lib/curtilage/zip_range.rb
50
+ - sql/build_from_shapefiles.sql
51
+ homepage: https://github.com/mohanenm/curtilage
52
+ licenses:
53
+ - MIT
54
+ metadata:
55
+ source_code_uri: https://github.com/mohanenm/curtilage
56
+ changelog_uri: https://github.com/mohanenm/curtilage/blob/main/CHANGELOG.md
57
+ bug_tracker_uri: https://github.com/mohanenm/curtilage/issues
58
+ documentation_uri: https://github.com/mohanenm/curtilage/blob/main/docs/reference.md
59
+ rubygems_mfa_required: 'true'
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '3.2'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.7.2
75
+ specification_version: 4
76
+ summary: Generate a site's premises polygon (building + lot) from OpenStreetMap data.
77
+ test_files: []