Created
April 4, 2020 20:52
-
-
Save 4gus71n/26589a508d8deca333bb05928fd4beb0 to your computer and use it in GitHub Desktop.
Short script to get all the street names in one city
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install requests | |
# pip install bs4 | |
# pip install overpass | |
# pip install html5lib | |
from pprint import pprint | |
import overpass | |
import urllib.parse | |
import requests | |
from bs4 import BeautifulSoup | |
def get_osm_relation_id(address): | |
url = "https://www.openstreetmap.org/geocoder/search_osm_nominatim?query=" + urllib.parse.quote(address) | |
r = requests.get(url) | |
soup = BeautifulSoup(r.content, 'html5lib') | |
osm_link = soup.find('a', attrs = {'class':'set_position'}) | |
relation_id = osm_link.get('data-id').strip() | |
print(relation_id) | |
# 3600000000 is the id offset | |
return int(relation_id) + 3600000000 | |
def load_street_names(address): | |
id = get_osm_relation_id(address) | |
street_names = [] | |
try: | |
api = overpass.API() | |
data = api.get('area('+str(id)+')->.a;(way(area.a)["name"]["highway"]["highway" !~ "path"]["highway" !~ "steps"]["highway" !~ "motorway"]["highway" !~ "motorway_link"]["highway" !~ "raceway"]["highway" !~ "bridleway"]["highway" !~ "proposed"]["highway" !~ "construction"]["highway" !~ "elevator"]["highway" !~ "bus_guideway"]["highway" !~ "footway"]["highway" !~ "cycleway"]["foot" !~ "no"]["access" !~ "private"]["access" !~ "no"];node(w)(area.a););out;') | |
for f in data.features: | |
if f.geometry['type'] == "LineString": | |
street_names.append(f.properties['name']) | |
# Remove duplicates | |
street_names = list(dict.fromkeys(street_names)) | |
# Sort it | |
street_names = sorted(street_names) | |
except Exception as e: | |
pprint(e) | |
finally: | |
return street_names | |
def main(): | |
street_names = load_street_names("Mar del Plata, Buenos Aires, Argentina") | |
pprint(street_names) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment