217 lines
8.3 KiB
JavaScript
217 lines
8.3 KiB
JavaScript
'use strict';
|
|
|
|
const { buildJunctionStructure } = require('./junction-structure');
|
|
const { CN_URBAN_DEFAULT_PROFILE, evaluateRange } = require('./junction-standards');
|
|
|
|
function featureCollection(value) {
|
|
return value && Array.isArray(value.features) ? value.features : [];
|
|
}
|
|
|
|
function heading(from, to) {
|
|
return (Math.atan2(to[0] - from[0], to[1] - from[1]) * 180) / Math.PI;
|
|
}
|
|
|
|
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 : [];
|
|
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
|
|
.map((road, index) => {
|
|
const endpoint = (road.sourceNodeIds || [])
|
|
.map(String)
|
|
.filter((nodeId) => !junctionNodeId || nodeId === junctionNodeId)
|
|
.map((nodeId) => endpointByNode.get(nodeId))
|
|
.find(Boolean);
|
|
if (!endpoint) return null;
|
|
return {
|
|
key: `road-arm:${road.id || index + 1}`,
|
|
label: String(road.tags?.name || road.name || `道路 ${index + 1}`),
|
|
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'
|
|
: road.direction
|
|
? 'two-way'
|
|
: 'unknown',
|
|
carriageway: { laneCount: Number(road.laneCount) || 0, effectiveWidthMeters: Number(road.widthMeters) || 0 },
|
|
pedestrian: {
|
|
connected: Boolean(road.sidewalkLeft || road.sidewalkRight),
|
|
bicycleConnected: ['cycleway', 'path', 'living_street'].includes(String(road.highway || '').toLowerCase()),
|
|
},
|
|
geometryRef: String(road.id || `road:${index + 1}`),
|
|
sourceRoadId: String(road.id || ''),
|
|
};
|
|
})
|
|
.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:${arm.key}>${next.key}`,
|
|
label: `角部 ${index + 1}`,
|
|
fromArmKey: arm.key,
|
|
toArmKey: next.key,
|
|
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 = [];
|
|
for (const [kind, collection] of [
|
|
['crossing', layers.crosswalks],
|
|
['stop-line', layers.vehicleStopLines],
|
|
]) {
|
|
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),
|
|
});
|
|
});
|
|
}
|
|
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 = 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',
|
|
label: `信号灯 ${index + 1}`,
|
|
geometryRef: String(id),
|
|
...(related ? { relatedFacilityKey: related } : {}),
|
|
});
|
|
}
|
|
for (const [kind, collection] of [
|
|
['island', layers.islands],
|
|
['median', layers.medians],
|
|
['sidewalk-turn', layers.sidewalkTurns],
|
|
]) {
|
|
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 ||
|
|
properties.shape_kind ||
|
|
(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: {
|
|
kind: shapeKind,
|
|
label: shape?.label || properties.shape_label || '路口',
|
|
effectiveArmCount: arms.length,
|
|
throughPairCount: Number(shape?.throughPairCount ?? properties.through_pair_count) || 0,
|
|
},
|
|
arms,
|
|
corners,
|
|
facilities,
|
|
standards: { profileId: CN_URBAN_DEFAULT_PROFILE.id },
|
|
});
|
|
}
|
|
|
|
function standardsDiagnostics(structure, profile = CN_URBAN_DEFAULT_PROFILE) {
|
|
const diagnostics = [];
|
|
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({
|
|
id: `standards:${rule.id}:${arm.key}`,
|
|
severity,
|
|
rule: rule.id,
|
|
objectKey: arm.key,
|
|
message: severity === 'pass' ? '车行道有效宽度符合标准范围。' : '车行道有效宽度需要复核。',
|
|
});
|
|
}
|
|
return diagnostics;
|
|
}
|
|
|
|
module.exports = { projectJunctionStructure, standardsDiagnostics };
|