How to get all the cities from OpenStreetMap

OpenStreetMap has an API called XAPI that allows you to query the OSM database and get XML data back. The correct way to tag a city on openstreetmap is place=city. Cities are tagged using nodes.

The XAPI query for this is: node/[place=city].

There are several Xapi servers. Initially I tried the http://xapi.openstreetmap.org/api/0.6/* one, but that wasn't returing results, so I switched to the new Java one http://jxapi.openstreetmap.org/xapi/api/0.6/*

Initially, I used this simple wget command line:

wget -O osm-places.osm.xml "http://jxapi.openstreetmap.org/xapi/api/0.6/node[place=city]"

It didn't work and I got this sort of output from wget, and no data:

Warning: wildcards not supported in HTTP.
--2011-05-27 13:41:05--  http://jxapi.openstreetmap.org/xapi/api/0.6/*[place=city]
Resolving jxapi.openstreetmap.org... 128.40.168.100
Connecting to jxapi.openstreetmap.org|128.40.168.100|:80... connected.
HTTP request sent, awaiting response... Read error (Connection timed out) in headers.
Retrying.

--2011-05-27 13:56:06--  (try: 2)  http://jxapi.openstreetmap.org/xapi/api/0.6/*[place=city]
Connecting to jxapi.openstreetmap.org|128.40.168.100|:80... connected.
HTTP request sent, awaiting response... 500 Internal Server Error
2011-05-27 13:56:06 ERROR 500: Internal Server Error.

The OSM database is big, and it takes a while to collect the data. Wget times out. To get around this you need to give wget the --timeout option to make it wait longer, this command makes wget wait up to 24 hours before giving out.

wget --timeout=$(( 24 * 60 * 60 )) -O osm-places.osm.xml "http://jxapi.openstreetmap.org/xapi/api/0.6/node[place=city]"

Comments !

blogroll