How would one implement the following functionality : a user draws a polygon on a map and receives as a result a list of streetnames (or addresses) which are located within the polygon. Doesn't matter which maps, api or scripting language. Any ideas? asked 14 Aug '12, 14:41 jvm985 |
If you are able to split the polygon into bounding boxes you can use multiple OverpassAPI queries. This example query will list all ways with a highway and a way tag:
You just have to parse this output and extract all name fields. answered 15 Aug '12, 08:36 scai ♦ |
First you need to determine what the inner surface of your closed way is (if the way is oriented, you can save this with "left" or "right"). You can do this by connecting an arbitrary point of your polygon with a special point that will never be included in the polygon (like the north pole, there are no streets there). Now, if the drawn line intersects with your polygon, draw the line again from that intersection point, until it doesn't intersect any more. After that, you can check if the line is right or left from the polygon boundary. After that, for each point in your dataset*, you draw a line from the point (the direction doesn't matter). If the line doesn't cut with the drawn polygon, it's not in it. If the line cuts, you can check wether the part between your node and the line is left or right from your polygon and thus check if it's in or out the polygon. And as a last thing, get all the ways that have a node in the selection you've received. *) you can filter your dataset a bit on beforehand by placing the polygon in a BBOX and filtering on that BBOX first. This method requires you know how to calculate intersection points and how to check if a line segment is left or right of an other segment (in other words, calculate the corner between two segments). answered 14 Aug '12, 16:49 Sanderd17 thanks for your reply which is quite helpful. The thing is that I don't have a dataset with coordinates. I want the script to return a set of streetnames that are (partly) enclosed. I could come up with a list of streetnames, but not simple coordinates. Does that make sense?
(14 Aug '12, 18:12)
jvm985
Oh, so you want to get it live from OSM? That depends on how big the polygon is, and how often you do a query in the same area. You can first calculate a BBOX (biggest and smallest lat-lon values), and get data from OSM in that BBOX. This would work if your polygon is small enough. Otherwise, you will have to work with an offline database.
(14 Aug '12, 18:18)
Sanderd17
|