marey
marey

Reputation: 93

Getting city boundaries from openstreetmap

I'm developing a website and I need to get all the boundaries of an area given depending on the user input. For example, the user want to know the boundaries of a city named x. How should I get it from openstreetmap? I've heard of xapi and osmosis but couldnt find any examples anywhere. Thanks!

Upvotes: 7

Views: 11264

Answers (1)

pgkelley
pgkelley

Reputation: 492

I took a stab at doing this with JavaScript here: https://github.com/pgkelley4/city-boundaries-google-maps

Basically it comes down to finding the relation that OpenStreetMap stores the city boundaries as.

I used something like the following query to get the area:

area[name="Seattle"]["is_in:state_code"="WA"];foreach(out;);

Or if that doesn't find anything, going through the node to find any associated areas:

node[name="New York"][is_in~"NY"];foreach(out;is_in;out;);

To get the relation ID, subtract 3600000000 from the area ID returned by the above queries. Then get the relation from its ID:

(relation(" + relationID + ");>;);out;

You can test out queries here, mine could probably be improved on: http://overpass-api.de/query_form.html

That is how to get the city boundaries, processing them is another matter as nothing is in order within the relation. For that see my GitHub project and: http://wiki.openstreetmap.org/wiki/Relation:multipolygon/Algorithm

Also I would note that the OpenStreetMap data for city boundaries is spotty. It is missing for big cities like Dallas and LA from what I can tell.

Upvotes: 2

Related Questions