refactor: consume external road compiler
This commit is contained in:
47
scripts/lib/road-compiler-cli.js
Normal file
47
scripts/lib/road-compiler-cli.js
Normal file
@@ -0,0 +1,47 @@
|
||||
"use strict";
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const { spawnSync } = require("child_process");
|
||||
const { toRoadCompilerInput } = require("./area-config");
|
||||
|
||||
function compilerCli() {
|
||||
const packageFile = require.resolve("@osm-asset/road-compiler/package.json");
|
||||
const packageRoot = path.dirname(packageFile);
|
||||
const manifest = require(packageFile);
|
||||
return path.join(packageRoot, typeof manifest.bin === "string" ? manifest.bin : manifest.bin["road-compiler"]);
|
||||
}
|
||||
|
||||
function writeInput(area) {
|
||||
const input = toRoadCompilerInput(area);
|
||||
fs.mkdirSync(area.outputs.pipelineDir, { recursive: true });
|
||||
const file = path.join(area.outputs.pipelineDir, "road-compiler.input.json");
|
||||
fs.writeFileSync(file, `${JSON.stringify(input, null, 2)}\n`);
|
||||
return { file, input };
|
||||
}
|
||||
|
||||
function parseCompletionMarker(stdout, input) {
|
||||
const lines = String(stdout).split(/\r?\n/).filter((line) => line.startsWith("NATIVE_ROAD_COMPILE_DONE "));
|
||||
if (lines.length !== 1) throw new Error(`Expected exactly one NATIVE_ROAD_COMPILE_DONE marker, found ${lines.length}`);
|
||||
let marker;
|
||||
try { marker = JSON.parse(lines[0].slice("NATIVE_ROAD_COMPILE_DONE ".length)); }
|
||||
catch (error) { throw new Error(`Invalid NATIVE_ROAD_COMPILE_DONE JSON: ${error.message}`); }
|
||||
if (!marker || typeof marker !== "object") throw new Error("NATIVE_ROAD_COMPILE_DONE payload must be an object");
|
||||
if (marker.areaId !== input.areaId) throw new Error(`NATIVE_ROAD_COMPILE_DONE areaId mismatch: expected ${input.areaId}, got ${marker.areaId}`);
|
||||
if (path.resolve(marker.output || "") !== path.resolve(input.outDir)) throw new Error(`NATIVE_ROAD_COMPILE_DONE output mismatch: expected ${input.outDir}, got ${marker.output}`);
|
||||
return marker;
|
||||
}
|
||||
|
||||
function compileArea(area) {
|
||||
const { file, input } = writeInput(area);
|
||||
const result = spawnSync(process.execPath, [compilerCli(), "--input", file], { encoding: "utf8", stdio: ["ignore", "pipe", "inherit"] });
|
||||
if (result.stdout) process.stdout.write(result.stdout);
|
||||
if (result.error) throw result.error;
|
||||
if (result.status !== 0) {
|
||||
const signal = result.signal ? ` signal=${result.signal}` : "";
|
||||
throw new Error(`Native road compiler failed with status=${result.status}${signal}`);
|
||||
}
|
||||
return parseCompletionMarker(result.stdout, input);
|
||||
}
|
||||
|
||||
module.exports = { compileArea, compilerCli, parseCompletionMarker, writeInput };
|
||||
@@ -1,8 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
const fs = require("fs");
|
||||
const { parseOsm } = require("../../packages/road-compiler/src/osm");
|
||||
const trafficSignals = require("../../packages/road-compiler/src/traffic-signals");
|
||||
const { osm: { parseOsm }, trafficSignals } = require("@osm-asset/road-compiler");
|
||||
|
||||
function readTrafficSignalFeatures(stopLinePath, intersectionPath, osmPath) {
|
||||
const controls = parseOsm(fs.readFileSync(osmPath, "utf8")).trafficSignalControls;
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
const fs = require("fs");
|
||||
const { parseOsm } = require("../../packages/road-compiler/src/osm");
|
||||
const {
|
||||
const { osm: { parseOsm }, laneGeometry: {
|
||||
appendCoordinates,
|
||||
haversineMeters,
|
||||
laneCenterline,
|
||||
@@ -10,7 +9,7 @@ const {
|
||||
orientPolyline,
|
||||
polylineLength,
|
||||
polylineMidpoint,
|
||||
} = require("../../packages/road-compiler/src/geometry/lane-geometry");
|
||||
} } = require("@osm-asset/road-compiler");
|
||||
|
||||
const MAX_ROUTES = 5;
|
||||
const MAX_PATH_EDGES = 7;
|
||||
|
||||
Reference in New Issue
Block a user