5 Commits

13 changed files with 220 additions and 24 deletions

View File

@@ -8,4 +8,4 @@ npx road-compiler --input /absolute/path/road-compiler.input.json
The compiler does not read host area configuration. The caller owns configuration normalization and creates the input JSON. Successful compilation writes exactly one `NATIVE_ROAD_COMPILE_DONE` JSON marker to stdout.
Run `npm test` and `npm run test:road-parity` after `npm ci` to validate the package without the host repository.
Run `npm test` after `npm ci` to validate the package and its two self-contained area fixtures without the host repository. The migrated baselines remain upgrade corpus; the host repository owns the strict path-normalized parity gate.

View File

@@ -4,13 +4,17 @@
const fs = require("fs");
const path = require("path");
const { compiler } = require("../src");
const { exportNativeRoadPackage } = require("../src/export/native-road-package");
const args = process.argv.slice(2);
const index = args.indexOf("--input");
if (index < 0 || !args[index + 1] || index + 2 !== args.length) {
throw new Error("Usage: road-compiler --input <RoadCompilerInput.json>");
const exportIndex = args.indexOf("--export-zip");
const expectedLength = exportIndex >= 0 ? 4 : 2;
if (index < 0 || !args[index + 1] || (exportIndex >= 0 && !args[exportIndex + 1]) || args.length !== expectedLength) {
throw new Error("Usage: road-compiler --input <RoadCompilerInput.json> [--export-zip <output.zip>]");
}
const inputFile = path.resolve(args[index + 1]);
const input = JSON.parse(fs.readFileSync(inputFile, "utf8"));
const { result, comparison } = compiler.compileInput(input);
if (exportIndex >= 0) exportNativeRoadPackage(input.outDir, path.resolve(args[exportIndex + 1]));
console.log(`NATIVE_ROAD_COMPILE_DONE ${JSON.stringify({ areaId: input.areaId, roads: result.model.roads.length, endpoints: result.model.endpoints.length, diagnostics: result.diagnostics.length, output: input.outDir, comparison })}`);

View File

@@ -1,4 +1,8 @@
# Native Road Package v1
# Native Road Package v1.1
`native-road-package/v1.1` adds a required `manifest.json` to the v1 output.
Consumers may ignore this file when reading legacy v1 packages, but v1.1
consumers must validate and consume it.
`native-road-package/v1` defines the boundary between the host area pipeline
and the native road compiler. The compiler accepts only the input below; it
@@ -35,6 +39,12 @@ shape and owns all path derivation.
`outDir` contains these JSON documents:
- `manifest.json`: `{ contract: "native-road-package/v1.1", areaId, layers }`.
`layers` declares exactly the twelve GeoJSON sources. Renderable entries have
`role` (`surface` or `marking`) and `materialLayer`; semantic entries have
`role: "semantic"` and no material. Optional `splitBy` contains one property
and exact-match cases plus one default case.
- `compiled.json`: top-level keys are `schema`, `areaId`, `source`, `model`,
`movements`, `trafficSignals`, `diagnostics`, and `layers`. `source` has
`osm`, `overrides`, and `trafficSignals` paths.
@@ -63,6 +73,20 @@ shape and owns all path derivation.
The editable signal source at `trafficSignalsFile` is a sibling of `outDir`.
It is included in the parity baseline because regeneration must be stable.
## ZIP Export
The Web workbench's **导出道路包** action and the CLI's optional
`--export-zip <output.zip>` emit the portable package form. The ZIP root is
flat: it contains the six JSON documents above and `layers/*.geojson`, without
an enclosing area directory. It contains no OSM input, overrides, or compiler
workspace paths. `compiled.json` retains only model and movement data needed by
the downstream preview; its internal `source` paths are removed.
The ZIP manifest adds informational `generator: { name, version }`. Consumers
must select compatibility only from `contract`, `areaId`, and declared files.
Entry order and timestamp are fixed, so unchanged compiled output exports to
the same ZIP hash.
Successful CLI execution prints exactly one completion marker:
```text

5
package-lock.json generated
View File

@@ -1,13 +1,14 @@
{
"name": "@osm-asset/road-compiler",
"version": "0.1.0",
"version": "0.2.2",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@osm-asset/road-compiler",
"version": "0.1.0",
"version": "0.2.2",
"dependencies": {
"fflate": "0.8.3",
"ol": "10.10.0"
},
"bin": {

View File

@@ -1,6 +1,6 @@
{
"name": "@osm-asset/road-compiler",
"version": "0.1.0",
"version": "0.3.0",
"private": false,
"type": "commonjs",
"main": "src/index.js",
@@ -8,11 +8,12 @@
"road-compiler": "bin/road-compiler.js"
},
"scripts": {
"test": "node test/index.js",
"test:road-parity": "node test/road-parity.js",
"road:workbench": "node bin/road-workbench.js"
"test": "node test/index.js && node test/fixtures.js",
"road:workbench": "node bin/road-workbench.js",
"road:export": "node bin/road-compiler.js"
},
"dependencies": {
"fflate": "0.8.3",
"ol": "10.10.0"
}
}

View File

@@ -4,6 +4,7 @@
const fs = require("fs");
const path = require("path");
const { compileRoadModel, compileGeometry, loadOverrides, validateOverrides, writeJsonAtomic } = require("./native-road");
const { LAYER_REGISTRY, manifestForArea, validatePublishedLayers } = require("./layer-manifest");
const { loadOrGenerate, runtime } = require("../native-traffic-signals");
function compileInput(input) {
@@ -53,7 +54,7 @@ function compileInput(input) {
movements: compiled.movements,
trafficSignals: { assemblies: "traffic-signal-assemblies.json", runtime: "traffic-signals.json", count: signalRuntime.signals.length },
diagnostics: compiled.diagnostics,
layers: { roadSurface: "layers/road_surface.geojson", edgeLines: "layers/edge_lines.geojson", sidewalkSurface: "layers/sidewalk_surface.geojson", intersectionSurface: "layers/intersection_surface.geojson", laneCenterlines: "layers/lane_centerlines.geojson", laneSeparators: "layers/lane_separators.geojson", centerLines: "layers/center_lines.geojson", directionArrows: "layers/direction_arrows.geojson", turnArrows: "layers/turn_arrows.geojson", crosswalks: "layers/crosswalks.geojson", vehicleStopLines: "layers/vehicle_stop_lines.geojson", connectors: "layers/connectors.geojson" },
layers: Object.fromEntries(LAYER_REGISTRY.map((layer) => [layer.key, `layers/${layer.source}.geojson`])),
};
const comparison = compareOsm2Streets(area, result.model, compiled);
writeJsonAtomic(path.join(staging, "compiled.json"), result);
@@ -61,18 +62,10 @@ function compileInput(input) {
writeJsonAtomic(path.join(staging, "comparison.json"), comparison);
writeJsonAtomic(path.join(staging, "traffic-signal-assemblies.json"), signalDocument.assemblies);
writeJsonAtomic(path.join(staging, "traffic-signals.json"), signalRuntime);
writeJsonAtomic(path.join(staging, "layers", "road_surface.geojson"), compiled.roadSurface);
writeJsonAtomic(path.join(staging, "layers", "edge_lines.geojson"), compiled.edgeLines);
writeJsonAtomic(path.join(staging, "layers", "sidewalk_surface.geojson"), compiled.sidewalkSurface);
writeJsonAtomic(path.join(staging, "layers", "intersection_surface.geojson"), compiled.intersectionSurface);
writeJsonAtomic(path.join(staging, "layers", "lane_centerlines.geojson"), compiled.laneCenterlines);
writeJsonAtomic(path.join(staging, "layers", "lane_separators.geojson"), compiled.laneSeparators);
writeJsonAtomic(path.join(staging, "layers", "center_lines.geojson"), compiled.centerLines);
writeJsonAtomic(path.join(staging, "layers", "direction_arrows.geojson"), compiled.directionArrows);
writeJsonAtomic(path.join(staging, "layers", "turn_arrows.geojson"), compiled.turnArrows);
writeJsonAtomic(path.join(staging, "layers", "crosswalks.geojson"), compiled.crosswalks);
writeJsonAtomic(path.join(staging, "layers", "vehicle_stop_lines.geojson"), compiled.vehicleStopLines);
writeJsonAtomic(path.join(staging, "layers", "connectors.geojson"), compiled.connectors);
for (const layer of LAYER_REGISTRY) writeJsonAtomic(path.join(staging, "layers", `${layer.source}.geojson`), compiled[layer.key]);
const manifest = manifestForArea(area.id);
validatePublishedLayers(staging, manifest);
writeJsonAtomic(path.join(staging, "manifest.json"), manifest);
fs.rmSync(area.outputs.nativeRoadDir, { recursive: true, force: true });
fs.renameSync(staging, area.outputs.nativeRoadDir);
return { area, result, comparison };

View File

@@ -0,0 +1,49 @@
"use strict";
// The compiler's layer registry is the single source of truth for published
// GeoJSON files and the Blender-facing manifest. Keep rendering details limited
// to material slot names; the host owns the actual material definitions.
const LAYER_REGISTRY = Object.freeze([
{ key: "roadSurface", source: "road_surface", role: "surface", materialLayer: "road_surface" },
{ key: "edgeLines", source: "edge_lines", role: "marking", materialLayer: "lane_separators" },
{ key: "intersectionSurface", source: "intersection_surface", role: "surface", materialLayer: "intersection_surface" },
{ key: "sidewalkSurface", source: "sidewalk_surface", role: "surface", materialLayer: "sidewalks" },
{ key: "laneSeparators", source: "lane_separators", role: "marking", materialLayer: "lane_separators", splitBy: { prop: "color", cases: [{ match: "yellow", material: "native_lane_separator_yellow" }, { default: true, material: "lane_separators" }] } },
{ key: "centerLines", source: "center_lines", role: "marking", materialLayer: "center_lines", splitBy: { prop: "color", cases: [{ match: "white", material: "native_center_line_white" }, { default: true, material: "center_lines" }] } },
{ key: "directionArrows", source: "direction_arrows", role: "marking", materialLayer: "lane_arrows_webscale" },
{ key: "turnArrows", source: "turn_arrows", role: "marking", materialLayer: "lane_arrows_webscale" },
{ key: "crosswalks", source: "crosswalks", role: "marking", materialLayer: "crosswalks" },
{ key: "vehicleStopLines", source: "vehicle_stop_lines", role: "marking", materialLayer: "vehicle_stop_lines" },
{ key: "laneCenterlines", source: "lane_centerlines", role: "semantic" },
{ key: "connectors", source: "connectors", role: "semantic" },
]);
function manifestForArea(areaId) {
return {
contract: "native-road-package/v1.1",
areaId,
layers: LAYER_REGISTRY.map(({ source, role, materialLayer, splitBy }) => ({
source,
role,
...(materialLayer ? { materialLayer } : {}),
...(splitBy ? { splitBy } : {}),
})),
};
}
function validatePublishedLayers(directory, manifest) {
const declared = new Set();
for (const layer of manifest.layers) {
if (!layer || typeof layer.source !== "string" || declared.has(layer.source)) throw new Error("Manifest has duplicate or invalid source.");
declared.add(layer.source);
const file = require("path").join(directory, "layers", `${layer.source}.geojson`);
if (!require("fs").existsSync(file)) throw new Error(`Manifest source is missing: ${layer.source}`);
}
const files = require("fs").existsSync(require("path").join(directory, "layers"))
? require("fs").readdirSync(require("path").join(directory, "layers")).filter((name) => name.endsWith(".geojson")).map((name) => name.slice(0, -8))
: [];
const extras = files.filter((source) => !declared.has(source));
if (extras.length) throw new Error(`Unmanifested GeoJSON source: ${extras.join(", ")}`);
}
module.exports = { LAYER_REGISTRY, manifestForArea, validatePublishedLayers };

View File

@@ -0,0 +1,56 @@
"use strict";
const fs = require("fs");
const path = require("path");
const { zipSync } = require("fflate");
const { LAYER_REGISTRY, validatePublishedLayers } = require("../compile/layer-manifest");
const ROOT_FILES = [
"manifest.json",
"compiled.json",
"diagnostics.json",
"comparison.json",
"traffic-signal-assemblies.json",
"traffic-signals.json",
];
const GENERATOR = Object.freeze({ name: "road-compiler", version: "0.3.0" });
function packageEntries(directory) {
const manifestPath = path.join(directory, "manifest.json");
if (!fs.existsSync(manifestPath)) throw new Error(`Native road package manifest is missing: ${manifestPath}`);
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
if (manifest.contract !== "native-road-package/v1.1") throw new Error("Native road package requires native-road-package/v1.1.");
if (!manifest.areaId || typeof manifest.areaId !== "string") throw new Error("Native road package manifest areaId is required.");
validatePublishedLayers(directory, manifest);
const files = [];
for (const relative of ROOT_FILES) files.push(relative);
for (const layer of LAYER_REGISTRY) files.push(`layers/${layer.source}.geojson`);
for (const relative of files) {
const file = path.join(directory, relative);
if (!fs.existsSync(file) || !fs.statSync(file).isFile()) throw new Error(`Native road package file is missing: ${relative}`);
}
return { manifest, files: [...new Set(files)].sort() };
}
function exportNativeRoadPackage(directory, destination = null) {
const { manifest, files } = packageEntries(directory);
const entries = Object.fromEntries(files.map((relative) => [relative, fs.readFileSync(path.join(directory, relative))]));
// The model/movements remain needed by host preview generation, but absolute
// compiler-workspace paths are not part of the exported package contract.
const compiled = JSON.parse(entries["compiled.json"].toString("utf8"));
delete compiled.source;
entries["compiled.json"] = Buffer.from(`${JSON.stringify(compiled, null, 2)}\n`);
entries["manifest.json"] = Buffer.from(`${JSON.stringify({ ...manifest, generator: GENERATOR }, null, 2)}\n`);
// ZIP timestamps start at 1980; a fixed value keeps repeated exports byte-stable.
const bytes = zipSync(entries, { level: 6, mtime: new Date("1980-01-01T00:00:00Z") });
if (destination) {
fs.mkdirSync(path.dirname(destination), { recursive: true });
const temporary = `${destination}.tmp-${process.pid}`;
fs.writeFileSync(temporary, bytes);
fs.renameSync(temporary, destination);
}
return { bytes, manifest, files };
}
module.exports = { ROOT_FILES, GENERATOR, packageEntries, exportNativeRoadPackage };

View File

@@ -9,6 +9,8 @@ module.exports = {
trafficSignals: require("./traffic-signals"),
nativeTrafficSignals: require("./native-traffic-signals"),
nativeRoad: require("./compile/native-road"),
layerManifest: require("./compile/layer-manifest"),
nativeRoadPackage: require("./export/native-road-package"),
compiler: require("./compile/compiler"),
check: require("./check"),
};

49
test/fixtures.js Normal file
View File

@@ -0,0 +1,49 @@
"use strict";
const assert = require("assert/strict");
const crypto = require("crypto");
const { execFileSync } = require("child_process");
const fs = require("fs");
const path = require("path");
const { unzipSync, strFromU8 } = require("fflate");
const { compiler } = require("../src");
const { exportNativeRoadPackage } = require("../src/export/native-road-package");
const root = path.resolve(__dirname, "..");
for (const areaId of ["fengshu-er-road", "nantaizi-lake-innovation-valley"]) {
const isFengshu = areaId === "fengshu-er-road";
const outputRoot = path.join(root, "outputs", areaId);
const input = {
areaId,
osmFile: path.join(root, "inputs", "osm", isFengshu ? "枫树二路.osm" : "南台子湖创新谷OSM.osm"),
outDir: path.join(outputRoot, "native-road"),
stagingDir: path.join(outputRoot, "_pipeline"),
overridesFile: path.join(outputRoot, "native-road-overrides.json"),
trafficSignalsFile: path.join(outputRoot, "native-traffic-signals.json"),
comparisonDir: path.join(outputRoot, "osm2streets_web_out"),
options: { edgeLines: false, junctionTemplates: { enabled: false, references: [] } },
};
assert.ok(fs.existsSync(path.join(root, "test", "baseline", `${areaId}.json`)));
const { result } = compiler.compileInput(input);
assert.equal(result.areaId, areaId);
assert.ok(fs.existsSync(path.join(input.outDir, "compiled.json")));
const first = exportNativeRoadPackage(input.outDir);
const second = exportNativeRoadPackage(input.outDir);
assert.equal(crypto.createHash("sha256").update(first.bytes).digest("hex"), crypto.createHash("sha256").update(second.bytes).digest("hex"));
const entries = unzipSync(first.bytes);
const manifest = JSON.parse(strFromU8(entries["manifest.json"]));
assert.equal(manifest.areaId, areaId);
assert.equal(manifest.contract, "native-road-package/v1.1");
assert.deepEqual(manifest.generator, { name: "road-compiler", version: "0.3.0" });
assert.equal("source" in JSON.parse(strFromU8(entries["compiled.json"])), false);
assert.equal(Object.keys(entries).some((entry) => entry.includes("override") || entry.endsWith(".osm")), false);
assert.equal(Object.keys(entries).length, 18);
if (isFengshu) {
const inputFile = path.join(outputRoot, "road-compiler-input.json");
const archive = path.join(outputRoot, "export.zip");
fs.writeFileSync(inputFile, `${JSON.stringify(input)}\n`);
execFileSync(process.execPath, [path.join(root, "bin", "road-compiler.js"), "--input", inputFile, "--export-zip", archive]);
assert.ok(fs.existsSync(archive));
}
}
console.log("road compiler fixture tests passed");

View File

@@ -11,6 +11,12 @@ assert.equal(typeof compiler.turnLaneArrows.buildCustomTurnLaneArrows, "function
assert.equal(typeof compiler.complexJunction.buildComplexJunctionGeometry, "function");
assert.equal(typeof compiler.nativeRoad.compileRoadModel, "function");
assert.equal(typeof compiler.check.checkOutput, "function");
assert.equal(typeof compiler.layerManifest.manifestForArea, "function");
const manifest = compiler.layerManifest.manifestForArea("fixture");
assert.equal(manifest.contract, "native-road-package/v1.1");
assert.equal(manifest.layers.length, 12);
assert.deepEqual(manifest.layers.filter((layer) => layer.role === "semantic").map((layer) => layer.source), ["lane_centerlines", "connectors"]);
assert.equal(manifest.layers.find((layer) => layer.source === "center_lines").splitBy.cases[0].match, "white");
const fixture = path.join(__dirname, "fixtures", "fengshu-er-road.osm");
assert.ok(fs.existsSync(fixture));
assert.throws(() => compiler.compiler.validateInput({ id: "area" }), /RoadCompilerInput/);

View File

@@ -1,4 +1,4 @@
<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>道路编译工作台</title><link rel="stylesheet" href="/vendor/ol/ol.css"><link rel="stylesheet" href="/app.css"></head>
<body><header><strong>道路编译工作台</strong><span id="area"></span><span id="status"></span><span id="dirty-state" aria-live="polite"></span><label style="display:inline;margin:0 0 0 auto;white-space:nowrap"><input id="scene-preview" type="checkbox"> 场景效果</label><button id="save">保存修改</button><button id="compile">保存并重新生成</button></header>
<body><header><strong>道路编译工作台</strong><span id="area"></span><span id="status"></span><span id="dirty-state" aria-live="polite"></span><label style="display:inline;margin:0 0 0 auto;white-space:nowrap"><input id="scene-preview" type="checkbox"> 场景效果</label><a id="export-package" href="/api/export.zip" download>导出道路包</a><button id="save">保存修改</button><button id="compile">保存并重新生成</button></header>
<main><aside class="issues"><h1>图层</h1><label><input data-layer="osm" type="checkbox" checked> OSM 道路中心线</label><label><input data-layer="native" type="checkbox" checked> 自研道路与路口面</label><label><input data-layer="sidewalks" type="checkbox" checked> 路缘与步行带</label><label><input data-layer="lanes" type="checkbox" checked> 车道与转向路径</label><label><input data-layer="gaodeReference" type="checkbox" checked> 高德规整路口参考</label><label><input data-layer="reference" type="checkbox"> osm2streets 参考面</label><hr><h1>当前编译概览</h1><dl id="summary"></dl><hr><h1>待检查问题</h1><div id="diagnostic-filters" class="segmented"><button data-diagnostic-filter="all" type="button">全部</button><button data-diagnostic-filter="candidates" type="button">可连接</button><button data-diagnostic-filter="other" type="button">其他</button></div><ul id="diagnostics"></ul></aside><section id="map" class="map"></section><aside class="inspector"><h1>当前道路设置</h1><p id="hint">点击道路、车道、转向路径或路口面以查看详情。</p><section id="selected-junction" hidden><h2>当前路口</h2><output id="junction-detail"></output></section><form id="road-form" hidden><label>道路</label><output id="road-name"></output><output id="movement-summary"></output><output id="lane-convention"></output><section id="selected-movement" hidden><h2>当前行驶动作</h2><output id="movement-detail"></output></section><div id="direction-switch"></div><label>本方向道路宽度(米)<input id="width" type="number" min="1" step="0.01"></label><label>本方向车道数<input id="lanes" type="number" min="1" step="1"></label><label><input id="left" type="checkbox"> 左侧有路缘与步行带</label><label><input id="right" type="checkbox"> 右侧有路缘与步行带</label><button type="submit">暂存本道路修改</button></form><form id="center-line-form" hidden><h2 id="marking-style-heading">道路中心线样式</h2><output id="center-line-segment"></output><label>样式<select id="center-line-style"><option value="yellow-dashed">黄色虚线(默认)</option><option value="white-dashed">白色虚线</option><option value="yellow-solid">黄色实线</option><option value="white-solid">白色实线</option></select></label><button type="submit">暂存标线样式</button></form><hr><h2>路口连接</h2><div id="connections">请选择一条道路。</div><button id="add-connection" type="button" hidden>手工新增驶出连接</button><details><summary>技术详情与来源</summary><pre id="evidence"></pre></details></aside></main><script type="importmap">{"imports":{"rbush":"/vendor/rbush/index.js","quickselect":"/vendor/quickselect/index.js"}}</script><script type="module" src="/app.js"></script></body></html>

View File

@@ -7,6 +7,7 @@ const path = require("path");
const { loadOverrides, validateOverrides, writeJsonAtomic } = require("../src/compile/native-road");
const { generate, validateDocument, runtime } = require("../src/native-traffic-signals");
const { convertGeoJson } = require("../src/reference/gaode");
const { exportNativeRoadPackage } = require("../src/export/native-road-package");
function startWorkbench({ area, configPath, repoRoot, compileFresh, readAreaConfig, junctionReference = null, debug = false, port = 8787 }) {
if (typeof junctionReference === "string") junctionReference = readJunctionReference(junctionReference);
@@ -31,6 +32,16 @@ function handle(request, response, area, context, junctionReference, debug = fal
if (request.method === "GET" && url.pathname === "/app.css") return sendFile(response, path.join(__dirname, "client", "app.css"), "text/css; charset=utf-8");
if (request.method === "GET" && url.pathname.startsWith("/vendor/")) return sendVendorFile(response, url.pathname, context.repoRoot);
if (request.method === "GET" && url.pathname === "/api/state") return sendJson(response, 200, state(area, junctionReference, debug));
if (request.method === "GET" && url.pathname === "/api/export.zip") return Promise.resolve().then(() => {
const exported = exportNativeRoadPackage(area.outputs.nativeRoadDir);
response.writeHead(200, {
"Content-Type": "application/zip",
"Content-Disposition": `attachment; filename="${area.id}.native-road.zip"`,
"Content-Length": exported.bytes.length,
"Cache-Control": "no-store",
});
response.end(Buffer.from(exported.bytes));
}).catch((error) => sendJson(response, 400, { ok: false, error: error.message }));
if (request.method === "POST" && url.pathname === "/api/traffic-signals") return readBody(request).then((body) => {
const document = validateDocument(body, fs.readFileSync(area.input, "utf8"));
writeJsonAtomic(area.outputs.nativeTrafficSignals, document);