feat(junction): establish stable complex junction structure
This commit is contained in:
1 parent
bd8b55e58b
commit
0a5a229ce3
22 files changed
+1386
-125
No files matched your search
@@ -11,30 +11,57 @@ function heading(from, to) {
|
||||
return (Math.atan2(to[0] - from[0], to[1] - from[1]) * 180) / Math.PI;
|
||||
}
|
||||
|
||||
function pointOf(feature) {
|
||||
const coordinates = feature?.geometry?.coordinates;
|
||||
if (feature?.geometry?.type === 'Point' && Array.isArray(coordinates)) return coordinates;
|
||||
if (Array.isArray(coordinates?.[0]?.[0])) return coordinates[0][0];
|
||||
if (Array.isArray(coordinates?.[0])) return coordinates[0];
|
||||
return null;
|
||||
}
|
||||
|
||||
function projectJunctionStructure({
|
||||
model = {},
|
||||
layers = {},
|
||||
trafficSignals = null,
|
||||
shape = null,
|
||||
junctionKey = null,
|
||||
junctionRef = undefined,
|
||||
} = {}) {
|
||||
if (junctionRef === null) return null;
|
||||
const roads = Array.isArray(model.roads) ? model.roads : [];
|
||||
const endpoints = Array.isArray(model.endpoints) ? model.endpoints : [];
|
||||
const core = featureCollection(layers.intersectionSurface).find(
|
||||
(feature) => feature.properties?.kind === 'complex-core',
|
||||
);
|
||||
const properties = core?.properties || {};
|
||||
const key = String(junctionKey || properties.cluster_id || endpoints[0]?.nodeId || 'junction:area');
|
||||
const center = pointOf(core) || endpoints[0]?.coordinate || [0, 0];
|
||||
const junctionNodeId = String(junctionKey || properties.cluster_id ? endpoints[0]?.nodeId || '' : key);
|
||||
if (junctionRef && !['node', 'cluster'].includes(junctionRef.type))
|
||||
throw new Error('Invalid junction reference type');
|
||||
if (junctionRef?.type === 'cluster') {
|
||||
const carrier = featureCollection(layers.roadSurface || layers.nativeRoadSurface || layers.road_surface).find(
|
||||
(feature) =>
|
||||
String(feature.properties?.cluster_id) === String(junctionRef.id) && feature.properties?.junction_structure,
|
||||
);
|
||||
if (!carrier) return null;
|
||||
const structure = buildJunctionStructure(carrier.properties.junction_structure);
|
||||
const nodeIds = new Set(structure.provenance?.nodeIds || []);
|
||||
const signals = new Map();
|
||||
for (const feature of featureCollection(trafficSignals?.assemblies)) {
|
||||
const p = feature.properties || {};
|
||||
const nodeId = p.node_id ?? p.control_id;
|
||||
if (nodeId == null || !nodeIds.has(String(nodeId)) || !p.signal_uid) continue;
|
||||
const uid = String(p.signal_uid);
|
||||
if (signals.has(uid)) {
|
||||
signals.get(uid).capability.reason = 'source-ambiguous';
|
||||
continue;
|
||||
}
|
||||
signals.set(uid, {
|
||||
key: `cluster:${encodeURIComponent(junctionRef.id)}:signal:${encodeURIComponent(uid)}`,
|
||||
kind: 'signal',
|
||||
label: '信号灯',
|
||||
geometryRef: uid,
|
||||
editable: false,
|
||||
capability: { editable: false, reason: 'complex-geometry-editing-not-implemented' },
|
||||
provenance: { nodeIds: [String(nodeId)], featureIds: [uid] },
|
||||
});
|
||||
}
|
||||
return buildJunctionStructure({
|
||||
...structure,
|
||||
facilities: [...structure.facilities, ...signals.values()].sort((a, b) => a.key.localeCompare(b.key)),
|
||||
});
|
||||
}
|
||||
const properties = {};
|
||||
const key = String(junctionRef?.id || junctionKey || endpoints[0]?.nodeId || 'junction:area');
|
||||
const junctionNodeId = key;
|
||||
if ((junctionRef || junctionKey) && !endpoints.some((endpoint) => String(endpoint.nodeId) === key)) return null;
|
||||
const center = endpoints.find((endpoint) => String(endpoint.nodeId) === key)?.coordinate || [0, 0];
|
||||
const endpointByNode = new Map();
|
||||
for (const endpoint of endpoints) endpointByNode.set(String(endpoint.nodeId), endpoint);
|
||||
const arms = roads
|
||||
@@ -48,7 +75,13 @@ function projectJunctionStructure({
|
||||
return {
|
||||
key: `road-arm:${road.id || index + 1}`,
|
||||
label: String(road.tags?.name || road.name || `道路 ${index + 1}`),
|
||||
heading: heading(center, endpoint.coordinate),
|
||||
heading:
|
||||
road.centerline?.length > 1
|
||||
? heading(
|
||||
center,
|
||||
String(road.sourceNodeIds?.[0]) === junctionNodeId ? road.centerline[1] : road.centerline.at(-2),
|
||||
)
|
||||
: heading(center, endpoint.coordinate),
|
||||
directionality:
|
||||
road.direction === 'oneway' || road.direction === 'one-way'
|
||||
? 'one-way'
|
||||
@@ -64,41 +97,53 @@ function projectJunctionStructure({
|
||||
sourceRoadId: String(road.id || ''),
|
||||
};
|
||||
})
|
||||
.filter(Boolean);
|
||||
.filter(Boolean)
|
||||
.sort((a, b) => a.heading - b.heading);
|
||||
const corners =
|
||||
arms.length > 1
|
||||
? arms.map((arm, index) => {
|
||||
const next = arms[(index + 1) % arms.length];
|
||||
return {
|
||||
key: `corner:${index + 1}`,
|
||||
key: `corner:${arm.key}>${next.key}`,
|
||||
label: `角部 ${index + 1}`,
|
||||
fromArmKey: arm.key,
|
||||
toArmKey: next.key,
|
||||
geometryRef: `${key}:corner:${index + 1}`,
|
||||
geometryRef: `corner:${arm.key}>${next.key}`,
|
||||
};
|
||||
})
|
||||
: [];
|
||||
const inScope = (feature) => {
|
||||
if (!junctionRef && !junctionKey) return true;
|
||||
const p = feature.properties || {};
|
||||
if (p.cluster_id) return false;
|
||||
const node = p.node_id ?? p.osm_node_id ?? p.crossing_node_id;
|
||||
if (node !== undefined) return String(node) === junctionNodeId;
|
||||
return p.road_id && arms.some((arm) => arm.sourceRoadId === String(p.road_id));
|
||||
};
|
||||
const facilities = [];
|
||||
const crossingKeys = [];
|
||||
for (const [kind, collection] of [
|
||||
['crossing', layers.crosswalks],
|
||||
['stop-line', layers.vehicleStopLines],
|
||||
]) {
|
||||
featureCollection(collection).forEach((feature, index) => {
|
||||
const id = feature.properties?.native_id || feature.properties?.id || `${kind}:${index + 1}`;
|
||||
facilities.push({
|
||||
key: String(id),
|
||||
kind,
|
||||
label: kind === 'crossing' ? `人行横道 ${index + 1}` : `停止线 ${index + 1}`,
|
||||
geometryRef: String(id),
|
||||
featureCollection(collection)
|
||||
.filter(inScope)
|
||||
.forEach((feature, index) => {
|
||||
const id = feature.properties?.native_id || feature.properties?.id || `${kind}:${index + 1}`;
|
||||
facilities.push({
|
||||
key: String(id),
|
||||
kind,
|
||||
label: kind === 'crossing' ? `人行横道 ${index + 1}` : `停止线 ${index + 1}`,
|
||||
geometryRef: String(id),
|
||||
});
|
||||
});
|
||||
if (kind === 'crossing') crossingKeys.push(String(id));
|
||||
});
|
||||
}
|
||||
for (const [index, feature] of featureCollection(trafficSignals?.assemblies).entries()) {
|
||||
for (const [index, feature] of featureCollection(trafficSignals?.assemblies).filter(inScope).entries()) {
|
||||
const id = feature.properties?.signal_uid || feature.properties?.id || `signal:${index + 1}`;
|
||||
const signalNode = feature.properties?.node_id || feature.properties?.control_id;
|
||||
const related = crossingKeys.find((crossingKey) => String(crossingKey).includes(String(signalNode || '')));
|
||||
const related = featureCollection(layers.crosswalks)
|
||||
.filter(inScope)
|
||||
.find((crossing) => signalNode != null && String(crossing.properties?.crossing_node_id) === String(signalNode))
|
||||
?.properties?.native_id;
|
||||
facilities.push({
|
||||
key: String(id),
|
||||
kind: 'signal',
|
||||
@@ -112,21 +157,23 @@ function projectJunctionStructure({
|
||||
['median', layers.medians],
|
||||
['sidewalk-turn', layers.sidewalkTurns],
|
||||
]) {
|
||||
featureCollection(collection).forEach((feature, index) => {
|
||||
if (feature.properties?.kind !== kind) return;
|
||||
const id = feature.properties?.native_id || feature.properties?.id || `${kind}:${index + 1}`;
|
||||
facilities.push({
|
||||
key: String(id),
|
||||
kind,
|
||||
label:
|
||||
kind === 'island'
|
||||
? `导流岛 ${index + 1}`
|
||||
: kind === 'median'
|
||||
? `中央分隔 ${index + 1}`
|
||||
: `人行道转角 ${index + 1}`,
|
||||
geometryRef: String(id),
|
||||
featureCollection(collection)
|
||||
.filter(inScope)
|
||||
.forEach((feature, index) => {
|
||||
if (feature.properties?.kind !== kind) return;
|
||||
const id = feature.properties?.native_id || feature.properties?.id || `${kind}:${index + 1}`;
|
||||
facilities.push({
|
||||
key: String(id),
|
||||
kind,
|
||||
label:
|
||||
kind === 'island'
|
||||
? `导流岛 ${index + 1}`
|
||||
: kind === 'median'
|
||||
? `中央分隔 ${index + 1}`
|
||||
: `人行道转角 ${index + 1}`,
|
||||
geometryRef: String(id),
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
const shapeKind =
|
||||
shape?.kind ||
|
||||
@@ -134,6 +181,7 @@ function projectJunctionStructure({
|
||||
(arms.length === 3 ? 't-junction' : arms.length >= 5 ? 'multi-arm' : arms.length === 4 ? 'cross' : 'unsupported');
|
||||
return buildJunctionStructure({
|
||||
junctionKey: key,
|
||||
junctionRef: { type: 'node', id: junctionNodeId },
|
||||
label: properties.shape_label || '路口',
|
||||
center,
|
||||
shape: {
|
||||
@@ -151,7 +199,7 @@ function projectJunctionStructure({
|
||||
|
||||
function standardsDiagnostics(structure, profile = CN_URBAN_DEFAULT_PROFILE) {
|
||||
const diagnostics = [];
|
||||
for (const arm of structure.arms) {
|
||||
for (const arm of structure?.arms || []) {
|
||||
const rule = profile.rules.find((item) => item.id === 'effective-carriageway-width');
|
||||
const severity = evaluateRange(arm.carriageway.effectiveWidthMeters, rule);
|
||||
diagnostics.push({
|
||||
|
||||
Reference in new issue
Block a user