Does anyone know the best algorithm for joining the ways returned by OSM into a polygon? I have 330 entities that I retrieve using overpass. These entities are sometimes countries, sometimes states within countries, sometimes just isolated islands. There are all just a list of ways that represent an entity. Some have holes (inner ring).
I wish to programatically rejoin all the ways to form polygons. I don’t wish to use an offline tool. My code is in VB.NET and I am using ESRI ArcGIS Runtime. It correctly converts the ways for 320 of the 330 entities to credible polygons. There are 10 however where I’m not getting the right result. There is one example (Qatar) below. It seems to have lost the land mass, but retained the littoral boundary. This is odd because Qatar is far from the most complex entity I can correctly convert to polygon. The query I use to get Qatar is rel["ISO3166-1"=QA]
I’m on about my 5th experimental version of the algorithm, and I think it might be quicker if I just ask. I have multiple unresolved issues.
-
I see a few end to end joins of ways and start to start joins. I’m not sure what this really means.
-
I don’t know how to handle a situation where 3 ways join.
I expect it’s horrendously difficult, but I prepared to give it a go, I just don’t know a robust algorithm. Can anyone offer a robust algorithm in pseudo code ?
// Pseudo code describing reduction of list of ways to a polygon
INPUT: an overpass QL query OSM for relations/ways
loop until no new connections made
loop for each way
try to connect end of way to beginning of another
if fail then try to connect beginning of way to end of another
if fail then try to connect beginning of way to beginning of another
if fail then try to connect end of way to end of another
// if a connection is made, then the two ways are joined into one, and one way is deleted
end loop
end loop
convert list of ways to polygon
simplify polygon
generalize polygon
OUTPUT: result is (usually) a nicely formed polygon with a handful of rings