I'm trying to match small segments with a larger segment they are the most probably related to: relatively close, similar bearing, and facing each other.
Here is a typical example of the data I have:
Here I'd need to match segment 652 to 198969, while having 711 and 707 not matching anything.
I've looked for different methods, in particular the Hausdorff distance (based on the answers here). I computed it using PostGIS but I'm getting odd results: the shortest distance I get is between 707 and 198985, and 652 has a greater distance to 198969 than to 198985 for example (I can add the query and results if needed).
Is Hausdorff actually the correct method to solve this? Are there other approaches? I thought of simply creating a set of checks on the parameters I mentioned (distance, bearing, etc.) but I'm afraid of having to add a whole bunch of conditions to handle edge cases or things like thresholding on how much they're facing each other.
Update: I found a method which seems like an acceptable compromise:
- I first find the 10 nearest black segments from the blue one I'm trying to match (using the PostGIS
<->
operator) which are less than 10 meters away. - I then create a new segment by finding the nearest points to the ends of the blue segment on each of the black ones (using
ST_ClosestPoint
) and filter out the results whose length is less than 90% of the blue one (meaning the segments aren't facing, or that the bearing difference is more than ~20°) - Then I get the first result sorted by distance and Hausdorff distance, if any.
There might be some fine tuning to do but it seems to do an acceptable job for now. Still looking for any other methods or additional checks to run if I missed some edge cases.