|
| 1 | +Overpass API python wrapper |
| 2 | +=========================== |
| 3 | + |
| 4 | +This is a thin wrapper around the OpenStreetMap `Overpass |
| 5 | +API <http://wiki.openstreetmap.org/wiki/Overpass_API>`__. |
| 6 | + |
| 7 | +.. image:: https://travis-ci.org/mvexel/overpass-api-python-wrapper.svg |
| 8 | + :target: https://travis-ci.org/mvexel/overpass-api-python-wrapper |
| 9 | + |
| 10 | + |
| 11 | +Install it |
| 12 | +========== |
| 13 | + |
| 14 | +.. code:: bash |
| 15 | +
|
| 16 | + $ pip install overpass |
| 17 | +
|
| 18 | +If you get an error similar to |
| 19 | + |
| 20 | +:: |
| 21 | + |
| 22 | + OSError: Could not find library geos_c or load any of its variants ['libgeos_c.so.1', 'libgeos_c.so'] |
| 23 | + |
| 24 | +you can install the required libraries on linux with |
| 25 | + |
| 26 | +.. code:: bash |
| 27 | +
|
| 28 | + $ sudo apt-get install libgeos-c1 libgeos-3.4.2 |
| 29 | +
|
| 30 | +Use it |
| 31 | +====== |
| 32 | + |
| 33 | +You can use the overpass command line interface or use it as a Python |
| 34 | +library. |
| 35 | + |
| 36 | +Command line interface |
| 37 | +--------------------- |
| 38 | + |
| 39 | +You can use the CLI to execute queries and save the result in a GeoJSON |
| 40 | +file. |
| 41 | + |
| 42 | +:: |
| 43 | + |
| 44 | + Usage: overpass [OPTIONS] QUERY OUTPUT_FILE |
| 45 | + |
| 46 | + Run query and save the result in output_file |
| 47 | + |
| 48 | + Options: |
| 49 | + --timeout INTEGER Timeout in seconds |
| 50 | + --endpoint TEXT URL of your prefered API |
| 51 | + --format TEXT Format to save the data. Options are 'geojson', 'json', 'xml'. Default format is geojson. |
| 52 | + --help Show this message and exit. |
| 53 | + |
| 54 | +For example: |
| 55 | + |
| 56 | +To make a query and save the result as GeoJSON: |
| 57 | + |
| 58 | +.. code:: bash |
| 59 | +
|
| 60 | + overpass --timeout 50 'node(area:3602758138)[amenity=cafe]' brasilia-cafe.geojson |
| 61 | +
|
| 62 | +Or to get the result as an OSM XML file: |
| 63 | + |
| 64 | +.. code:: bash |
| 65 | +
|
| 66 | + overpass --timeout 50 --format xml 'node(area:3602758138)[amenity=cafe]' brasilia-cafe.osm |
| 67 | +
|
| 68 | +Python Library |
| 69 | +------------- |
| 70 | + |
| 71 | +.. code:: python |
| 72 | +
|
| 73 | + >>> import overpass |
| 74 | + >>> api = overpass.API() |
| 75 | + >>> response = api.Get('node["name"="Salt Lake City"]') |
| 76 | +
|
| 77 | +Note that you don't have to include any of the output meta statements. |
| 78 | +The wrapper will, well, wrap those. |
| 79 | + |
| 80 | +You will get your result as a dictionary, which (for now) represents the |
| 81 | +JSON output you would get `from the Overpass API |
| 82 | +directly <http://overpass-api.de/output_formats.html#json>`__. So you |
| 83 | +could do this for example: |
| 84 | + |
| 85 | +.. code:: python |
| 86 | +
|
| 87 | + >>> print [(feature['tags']['name'], feature['id']) for feature in response['elements']] |
| 88 | + [(u'Salt Lake City', 150935219), (u'Salt Lake City', 585370637), (u'Salt Lake City', 1615721573)] |
| 89 | +
|
| 90 | +You can specify the format of the response. By default, you will get GeoJSON using the `responseformat` parameter. Alternatives are plain JSON (`json`) and OSM XML (`xml`), as ouput directly by the Overpass API. |
| 91 | + |
| 92 | +.. code:: python |
| 93 | +
|
| 94 | + >>> import overpass |
| 95 | + >>> api = overpass.API() |
| 96 | + >>> response = api.Get('node["name"="Salt Lake City"]', responseformat="xml") |
| 97 | +
|
| 98 | +Parameters |
| 99 | +~~~~~~~~~~ |
| 100 | + |
| 101 | +The API takes a few parameters: |
| 102 | + |
| 103 | +``endpoint`` |
| 104 | +^^^^^^^^^^ |
| 105 | + |
| 106 | +The default endpoint is ``http://overpass-api.de/api/interpreter`` but |
| 107 | +you can pass in the rambler instance |
| 108 | +(``http://overpass.osm.rambler.ru/cgi/interpreter``) or your own: |
| 109 | + |
| 110 | +.. code:: python |
| 111 | +
|
| 112 | + api = overpass.API(endpoint=http://overpass.myserver/interpreter) |
| 113 | +
|
| 114 | +``timeout`` |
| 115 | +^^^^^^^^^^ |
| 116 | + |
| 117 | +The default timeout is 25 seconds, but you can set it to whatever you |
| 118 | +want. |
| 119 | + |
| 120 | +.. code:: python |
| 121 | +
|
| 122 | + api = overpass.API(timeout=600) |
| 123 | +
|
| 124 | +``debug`` |
| 125 | +^^^^^^^^^^ |
| 126 | + |
| 127 | +Setting this to ``True`` will get you debug output. |
| 128 | + |
| 129 | +Simple queries |
| 130 | +~~~~~~~~~~~ |
| 131 | + |
| 132 | +In addition to just send your query and parse the result, the wrapper |
| 133 | +provides shortcuts for often used map queries. To use them, just pass |
| 134 | +them like to normal query to the API. |
| 135 | + |
| 136 | +MapQuery |
| 137 | +^^^^^^^^ |
| 138 | + |
| 139 | +This is a shorthand for a `complete ways and |
| 140 | +relations <http://wiki.openstreetmap.org/wiki/Overpass_API/Language_Guide#Completed_ways_and_relations>`__ |
| 141 | +query in a bounding box (the 'map call'). You just pass the bounding box |
| 142 | +to the constructor: |
| 143 | + |
| 144 | +.. code:: python |
| 145 | +
|
| 146 | + >>> map_query = overpass.MapQuery(50.746,7.154,50.748,7.157) |
| 147 | + >>> response = api.Get(map_query) |
| 148 | +
|
| 149 | +WayQuery |
| 150 | +^^^^^^^^ |
| 151 | + |
| 152 | +This is shorthand for getting a set of ways and their child nodes that |
| 153 | +satisfy certain criteria. Pass the criteria as a Overpass QL stub to the |
| 154 | +constructor: |
| 155 | + |
| 156 | +.. code:: python |
| 157 | +
|
| 158 | + >>> way_query = overpass.WayQuery('[name=Highway 51]') |
| 159 | + >>> response = api.Get(way_query) |
| 160 | +
|
| 161 | +Need help? Want feature? |
| 162 | +======================= |
| 163 | + |
| 164 | +Create a `new |
| 165 | +issue <https://github.com/mvexel/overpass-api-python-wrapper/issues>`__. |
| 166 | + |
| 167 | +Test it |
| 168 | +------- |
| 169 | + |
| 170 | +:: |
| 171 | + |
| 172 | + py.test |
| 173 | + |
| 174 | +Fork it |
| 175 | +------- |
| 176 | + |
| 177 | +`Yes |
| 178 | +please <https://github.com/mvexel/overpass-api-python-wrapper/fork>`__. |
| 179 | +`Help |
| 180 | +wanted <https://github.com/mvexel/overpass-api-python-wrapper/labels/help%20wanted>`__. |
0 commit comments