-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
node.js
242 lines (196 loc) 路 7.54 KB
/
node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { osmEntity } from './entity';
import { geoAngle, geoExtent } from '../geo';
import { utilArrayUniq } from '../util';
export 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
};
export function osmNode() {
if (!(this instanceof osmNode)) {
return (new osmNode()).initialize(arguments);
} else if (arguments.length) {
this.initialize(arguments);
}
}
osmEntity.node = osmNode;
osmNode.prototype = Object.create(osmEntity.prototype);
Object.assign(osmNode.prototype, {
type: 'node',
loc: [9999, 9999],
extent: function() {
return new geoExtent(this.loc);
},
geometry: function(graph) {
return graph.transient(this, 'geometry', function() {
return graph.isPoi(this) ? 'point' : 'vertex';
});
},
move: function(loc) {
return this.update({loc: loc});
},
isDegenerate: function() {
return !(
Array.isArray(this.loc) && this.loc.length === 2 &&
this.loc[0] >= -180 && this.loc[0] <= 180 &&
this.loc[1] >= -90 && this.loc[1] <= 90
);
},
// Inspect tags and geometry to determine which direction(s) this node/vertex points
directions: function(resolver, projection) {
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;
}
}
}
if (val === '') return [];
var values = val.split(';');
var results = [];
values.forEach(function(v) {
// swap cardinal for numeric directions
if (cardinal[v] !== undefined) {
v = cardinal[v];
}
// numeric direction - just add to results
if (v !== '' && !isNaN(+v)) {
results.push(+v);
return;
}
// 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 geoAngle returns angle from X axis, not Y (north)
results.push(
(geoAngle(this, resolver.entity(nodeId), projection) * (180 / Math.PI)) + 90
);
}, this);
}, this);
return utilArrayUniq(results);
},
isCrossing: function(){
return this.tags.highway === 'crossing' ||
this.tags.railway && this.tags.railway.indexOf('crossing') !== -1;
},
isEndpoint: function(resolver) {
return resolver.transient(this, 'isEndpoint', function() {
var id = this.id;
return resolver.parentWays(this).filter(function(parent) {
return !parent.isClosed() && !!parent.affix(id);
}).length > 0;
});
},
isConnected: function(resolver) {
return resolver.transient(this, 'isConnected', function() {
var parents = resolver.parentWays(this);
if (parents.length > 1) {
// vertex is connected to multiple parent ways
for (var i in parents) {
if (parents[i].geometry(resolver) === 'line' &&
parents[i].hasInterestingTags()) return true;
}
} else if (parents.length === 1) {
var way = parents[0];
var nodes = way.nodes.slice();
if (way.isClosed()) { nodes.pop(); } // ignore connecting node if closed
// return true if vertex appears multiple times (way is self intersecting)
return nodes.indexOf(this.id) !== nodes.lastIndexOf(this.id);
}
return false;
});
},
parentIntersectionWays: function(resolver) {
return resolver.transient(this, 'parentIntersectionWays', function() {
return resolver.parentWays(this).filter(function(parent) {
return (parent.tags.highway ||
parent.tags.waterway ||
parent.tags.railway ||
parent.tags.aeroway) &&
parent.geometry(resolver) === 'line';
});
});
},
isIntersection: function(resolver) {
return this.parentIntersectionWays(resolver).length > 1;
},
isHighwayIntersection: function(resolver) {
return resolver.transient(this, 'isHighwayIntersection', function() {
return resolver.parentWays(this).filter(function(parent) {
return parent.tags.highway && parent.geometry(resolver) === 'line';
}).length > 1;
});
},
isOnAddressLine: function(resolver) {
return resolver.transient(this, 'isOnAddressLine', function() {
return resolver.parentWays(this).filter(function(parent) {
return parent.tags.hasOwnProperty('addr:interpolation') &&
parent.geometry(resolver) === 'line';
}).length > 0;
});
},
asJXON: function(changeset_id) {
var r = {
node: {
'@id': this.osmId(),
'@lon': this.loc[0],
'@lat': this.loc[1],
'@version': (this.version || 0),
tag: Object.keys(this.tags).map(function(k) {
return { keyAttributes: { k: k, v: this.tags[k] } };
}, this)
}
};
if (changeset_id) r.node['@changeset'] = changeset_id;
return r;
},
asGeoJSON: function() {
return {
type: 'Point',
coordinates: this.loc
};
}
});