-
Notifications
You must be signed in to change notification settings - Fork 44
/
MapboxIsochrones.html
142 lines (121 loc) · 4.86 KB
/
MapboxIsochrones.html
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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Isochrone</title>
<script src="https://api.tiles.mapbox.com/mapbox.js/v2.2.2/mapbox.js"></script>
<script src="https://api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.0/turf.min.js" charset="utf-8"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href='https://api.tiles.mapbox.com/mapbox.js/v2.2.2/mapbox.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
var accessToken = '$$$ YOUR ACCESS TOKEN HERE $$$';
L.mapbox.accessToken = accessToken;
var map = L.mapbox.map('map', 'mapbox.streets')
.setView([31.2, -99.3], 6);
map.on('click', function(e) {
create_isochrones(map, [e['latlng']['lat'], e['latlng']['lng']]);
});
function create_isochrones(map, origin) {
map.setView(origin, 8);
var well = {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [
origin[0],
origin[1]
]
},
properties: {}
};
L.mapbox.featureLayer(turf.flip(well)).addTo(map);
var m = [50, 150, 300];
var s = [50, 100, 100];
for (var i in m) {
if (i > 0) {
var circle = turf.buffer(well, m[i], 'miles');
var extent = turf.extent(circle);
var additions = turf.random('points', s[i], {bbox: extent});
points['features'].push.apply(points['features'], additions['features']);
} else {
var circle = turf.buffer(well, m[i], 'miles');
var extent = turf.extent(circle);
var points = turf.random('points', s[i], {bbox: extent});
};
};
var deferreds = [];
var destinations = {
type: 'FeatureCollection',
features: []
};
for (i = 0; i < points.features.length; i++) {
var url = 'https://api.tiles.mapbox.com/v4/directions/mapbox.driving/' + well.geometry.coordinates[1].toString() + ',' + well.geometry.coordinates[0].toString() + ';' + points.features[i].geometry.coordinates[1].toString() + ',' + points.features[i].geometry.coordinates[0].toString() + '.json?access_token=' + L.mapbox.accessToken;
deferreds.push(
$.ajax({
url: url,
contentType: 'text/plain',
type: 'GET',
xhrFields: {
withCredentials: false
},
})
.success(function(data) {
var min_dur = 9999999;
for (r = 0; r < data['routes'].length; r++) {
min_dur = Math.min(min_dur, data['routes'][r]['duration'] / 60);
};
if (data['destination']) {
var d = {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [
data['destination']['geometry']['coordinates'][1],
data['destination']['geometry']['coordinates'][0]
]
},
properties: {
'z': min_dur
}
};
destinations['features'].push(d);
};
})
.fail(function() {
alert("Ajax failed to fetch data")
})
);
}
$.when.apply($, deferreds).done(
function() {
var breaks = [30, 60, 90, 120];
var colors = {};
colors[breaks[0]] = 'rgb(255, 0, 0)';
colors[breaks[1]] = 'rgb(255, 255, 0)';
colors[breaks[2]] = 'rgb(0, 255, 0)';
colors[breaks[3]] = 'rgb(0, 0, 255)';
var isolined = turf.isolines(destinations, 'z', 200, breaks);
for (i = isolined.features.length - 1; i >= 0; i--) {
var polygon_options = {
color: '#000',
opacity: 0.25,
weight: 1,
fillColor: colors[isolined.features[i].properties.z],
fillOpacity: 0.25
};
L.polygon(isolined.features[i].geometry.coordinates, polygon_options).addTo(map);
}
}
);
};
</script>
</body>
</html>