def Map(min_lon, min_lat, max_lon, max_lat, area): global mini_lon global mini_lat global maxi_lon global maxi_lat mini_lon = min_lon mini_lat = min_lat maxi_lon = max_lon maxi_lat = max_lat #time is started start_time = time.time() MyApi = OsmApi() result = MyApi.Map(min_lon, min_lat, max_lon, max_lat) #get dataset from OSM website #create new figure for map fig, ax = plt.subplots() #add labels, title and axes ticks ax.ticklabel_format(useOffset=False) ax.set_ylabel('Longitude') ax.set_xlabel('Latitude') ax.set_title(area) for rs in result: if (rs['type'] == "node"): #to print the places if (len(rs['data']['tag']) != 0): #check place is not empty try: #try if a place has no name key = rs['data']['tag'].keys() detail = "" for k in key: detail = detail + k + " - " + rs['data']['tag'][ k] + "\n" #collect all information of particular node #print (detail) placesDraw(lat=rs['data']['lat'], long=rs['data']['lon'], name=detail) except: print("error") elif (rs['type'] == "way"): #to print the ways if (len(rs['data']['nd']) != 0): key = rs['data']['tag'].keys() detail = "" for k in key: detail = detail + k + " - " + rs['data']['tag'][ k] + "\n" #collect all information of particular way #print (detail) global f f = 0 # a new way is started for nd in rs['data']['nd']: getNode(nd, detail) draw_graphs(min_lat, max_lat, min_lon, max_lon) return result
def getosm(**kwargs): """ get osm region from osmapi Parameters ---------- address : string latlon : tuple or 0 dist_m : float bcart : boolean level_height : float typical level height for deriving building height from # levels typical_height : float typical height for building when no information Returns ------- coords nodes ways dpoly m latlon : tuple or 0 Notes ----- There are 3 ways to read an Open Street Map structure 1 - From an osm file ex : filename = 'B11.osm' 2 - From an osm (lat,lon) string or tuple of float 3 - From an osm address string if latlon tuple is precised, it has priority over the address string """ filename = kwargs.pop('filename', '') bcart = kwargs.pop('cart', False) typ = kwargs.pop('typ', 'indoor') level_height = kwargs.pop('level_height', 3.45) typical_height = kwargs.pop('typical_height', 10) if filename == '': address = kwargs.pop('address', 'Rennes') latlon = kwargs.pop('latlon', 0) dist_m = kwargs.pop('dist_m', 400) rad_to_deg = (180 / np.pi) if latlon == 0: place = geo.osm(address) try: lat, lon = place.latlng except: print(place) else: lat = latlon[0] lon = latlon[1] r_earth = 6370e3 alpha = (dist_m / r_earth) * rad_to_deg # # get map from OsmApi (Database query) # Osm = OsmApi() osmmap = Osm.Map(lon - alpha, lat - alpha, lon + alpha, lat + alpha) else: # # get map from osm (xml) file # type : 'node' # 'ways' # latlon = 0 e = xml.parse(filename).getroot() osmmap = [] lnode_key = ['id', 'lat', 'lon'] lway_key = ['id'] for i in e: d = {} d['type'] = i.tag d['data'] = i.attrib #print(i.tag) if d['type'] == 'node': for k in lnode_key: try: d['data'][k] = eval(d['data'][k]) except: pass if k == 'id': if not 'action' in d['data']: d['data'][k] = -d['data'][k] d['data']['tag'] = {} elif d['type'] == 'way': lk = i.getchildren() nd = [] tag = {} for k in lk: if k.tag == 'nd': nd.append(eval(k.get('ref'))) if k.tag == 'tag': tag[k.get('k')] = k.get('v') d['data']['nd'] = nd d['data']['tag'] = tag # for k in lway_key: # lk = k.get_children() # print(lk) # d['data'][k]=eval(d['data'][k]) # d['data']['visible']=eval(d['data']['visible']) osmmap.append(d) nodes = Nodes() nodes.clean() nodes.readmap(osmmap) coords = Coords() coords.clean() coords.from_nodes(nodes) m = coords.cartesian(cart=bcart) ways = Ways() ways.clean() lat = coords.latlon[list(coords.latlon.keys())[0]][0] lon = coords.latlon[list(coords.latlon.keys())[0]][1] if typ == 'indoor': ways.readmap1(osmmap, coords) else: ways.readmap2(osmmap, coords) # list of nodes involved in buildings lnodes_id = [] for iw in ways.w: lnodes_id += ways.w[iw][0] # list of all nodes of coords lnodes_id = np.unique(np.array(lnodes_id)) lnodes_full = np.unique(np.array(list(coords.latlon.keys()))) mask = np.in1d(lnodes_full, lnodes_id, invert=True) # nodes not involved in buildings #if typ != 'indoor': # lexcluded = lnodes_full[mask] # coords.filter(lexcluded) # dpoly = {} # for iw in ways.w: # # ways.way[iw].tags = {} # # # material # # if 'material' in ways.w[iw][1]: # # ways.way[iw].tags['name']=ways.w[iw][1]['material'] # # elif 'building:material' in ways.w[iw][1]: # # ways.way[iw].tags['name']=ways.w[iw][1]['building:material'] # # else: # # ways.way[iw].tags['name']='WALL' # # # min_height # # if 'building:min_height' in ways.w[iw][1]: # # min_height = eval(ways.w[iw][1]['building:min_height']) # # else: # # min_height = 0 # # # height # # if 'height' in ways.w[iw][1]: # # ways.way[iw].tags['z'] = (min_height, eval(ways.w[iw][1]['height'])) # # elif 'building:height' in ways.w[iw][1]: # # ways.way[iw].tags['z'] = (min_height, eval(ways.w[iw][1]['building:height'])) # # elif 'building:levels' in ways.w[iw][1]: # # nb_levels = eval(ways.w[iw][1]['building:levels']) # # if type(nb_levels)!=int: # # try: # # nb_levels = max(nb_levels) # # except: # # nb_levels=2 # # ways.way[iw].tags['z']=(min_height,nb_levels*level_height) # # elif 'levels' in ways.w[iw][1]: # # nb_levels = eval(ways.w[iw][1]['levels']) # # if type(nb_levels)!=int: # # try: # # nb_levels=max(nb_levels) # # except: # # nb_levels=2 # # ways.way[iw].tags['z'] = (min_height,nb_levels*level_height) # # else: # # ways.way[iw].tags['z'] = (0,typical_height) # ptpoly = [coords.xy[x] for x in ways.w[iw][0]] # dpoly[iw] = geu.Polygon(ptpoly,vnodes=ways.w[iw][0]) # dpoly[iw].coorddeter() #return coords,nodes,ways,dpoly,m return coords, nodes, ways, m, (lat, lon)
def getosm(address='Rennes', latlon=0, dist_m=400, cart=False): """ get osm region from osmapi Parameters ---------- address : string latlon : tuple or 0 dist_m : float cart : boolean Notes ----- if latlon tuple is precised it has priority over the string """ level_height = 3.45 rad_to_deg = (180 / np.pi) deg_to_rad = (np.pi / 180) if latlon == 0: place = geo.google(address) try: lat, lon = place.latlng except: print place else: lat = latlon[0] lon = latlon[1] r_earth = 6370e3 alpha = (dist_m / r_earth) * rad_to_deg Osm = OsmApi() # # Get Map around the specified coordinates # osmmap = Osm.Map(lon - alpha, lat - alpha, lon + alpha, lat + alpha) #print(osmmap) nodes = Nodes() nodes.clean() nodes.readmap(osmmap) coords = Coords() coords.clean() coords.from_nodes(nodes) m = coords.cartesian(cart=cart) ways = Ways() ways.clean() ways.readmap(osmmap, coords) # list of nodes involved in buildings lnodes_id = [] for iw in ways.w: lnodes_id += ways.w[iw][0] # list of all nodes of coords lnodes_id = np.unique(np.array(lnodes_id)) lnodes_full = np.unique(np.array(coords.latlon.keys())) mask = np.in1d(lnodes_full, lnodes_id, invert=True) # nodes not involved in buildings lexcluded = lnodes_full[mask] coords.filter(lexcluded) dpoly = {} for iw in ways.w: ways.way[iw].tags = {} # material if ways.w[iw][1].has_key('material'): ways.way[iw].tags['name'] = ways.w[iw][1]['material'] elif ways.w[iw][1].has_key('building:material'): ways.way[iw].tags['name'] = ways.w[iw][1]['building:material'] else: ways.way[iw].tags['name'] = 'WALL' # height if ways.w[iw][1].has_key('height'): ways.way[iw].tags['z'] = (0, eval(ways.w[iw][1]['height'])) elif ways.w[iw][1].has_key('building:height'): ways.way[iw].tags['z'] = (0, eval(ways.w[iw][1]['building:height'])) elif ways.w[iw][1].has_key('building:levels'): nb_levels = eval(ways.w[iw][1]['building:levels']) if type(nb_levels) != int: try: nb_levels = max(nb_levels) except: nb_levels = 2 ways.way[iw].tags['z'] = (0, nb_levels * level_height) elif ways.w[iw][1].has_key('levels'): nb_levels = eval(ways.w[iw][1]['levels']) if type(nb_levels) != int: try: nb_levels = max(nb_levels) except: nb_levels = 2 ways.way[iw].tags['z'] = (0, nb_levels * level_height) else: ways.way[iw].tags['z'] = (0, 12) ptpoly = [coords.xy[x] for x in ways.w[iw][0]] dpoly[iw] = geu.Polygon(ptpoly, vnodes=ways.w[iw][0]) dpoly[iw].coorddeter() return coords, nodes, ways, dpoly, m
def Map(min_lon, min_lat, max_lon, max_lat, area): global placecount global waycount global atmcount global buildingcount global tollcount global progress global www global swoh global wwr global dt global wocuisine global mtag global nomxsp global nochrg global nort global nolyr global poi global bmt #time is started start_time = time.time() MyApi = OsmApi() result = MyApi.Map(min_lon, min_lat, max_lon, max_lat) #get dataset from OSM website nodeChart.drawNodeBarchart(result, plt) #draw bar chart of nodes wayChart.drawWayBarchart(result, plt) #draw barchart of ways buildingChart.drawBuildingBarchart(result, plt) ##draw barchart of building #create new figure for map fig, ax = plt.subplots() #add labels, title and axes ticks ax.ticklabel_format(useOffset=False) ax.set_ylabel('Longitude') ax.set_xlabel('Latitude') ax.set_title(area) num = 0 node_num = 0 for rs in result: if (rs['type'] == "node"): #to print the places node_num += 1 if (len(rs['data']['tag']) == 0): #missing tags mtag += 1 if (len(rs['data']['tag']) != 0): #check place is not empty nodeHistory.getNodeHistory(int(rs['data']['id'])) placecount += 1 #print(rs['data']) try: #try if a place has no name key = rs['data']['tag'].keys() if 'building' in key: #check building, atm, and toll tags buildingcount += 1 if 'height' not in key or 'building:levels' not in key: bmt += 1 if 'atm' in key: atmcount += 1 if 'toll' in key: tollcount += 1 if 'charge' not in key: #toll without charge nochrg += 1 if 'amenity' in key: #place of worship start if (rs['data']['tag']['amenity'] == 'place_of_worship' ): if 'religion' not in key: wwr += 1 if 'religion' in key: if (rs['data']['tag']['religion'] == ''): wwr += 1 if 'name' not in key or 'addr:housenumber' not in key: poi += 1 #end place of worship #drive through if (rs['data']['tag']['amenity'] == 'pharmacy' or rs['data']['tag']['amenity'] == 'bank' or rs['data']['tag']['amenity'] == 'atm' or rs['data']['tag']['amenity'] == 'fast_food' or rs['data']['tag']['amenity'] == 'post_office'): if 'drive_through' in key: dt += 1 if 'name' not in key or 'addr:housenumber' not in key: poi += 1 #end drive through #food place if (rs['data']['tag']['amenity'] == 'fast_food' or rs['data']['tag']['amenity'] == 'restaurant' or rs['data']['tag']['amenity'] == 'bar' or rs['data']['tag']['amenity'] == 'cafe'): if 'cuisine' not in key: wocuisine += 1 if 'name' not in key or 'addr:housenumber' not in key: poi += 1 #end food place #place which are shop if 'shop' in key: if 'opening_hours' not in key: swoh += 1 if 'opening_hours' in key: if (rs['data']['tag']['opening_hours'] == ''): swoh += 1 if 'name' not in key or 'addr:housenumber' not in key: poi += 1 detail = "" for k in key: detail = detail + k + " - " + rs['data']['tag'][ k] + "\n" #collect all information of particular node #print (detail) placesDraw(lat=rs['data']['lat'], long=rs['data']['lon'], name=detail) except: print("error") elif (rs['type'] == "way"): #to print the ways num += 1 if (len(rs['data']['tag']) == 0): #missing tags mtag += 1 progress = (num / (len(result) - node_num)) * 100 if (len(rs['data']['nd']) != 0): waycount += 1 #print(rs['data']) key = rs['data']['tag'].keys() if 'building' in key: #check building, atm, and toll tags buildingcount += 1 if 'atm' in key: atmcount += 1 if 'toll' in key: tollcount += 1 if 'charge' not in key: #toll without charge nochrg += 1 if 'railway' in key: # check for railway crossing tag if 'crossing' not in key: nort += 1 if 'layer' in key: # layer wothout tunnel or bridge tag if ('bridge' not in key and 'tunnel' not in key): nolyr += 1 if 'width' not in key: www += 1 if 'maxspeed' not in key: nomxsp += 1 detail = "" for k in key: detail = detail + k + " - " + rs['data']['tag'][ k] + "\n" #collect all information of particular way #print (detail) global f f = 0 # a new way is started for nd in rs['data']['nd']: getNode(nd, detail) #print("Time taken in the Process --- %s seconds ---" % (time.time() - start_time)) print('1. General Information on Study Area ') print(waycount, 'Total ways') print(www, 'ways with out width') print(mtag, 'missing tags') print('2. Points-Of-Interest') print(swoh, 'shop without opening hour') print(wwr, 'worship without relegion') print(dt, 'drive through') print(wocuisine, 'places with out cuisine details') print('3. Geocoding') print(poi, 'No. of POIs without names and housenumbers ') print(bmt, 'No. of building with missing tags Height and Levels') print('4. Routing and Navigation') print(nomxsp, 'no max speed tag') print(nochrg, 'no charge tag in toll') print(nort, 'railway without crossing tag') print(nolyr, 'layer tag without bridge or tunnel') progress = 100 return result
def print_result(result, result_name): print(result_name, ": ", len(result)) if len(result) > 0: print(result) ################################### if __name__ == "__main__": user = input("Username: "******"Password: ") api = OsmApi(username=user, password=passwrd) print('Downloading...') box = api.Map(19.955807, 45.385807, 20.012455, 45.421212) print('Done.') cyr_num = 0 i = 0 missing_lat = [] wrong_latin = [] for node in box: if node['type'] == 'way': tags = get_name_tags(node) cir_tag = get_cyrilic_tag(tags) if cir_tag: cyr_num += 1
EndLon.append(temp2["lon"]) link_id.append(i) Start_Node_id.append(WayDict[i][0]) End_Node_id.append(WayDict[i][1]) return StartLat, StartLon, EndLat, EndLon, link_id, Start_Node_id, End_Node_id from osmapi import OsmApi MyApi = OsmApi() ### latitude and longitude of approximate centre of the area. lat = 41.878299 lng = -87.632304 ###Create a box for selecting map area and loading its data data = MyApi.Map(lng - 0.004445, lat - 0.001477, lng + 0.004479, lat + 0.003758) nodes = [doc for doc in data if doc["type"] == "node"] ways = [doc for doc in data if doc["type"] == "way"] ###Create link dictionaries by considering and avoiding oneway links for way in ways: if way["data"]["tag"].get("building") == None and way["data"]["tag"].get( "highway" ) != "pedestrian" and way["data"]["tag"].get("highway") != None and way[ "data"]["tag"].get("highway") != "footway" and way["data"][ "tag"].get("highway") != "steps" and way["data"]["tag"].get( "highway") != "cycleway" and way["data"]["tag"].get( "highway") != "elevator" and way["data"]["tag"].get( "highway") != "residential": nodeList = way["data"]["nd"]
def query_osm_api(bbox): my_api = OsmApi() city = my_api.Map(bbox[0], bbox[1], bbox[2], bbox[3]) nodes = [element for element in city if element['type'] == 'node'] ways = [element for element in city if element['type'] == 'way'] return nodes, ways
def osm_mat(min_lat, max_lat, min_lon, max_lon): ic_Matrix = [] MyApi = OsmApi() data = MyApi.Map(min_lon, min_lat, max_lon, max_lat) node = [] way = [] node_size = 0 way_size = 0 wat_size_limit = 1 for i in data: if i[u'type'] == "node": if ((i[u'data'][u'lat'] > min_lat) & (i[u'data'][u'lat'] < max_lat) & (i[u'data'][u'lon'] > min_lon) & (i[u'data'][u'lon'] < max_lon)): # node.append({'number': node_size + 1, 'lat': i[u'data'][u'lat'], 'lon': i[u'data'][u'lon'], # 'id': i[u'data'][u'id'], 'tag': i[u'data'][u'tag']}) node.append({ 'number': node_size + 1, 'lat': i[u'data'][u'lat'], 'lon': i[u'data'][u'lon'], 'id': i[u'data'][u'id'] }) node_size += 1 if i[u'type'] == "way": c = 0 for j in i[u'data'][u'nd']: c += 1 if c > wat_size_limit: if u'highway' in i[u'data'][u'tag']: if (i[u'data'][u'tag'][u'highway'] != 'service') & ( i[u'data'][u'tag'][u'highway'] != 'secondary_link' ) & (i[u'data'][u'tag'][u'highway'] != 'primary_link'): way.append({ 'number': way_size + 1, 'id': i[u'data'][u'id'], 'tag': i[u'data'][u'tag'], 'nd': i[u'data'][u'nd'] }) way_size += 1 temp_ar = [] for i in node: for j in way: temp = 0 for k in j['nd']: if k == i['id']: temp = 1 if temp == 1: temp_ar.append(1) else: temp_ar.append(0) ic_Matrix.append(temp_ar) temp_ar = [] ad_Matrix = [] for i in range(0, node_size): temp_ar = [] for j in range(0, node_size): temp_ar.append(0) ad_Matrix.append(temp_ar) for i in way: for j in i['nd']: temp = 0 for k in i['nd']: if j == k: temp = 1 else: if temp == 1: x = find_ind(j, node) y = find_ind(k, node) if (x != -1) & (y != -1): ad_Matrix[x][y] = 1 ad_Matrix[y][x] = 1 break delete_list = [] for i in range(0, node_size - 1): c = 0 for j in range(0, node_size - 1): if ad_Matrix[i][j] == 1: c += 1 if c == 0: delete_list.append(i) ad_Matrix = np.delete(ad_Matrix, delete_list, axis=0) ad_Matrix = np.delete(ad_Matrix, delete_list, axis=1) node_size -= len(delete_list) cross = [] for i in ad_Matrix: temp = 0 for j in i: temp += j cross.append(temp) new_cross = [[[node[i]['lat'], node[i]['lon']], cross[i]] for i in range(node_size) if cross[i] > 2] return new_cross, ad_Matrix