import pandas as pd import requests import json pd.set_option('display.max_columns', 200) # Get yours at: http://boundingbox.klokantech.com/ bbox = [13.521945,50.919085,13.976063,51.18772] # Links unten minLat = bbox[1] minLon = bbox[0] # Rechts oben maxLat = bbox[3] maxLon = bbox[2] tags = ['primary','secondary','secondary_link','tertiary','tertiary_link','living_street','residential'] objects = ['way'] # like way, node, relation compactOverpassQLstring = '[out:json][timeout:60];(' for tag in tags: for obj in objects: compactOverpassQLstring += '%s["highway"="%s"](%s,%s,%s,%s);' % (obj, tag, minLat, minLon, maxLat, maxLon) compactOverpassQLstring += ');out body;>;out skel qt;' osmrequest = {'data': compactOverpassQLstring} osmurl = 'http://overpass-api.de/api/interpreter' # Ask the API osm = requests.get(osmurl, params=osmrequest) osmdata = osm.json() osmdata = osmdata['elements'] for dct in osmdata: if dct['type']=='way': for key, val in dct['tags'].iteritems(): dct[key] = val del dct['tags'] else: pass # nodes osmdf = pd.DataFrame(osmdata) osmdf.tail(5) for tag in osmdf.highway.unique(): print tag for tag in osmdf.maxspeed.unique(): print tag highwaydf = osmdf[['id', 'highway', 'lanes', 'name', 'maxspeed', 'nodes', 'ref']].dropna(subset=['name','highway'], how='any') highwaydf.tail(5) highwaydf = highwaydf[highwaydf['highway'].isin(tags)] highwaydf = highwaydf.fillna(u'unknown').reset_index(drop=True) highwaydf.tail(5) nodes = [] for dct in osmdata: #print dct if dct['type']=='way': pass elif dct['type']=='node': nodes.append(dct) else: pass nodesdf = pd.DataFrame(nodes) nodesdf.tail(5) def gethighwayids(nodeid): highwayids = [] for n, nodes in enumerate(highwaydf['nodes']): for nid in nodes: # alle nodes des highways if nid == int(nodeid): highwayids.append(highwaydf['id'][n]) return highwayids nodesdf['highwayids'] = nodesdf['id'].apply(gethighwayids) nodesdf.head(10) def identifyjunction(highwayids): #print highwayids if len(highwayids) < 2: return False elif len(highwayids)==2: # Wenn beide Highways den gleichen Namen haben, ist es nur # ein Zwischenstück und keine Kreuzung name1=unicode(highwaydf[highwaydf['id']==highwayids[0]]['name'].values) name2=unicode(highwaydf[highwaydf['id']==highwayids[1]]['name'].values) if name1==name2: #print('%s=%s' % (name1, name2)) return False else: return True else: return True nodesdf['junction'] = nodesdf['highwayids'].apply(identifyjunction) nodesdf.tail(10) junctionsdf = nodesdf[['id','lat','lon']][nodesdf['junction']==True] junctionsdf.to_csv('junctions.csv', index=False) print('done.')