Opened 8 hours ago
Last modified 71 minutes ago
#24985 new enhancement
New action: create a buffer (area at a given distance) around selected ways
| Reported by: | wangi | Owned by: | team |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Core | Version: | |
| Keywords: | buffer halo area distance parallel | Cc: | GerdP |
Description
This is a request for comments before any code is written. It follows up the suggestion in #24891 (comments 2 and 4) and the bug in #24983.
Use case
From #24891: a GPX track, possibly with excursions, and a GeoJSON file with many thousands of POI. The POI close to the track should be kept and all others removed. The approach is to draw a polygon at e.g. 100 m around the track and then use "All inside [testing]" to select the POI inside it. GerdP suggested an action like "Calculate buffer" or "Calculate halo" for this.
Why the Parallel mode is not the tool
Parallel offsets a path to one side. Running it once to the left and once to the right gives something which looks like a buffer, but is not one:
- The two runs produce two separate sets of ways. They have no round ends at the start and end of the path, and they are not joined into a closed way.
- Where the path crosses itself or comes back close to itself, each side consists of several pieces (see #24983). The pieces of one side meet those of the other side, but at separate nodes, so they would have to be merged by hand.
- An area enclosed by the path but further than the distance from it (e.g. inside a loop) is a hole of the buffer, made of pieces from both sides.
Turning that into a closed area is a lot of manual work, and easy to get wrong.
Proposal
A new action Tools > Create Buffer..., next to Create Circle:
- It takes the selected ways, asks for the distance (in the current system of measurement, remembering the last value) and creates the outline of everything within that distance of them.
- The result is a closed way, or a multipolygon if the outline has holes or several separate parts. It is selected afterwards, so "All inside" can be used right away.
- The outline has round ends and round outer corners, approximated by chords like the arcs of the Parallel mode. Where the ways cross or come close to each other, the outline is simply the outline of the union.
- The distance is in metres, converted to projected units at the centre of the selection, as the Parallel mode does at the mouse position. Over very large extents the true distance varies with the scale of the projection.
Questions
- Core, or rather a plugin (utilsplugin2 has many tools of this kind)?
- Inputs: ways only, or also nodes (giving circles)? For a closed way: the buffer of the line (a band on both sides) or of the area (grown outwards)?
- Several unconnected ways: one result for their union, or one per way?
- Tags of the result: none (it is a construction helper),
area=yes, or chosen by the user? - Menu placement and shortcut.
Implementation ideas
Two ways to compute the outline:
- Reuse the engine of the Parallel mode (#24891, #24983). A path traversed out and back is a closed ring whose outward offset is exactly the buffer outline, including the round ends. A first experiment gives the right outline (outer ring and the hole) for a path looping round and passing under itself. Two things need work: the corners must stay round (the Parallel mode replaces gentle arcs by mitres), and a path with an out-and-back excursion of its own does not close yet. It handles one branchless path, like the Parallel mode.
- Java2D: stroke the ways with a
BasicStrokeof width 2 × distance with round caps and joins, and merge the shapes withjava.awt.geom.Area. Much less code, and several ways or nodes are merged naturally. To check: precision (the stroker works in floats, so the coordinates would have to be shifted to a local origin first) and speed on large inputs such as a long track or a coastline.



Thanks for looking into this. I think besides problematic parts are loops and omega-shaped lines (U+03A9).