258 lines
8.1 KiB
JavaScript
Executable File
258 lines
8.1 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const { spawnSync } = require("child_process");
|
|
|
|
const repoRoot = path.resolve(__dirname, "..");
|
|
const args = parseArgs(process.argv.slice(2));
|
|
const configPath = path.resolve(
|
|
args.config || path.join(repoRoot, "config", "areas", "nantaizi-lake-innovation-valley.json"),
|
|
);
|
|
const area = normalizeAreaConfig(readJson(configPath));
|
|
const requestedStages = args.stages
|
|
? splitList(args.stages)
|
|
: null;
|
|
const stages = resolveStages(area.stages, requestedStages);
|
|
|
|
console.log(`Area: ${area.id}`);
|
|
console.log(`Config: ${configPath}`);
|
|
console.log(`Output: ${area.outputs.areaDir}`);
|
|
|
|
if (stages.intermediates) {
|
|
buildIntermediates(area);
|
|
}
|
|
if (stages.blender) {
|
|
buildBlenderScene(area);
|
|
}
|
|
if (stages.cesium) {
|
|
exportCesium(area);
|
|
}
|
|
|
|
console.log("Done.");
|
|
|
|
function parseArgs(argv) {
|
|
const out = {};
|
|
for (let i = 0; i < argv.length; i += 1) {
|
|
const arg = argv[i];
|
|
if (!arg.startsWith("--")) continue;
|
|
const key = arg.slice(2).replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
const next = argv[i + 1];
|
|
if (!next || next.startsWith("--")) {
|
|
out[key] = "true";
|
|
} else {
|
|
out[key] = next;
|
|
i += 1;
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function readJson(file) {
|
|
if (!fs.existsSync(file)) {
|
|
throw new Error(`Config file not found: ${file}`);
|
|
}
|
|
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
}
|
|
|
|
function normalizeAreaConfig(raw) {
|
|
const id = requireText(raw.id, "id");
|
|
const input = path.resolve(requireText(raw.input, "input"));
|
|
if (!fs.existsSync(input)) {
|
|
throw new Error(`Input OSM XML not found: ${input}`);
|
|
}
|
|
|
|
const outputRoot = path.resolve(raw.outputRoot || path.join(repoRoot, "outputs"));
|
|
const outputOverrides = raw.outputs || {};
|
|
const areaDir = path.resolve(outputOverrides.areaDir || path.join(outputRoot, id));
|
|
const fileStem = outputOverrides.fileStem || id;
|
|
const outputs = {
|
|
areaDir,
|
|
geojsonDir: path.resolve(outputOverrides.geojsonDir || path.join(areaDir, "osm2streets_web_out")),
|
|
gpkg: path.resolve(outputOverrides.gpkg || path.join(areaDir, `${fileStem}.gpkg`)),
|
|
qgisProject: path.resolve(outputOverrides.qgisProject || path.join(areaDir, `${fileStem}.qgz`)),
|
|
qgisPreview: path.resolve(outputOverrides.qgisPreview || path.join(areaDir, `${fileStem}-preview.png`)),
|
|
blend: path.resolve(outputOverrides.blend || path.join(areaDir, `${fileStem}.blend`)),
|
|
render: path.resolve(outputOverrides.render || path.join(areaDir, `${fileStem}.png`)),
|
|
glb: path.resolve(outputOverrides.glb || path.join(areaDir, `${fileStem}.glb`)),
|
|
metadata: path.resolve(outputOverrides.metadata || path.join(areaDir, `${fileStem}.json`)),
|
|
pipelineDir: path.resolve(outputOverrides.pipelineDir || path.join(areaDir, "_pipeline")),
|
|
};
|
|
|
|
return {
|
|
id,
|
|
input,
|
|
outputRoot,
|
|
qgisApp: raw.qgisApp || "/Applications/QGIS.app",
|
|
blenderApp: raw.blenderApp || "/Applications/Blender.app",
|
|
stages: {
|
|
intermediates: raw.stages?.intermediates ?? raw.stages?.qgis ?? true,
|
|
blender: raw.stages?.blender ?? true,
|
|
cesium: raw.stages?.cesium ?? true,
|
|
},
|
|
qgis: {
|
|
arrowScale: raw.qgis?.arrowScale ?? raw.arrowScale ?? 0.8,
|
|
clipPad: raw.qgis?.clipPad ?? raw.clipPad ?? 0.002,
|
|
canvasPad: raw.qgis?.canvasPad ?? raw.canvasPad ?? 0.001,
|
|
previewPad: raw.qgis?.previewPad ?? raw.previewPad ?? 0.0007,
|
|
canvasExtent: raw.qgis?.canvasExtent ?? raw.canvasExtent ?? null,
|
|
previewExtent: raw.qgis?.previewExtent ?? raw.previewExtent ?? null,
|
|
layerPrefix: raw.qgis?.layerPrefix ?? raw.layerPrefix ?? "osm2streets",
|
|
},
|
|
osm2streets: raw.osm2streets || {
|
|
debug_each_step: false,
|
|
dual_carriageway_experiment: false,
|
|
sidepath_zipping_experiment: false,
|
|
inferred_sidewalks: true,
|
|
osm2lanes: true,
|
|
},
|
|
blender: {
|
|
treeStyle: raw.blender?.treeStyle || "natural",
|
|
officeOverrides: raw.blender?.officeOverrides || raw.blender?.office_overrides || "",
|
|
},
|
|
outputs,
|
|
};
|
|
}
|
|
|
|
function requireText(value, key) {
|
|
if (typeof value !== "string" || value.trim() === "") {
|
|
throw new Error(`Missing config key: ${key}`);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function splitList(value) {
|
|
return String(value)
|
|
.split(",")
|
|
.map((item) => item.trim())
|
|
.filter(Boolean);
|
|
}
|
|
|
|
function resolveStages(defaults, requested) {
|
|
if (!requested) return defaults;
|
|
const aliases = {
|
|
all: ["intermediates", "blender", "cesium"],
|
|
qgis: ["intermediates"],
|
|
osm2streets: ["intermediates"],
|
|
geojson: ["intermediates"],
|
|
intermediate: ["intermediates"],
|
|
intermediates: ["intermediates"],
|
|
blender: ["blender"],
|
|
scene: ["blender"],
|
|
cesium: ["cesium"],
|
|
glb: ["cesium"],
|
|
};
|
|
const out = { intermediates: false, blender: false, cesium: false };
|
|
for (const stage of requested) {
|
|
const mapped = aliases[stage];
|
|
if (!mapped) {
|
|
throw new Error(`Unknown stage '${stage}'. Use intermediates, blender, cesium, or all.`);
|
|
}
|
|
for (const key of mapped) out[key] = true;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function buildIntermediates(area) {
|
|
fs.mkdirSync(area.outputs.pipelineDir, { recursive: true });
|
|
const derivedConfig = {
|
|
qgisApp: area.qgisApp,
|
|
input: area.input,
|
|
outDir: area.outputs.geojsonDir,
|
|
gpkg: area.outputs.gpkg,
|
|
project: area.outputs.qgisProject,
|
|
preview: area.outputs.qgisPreview,
|
|
arrowScale: area.qgis.arrowScale,
|
|
clipPad: area.qgis.clipPad,
|
|
canvasPad: area.qgis.canvasPad,
|
|
previewPad: area.qgis.previewPad,
|
|
canvasExtent: area.qgis.canvasExtent,
|
|
previewExtent: area.qgis.previewExtent,
|
|
layerPrefix: area.qgis.layerPrefix,
|
|
osm2streets: area.osm2streets,
|
|
};
|
|
const derivedConfigPath = path.join(area.outputs.pipelineDir, "osm2streets-qgis.config.json");
|
|
fs.writeFileSync(derivedConfigPath, `${JSON.stringify(derivedConfig, null, 2)}\n`);
|
|
|
|
console.log("Stage: intermediates (osm2streets GeoJSON + QGIS)");
|
|
runCommand(process.execPath, [
|
|
path.join(repoRoot, "scripts", "build-osm2streets-qgis.js"),
|
|
"--config",
|
|
derivedConfigPath,
|
|
], "intermediates");
|
|
}
|
|
|
|
function buildBlenderScene(area) {
|
|
ensureFile(blenderExecutable(area), "Blender executable");
|
|
ensureFile(path.join(repoRoot, "blender", "generate_scene.py"), "Blender scene generator");
|
|
fs.mkdirSync(path.dirname(area.outputs.blend), { recursive: true });
|
|
fs.mkdirSync(path.dirname(area.outputs.render), { recursive: true });
|
|
|
|
const blenderArgs = [
|
|
"--background",
|
|
"--factory-startup",
|
|
"--python",
|
|
path.join(repoRoot, "blender", "generate_scene.py"),
|
|
"--",
|
|
"--osm",
|
|
area.input,
|
|
"--geojson",
|
|
area.outputs.geojsonDir,
|
|
"--output",
|
|
area.outputs.blend,
|
|
"--render",
|
|
area.outputs.render,
|
|
"--tree-style",
|
|
area.blender.treeStyle,
|
|
];
|
|
if (area.blender.officeOverrides) {
|
|
blenderArgs.push("--office-overrides", area.blender.officeOverrides);
|
|
}
|
|
|
|
console.log("Stage: blender");
|
|
runCommand(blenderExecutable(area), blenderArgs, "blender");
|
|
}
|
|
|
|
function exportCesium(area) {
|
|
ensureFile(blenderExecutable(area), "Blender executable");
|
|
ensureFile(area.outputs.blend, "Blend scene");
|
|
ensureFile(path.join(repoRoot, "blender", "export_cesium.py"), "Cesium exporter");
|
|
fs.mkdirSync(path.dirname(area.outputs.glb), { recursive: true });
|
|
fs.mkdirSync(path.dirname(area.outputs.metadata), { recursive: true });
|
|
|
|
console.log("Stage: cesium");
|
|
runCommand(blenderExecutable(area), [
|
|
"--background",
|
|
"--python",
|
|
path.join(repoRoot, "blender", "export_cesium.py"),
|
|
"--",
|
|
"--blend",
|
|
area.outputs.blend,
|
|
"--glb",
|
|
area.outputs.glb,
|
|
"--metadata",
|
|
area.outputs.metadata,
|
|
], "cesium");
|
|
}
|
|
|
|
function blenderExecutable(area) {
|
|
return path.join(area.blenderApp, "Contents", "MacOS", "Blender");
|
|
}
|
|
|
|
function ensureFile(file, label) {
|
|
if (!fs.existsSync(file)) {
|
|
throw new Error(`${label} not found: ${file}`);
|
|
}
|
|
}
|
|
|
|
function runCommand(command, commandArgs, stage) {
|
|
const result = spawnSync(command, commandArgs, { stdio: "inherit" });
|
|
if (result.error) {
|
|
throw result.error;
|
|
}
|
|
if (result.status !== 0) {
|
|
const signal = result.signal ? ` signal=${result.signal}` : "";
|
|
throw new Error(`Stage '${stage}' failed with status=${result.status}${signal}`);
|
|
}
|
|
}
|