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's gravatar image

jvm985
11112
accept rate: 0%


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:

http://overpass.osm.rambler.ru/cgi/interpreter?data=[out:json];way[highway][name]%2850.66%2C10.91%2C50.7%2C10.95%29%3Bout%3B

(Note: the regular expression name~'.' is only required due to a recently introduced bug and can be simplified by just specifying name in the future).

You just have to parse this output and extract all name fields.

permanent link

answered 15 Aug '12, 08:36

scai's gravatar image

scai ♦
31.0k18278434
accept rate: 23%

edited 15 Aug '12, 11:56

1

The bug has been fixed.

(15 Aug '12, 11:43) Roland Olbricht

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).

permanent link

answered 14 Aug '12, 16:49

Sanderd17's gravatar image

Sanderd17
1.1k51637
accept rate: 33%

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
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×111
×31

question asked: 14 Aug '12, 14:41

question was seen: 4,534 times

last updated: 15 Aug '12, 11:56

powered by OSQA