feat(junction): expose stage1 structure and standards panel
This commit is contained in:
1 parent
0f804b71d1
commit
fc7fe81eff
15 files changed
+434
-17
No files matched your search
@@ -0,0 +1,133 @@
|
||||
'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 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,
|
||||
} = {}) {
|
||||
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);
|
||||
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: 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 },
|
||||
geometryRef: String(road.id || `road:${index + 1}`),
|
||||
sourceRoadId: String(road.id || ''),
|
||||
};
|
||||
})
|
||||
.filter(Boolean);
|
||||
const corners =
|
||||
arms.length > 1
|
||||
? arms.map((arm, index) => {
|
||||
const next = arms[(index + 1) % arms.length];
|
||||
return {
|
||||
key: `corner:${index + 1}`,
|
||||
label: `角部 ${index + 1}`,
|
||||
fromArmKey: arm.key,
|
||||
toArmKey: next.key,
|
||||
geometryRef: `${key}:corner:${index + 1}`,
|
||||
};
|
||||
})
|
||||
: [];
|
||||
const facilities = [];
|
||||
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),
|
||||
});
|
||||
});
|
||||
}
|
||||
for (const [index, feature] of featureCollection(trafficSignals?.assemblies).entries()) {
|
||||
const id = feature.properties?.signal_uid || feature.properties?.id || `signal:${index + 1}`;
|
||||
facilities.push({ key: String(id), kind: 'signal', label: `信号灯 ${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,
|
||||
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 };
|
||||
Reference in new issue
Block a user