Closed wolframteetz closed 8 years ago
Looking at the code carefully
rmax = [1.25 * duration] * number_of_angles # rmax based on 75 mph speed
Looking at the map, I'd say you imposed a 75 KMH speed limit Which is quite unrealistic for Autobahn - so in Germany, you will get perfect circles for longer distances ;)
Cannot check if altering this to 290kmh (for my motorbike ;) (no really, 150 kmh, so 2.5 is fine) works, will do tomorrow once my quota is back
Let me know if switching it up to a higher limit works. I was well aware that this was completely not an ideal way to handle this.
Also, I'm going to post below a different take on this whole project I worked on for a different company. It uses Mapbox, is entirely JS, allows you to click on a point on a map and have it draw isochrones dynamically. Mapbox just released their directions API to the public (I had a private beta earlier). In doing so, or because of something else, my code now throws Ajax errors and I haven't fixed it yet (I'm no JS guy). So, it doesn't work. But maybe it's useful. It's certainly quicker and more robust if fixed.
The basics of this algorithm: for a central point on a map (clicked by the user), throw a bunch of points around it and test the duration to each of these points. Use isolines turf.js to draw contours using these durations as values to be contoured over. Quick, easy. But now with Ajax errors.
<!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="turf.min.js" charset="utf-8"></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 = 'GetYourAPITokenFromMapboxAndPutItHere';
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 = 'http://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
})
.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>
Somewhere I still have a nice Radial Basis Function network fitting written in Octave, need to transfer that to JS and include it to make really nice interpolations ;)
Before I saw your blog I had actually thought about doing exactly what you have just posted - only that I didn't have early access to the Mapbox api. I'm not a JS expert either, but I surely know more than I do about python.. too busy right now but that looks like that way to go. I will definitely take a look at it later.
Fixed & Pull requested the example
Confirmed, if you move to any country with metric system, better replace 75 "mph" with 150 "kph" ;)
Cool. That's good to know. You're amazing. Appreciate the help!
Something's weird about the isochrones... the 90 and 120 minute isochrones are almost identical to the west although there is a highway in that direction. I strongly doubt that these are correct. Need to investigate the cause.
(html file with 60,90,120min isochrones and 48 directional vectors)
Unfortunately, my daily limit's gone again --