User:Tagtheworld/Python und OSM

From OpenStreetMap Wiki
Jump to navigation Jump to search

Python und OSM: here we describe things that adresses Python and OSM




Extract data from Open Street Map with Overpy overpass API in CSV, Excel format using Python code 266 Aufrufe•29.04.2020 video: https://www.youtube.com/watch?v=DnKJ5HrS_NI

This video tells about overpy and overpass API , how to extract data from OSM(open street maps) in excel format. Given code can extract data of hospitals, schools, roads, electricity nodes etc. check github repo - https://github.com/nikhilnagargit/OpenStreetMapDataExtraction

another video for OSM research - https://www.youtube.com/watch?v=gfuUbpAFoys




janakiv.com shows interesting details regading Loading Data from OpenStreetMap with Python and the Overpass API

04 Mar 2018 https://janakiev.com/blog/openstreetmap-with-python-and-overpass-api/

an interesting example regarding Biergarten in Germany and how many banks are hidden in Switzerland? OpenStreetMap is a great open source map of the world which can give us some insight into these and similar questions. There is a lot of data hidden in this data set, full of useful labels and geographic information, but how do we get our hands on the data?


derived from his example we have a abstraction - applied on bus stops in the USA

https://stackoverflow.com/questions/57457794/how-to-retrieve-bus-stops-from-openstreetmap-using-python


import requests
import json

overpass_url = "http://overpass-api.de/api/interpreter"
overpass_query = """
[out:json];
area["ISO3166-1"="US"][admin_level=2];
(node["highway"="bus_stop"](area);
 way["highway"="bus_stop"](area);
 rel["highway"="bus_stop"](area);
);
out center;
"""
response = requests.get(overpass_url, 
                        params={'data': overpass_query})
data = response.json()

the author run into issues : he talks like so: The result is an empty list. I would appreciate any hints on this problem – thanks!

cf https://stackoverflow.com/questions/57457794/how-to-retrieve-bus-stops-from-openstreetmap-using-python



we have to be carefully - with the nominatim: Overpass query works on overpass-turbo.eu, but not locally

cf: https://stackoverflow.com/questions/55041191/overpass-query-works-on-overpass-turbo-eu-but-not-locally


I'm trying to execute Overpass queries from a Python script. I'm practicing at overpass-turbo.eu and found the following query to work as intended:

[out:json][timeout:600];
{{geocodeArea:Niedersachsen}}->.searchArea;
(
  node[place=city](area.searchArea);  
  node[place=town](area.searchArea);  

);
out;

However, when I submit the exact same query from a Python script, I get an error:

import requests
overpass_query = """
[out:json][timeout:600];
{{geocodeArea:Niedersachsen}}->.searchArea;
(
  node[place=city](area.searchArea);  
  node[place=town](area.searchArea);  

);
out;
"""
overpass_url = "http://overpass-api.de/api/interpreter"
response = requests.get(overpass_url, params={'data': overpass_query})
data = response.json()


cf: https://stackoverflow.com/questions/55041191/overpass-query-works-on-overpass-turbo-eu-but-not-locally

The curly braces (aka Template:GeocodeArea:Niedersachsen) are a special feature of overpass turbo and are not part of Overpass API. See extended overpass turbo queries for a list of these shortcuts. Template:GeocodeArea:name will tell overpass turbo to perform a geocoding request using Nominatim. It will then use the first result to construct an area(id) query. You have to perform the same step (using Nominatim or any other geocoder) in your program.




https://stackoverflow.com/questions/48568555/python-wrapper-to-run-requests-on-the-endpoint-of-overpass-api

we have with Overpass API python wrapper a thin Python wrapper around the OpenStreetMap Overpass API https://github.com/mvexel/overpass-api-python-wrapper we have some Simple example:


You can try this.

import overpass    

api = overpass.API()
query = """
[out:csv(::id,::type,"name","addr:postcode","addr:city",
"addr:street","addr:housenumber","website"," contact:email=*")][timeout:30];
area[name="Madrid"]->.a;
( node(area.a)[amenity=hospital];
  way(area.a)[amenity=hospital];
  rel(area.a)[amenity=hospital];);
out;
"""
resp = api._get_from_overpass(query)
data = [row.split('\t') for row in resp.text.split('\n')]

or you can try this:

api = overpass.API()
query = """
area[name="Madrid"]->.a;
( node(area.a)[amenity=hospital];
  way(area.a)[amenity=hospital];
  rel(area.a)[amenity=hospital];);
"""
fmt = 'csv(::id,::type,"name","addr:postcode","addr:city","addr:street","addr:housenumber","website"," contact:email=*")'
data = api.get(query, responseformat=fmt)


Output:

for x in data[:5]:
    print(x)

 # ['@id', '@type', 'name', 'addr:postcode', 'addr:city', 'addr:street', 'addr:housenumber', 'website', ' contact:email=*']
 # ['597375537', 'node', 'Centro de especialidades Emigrantes', '', '', '', '', '', '']
 # ['1437313175', 'node', '', '', '', '', '', '', '']
 # ['1595068136', 'node', '', '', '', '', '', '', '']
 # ['2320596216', 'node', '', '', '', '', '', '', '']


cf. https://stackoverflow.com/questions/48568555/python-wrapper-to-run-requests-on-the-endpoint-of-overpass-api