I would like to query OpenStreetMap (remotely) for a list of US counties (and their boundary vectors if possible), in a scriptable automated fashion.
Preferred format is CSV.
How do I do so? I am new to querying OpenStreetMap
I would like to query OpenStreetMap (remotely) for a list of US counties (and their boundary vectors if possible), in a scriptable automated fashion.
Preferred format is CSV.
How do I do so? I am new to querying OpenStreetMap
Overpass is the way to selectively query OSM data. There are two things you need to work out to make effective use Overpass:
The easiest way to do both of those things is to use an interactive tool to develop the query. I suggest Overpass Turbo.
From the Overpass API language guide, you can get an idea of the general syntax. Clearly you don't want to search for everything in the US and then try to filter out the list of US counties, so you'll need to figure out how to ask for "just counties, thanks". You might be able to look at the OSM data schema to figure this out, but you can just use Overpass Turbo for the right attributes based on visual inspection.
Pan and scale the map to a region where there aren't too many features, but there is something that looks like a county boundary. I used a place south of Destin in Florida, took out the key/value part of the query, and pressed run.
Now if you select the little circle (node), you'll see a popup. I've zoomed out a little so you can get the general area (Florida panhandle), but you do not want to run Overpass Turbo at this scale without some filters, because of browser limits.
If you click on the node number (the underlined part), you'll be taken to OpenStreetMap, and shown what "ways" (polylines) are associated with that node:
Note the "Part of" list on the left. You can click on each of those to see the attributes on the tag. For example, for Way: 80670850 (polyline running west from the node), we have:
And for Way:80670677 (running north from the node)
So at this point, we can reconsider whether OSM really is the best way, because we could just choose to download the source data (TIGER data from 2009).
However lets keep going. We could choose objects that have border_type=county (and note that this may not give you polygons, since some will be border_type=state), or we could just choose any kind of boundary=administrative, and select where admin_level is at most 6.
Now we don't just want the nodes, we want the ways (since those give us the polylines) the areas that they define (which are the counties). So we end up with something like:
You can then click export to get one of the selected formats (not CSV unfortunately, but GeoJSON or KML should be OK as a source for future conversions). GeoJSON for a county will start with:
{
"type": "Feature",
"id": "relation/1210691",
"properties": {
"@id": "relation/1210691",
"admin_level": "6",
"border_type": "county",
"boundary": "administrative",
"is_in": "Florida",
"name": "Okaloosa County",
"ref": "12-091",
"timezone": "America/Chicago",
"type": "boundary",
"wikipedia": "en:Okaloosa County, Florida"
},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-86.800554,
30.334492
],
[
-86.800544,
30.375199
],
[
-86.800391,
30.38337
],
[
-86.799974,
30.39307
],
[
-86.799574,
30.406097
],
[
-86.799157,
30.406298
],
[
-86.799404,
30.40937
],
[
-86.799056,
30.421034
],
[
-86.798926,
30.430998
],
[
-86.798926,
30.437446
],
Clearly, that is going to produce a lot of data (and your browser is going to be sagging under all those points!), so you might want to export the query (which is an option under Export), and use the normal Overpass API endpoint. For example, you can export the query (default is a file name something like query.txt), which will look like:
<query type="way">
<has-kv k="boundary" v="administrative"/>
<has-kv k="admin_level" regv="1|2|3|4|5|6"/>
<bbox-query s="30.217541849095714" w="-87.09686279296874" n="31.395846541938496" e="-86.13418579101562"/>
</query>
<union>
<item/>
<recurse type="up"/>
<recurse type="down"/>
</union>
<print/>
Now you can use that with a command line tool such as wget:
wget --post-file=query.txt http://overpass-api.de/api/interpreter --output-document=out.xml
You can easily edit the query file to change the output file type, or the search bounds, or to really only get county boundaries (regv="1|2|3|4|5|6" would just be v="6"), or whatever other data you need, and just resubmit it.
Hopefully that has given you enough to work from, and you can now make good use of the documentation.