This repository has been archived by the owner on Nov 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
sidewalker.js
197 lines (157 loc) · 5.94 KB
/
sidewalker.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
var normalize = require('geojson-normalize'),
gju = require('geojson-utils'),
turf = require('turf'),
tilebelt = require('tilebelt'),
lineChunk = require('turf-line-chunk'),
sliceAtIntersect = require('turf-line-slice-at-intersection'),
rbush = require('rbush');
module.exports = function (tileLayers, tile, writeData, done) {
var footways = filterAndClipFootways(tileLayers.osm.osm, tile),
roads = filterAndClipRoads(tileLayers.osm.osm, tile),
proposals = [];
var roadIndex = rbush();
for(var r = 0; r < roads.length; r++) {
roadIndex.insert(turf.extent(roads[r]).concat({road_id: r}));
}
for (var f = 0; f < footways.length; f++) {
var segments = sliceAtIntersect(footways[f], findProbablyIntersects(footways[f], roadIndex, roads));
// Find which of the remaining segments stay close to a road (should be a sidewalk)
segments.features.forEach(function (segment) {
// found a case where the original linestring is a single coordinate, not wrapped in an array
if (segment.geometry.coordinates.length < 2 || !segment.geometry.coordinates[0].length) return;
// skip short little segments
if (turf.lineDistance(segment, 'miles') <= 10/5280) return;
var segmented = lineChunk(segment, 250/5280, 'miles');
segmented.features.forEach(function (seg) {
if (turf.lineDistance(seg, 'miles') <= 150/5280) return;
// Get bisectors fo this segment, and match it against
// each road.
var bisectors = buildBisectors(seg);
var isMatched = false;
var bisectBox = turf.extent(turf.featurecollection(bisectors));
var maybeCollides = roadIndex.search(bisectBox);
maybeCollides.forEach(function (maybe) {
var road = roads[maybe[4].road_id];
if (isMatched || road.properties.layer !== footways[f].properties.layer) return;
var matched = 0;
bisectors.forEach(function (bisector) {
if (gju.lineStringsIntersect(bisector.geometry, road.geometry)) matched++;
});
if (matched / bisectors.length > 0.7) {
isMatched = true;
seg.properties['_osm_way_id'] = footways[f].properties._osm_way_id;
seg.properties['proposed:footway'] = 'sidewalk';
seg.properties['proposed:associatedStreet'] = road.properties.name;
writeData(JSON.stringify(seg)+'\n');
}
});
});
});
}
done(null, proposals);
};
/**
* Generates array of potentially erroneous footways
*/
function filterAndClipFootways(osm, tile) {
var features = [];
var keepSurfaces = [
'paved',
'concrete',
'asphalt',
'concrete:plates',
'cobblestone',
'cobblestone:flattened',
'sett',
];
for (var i = 0; i < osm.length; i++) {
var ft = osm.feature(i);
// Grab all footways missing footway=[sidewalk, crossing].
// Exclude surfaces not in our keep list, tunnels and areas.
if (ft.properties.highway === 'footway'
&& ft.properties.footway !== 'sidewalk'
&& ft.properties.footway !== 'crossing'
&& ft.properties.area !== 'yes'
&& !ft.properties['area:highway']
&& ft.properties.tunnel !== 'yes'
&& (!ft.properties.surface || keepSurfaces.indexOf(ft.properties.surface) > -1)
) {
features.push(ft.toGeoJSON(tile[0], tile[1], tile[2]));
}
}
return clipNormalize(features, tile);
}
/**
* Generates an array of roads in the tile to check footways against
*/
function filterAndClipRoads(osm, tile) {
var roadTypes = [
'trunk_link',
'trunk',
'primary',
'secondary',
'tertiary',
'unclassified',
'residential',
'road',
];
var features = [];
for (var i = 0; i < osm.length; i++) {
var ft = osm.feature(i);
// Grab all footways missing footway=[sidewalk, crossing]
if (roadTypes.indexOf(ft.properties.highway) > -1) {
features.push(ft.toGeoJSON(tile[0], tile[1], tile[2]));
}
}
return clipNormalize(features, tile);
}
/**
* normalizes the input features to linestrings
*/
function clipNormalize(features, tile) {
var newLines = [];
for (var i = 0; i < features.length; i++) {
if (features[i].geometry.type !== 'LineString' && features[i].geometry.type !== 'MultiLineString') continue;
var coords = (features[i].geometry.type === 'MultiLineString') ?
features[i].geometry.coordinates :
[features[i].geometry.coordinates];
for (var c = 0; c < coords.length; c++) {
// Certain erroneous linestrings are present in the ways data. Possibly an artifact
// of tile clippingice
if (coords[c].length > 0 && typeof coords[c][0] === 'number') continue;
newLines.push(turf.linestring(coords[c], features[i].properties));
}
}
return newLines;
}
/**
* Generates bisectors for all the given footway segments.
* Bisectors are generated every 20 feet, and extend 75 feet on either
* side of the street.
*/
function buildBisectors(footwaySegment) {
var bisectors = [];
var segmented = lineChunk(footwaySegment, 50/5280, 'miles');
segmented.features.forEach(function (segment) {
var seglen = segment.geometry.coordinates.length;
var endpoint = turf.point(segment.geometry.coordinates[seglen - 1]);
var bearing = turf.bearing(
turf.point(segment.geometry.coordinates[seglen - 2]), endpoint);
var start = turf.destination(endpoint, 75/5280, bearing - 90, 'miles');
var end = turf.destination(endpoint, 75/5280, bearing + 90, 'miles');
bisectors.push(turf.linestring([start.geometry.coordinates, end.geometry.coordinates]));
});
return bisectors;
}
/**
* Using our rbush index, find which roads probably intersect the sidewalk
*/
function findProbablyIntersects(footway, roadIndex, roads) {
var extent = turf.extent(footway);
var colliding = roadIndex.search(extent);
var fc = [];
for (var i = 0; i < colliding.length; i++) {
fc.push(roads[colliding[i][4].road_id])
}
return turf.featurecollection(fc);
}