Giter Club home page Giter Club logo

Comments (7)

wolframteetz avatar wolframteetz commented on September 3, 2024

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

from isocronut.

drewfustin avatar drewfustin commented on September 3, 2024

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.

from isocronut.

drewfustin avatar drewfustin commented on September 3, 2024

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>

from isocronut.

wolframteetz avatar wolframteetz commented on September 3, 2024

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.

from isocronut.

wolframteetz avatar wolframteetz commented on September 3, 2024

Fixed & Pull requested the example

from isocronut.

wolframteetz avatar wolframteetz commented on September 3, 2024

Confirmed, if you move to any country with metric system, better replace 75 "mph" with 150 "kph" ;)

from isocronut.

drewfustin avatar drewfustin commented on September 3, 2024

Cool. That's good to know. You're amazing. Appreciate the help!

from isocronut.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.