Queries against the database

As I understand it the OSM data is held in a database.

I am quite interested in being able to query the database from my website to get things like bus routes, distance between towns and other information so that I can present this to my users in a format other than the generated image tile.

Where can I get information on how to do this?

Is there a published API?

Can you query the database schema directly?

Indeed there is a published API.

The main database access API is documented here: http://wiki.openstreetmap.org/wiki/OSM_Protocol_Version_0.6

Some tips on getting areas of the map here: http://wiki.openstreetmap.org/wiki/Getting_Data

The extended API (XAPI) may be more useful some kinds of queries: http://wiki.openstreetmap.org/wiki/Xapi

You cannot query the database schema directly, but you could load a copy of the planet dump into your own database server. You might go for PostGIS database populated using Osm2pgsql. There’s loads of options for converting between formats to load OSM data in other software. All depends exactly what kinds of queries you want to run.

Basically I want to get the data by name.

If I have the name of a place, “Bayview” and I know it’s in Cape Town, so basically I know the rough cordinates, but I want to get the exact coordinates, so that, if I have 2 such points I can identify a route between them? Including the roads to take?

This is the sort of thing that the GPS navigation instruments do all the time.

Is it possible to replicate this behaviour through querying the OSM database?

I’ve had a look at the links that you’ve provided, but I can’t really see how to extract the coordinates by the name?

Well a XAPI query for that would look like:


That query yields too many results - lots of stuff in North America, for example. The only town in the list is one in South Africa, however.

You could further add a bounding box (as you know the general co-ords). Example:


But really I think what you’re trying to do is too specific to use the online tools. As Harry said above, you can download the weekly planet or even an extract of South Africa, for example, and load it into a database of your own. That way you could pre-process the data to remove ambiguities and so forth.

As for the routing side of things, there are several open source routing engines.

Thank you.

“Routing Engine” gave me another term to stick into google which turned up Lambertus’ thread on Gosmore which looks interesting. It also turned up YOURS.

I’m on shared hosting, so I’m a bit chary about running database dumps which might possibly run to a few hundred meg!

Glad I helped. :slight_smile:

Yes, definitely wouldn’t recommend using a database on a shared server.

What I meant, probably Harry did as well, is to set up a database on your computer. Osmosis is a program which is used to extract OSM data and populate a database with it.

If your project is a website then you’ll have to do the processing off-line and create a simplified, much reduced form of the data - as concise as possible. That should be OK-ish - assuming you’re not getting large amounts of usage!

CloudMade provide APIs for geocoding and routing based on OpenStreetMap data.

These are nice simple APIs returning XML, or JSON formatted data or a display HTML page for easy testing in your browser. For example:


the same as a JSON response:

There’s also a routing HTTP api. Routes can easily be displayed on a map. There’s also wrapper APIs available in ruby, java, and python.