Search code examples
leafletgisturfjs

How to display the area fully within 5km of a line


I am wanting to draw a polygon around a line that shows the area that is fully within 5km of the line.

Kind of like a buffer shows the area that is always 5km away from a line, I want to show an area that is always within 5km of the whole line - so the area will be smaller than the buffer.

The real-life situation is that we now have travel restrictions which in part means you must stay within 5km of your home. So I want to draw an area around a hiking trail - if the person's house is within the area, then they can do the full walk. Part of the issue is I don't even know what this kind of area is called.

Currently using Leaflet and turf like this -- but this is just a masked buffer.

                    var mask = turf.polygon([[[90, -55], [170, -55], [170, 10], [90, 10], [90, -55]]]);
                var polygon = turf.mask(turf.buffer(geojsonLines[0], 5, {
                units: 'kilometers'
                }),mask);

                L.geoJSON(polygon, {
                style: function(feature) {
                    return {
                    color: "red"
                    };
                }
                }).addTo(journey_map);

(not how I will deal with tracks that extend more than 5km from a BBOX -- but will deal with that). Image of buffer area similar to what I want to make


Solution

  • Am I right that you want area within 5km of every point in a shape, rather than buffer which is area within 5km of any point in a shape?

    If so, I don't know of an existing operation to compute this or how it is called, but it seems easy to compute.

    If you look at a line segment [A, B] and distance from any point C on this segment to some distant point D, it is easy to see CD is less than or equal to max(AD, BD). So if D is within 5km from corners A and B, it is within 5km of all points on AB. Same for a triangle - points within 5km of all points in triangle should be within 5km of each corner of triangle.

    This leads to simple (although probably not very efficient) algorithm: take all vertices in the linestring describing a trail or polygon describing a park, buffer each of them by 5km separately and intersect all the resulting circles (https://turfjs.org/docs/#intersect). The result of intersection is within 5km of each vertex, and thus within 5km of all points in trail.

    A simple optimization you do is first compute convex hull of the shape - this would simplify a typical trail, reducing the number of vertices, and the number of circles you need to intersect.