222 lines
6.7 KiB
JavaScript
222 lines
6.7 KiB
JavaScript
// Build the small, committed data artifacts used by /use-cases.
|
|
//
|
|
// Sources (outside this repo):
|
|
// ../../cool-city-data/route/raw/area_osm.xml OSM extract, ODbL
|
|
// ../../ML-py.git/feat-prezero-api/artifacts/prezero_priority.json
|
|
//
|
|
// Outputs:
|
|
// public/cool-city/buildings.json real building footprints around the routes
|
|
// public/prezero/priority.json summary + top priority containers
|
|
//
|
|
// Run from the web repo root:
|
|
// node scripts/build-use-case-data.mjs
|
|
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
|
|
const root = process.cwd()
|
|
const dataRoot = path.resolve(root, '../..')
|
|
|
|
// Route window with a small margin: the three routes span
|
|
// lat 49.14023..49.14254, lon 9.21753..9.22039.
|
|
const BOUNDS = { minlat: 49.1394, maxlat: 49.1432, minlon: 9.2169, maxlon: 9.2212 }
|
|
const SIMPLIFY_TOLERANCE = 0.000018
|
|
|
|
function decodeXml(value) {
|
|
return value
|
|
.replaceAll('&', '&')
|
|
.replaceAll('"', '"')
|
|
.replaceAll(''', "'")
|
|
.replaceAll('<', '<')
|
|
.replaceAll('>', '>')
|
|
}
|
|
|
|
function readOsm(file) {
|
|
const xml = fs.readFileSync(file, 'utf8')
|
|
const nodes = new Map()
|
|
const nodeRe = /<node\b([^>]*?)\/?>/g
|
|
let match
|
|
while ((match = nodeRe.exec(xml))) {
|
|
const attrs = match[1]
|
|
const id = attrs.match(/\bid="(-?\d+)"/)?.[1]
|
|
const lat = Number(attrs.match(/\blat="(-?[\d.]+)"/)?.[1])
|
|
const lon = Number(attrs.match(/\blon="(-?[\d.]+)"/)?.[1])
|
|
if (!id || !Number.isFinite(lat) || !Number.isFinite(lon)) continue
|
|
const tags = {}
|
|
const tagRe = /<tag\b([^>]*?)\/?>/g
|
|
let tag
|
|
while ((tag = tagRe.exec(match[0]))) {
|
|
const k = tag[1].match(/\bk="([^"]*)"/)?.[1]
|
|
const v = tag[1].match(/\bv="([^"]*)"/)?.[1]
|
|
if (k) tags[decodeXml(k)] = decodeXml(v ?? '')
|
|
}
|
|
nodes.set(id, { lat, lon, tags })
|
|
}
|
|
|
|
const ways = []
|
|
const wayRe = /<way\b([^>]*)>([\s\S]*?)<\/way>/g
|
|
while ((match = wayRe.exec(xml))) {
|
|
const body = match[2]
|
|
const refs = [...body.matchAll(/<nd\b[^>]*\bref="(-?\d+)"/g)].map((row) => row[1])
|
|
const tags = {}
|
|
const tagRe = /<tag\b([^>]*?)\/?>/g
|
|
let tag
|
|
while ((tag = tagRe.exec(body))) {
|
|
const k = tag[1].match(/\bk="([^"]*)"/)?.[1]
|
|
const v = tag[1].match(/\bv="([^"]*)"/)?.[1]
|
|
if (k) tags[decodeXml(k)] = decodeXml(v ?? '')
|
|
}
|
|
ways.push({ refs, tags })
|
|
}
|
|
return { nodes, ways }
|
|
}
|
|
|
|
function inBounds(point, bounds) {
|
|
return (
|
|
point.lat >= bounds.minlat &&
|
|
point.lat <= bounds.maxlat &&
|
|
point.lon >= bounds.minlon &&
|
|
point.lon <= bounds.maxlon
|
|
)
|
|
}
|
|
|
|
function distanceToSegment(p, a, b) {
|
|
const dx = b[0] - a[0]
|
|
const dy = b[1] - a[1]
|
|
if (dx === 0 && dy === 0) return Math.hypot(p[0] - a[0], p[1] - a[1])
|
|
const t = Math.max(
|
|
0,
|
|
Math.min(1, ((p[0] - a[0]) * dx + (p[1] - a[1]) * dy) / (dx * dx + dy * dy)),
|
|
)
|
|
return Math.hypot(p[0] - (a[0] + t * dx), p[1] - (a[1] + t * dy))
|
|
}
|
|
|
|
function simplify(points, tolerance) {
|
|
if (points.length <= 4) return points
|
|
let maxDistance = 0
|
|
let index = 0
|
|
for (let i = 1; i < points.length - 1; i += 1) {
|
|
const distance = distanceToSegment(points[i], points[0], points.at(-1))
|
|
if (distance > maxDistance) {
|
|
maxDistance = distance
|
|
index = i
|
|
}
|
|
}
|
|
if (maxDistance > tolerance) {
|
|
const left = simplify(points.slice(0, index + 1), tolerance)
|
|
const right = simplify(points.slice(index), tolerance)
|
|
return [...left.slice(0, -1), ...right]
|
|
}
|
|
return [points[0], points.at(-1)]
|
|
}
|
|
|
|
function buildBuildings(source) {
|
|
const { nodes, ways } = readOsm(source)
|
|
const buildings = []
|
|
for (const way of ways) {
|
|
if (!way.tags.building && !way.tags['building:part']) continue
|
|
if (way.tags['building:part'] === 'yes' && !way.tags.building) {
|
|
// Roof-only parts duplicate the main footprint at this zoom.
|
|
continue
|
|
}
|
|
const points = way.refs
|
|
.map((ref) => nodes.get(ref))
|
|
.filter(Boolean)
|
|
.filter((node) => inBounds(node, BOUNDS))
|
|
.map((node) => [node.lon, node.lat])
|
|
if (points.length < 3) continue
|
|
if (points[0][0] !== points.at(-1)[0] || points[0][1] !== points.at(-1)[1]) {
|
|
points.push(points[0])
|
|
}
|
|
const simplified = simplify(points, SIMPLIFY_TOLERANCE)
|
|
if (simplified.length < 3) continue
|
|
buildings.push({
|
|
name: way.tags.name || null,
|
|
kind: way.tags.building === 'yes' ? 'building' : way.tags.building || 'building',
|
|
points: simplified.map(([lon, lat]) => [
|
|
Math.round(lon * 1e6) / 1e6,
|
|
Math.round(lat * 1e6) / 1e6,
|
|
]),
|
|
})
|
|
}
|
|
buildings.sort((a, b) => {
|
|
const areaA = polygonArea(a.points)
|
|
const areaB = polygonArea(b.points)
|
|
return areaB - areaA
|
|
})
|
|
return {
|
|
source: 'OpenStreetMap contributors, ODbL',
|
|
generated: new Date().toISOString().slice(0, 10),
|
|
bounds: BOUNDS,
|
|
buildings,
|
|
}
|
|
}
|
|
|
|
function polygonArea(points) {
|
|
let area = 0
|
|
for (let i = 0; i < points.length - 1; i += 1) {
|
|
area += points[i][0] * points[i + 1][1] - points[i + 1][0] * points[i][1]
|
|
}
|
|
return Math.abs(area / 2)
|
|
}
|
|
|
|
function buildPrezero(source) {
|
|
const full = JSON.parse(fs.readFileSync(source, 'utf8'))
|
|
const containers = [...full.containers]
|
|
.sort((a, b) => {
|
|
const rank = { high: 0, medium: 1, low: 2 }
|
|
return (
|
|
rank[a.priority] - rank[b.priority] ||
|
|
b.priority_score - a.priority_score ||
|
|
b.overfull_count - a.overfull_count
|
|
)
|
|
})
|
|
.slice(0, 24)
|
|
.map((row) => ({
|
|
location_id: row.location_id,
|
|
priority: row.priority,
|
|
priority_score: row.priority_score,
|
|
readings: row.readings,
|
|
overfull_count: row.overfull_count,
|
|
overfull_rate: row.overfull_rate,
|
|
p95_fill: row.p95_fill,
|
|
max_fill: row.max_fill,
|
|
mean_fill: row.mean_fill,
|
|
last_fill: row.last_fill,
|
|
last_is_overfull: row.last_is_overfull,
|
|
last_date: row.last_date,
|
|
days_since_last: row.days_since_last,
|
|
trend: row.trend,
|
|
materials: row.materials,
|
|
}))
|
|
return {
|
|
meta: full.meta,
|
|
summary: full.summary,
|
|
top: containers,
|
|
}
|
|
}
|
|
|
|
const buildings = buildBuildings(
|
|
path.join(dataRoot, 'cool-city-data/route/raw/area_osm.xml'),
|
|
)
|
|
fs.mkdirSync(path.join(root, 'public/cool-city'), { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(root, 'public/cool-city/buildings.json'),
|
|
`${JSON.stringify(buildings)}\n`,
|
|
)
|
|
|
|
const prezero = buildPrezero(
|
|
path.join(dataRoot, 'ML-py.git/feat-prezero-api/artifacts/prezero_priority.json'),
|
|
)
|
|
fs.mkdirSync(path.join(root, 'public/prezero'), { recursive: true })
|
|
fs.writeFileSync(
|
|
path.join(root, 'public/prezero/priority.json'),
|
|
`${JSON.stringify(prezero)}\n`,
|
|
)
|
|
|
|
console.log(
|
|
`buildings: ${buildings.buildings.length} -> public/cool-city/buildings.json`,
|
|
)
|
|
console.log(
|
|
`prezero: ${prezero.top.length} of ${prezero.meta.containers} containers -> public/prezero/priority.json`,
|
|
)
|