Skip to content

Commit

Permalink
Don't show direction cones in non-routable directions
Browse files Browse the repository at this point in the history
  • Loading branch information
bhousel committed Dec 17, 2024
1 parent d553ff9 commit b4d5034
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 134 deletions.
172 changes: 86 additions & 86 deletions modules/osm/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,100 +48,100 @@ Object.assign(osmNode.prototype, {

// Inspect tags and geometry to determine which direction(s) this node/vertex points
directions: function(resolver, viewport) {
var val;
var i;

// which tag to use?
if (this.isHighwayIntersection(resolver) && (this.tags.stop || '').toLowerCase() === 'all') {
// all-way stop tag on a highway intersection
val = 'all';
} else {
// generic direction tag
val = (this.tags.direction || '').toLowerCase();

// better suffix-style direction tag
var re = /:direction$/i;
var keys = Object.keys(this.tags);
for (i = 0; i < keys.length; i++) {
if (re.test(keys[i])) {
val = this.tags[keys[i]].toLowerCase();
break;
}
}
let val;

// which tag to use?
if (this.isHighwayIntersection(resolver) && (this.tags.stop || '').toLowerCase() === 'all') {
// all-way stop tag on a highway intersection
val = 'all';

} else {
// Generic `direction` tag
val = (this.tags.direction || '').toLowerCase();

// Look for a better suffix-style `*:direction` tag
const re = /:direction$/i;
for (const [k, v] of Object.entries(this.tags)) {
if (re.test(k)) {
val = v.toLowerCase();
break;
}
}
}

if (val === '') return [];

const cardinal = {
north: 0, n: 0,
northnortheast: 22, nne: 22,
northeast: 45, ne: 45,
eastnortheast: 67, ene: 67,
east: 90, e: 90,
eastsoutheast: 112, ese: 112,
southeast: 135, se: 135,
southsoutheast: 157, sse: 157,
south: 180, s: 180,
southsouthwest: 202, ssw: 202,
southwest: 225, sw: 225,
westsouthwest: 247, wsw: 247,
west: 270, w: 270,
westnorthwest: 292, wnw: 292,
northwest: 315, nw: 315,
northnorthwest: 337, nnw: 337
};


const vals = val.split(';');
const results = [];

for (let v of vals) {
// swap cardinal for numeric directions
if (cardinal[v] !== undefined) {
v = cardinal[v];
}

if (val === '') return [];

var cardinal = {
north: 0, n: 0,
northnortheast: 22, nne: 22,
northeast: 45, ne: 45,
eastnortheast: 67, ene: 67,
east: 90, e: 90,
eastsoutheast: 112, ese: 112,
southeast: 135, se: 135,
southsoutheast: 157, sse: 157,
south: 180, s: 180,
southsouthwest: 202, ssw: 202,
southwest: 225, sw: 225,
westsouthwest: 247, wsw: 247,
west: 270, w: 270,
westnorthwest: 292, wnw: 292,
northwest: 315, nw: 315,
northnorthwest: 337, nnw: 337
};


var values = val.split(';');
var results = [];

values.forEach(function(v) {
// swap cardinal for numeric directions
if (cardinal[v] !== undefined) {
v = cardinal[v];
}
// `v` looks like a numeric direction - just append to results
if (v !== '' && !isNaN(+v)) {
results.push(+v);
continue;
}

// numeric direction - just add to results
if (v !== '' && !isNaN(+v)) {
results.push(+v);
return;
// `v` looks like a string direction - look at nearby nodes
const lookBackward = (this.tags['traffic_sign:backward'] || v === 'backward' || v === 'both' || v === 'all');
const lookForward = (this.tags['traffic_sign:forward'] || v === 'forward' || v === 'both' || v === 'all');
if (!lookForward && !lookBackward) continue;

// Gather nodes to look at
const nodeIDs = new Set();
for (const parent of resolver.parentWays(this)) {
if (parent.geometry(resolver) !== 'line') continue;
if (!(parent.tags.highway || parent.tags.railway || parent.tags.waterway || parent.tags.aeroway)) continue; // not routable?

const nodes = parent.nodes;
for (let i = 0; i < nodes.length; i++) {
if (nodes[i] === this.id) { // match current node
if (lookForward && i > 0) {
nodeIDs.add(nodes[i - 1]); // look back to prev node
}
if (lookBackward && i < nodes.length - 1) {
nodeIDs.add(nodes[i + 1]); // look ahead to next node
}
}
}
}

// string direction - inspect parent ways
var lookBackward =
(this.tags['traffic_sign:backward'] || v === 'backward' || v === 'both' || v === 'all');
var lookForward =
(this.tags['traffic_sign:forward'] || v === 'forward' || v === 'both' || v === 'all');

if (!lookForward && !lookBackward) return;

var nodeIds = {};
resolver.parentWays(this).forEach(function(parent) {
var nodes = parent.nodes;
for (i = 0; i < nodes.length; i++) {
if (nodes[i] === this.id) { // match current entity
if (lookForward && i > 0) {
nodeIds[nodes[i - 1]] = true; // look back to prev node
}
if (lookBackward && i < nodes.length - 1) {
nodeIds[nodes[i + 1]] = true; // look ahead to next node
}
}
}
}, this);

Object.keys(nodeIds).forEach(function(nodeId) {
// +90 because vecAngle returns angle from X axis, not Y (north)
var a = viewport.project(this.loc);
var b = viewport.project(resolver.entity(nodeId).loc);
results.push( (vecAngle(a, b) * 180 / Math.PI) + 90 );
}, this);

}, this);
for (const nodeID of nodeIDs) {
// +90 because vecAngle returns angle from X axis, not Y (north)
const a = viewport.project(this.loc);
const b = viewport.project(resolver.entity(nodeID).loc);
results.push( (vecAngle(a, b) * 180 / Math.PI) + 90 );
}
}

return utilArrayUniq(results);
return utilArrayUniq(results).sort((a, b) => a - b);
},


isCrossing: function(){
return this.tags.highway === 'crossing' ||
this.tags.railway && this.tags.railway.indexOf('crossing') !== -1;
Expand Down
Loading

0 comments on commit b4d5034

Please sign in to comment.