-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
69 lines (60 loc) · 1.84 KB
/
index.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
var query_overpass = require('query-overpass'),
turf = require('turf');
var radius = 350 // meters
roadWidth = 5, // meters
center = {
type: "Feature",
geometry: {
type: "Point",
coordinates: [4.88329,52.38258]
}
},
circle = turf.buffer(center, radius, "meters").features[0];
var angles = [];
for (var a = 0; a <= 360; a++) {
angles.push(a);
}
var circle2 = {
type: "Feature",
properties: {},
geometry: {
type: "Polygon",
coordinates: [
angles.map(function(a) {
var point = turf.destination(center, radius / 1000, a, "kilometers");
return point.geometry.coordinates;
})
]
}
};
getStreets(center, radius, function(error, geojson) {
if (error) {
console.error(error);
} else {
var union;
geojson.features.forEach(function(feature, i) {
var buffered = turf.buffer(feature, roadWidth, 'meters').features[0];
var intersection = turf.intersect(buffered, circle2);
if (intersection) {
if (union) {
union = turf.union(intersection, union);
} else {
union = buffered;
}
}
});
console.log(JSON.stringify(union, null, 2));
}
});
function getStreets(center, radius, callback) {
var distance = Math.sqrt(radius * radius * 2) / 1000,
southWestFeature = turf.destination(center, distance, -135, "kilometers"),
northEastFeature = turf.destination(center, distance, 45, "kilometers"),
southWestCoordinates = southWestFeature.geometry.coordinates,
northEastCoordinates = northEastFeature.geometry.coordinates,
boundingBox = southWestCoordinates.reverse().concat(northEastCoordinates.reverse()),
query = '[out:json];way["highway"](' + boundingBox.join(",") + ');(._;>;);out;';
query_overpass(query, function(error, data) {
callback(error, data);
});
}