Xapi

From OpenStreetMap Wiki
(Redirected from XAPI)
Jump to: navigation, search
Help
Available languages
Deutsch English Français 日本語 Português do Brasil

The OSM Extended API (or xapi, pronounced zappy) is a read-only API, based on a modified version of the standard API, that provides enhanced search and querying capabilities. It implements the standard map request and a number of additional ways of querying OSM data by tag values. Xapi uses a REST style interface with X-path flavouring.

Xapi only ever deals with elements that are current and does not return any elements that are historical or deleted. The source database is a mirror of the main OSM database and is updated via the per-minute diff dumps. The data is normally no more than about 10 minutes behind the main database.

All responses are in the same format as the standard protocol, but with the addition of some namespaced extensions. Tag queries use an X-path like syntax to specify search terms. The visible attribute is excluded from the response because it always has a true value.

Contents

Servers

Traditional

The traditional (original) XAPI was created using GT.M (formerly MUMPS) by User:80n. There were multiple instances of the xapi service running on several different servers. Each server could be accessed directly or via a redirect which was used to direct the request to a suitable server of its choosing.

All of the original XAPI servers have been decommissioned or are otherwise no longer running.

Overpass API

Overpass API now has a XAPI Compability Layer. (status: The server has low load.). It supports the more common XAPI queries, as well as its own, more extensible query language. Details are explained in the section XAPI Compability Layer.

jXAPI

In January 2011 Ian Dees created a new Java version of XAPI backed onto postGIS. See his diary entry for details. Now available on new hardware with a service base URL: http://jxapi.openstreetmap.org/xapi/api/0.6/...

You can also try it out via "uixapi" here.

Note that bbox queries have a maximum of 10 square degrees.

An advantage of jXAPI is that java and postGIS are a bit less exotic than GT.M (which few developers have heard of), however installation and tuning is still quite a technical challenge. See pnorman's blog post on jXAPI setup

Query Map

The Map query is identical to the main API map query and returns:

GET /api/0.6/map?bbox=left,bottom,right,top

where:

Example

http://www.informationfreeway.org/api/0.6/map?bbox=11.54,48.14,11.543,48.145

Notes

Query Tags

Xapi implements queries for all three kinds of elements; nodes, ways and relations.

Nodes

A node request will return an xml document containing nodes and their associated tags. The URL for a node request takes the following form:

http://www.informationfreeway.org/api/0.6/node[...]

This will return a standard OSM API response containing nodes and tags. For example:

  <?xml version='1.0' standalone='no'?>
  <osm version='0.6' generator='xapi: OSM Extended API' 
   xmlns:xapi='http://www.informationfreeway.org/xapi/0.6'
   xapi:uri='/api/0.6/node[amenity=hospital]'
   xapi:planetDate='200803150826' 
   xapi:copyright='2008 OpenStreetMap contributors' 
   xapi:instance='zappy2'>
    <node id='672180' lat='48.2111685091189' lon='16.3035366605548' timestamp='2006-09-11T16:28:25+01:00' version='1' changeset='10968'>
      <tag  k='amenity' v='hospital'/>
      <tag  k='name' v='Wilhelminenspital'/>
    </node>
    <node id='3596186' lat='53.4633699598014' lon='-2.22667910006381' timestamp='2007-06-21T17:10:58+01:00' version='2' changeset='2213'>
      <tag  k='amenity' v='hospital'/>
      <tag  k='name' v='Manchester Royal Infirmary'/>
    </node>
    ...
  </osm>

Ways

The URL for way requests has the following form:

http://www.informationfreeway.org/api/0.6/way[...]

This returns an xml document containing ways that match the search terms. For each matching way the nodes referenced by that way are also returned. An example of the response from a way query looks like this:

  <?xml version='1.0' standalone='no'?>
  <osm version='0.6' generator='xapi: OSM Extended API' 
   xmlns:xapi='http://www.informationfreeway.org/xapi/0.6'
   xapi:uri='/api/0.6/way[landuse=residential]'
   xapi:planetDate='200803150826' 
   xapi:copyright='2008 OpenStreetMap contributors' 
   xapi:instance='zappy2'>
    <node id='218963' lat='52.5611324692581' lon='-1.79024812573334' timestamp='2006-03-22T16:47:48+00:00' version='1' changeset='2211'>
    </node>
    <node id='331193' lat='53.7091237972264' lon='-1.50282510180841' timestamp='2007-03-31T00:09:22+01:00' version='1' changeset='2211'>
      <tag  k='highway' v='traffic_signals'/>
      <tag  k='source' v='Yahoo'/>
    </node>
    ...
    <way id='4958218' timestamp='2007-07-25T01:55:35+01:00' version='3' changeset='2211'>
      <nd ref='218963'/>
      <nd ref='331193'/>
      ...
      <tag  k='landuse' v='residential'/>
      <tag  k='source' v='landsat'/>
    </way>
  </osm>

Relations

The URL for relation requests has the following form:

http://www.informationfreeway.org/api/0.6/relation[...]

This returns an xml document containing relations that match the search terms. For each matching relation the nodes and ways referenced by that relation are also returned. An example of the response from a relation query looks like this:

  <?xml version='1.0' standalone='no'?>
  <osm version='0.6' generator='xapi: OSM Extended API' 
   xmlns:xapi='http://www.informationfreeway.org/xapi/0.6'
   xapi:uri='/api/0.6/way[landuse=residential]'
   xapi:planetDate='200803150826' 
   xapi:copyright='2008 OpenStreetMap contributors' 
   xapi:instance='zappy2'>
    <node ...
    <way ...
    <relation id='2670' timestamp='2007-10-25T03:05:34Z' version='32' changeset='2211'>
      <member type='way' ref='3992472' role=''/>
      <member type='way' ref='3992524' role=''/>
      <member type='way' ref='4253050' role=''/>
      <member type='way' ref='4253053' role=''/>
      <member type='way' ref='4266813' role=''/>
      <member type='way' ref='10285106' role=''/>
      <tag k='name' v='Fonnereau Way'/>
      <tag k='network' v='Ipswich footpaths'/>
      <tag k='type' v='route'/>
    </relation>    
  </osm>

All Elements

All elements (nodes, ways and relations) that match the given filter predicates can be requested using the following URL:

http://www.informationfreeway.org/api/0.6/*[...]

This returns an xml document containing nodes, ways and relations that match the search terms. For each matching way the nodes and referenced by that way are also returned. Likewise, for each matching relation the ways and nodes referenced by that relation are also returned. An example of the response from this type of query looks like this:

  <?xml version='1.0' standalone='no'?>
  <osm version='0.6' generator='xapi: OSM Extended API' 
   xmlns:xapi='http://www.informationfreeway.org/xapi/0.6'
   xapi:uri='/api/0.6/*[amenity=hotel]'
   xapi:planetDate='200803150826' 
   xapi:copyright='2008 OpenStreetMap contributors' 
   xapi:instance='zappy2'>
    <node id='218963' lat='52.5611324692581' lon='-1.79024812573334' timestamp='2006-03-22T16:47:48+00:00' version='1' changeset='2211'>
    </node>
    <node id='331193' lat='53.7091237972264' lon='-1.50282510180841' timestamp='2007-03-31T00:09:22+01:00' version='1' changeset='2211'>
      <tag k='amenity' v='hotel'/>
    </node>
    ... 
    <way id='4958218' timestamp='2007-07-25T01:55:35+01:00' version='1' changeset='2211'>
      <nd ref='218963'/>
      <nd ref='331193'/>
      ...
      <tag k='amenity' v='hotel'/>
      <tag k='building' v='hotel'/>
    </way>
    <relation id='123456' timestamp='2007-10-25T03:05:34Z' version='32' changeset='2211'>
      <member type='node' ref='331193' role=''/>
      <member type='node' ref='331194' role=''/>
      ...
      <tag k='amenity' v='hotel'/>
      <tag k='operator' v='Premier Inns'/>
      <tag k='type' v='operators'/>
    </relation>    
  </osm>

Predicates

Each API request can be suffixed with selection predicates which determine which elements to select. For example [amenity=hospital] will select all elements that have an amenity tag with a value of hospital. The full url to select all nodes that are tagged as hospitals would be:

http://www.informationfreeway.org/api/0.6/node[amenity=hospital]

A request can be suffixed with multiple predicates, each of which further constrains the results (currently limited to one tag predicate and one bbox predicate, though jXAPI and Overpass servers support multiple tag predicates). For example:

http://www.informationfreeway.org/api/0.6/node[amenity=hospital][bbox=-6,50,2,61]

this request will select and return all nodes with a tag of amenity=hospital within the bounding box that covers the whole of the England, Wales and Scotland.

Tag Predicates

A selection predicate must be of the form [key=value]. For example: node[amenity=hospital] will match any node that has an amenity key with a value of hospital, ie
<tag k="amenity" v="hospital"/>
.

A union operator may be used to select multiple values. For example to select all major roads:

The union operator can also be used with the key. For example to select golf courses:

A wildcard can be used for the value (but not for the key), so predicates similar to the following examples are possible:

Key wildcards were supported in the past, but this is no longer maintained.

Note: The URL needs to be UTF-8 url encoded in case of non-ascii characters.

BBox Predicates

Another form of selection predicate is the bbox pseudo key. A selection predicate of the form [bbox=left,bottom,right,top] defines a bounding box that is used to limit the extent of the result document. Only elements that have some part within the bbox will be included in the result document.

Unlike the standard OSM API, the bbox predicate is not limited in size unless it is used without a tag predicate, in which case it is limited to 100 square degrees.

The default extent, if a bbox predicate is not specified, is the whole planet. This is equivalent to specifying [bbox=-180,-90,180,90].

Child Element Predicates

Items can be selected based on whether or not they have child elements. For example, ways can be selected that do not have any tags or do not have any nodes. This is achieved by using an XPath-like element test. Specifically the following are implemented:

Notes:

  1. If using wget then parenthesis characters need to be escaped, so for negation predicates, use [not\(way\)] rather than [not(way)].
  2. In the OSM xml schema nodes do not really have ways as child elements. However, there is, logically, a many-to-many relationship between nodes and ways, and so xapi implements this relationship and enables nodes to be selected based on whether or not they are members of a way. Of course, the output xml is the same as normal and does not include way elements within a node element.

Escaping

Values within predicates can be escaped by prefixing a backslash character. For example to retrieve elements with a tag value that contains a pipe character such as [route=46|46A] you would need to use [route=46\|46A].

The following characters can be escaped in this way: | [ ] * / = ( ) \ and space

Spaces in tag values do not normally need to be escaped but this is dependent on the client. In this case you need to use URL escaping which requires that each space be replaced by %20. So [route=46 46A] would become [route=46%2046A].

Tags

The main method of querying Xapi is using a tag/value combination in a predicate. In addition to all the normal tags that you expect to find, the following attributes can also be queried using /api/0.6/*[@attribute=value]

These queries are best run on the normal XAPI server as the jxapi server runs these extremely slowly

Usage

Xapi URLs can be used from within any web-browser, however a simple query can return several Mb of data which can overwhelm some browsers. It is recommended that a tool such as wget or curl be used with the output directed to a file. For example, the following command uses wget to select all hospitals and store the result document in a file named data.osm. Note that wget has a default timeout of 15 minutes which may be too short for some queries so be sure to add the --timeout=0 option to turn this off.

wget --timeout=0 http://www.informationfreeway.org/api/0.6/node[amenity=hospital] -O data.osm

Note: If you use curl, it will handle redirections automatically, so you need to query the host server directly or use the "--location" option. Also you need to use the "--globoff" option, otherwise it will try to interpret the brackets ("[", "]") as part of the command line:

curl --location --globoff "http://www.informationfreeway.org/api/0.6/node[amenity=hospital]" -o data.osm

If you use a script regularly to fetch data from Xapi it would be helpful to set the user agent string to something meaningful. This will help us understand more about how Xapi is being used.

Limitations

Proposed Future Extensions

Alternatives

See also

Personal tools
Namespaces
Variants
Actions
site
Toolbox