I want to efficiently find all markers that have a minimum distance to a line (road).
The road I have is in the form of coordinates from a geojson from OSM.
"geometry": {
"type": "LineString",
"coordinates": [
[
4453703.425184186547995,
5750173.232290416955948
],
[
4453662.839535089209676,
5750035.606657268479466
],
[
4453630.598591844551265,
5749938.207207129336894
],
[
4453598.337730905041099,
5749836.557515981607139
],
[
4453585.398518323898315,
5749792.02349347807467
],
[
4453574.44069469999522,
5749725.005126525647938
],
[
4453585.525054411962628,
5749643.628650147467852
]
]
}
The list of markers is large and looping is inefficient. The list is provided as a .csv file.
There are efficient ways to index points/ markers on an Euclidean plane. See quadtrees and k-d trees.
My main idea would be to generate a polygon from the line, where each point of the polygon has the minimum distance to the line. The endpoints would be circles, but with a reduced number of points this would be sufficient.
There are several implementations I have found so far. But I am not sure which one suits my needs best. Here is a list of things I thought of:
- Pyqtree Only allows rectangular selections. So I would have to construct a bounding box around the line and then loop over the returned list to get the exact values. The disadvantage is that the project is no longer active.
- Scipy k-d tree Only takes a list of points as query. The problem in this package would be the following edge case: Assume that the minimum distance is 1. Two points of the line have a distance > 2. If a marker is exactly in the middle of these two points, it would not be retrieved. A workaround would be to increase the points of the road, so that no point has a distance less than x.
- QGIS Somewhere in QGIS this should be solved, but the library is large and I do not know my way around. Also I do not want to use the whole QGIS package for this problem.
Questions:
Is there a better option than the ones listed above? Which approach do you think is the best?