turf.js

spatial for JavaScript

How long was my run?

Which is the closest ATM from my current location?

One lib with many functions

...or many libraries with one function each

(at least 95 libs)

Heavy weight server GIS

...but in the browser (or in Node, of course)

GeoJSON

{
    "type": "Point",
    "coordinates": [11.7, 57.7]
}
{
    "type": "LineString",
    "coordinates": [[11, 57], [12, 58]]
}
{
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [11.7, 57.7]
    },
    "properties": {
        "name": "Göteborg",
        "population": 491630
    }
}

Demo

Find nearest ATM

function findAtms(latlng) {
    var location = 
        turf.point([latlng.lng, latlng.lat]);

    atmsRequest.then(function(resp) {
        atms = atms || resp;
        var nearest = turf.nearest(location, atms);
        atmLayer.clearLayers();
        atmLayer.addData(nearest);
        map.fitBounds(
            atmLayer.getBounds().extend(latlng),
            {maxZoom: 16});
    });
}

Demo

Find ATMs along a route

function findAtms(route) {
    var line = turf.flip(turf.linestring(route.coordinates)),
        buffered = turf.buffer(line, 1000, 'meters'),
        nearbyAtms = turf.within(atms, buffered);

        [...]
}
map.on('mousemove', function(e) {
    var p = turf.point([e.latlng.lng, e.latlng.lat]),
        pointOnLine = turf.pointOnLine(line, p),
        nearest = turf.nearest(pointOnLine, nearbyAtms);
    lineMarker.clearLayers();
    lineMarker.addData(pointOnLine);
    closestAtm.clearLayers();
    closestAtm.addData(nearest);
});

Beautifully documented

Beautifully documented

turf.js


http://turfjs.org/

http://www.liedman.net/turfjs-gbgnodejs/
@liedman