369 lines
12 KiB
JavaScript
369 lines
12 KiB
JavaScript
'use strict';
|
|
|
|
const fs = require('fs');
|
|
const { structureFromTopology } = require('./junction-topology');
|
|
const { resolveJunctionBuildPlan } = require('./junction-build-plan');
|
|
const { buildJunctionBoundaryGraph, buildCoreClosure, ringsOverlap } = require('./junction-boundary-graph');
|
|
const { rectangle } = require('./junction-pavement-region');
|
|
const { assemblePavementSurfaces } = require('./complex-junction-surfaces');
|
|
const { complexJunctionMetrics: calculateComplexJunctionMetrics } = require('./junction-metrics');
|
|
const { convertGeoJson, boundsOf } = require('../reference/gaode');
|
|
const { normalizeHeading, positiveHeadingDelta } = require('./complex-junction-geometry');
|
|
const {
|
|
applyCrossingDisplacements,
|
|
buildApproachSurfacesAndStops,
|
|
buildArmCrossings,
|
|
buildCornerCompositions,
|
|
planArmCrossings,
|
|
} = require('./complex-junction-facilities');
|
|
const metricsCache = new WeakMap();
|
|
const CORNER_FILLET_SEGMENTS = 12;
|
|
// Must match DEFAULT_SIDEWALK_WIDTH_METERS in native-road.js so the corner band
|
|
// lines up with the straight strips it joins.
|
|
const SIDEWALK_WIDTH_METERS = 2;
|
|
// Compatibility export for current compiler consumers. The build-plan module is
|
|
// pure and is the shared authority for geometry and direct-edit identity.
|
|
const resolveComplexJunctionTopology = resolveJunctionBuildPlan;
|
|
|
|
function buildComplexJunctionGeometry(model, cluster, helpers) {
|
|
const nodeIds = new Set(cluster.nodeIds.map(String));
|
|
const nodes = cluster.nodeIds.map((id) => helpers.junctionPlans.get(String(id))?.node).filter(Boolean);
|
|
if (nodes.length < 2)
|
|
return {
|
|
features: [],
|
|
diagnostics: [
|
|
helpers.diagnostic(
|
|
'warning',
|
|
`junction-cluster:${cluster.id}`,
|
|
[...nodeIds],
|
|
'complex-junction-insufficient-nodes',
|
|
'复合路口至少需要两个有效节点。',
|
|
null,
|
|
),
|
|
],
|
|
};
|
|
const center = nodes.reduce(
|
|
(sum, point) => [sum[0] + point[0] / nodes.length, sum[1] + point[1] / nodes.length],
|
|
[0, 0],
|
|
);
|
|
const topology = resolveComplexJunctionTopology(model, cluster, helpers);
|
|
const { carriageways, arms } = topology;
|
|
const approaches = arms.map((arm) => arm.representative);
|
|
if (approaches.length < 3)
|
|
return {
|
|
features: [],
|
|
diagnostics: [
|
|
helpers.diagnostic(
|
|
'warning',
|
|
`junction-cluster:${cluster.id}`,
|
|
[...nodeIds],
|
|
'complex-junction-insufficient-approaches',
|
|
'复合路口无法识别足够的外部进口。',
|
|
center,
|
|
),
|
|
],
|
|
};
|
|
const { calibration, coreRadius } = complexJunctionMetrics(cluster);
|
|
const shape = classifyJunctionShape(arms, center, helpers);
|
|
if (shape.kind === 'unsupported')
|
|
return {
|
|
features: [],
|
|
diagnostics: [
|
|
helpers.diagnostic(
|
|
'warning',
|
|
`junction-cluster:${cluster.id}`,
|
|
[...nodeIds],
|
|
'complex-junction-shape-fallback',
|
|
`${shape.label},已回退为普通路口渲染。`,
|
|
center,
|
|
),
|
|
],
|
|
shape,
|
|
center,
|
|
};
|
|
const outerRadius = complexJunctionMetrics(cluster).approachOuterRadius;
|
|
const crossingControls = helpers.clusterCrossingControls || new Map();
|
|
const { crossingPlans, armCrosswalkRadiusFor } = planArmCrossings(
|
|
model,
|
|
arms,
|
|
center,
|
|
coreRadius,
|
|
crossingControls,
|
|
helpers,
|
|
);
|
|
const boundaryGraph =
|
|
helpers.boundaryGraph ||
|
|
buildComplexApproachBoundaryGraph(topology, center, cluster, helpers, model.exclusionRegions || []);
|
|
const {
|
|
boundaryParts,
|
|
stopLines,
|
|
diagnostics: transitionDiagnostics,
|
|
} = buildApproachSurfacesAndStops({
|
|
model,
|
|
cluster,
|
|
helpers,
|
|
center,
|
|
arms,
|
|
carriageways,
|
|
boundaryGraph,
|
|
crossingControls,
|
|
crossingPlans,
|
|
armCrosswalkRadiusFor,
|
|
});
|
|
const publishedCarriageways = new Set(boundaryParts.map(({ item }) => item.key));
|
|
const {
|
|
crosswalks,
|
|
crossingRegions,
|
|
diagnostics: crossingDiagnostics,
|
|
} = buildArmCrossings({
|
|
arms,
|
|
center,
|
|
cluster,
|
|
helpers,
|
|
publishedCarriageways,
|
|
crossingPlans,
|
|
armCrosswalkRadiusFor,
|
|
});
|
|
transitionDiagnostics.push(...crossingDiagnostics);
|
|
const coreClosure = buildCoreClosure(topology, boundaryGraph, center);
|
|
const {
|
|
cornerFills,
|
|
sidewalkCorners,
|
|
islands,
|
|
crosswalks: cornerCrosswalks,
|
|
armEndpointOffsets,
|
|
diagnostics: cornerDiagnostics,
|
|
} = buildCornerCompositions({
|
|
model,
|
|
cluster,
|
|
helpers,
|
|
center,
|
|
nodeIds,
|
|
arms,
|
|
boundaryGraph,
|
|
publishedCarriageways,
|
|
coreRadius,
|
|
outerRadius,
|
|
});
|
|
crosswalks.push(...cornerCrosswalks);
|
|
applyCrossingDisplacements(arms, armEndpointOffsets, crosswalks, crossingRegions, stopLines);
|
|
const { features, diagnostics: pavementDiagnostics } = assemblePavementSurfaces({
|
|
model,
|
|
cluster,
|
|
helpers,
|
|
center,
|
|
nodeIds,
|
|
topology,
|
|
shape,
|
|
coreRadius,
|
|
coreClosure,
|
|
boundaryParts,
|
|
cornerFills,
|
|
crossingRegions,
|
|
});
|
|
cornerDiagnostics.push(...pavementDiagnostics);
|
|
return publishComplexJunction({
|
|
model,
|
|
cluster,
|
|
helpers,
|
|
center,
|
|
nodeIds,
|
|
topology,
|
|
shape,
|
|
approaches,
|
|
calibration,
|
|
coreRadius,
|
|
outerRadius,
|
|
features,
|
|
islands,
|
|
sidewalkCorners,
|
|
crosswalks,
|
|
stopLines,
|
|
transitionDiagnostics,
|
|
cornerDiagnostics,
|
|
});
|
|
}
|
|
|
|
function publishComplexJunction({
|
|
model,
|
|
cluster,
|
|
helpers,
|
|
center,
|
|
nodeIds,
|
|
topology,
|
|
shape,
|
|
approaches,
|
|
calibration,
|
|
coreRadius,
|
|
outerRadius,
|
|
features,
|
|
islands,
|
|
sidewalkCorners,
|
|
crosswalks,
|
|
stopLines,
|
|
transitionDiagnostics,
|
|
cornerDiagnostics,
|
|
}) {
|
|
// `coreRadiusMeters` is only consulted when there is no reference geometry.
|
|
// Under calibration the radius comes from the reference span, so a configured
|
|
// value that silently does nothing has to be reported, not swallowed.
|
|
const configuredRadiusIgnored =
|
|
calibration && Number.isFinite(cluster.coreRadiusMeters) && Math.abs(coreRadius - cluster.coreRadiusMeters) > 0.5;
|
|
const configurationDiagnostics = configuredRadiusIgnored
|
|
? [
|
|
helpers.diagnostic(
|
|
'info',
|
|
`junction-cluster:${cluster.id}`,
|
|
[...nodeIds],
|
|
'complex-junction-configured-radius-ignored',
|
|
`已按参考几何校准核心半径为 ${Math.round(coreRadius * 10) / 10} 米,配置的 coreRadiusMeters=${cluster.coreRadiusMeters} 在有参考文件时不生效。`,
|
|
center,
|
|
),
|
|
]
|
|
: [];
|
|
const knownIslandFeatures = (model.exclusionRegions || [])
|
|
.filter(
|
|
(region) =>
|
|
Array.isArray(region.ring) &&
|
|
region.ring.length >= 4 &&
|
|
region.ring.some((point) => helpers.distanceMeters(point, center) <= outerRadius + 12),
|
|
)
|
|
.map((region) => ({
|
|
type: 'Feature',
|
|
properties: {
|
|
native_id: `osm-traffic-island:${region.id}`,
|
|
object_key: `island:osm/${region.id}`,
|
|
kind: 'island',
|
|
osm_way_ids: String(region.id),
|
|
source: region.source,
|
|
facility_evidence: 'source-osm-region',
|
|
},
|
|
geometry: { type: 'Polygon', coordinates: [region.ring] },
|
|
}));
|
|
const allFeatures = [
|
|
...features,
|
|
...islands,
|
|
...sidewalkCorners,
|
|
...crosswalks,
|
|
...stopLines,
|
|
...knownIslandFeatures,
|
|
];
|
|
const structure = structureFromTopology(topology, shape, center, allFeatures);
|
|
for (const feature of allFeatures) {
|
|
feature.properties.junction_key = structure.junctionKey;
|
|
feature.properties.topology_fingerprint = structure.topologyFingerprint;
|
|
}
|
|
// Persist the compiler authority with a generated road surface, including the
|
|
// supported-shape case where only the central polygon had to be skipped.
|
|
const carrier = features.find((feature) => feature.properties.kind === 'complex-core') || features[0];
|
|
if (carrier) carrier.properties.junction_structure = structure;
|
|
return {
|
|
structure,
|
|
features,
|
|
islands: [...islands, ...sidewalkCorners],
|
|
crosswalks,
|
|
stopLines,
|
|
center,
|
|
shape,
|
|
approaches,
|
|
diagnostics: [
|
|
...transitionDiagnostics,
|
|
...cornerDiagnostics,
|
|
...configurationDiagnostics,
|
|
helpers.diagnostic(
|
|
'info',
|
|
`junction-cluster:${cluster.id}`,
|
|
[...nodeIds],
|
|
calibration ? 'complex-junction-reference-calibrated' : 'complex-junction-generated',
|
|
calibration
|
|
? `已识别为${shape.label},使用参考几何校准参数后重新生成 ${approaches.length} 个进口、道路面、${crosswalks.length} 组斑马线和停止线。`
|
|
: `已识别为${shape.label},独立生成 ${approaches.length} 个进口、道路面、${crosswalks.length} 组斑马线和停止线。`,
|
|
center,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
function buildComplexApproachBoundaryGraph(topology, center, cluster, helpers, exclusionRegions = []) {
|
|
const metrics = complexJunctionMetrics(cluster);
|
|
return buildJunctionBoundaryGraph(
|
|
topology,
|
|
center,
|
|
{
|
|
outerRadiusFor: (item) =>
|
|
Math.max(metrics.coreRadius + 4, metrics.approachOuterRadius - Math.max(0, Number(item.approach.cutback) || 0)),
|
|
innerRadiusFor: () => metrics.approachInnerRadius,
|
|
},
|
|
helpers,
|
|
exclusionRegions,
|
|
);
|
|
}
|
|
|
|
function classifyJunctionShape(arms, center, helpers) {
|
|
const headings = arms.map((arm) => arm.heading);
|
|
const throughPairs = [];
|
|
for (let first = 0; first < headings.length; first += 1) {
|
|
for (let second = first + 1; second < headings.length; second += 1) {
|
|
const delta = Math.abs(normalizeHeading(headings[first] - headings[second]));
|
|
if (delta >= 155) throughPairs.push([first, second]);
|
|
}
|
|
}
|
|
const sorted = [...headings].sort((a, b) => a - b);
|
|
const gaps = sorted.map((heading, index) => positiveHeadingDelta(heading, sorted[(index + 1) % sorted.length]));
|
|
const hasRightAngleCross =
|
|
arms.length === 4 && gaps.filter((gap) => gap >= 55 && gap <= 125).length >= 3 && throughPairs.length >= 2;
|
|
const hasOppositePair = throughPairs.length >= 1;
|
|
if (arms.length === 3 && hasOppositePair)
|
|
return { kind: 't-junction', label: '3 臂 T 字路口', throughPairCount: throughPairs.length };
|
|
if (hasRightAngleCross) {
|
|
const centers = arms.map((arm) => arm.members[0]?.approach?.line?.at?.(-1)).filter(Boolean);
|
|
const spread =
|
|
centers.length === arms.length
|
|
? Math.max(...centers.map((point) => helpers.distanceMeters(point, center))) -
|
|
Math.min(...centers.map((point) => helpers.distanceMeters(point, center)))
|
|
: 0;
|
|
return {
|
|
kind: spread > 12 ? 'offset-cross' : 'cross',
|
|
label: spread > 12 ? '4 臂偏置十字路口' : '4 臂十字路口',
|
|
throughPairCount: throughPairs.length,
|
|
};
|
|
}
|
|
if (arms.length >= 3 && arms.length <= 8) {
|
|
return { kind: 'multi-arm', label: `${arms.length} 臂非规则路口`, throughPairCount: throughPairs.length };
|
|
}
|
|
return { kind: 'unsupported', label: '暂不支持的路口形态', throughPairCount: throughPairs.length };
|
|
}
|
|
|
|
function readReferenceCalibration(cluster) {
|
|
if (!cluster.referenceFile || !fs.existsSync(cluster.referenceFile)) return null;
|
|
try {
|
|
const converted = convertGeoJson(JSON.parse(fs.readFileSync(cluster.referenceFile, 'utf8')));
|
|
const bounds = boundsOf({
|
|
features: converted.features.filter((feature) => [1, 2, 3, 4].includes(Number(feature.properties?.type))),
|
|
});
|
|
const lonScale = 111320 * Math.cos((((bounds.minLat + bounds.maxLat) / 2) * Math.PI) / 180);
|
|
return {
|
|
longSpanMeters: (bounds.maxLon - bounds.minLon) * lonScale,
|
|
shortSpanMeters: (bounds.maxLat - bounds.minLat) * 111320,
|
|
};
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function complexJunctionMetrics(cluster) {
|
|
if (metricsCache.has(cluster)) return metricsCache.get(cluster);
|
|
const calibration = readReferenceCalibration(cluster);
|
|
const metrics = calculateComplexJunctionMetrics(cluster, calibration);
|
|
metricsCache.set(cluster, metrics);
|
|
return metrics;
|
|
}
|
|
|
|
module.exports = {
|
|
resolveComplexJunctionTopology,
|
|
buildComplexJunctionGeometry,
|
|
buildComplexApproachBoundaryGraph,
|
|
classifyJunctionShape,
|
|
complexJunctionMetrics,
|
|
};
|