jazzband / geojson

Python bindings and utilities for GeoJSON
https://pypi.python.org/pypi/geojson/
BSD 3-Clause "New" or "Revised" License
913 stars 121 forks source link

[Feature request] Convert overpass-API json to geojson #140

Closed lucianopaz closed 4 years ago

lucianopaz commented 4 years ago

First of all, thanks for this great project!

I'm trying to query data from open street maps using their overpass API. It's easy to get results as a json using their overpass-ql language, but I would really love to convert those into valid geojson.

For example:

import json
import requests

overpass_url = "https://lz4.overpass-api.de/api/interpreter"
headers = {"content-type": "application/x-www-form-urlencoded"}
query = """
[out:json][timeout:25];
node["addr:city"="Washington"]["addr:street"="Pennsylvania Avenue Northwest"]["addr:housenumber"=1600]->.whitehouse;
(
  node(around.whitehouse:500)["amenity"="pub"];
  way(around.whitehouse:500)["amenity"="pub"];
  relation(around.whitehouse:500)["amenity"="pub"];
);
out body;
>;
out skel qt;
"""

r = requests.post(overpass_url, data=query, headers=headers)
print(json.loads(r.content))

This prints out:

{'version': 0.6, 'generator': 'Overpass API 0.7.55.7 8b86ff77', 'osm3s': {'timestamp_osm_base': '2019-12-20T09:15:02Z', 'copyright': 'The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.'}, 'elements': [{'type': 'node', 'id': 734267231, 'lat': 38.89646, 'lon': -77.032238, 'tags': {'amenity': 'pub', 'name': 'Round Robin Bar'}}, {'type': 'node', 'id': 739150358, 'lat': 38.8996053, 'lon': -77.033411, 'tags': {'addr:housenumber': '727', 'addr:street': '15th Street Northwest', 'amenity': 'pub', 'dataset': 'ABRA locations', 'name': 'Recess Bar', 'source': 'dcgis'}}, {'type': 'node', 'id': 739528064, 'lat': 38.9008791, 'lon': -77.0402319, 'tags': {'addr:housenumber': '1716', 'addr:street': 'I Street Northwest', 'amenity': 'pub', 'dataset': 'ABRA locations', 'name': 'Eye Bar', 'source': 'dcgis'}}, {'type': 'node', 'id': 739528139, 'lat': 38.9009945, 'lon': -77.0401739, 'tags': {'addr:housenumber': '1716', 'addr:street': 'I Street Northwest', 'amenity': 'pub', 'dataset': 'ABRA locations', 'name': 'The Bottom Line', 'source': 'dcgis'}}]}

If this is out of your project's scope, feel free to close this issue.

kylebarron commented 4 years ago

osmnx has a couple functions to parse an Overpass API response

lucianopaz commented 4 years ago

Thanks @kylebarron, but it looks like osmnx returns a regular json response from requests to overpass.

rayrrr commented 4 years ago

Hi @lucianopaz, unfortunately I believe this is out of scope for this package. The purpose of this geojson package is to adhere to the GeoJSON spec. Nothing in the spec describes conversion to the spec from arbitrary other formats. Ideally you should let your request be known to the OSM team; I would like to see them build GeoJSON export directly into the Overpass API. Alternately perhaps you could convince https://github.com/brandonxiang/geojson-python-utils to add it to their package. Building a standalone osm-geojson package from scratch is another option to consider. Lastly, do a thorough search of the web to see if someone hasn't already shared a script to do this conversion, if you haven't already. Good luck!

rayrrr commented 4 years ago

Did a bit of searching for you. It looks like https://github.com/tyrasd/overpass-turbo supports GeoJSON export. @lucianopaz can you let us know if that works for you?

lucianopaz commented 4 years ago

Thanks @rayrrr. Yeah, I felt that this was a bit out of scope of your project. I'll close the issue.

Yes, I'd seen overpass-turbo, and also found osmtogeojson and query-overpass. I had to work on other stuff first, so I still haven't decided on whether to use these nodejs packages and then collect their geojson output into python. I would prefer to have a pure python solution, because I'll be using the query results in a analysis pipeline that is in python, and nodejs packages felt as unnecessary clobber. Thanks again for your help