mvexel / overpass-api-python-wrapper

Python bindings for the OpenStreetMap Overpass API
Apache License 2.0
371 stars 91 forks source link

Support area as geojson #154

Open TheSnoozer opened 2 years ago

TheSnoozer commented 2 years ago
[out:json][timeout:25];
// gather results
(
  // (around:radius,latitude,longitude)
  is_in(43.5391108,6.9376076);
);
// print results
out body;
>;
out skel qt;

returns valid data (e.g. area) however the api just states overpass.errors.UnknownOverpassError: Received corrupt data from Overpass (invalid element).

Besides that a better error message would be:

raise UnknownOverpassError(f"Received element {elem_type=} which is currently not implemented, or represents corrupted data.")
mvexel commented 9 months ago

This should be solved on the main branch now after merging #140. Haven't tested extensively but

import overpass
api = overpass.API(responseformat="geojson")
query = "is_in(43.5391108,6.9376076);out body;>;"
resp = api.get(query)

returns the GeoJSON (you'd have to json.dumps() the dict to get a valid JSON string.

We'll get these improvements out in the next release 0.7.1.

TheSnoozer commented 9 months ago

Thanks a lot!

TheSnoozer commented 9 months ago

Sorry I closed this too early. It seems the "area" information is not available.

Consider:

import overpass
import pprint

api = overpass.API(responseformat="geojson", debug=True)
query = "is_in(43.5391108,6.9376076);out body;>;"
pprint.print(api.get(query, verbosity="skel qt"))

which seems to run the following query:

[out:json];is_in(43.5391108,6.9376076);out body;>;out skel qt;

result is:

{'features': [{'geometry': {'coordinates': [[[6.9363278, 43.5388949],
                                             // SNIP
                                             [6.9363278, 43.5388949]]],
                            'type': 'Polygon'},
               'properties': {'id': 494418615,
                              'nodes': [3812825108,
                                        // SNIP
                                        3812825108],
                              'tags': {'landuse': 'residential'},
                              'type': 'way'},
               'type': 'Feature'}],
 'type': 'FeatureCollection'}

when using the same query with curl:

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'data=%5Bout%3Ajson%5D%3Bis_in%2843.5391108%2C6.9376076%29%3Bout+body%3B%3E%3Bout+skel+qt%3B' https://overpass-api.de/api/interpreter
{
  "version": 0.6,
  "generator": "Overpass API 0.7.61.5 4133829e",
  "osm3s": {
    "timestamp_osm_base": "2024-02-13T22:07:10Z",
    "timestamp_areas_base": "2024-02-13T18:32:04Z",
    "copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL."
  },
  "elements": [

{
  "type": "way",
  "id": 494418615,
  "nodes": [
    3812825108,
   // SNIP
    3812825108
  ],
  "tags": {
    "landuse": "residential"
  }
},
{
  "type": "area",
  "id": 3600007385,
  "tags": {
    "ISO3166-2": "FR-06",
    "admin_level": "6",
    "alt_name:de": "Meeralpen",
    "border_type": "departement",
    "boundary": "administrative",
    "name": "Alpes-Maritimes",
....

The id 3600007385 (area) is not included in the results provided by overpass.API. Thanks :-)