refactor: move road compiler core into package

This commit is contained in:
2026-08-25 17:12:58 +08:00
parent 5220c6b2a2
commit 73707dabbe
35 changed files with 11739 additions and 454 deletions

View File

@@ -8,7 +8,7 @@ function readAreaConfig(file, options = {}) {
if (!fs.existsSync(file)) {
throw new Error(`Config file not found: ${file}`);
}
return normalizeAreaConfig(JSON.parse(fs.readFileSync(file, "utf8")), options);
return normalizeAreaConfig(JSON.parse(fs.readFileSync(file, "utf8")), { ...options, configDir: path.dirname(path.resolve(file)) });
}
function normalizeAreaConfig(raw, options = {}) {
@@ -113,7 +113,7 @@ function normalizeAreaConfig(raw, options = {}) {
},
nativeRoad: {
edgeLines: booleanOption(raw.nativeRoad?.edgeLines, false, "nativeRoad.edgeLines"),
junctionTemplates: normalizeJunctionTemplates(raw.nativeRoad?.junctionTemplates, repoRoot),
junctionTemplates: normalizeJunctionTemplates(raw.nativeRoad?.junctionTemplates, repoRoot, options.configDir || repoRoot),
},
osm2streets: raw.osm2streets || {
debug_each_step: false,
@@ -154,7 +154,7 @@ function normalizeV2xPreviewConfig(raw) {
};
}
function normalizeJunctionTemplates(raw, repoRoot) {
function normalizeJunctionTemplates(raw, repoRoot, configDir) {
if (raw === undefined || raw === null) return { enabled: false, references: [] };
if (typeof raw !== "object" || Array.isArray(raw)) throw new Error("nativeRoad.junctionTemplates must be an object");
const enabled = booleanOption(raw.enabled, false, "nativeRoad.junctionTemplates.enabled");
@@ -164,7 +164,7 @@ function normalizeJunctionTemplates(raw, repoRoot) {
const nodeId = requireText(item.nodeId, `nativeRoad.junctionTemplates.references[${index}].nodeId`);
const template = item.template ?? "cross-v1";
if (template !== "cross-v1") throw new Error(`nativeRoad.junctionTemplates.references[${index}].template must be \"cross-v1\"`);
const referenceFile = item.referenceFile ? path.resolve(item.referenceFile) : null;
const referenceFile = item.referenceFile ? path.resolve(configDir, item.referenceFile) : null;
const coordinateSystem = item.coordinateSystem ?? "GCJ-02";
if (coordinateSystem !== "GCJ-02") throw new Error(`nativeRoad.junctionTemplates.references[${index}].coordinateSystem must be \"GCJ-02\"`);
const cornerRadiusMultiplier = numberOption(item.cornerRadiusMultiplier, 1, `nativeRoad.junctionTemplates.references[${index}].cornerRadiusMultiplier`, 0.75, 1.25);
@@ -181,7 +181,7 @@ function normalizeJunctionTemplates(raw, repoRoot) {
if (!Array.isArray(nodeIds) || nodeIds.length < 2 || nodeIds.some((value) => typeof value !== "string" && typeof value !== "number")) throw new Error(`nativeRoad.junctionTemplates.clusters[${index}].nodeIds must contain at least two node ids`);
const template = item.template ?? "complex-junction-v1";
if (template !== "cross-cluster-v1" && template !== "complex-junction-v1") throw new Error(`nativeRoad.junctionTemplates.clusters[${index}].template must be \"cross-cluster-v1\" or \"complex-junction-v1\"`);
const referenceFile = item.referenceFile ? path.resolve(item.referenceFile) : null;
const referenceFile = item.referenceFile ? path.resolve(configDir, item.referenceFile) : null;
const approachWidthMultiplier = numberOption(item.approachWidthMultiplier, 1.45, `nativeRoad.junctionTemplates.clusters[${index}].approachWidthMultiplier`, 1, 1.8);
const approachLengthMeters = numberOption(item.approachLengthMeters, 32, `nativeRoad.junctionTemplates.clusters[${index}].approachLengthMeters`, 10, 50);
const coreRadiusMeters = numberOption(item.coreRadiusMeters, 28, `nativeRoad.junctionTemplates.clusters[${index}].coreRadiusMeters`, 12, 80);
@@ -272,7 +272,24 @@ function booleanOption(value, fallback, label) {
throw new Error(`${label} must be boolean`);
}
function toRoadCompilerInput(area) {
return {
areaId: area.id,
osmFile: area.input,
outDir: area.outputs.nativeRoadDir,
stagingDir: area.outputs.pipelineDir,
overridesFile: area.outputs.nativeRoadOverrides,
trafficSignalsFile: area.outputs.nativeTrafficSignals,
comparisonDir: area.outputs.geojsonDir,
options: {
edgeLines: area.nativeRoad.edgeLines,
junctionTemplates: area.nativeRoad.junctionTemplates,
},
};
}
module.exports = {
normalizeAreaConfig,
readAreaConfig,
toRoadCompilerInput,
};