Beispiel #1
0
def test_network_saving_loading():

    # save/load graph as shapefile and graphml file
    G = ox.graph_from_place('Piedmont, California, USA')
    G_projected = ox.project_graph(G)
    ox.save_graph_shapefile(G_projected)
    ox.save_graphml(G_projected)
    ox.save_graphml(G_projected, filename='gephi.graphml', gephi=True)
    G2 = ox.load_graphml('graph.graphml')
    G3 = ox.load_graphml('graph.graphml', node_type=str)

    # convert graph to node/edge GeoDataFrames and back again
    gdf_edges = ox.graph_to_gdfs(G, nodes=False, edges=True, fill_edge_geometry=False)
    gdf_nodes, gdf_edges = ox.graph_to_gdfs(G, nodes=True, edges=True, node_geometry=True, fill_edge_geometry=True)
    G4 = ox.gdfs_to_graph(gdf_nodes, gdf_edges)

    # find graph nodes nearest to some set of points
    X = gdf_nodes['x'].head()
    Y = gdf_nodes['y'].head()
    nn1 = ox.get_nearest_nodes(G, X, Y)
    nn2 = ox.get_nearest_nodes(G, X, Y, method='kdtree')
    nn3 = ox.get_nearest_nodes(G, X, Y, method='balltree')

    # find graph edges nearest to some set of points
    ne1 = ox.get_nearest_edges(G, X, Y)
    ne2 = ox.get_nearest_edges(G, X, Y, method='kdtree')
    ne3 = ox.get_nearest_edges(G, X, Y, method='kdtree', dist=50)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 27 19:26:13 2020

@author: niall
"""

import osmnx as ox
import pandas as pd

G = ox.load_graphml('/Users/niall/saferoute/data/cleaned/Toronto.graphml')
df = pd.read_csv('/Users/niall/saferoute/data/processed/roads_and_probs.csv')
df = df[['u', 'v', 'collision_prob', 'collision_yn']]
df['colour'] = 'red'

#add coloour to the edges that
nodes, edges = ox.graph_to_gdfs(G)
edges = edges.merge(df, on=['u', 'v'], how='left')
edges.colour[edges.colour.isna()] = 'grey'

ox.plot.plot_graph(G, edge_color=edges.colour, node_size=0, fig_height=12, fig_width=12)


#ox.plot.get_edge_colors_by_attr(G, attr, num_bins=5, cmap='viridis', start=0, stop=1, na_color='none')
Beispiel #3
0
# import db_connect
import datetime
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from folium_stuff_FK_map_matching import plot_graph_folium_FK
import glob
from funcs_network_FK import cost_assignment
import statistics


# load grafo
# file_graphml = 'Catania__Italy.graphml'
file_graphml = 'Catania__Italy_cost.graphml'
grafo = ox.load_graphml(file_graphml)
# ox.plot_graph(grafo)
place_country = "Catania, Italy"
# cost_assignment(file_graphml, place_country)

####################################################
#### overlay Hexagonal grid  #######################


os.chdir('C:\\ENEA_CAS_WORK\\Catania_RAFAEL\\postprocessing')
# load all EDGES obtained from the map-matching algorithm
TIME_EDGES = gpd.read_file("TIME_EDGES.geojson")
# TIME_EDGES.plot()


# load all EDGES obtained from the map-matching algorithm (RECORDS and FREQUENCIES)
            edge = G.get_edge_data(route[i], route[i - 1])
            try:
                # Meters to Miles conversion
                edge_length = edge[0]['length'] * 0.00062137
            except:
                edge_length = 0
            total_length += edge_length
        i += 1
    return (total_length)


print('Getting road network')

if os.path.isfile(root + 'data/' + filename):
    # Read in the file
    G = ox.load_graphml(root + 'data/' + filename)
else:
    # Create file since it doesn't exist
    ox.config(use_cache=True, log_console=False)
    G = ox.graph_from_place('Dutchess County, New York, USA',
                            network_type='drive')
    ox.save_graphml(G, filename=filename)

stations = pd.read_excel(root + 'Drive Distance Data.xlsx', 'Stations')
events = pd.read_excel(root + 'Drive Distance Data.xlsx', 'Events')
length_data = list()

for index, row in stations.iterrows():
    station_name = row['Station']
    station = (row['Lat'], row['Lon'])
    station_node = ox.get_nearest_node(G, station)
Beispiel #5
0
import json
from chromosome import Chromosome
from helper import Helper
import osmnx as ox
import networkx as nx

LISBON_GRAPH = 'lisbon_graph.graphml'
G_lisbon = ox.load_graphml(LISBON_GRAPH)


def show_route(edges):
    route = []
    route.append(edges[0][0])
    previous_node = edges[0][1]
    for edge in edges[1:]:
        go_node = edge[0]
        r = nx.shortest_path(G_lisbon, previous_node, go_node, weight='length')
        if False and len(r) <= 1:
            print 'strange... => previus_node:' + str(
                previous_node) + ' go_node:' + str(go_node)
            print r
        route.extend(r)
        previous_node = edge[1]

    print 'route with ' + str(len(route)) + ' nodes'
    ox.plot_graph_route(G_lisbon, route, route_linewidth=2)


result = None

with open("best_solutions.json", "r") as f:
Beispiel #6
0
_graphml_file = os.path.join(DATA_FOLDER, GRAPHML_FILE)
_grid_file = os.path.join(DATA_FOLDER, GRID_FILE)
_hospital_shapefile = os.path.join(DATA_FOLDER, HOSPITAL_DATA)
_population = os.path.join(DATA_FOLDER, POPULATION_DATA)

# In[5]:

assert os.path.exists(_graphml_file)
assert os.path.exists(_grid_file)
assert os.path.exists(_hospital_shapefile)
assert os.path.exists(_population)

# In[6]:

G = ox.load_graphml(_graphml_file, node_type=str)
grid_file = gpd.read_file(_grid_file)
hospitals = gpd.read_file(_hospital_shapefile)
pop_data = gpd.read_file(_population)

# In[7]:


def network_setting(network):
    _nodes_removed = len([n for (n, deg) in network.out_degree() if deg == 0])
    network.remove_nodes_from(
        [n for (n, deg) in network.out_degree() if deg == 0])
    for component in list(nx.strongly_connected_components(network)):
        if len(component) < 10:
            for node in component:
                _nodes_removed += 1
Beispiel #7
0

"""
Settings
"""
district_name = 'simple_drone'
"""
Settings
"""

path = f'./data/{district_name}_matrix'
ga = GenericAlgorithm(path, generations=500, population_size=300, alpha=3)
result = ga.run()

# get Graph from graphml
g = ox.load_graphml(f'./data/{district_name}.xml')
g = nx.to_undirected(g)

# save result
save_dict_to_file(result, district_name)

# convert index to node_id
route = []
route.append(result['route'][0][0])
for edge in result['route']:
    route.append(edge[1])

# route
nodes = list(g.nodes.data())
for i in range(len(route)):
    route[i] = nodes[route[i]][0]
from flask import *
import osmnx as ox
from IPython.display import IFrame
from bs4 import BeautifulSoup
import shutil
from polyline_string import PolyLine_String
import json
import os
"""program shows behaviour of graph after deleting node and
   visualise chosen edges as a tram loop for a testing purposes"""
"""store as G osmnx Wroclaw tramline graph as a base for computation"""

app = Flask(__name__)
shutil.copy('data/osmnx_graph_origin.graphml', 'data/osmnx_graph.graphml')
G = ox.load_graphml('osmnx_graph.graphml')
ox.config(log_console=True, use_cache=True)
"""Polyline object stores loops chosen during usage"""

polyline_string = PolyLine_String()


@app.route('/')
def home():
    make_updated_graph_model()
    return render_template('index.html',
                           string=polyline_string.polyline_string)


"""function gets data from  template"""

Beispiel #9
0
            'graph_file': COCHRANE_GRAPH,
            'shortest_path': COCHRANE_SHORTEST_PATH
        },
        'lethbridge': {
            'graph_file': LETHBRIDGE_GRAPH,
            'shortest_path': LETHBRIDGE_SHORTEST_PATH
        }
    }

    #city_to_use = 'lethbridge'
    city_to_use = 'cochrane'

    city_info = cities[city_to_use]

    # Load City of Lethbridge map from file, to save time
    city_graph = ox.load_graphml(city_info['graph_file'], GRAPH_FOLDER)
    shortest_path_file = city_info['shortest_path']
    """
    city_graph = ox.graph_from_place(
        'Town of Cochrane, Alberta',
        network_type='drive',
        simplify=True,
        clean_periphery=True,
        truncate_by_edge=True
    )
    """

    p = 1

    before = datetime.utcnow()
    median_result_costs, median_result_nodes = \
Beispiel #10
0
def test_graph_save_load():

    # save graph as shapefile and geopackage
    G = ox.graph_from_place(place1, network_type="drive")
    ox.save_graph_shapefile(G)
    ox.save_graph_geopackage(G, directed=False)

    # save/load geopackage and convert graph to/from node/edge GeoDataFrames
    fp = ".temp/data/graph-dir.gpkg"
    ox.save_graph_geopackage(G, filepath=fp, directed=True)
    gdf_nodes1 = gpd.read_file(fp, layer="nodes").set_index("osmid")
    gdf_edges1 = gpd.read_file(fp, layer="edges").set_index(["u", "v", "key"])
    G2 = ox.graph_from_gdfs(gdf_nodes1, gdf_edges1)
    G2 = ox.graph_from_gdfs(gdf_nodes1, gdf_edges1, graph_attrs=G.graph)
    gdf_nodes2, gdf_edges2 = ox.graph_to_gdfs(G2)
    assert set(gdf_nodes1.index) == set(gdf_nodes2.index) == set(
        G.nodes) == set(G2.nodes)
    assert set(gdf_edges1.index) == set(gdf_edges2.index) == set(
        G.edges) == set(G2.edges)

    # create random boolean graph/node/edge attributes
    attr_name = "test_bool"
    G.graph[attr_name] = False
    bools = np.random.randint(0, 2, len(G.nodes))
    node_attrs = {n: bool(b) for n, b in zip(G.nodes, bools)}
    nx.set_node_attributes(G, node_attrs, attr_name)
    bools = np.random.randint(0, 2, len(G.edges))
    edge_attrs = {n: bool(b) for n, b in zip(G.edges, bools)}
    nx.set_edge_attributes(G, edge_attrs, attr_name)

    # save/load graph as graphml file
    ox.save_graphml(G, gephi=True)
    ox.save_graphml(G, gephi=False)
    filepath = Path(ox.settings.data_folder) / "graph.graphml"
    G2 = ox.load_graphml(
        filepath,
        graph_dtypes={attr_name: ox.io._convert_bool_string},
        node_dtypes={attr_name: ox.io._convert_bool_string},
        edge_dtypes={attr_name: ox.io._convert_bool_string},
    )

    # verify everything in G is equivalent in G2
    assert tuple(G.graph.keys()) == tuple(G2.graph.keys())
    assert tuple(G.graph.values()) == tuple(G2.graph.values())
    z = zip(G.nodes(data=True), G2.nodes(data=True))
    for (n1, d1), (n2, d2) in z:
        assert n1 == n2
        assert tuple(d1.keys()) == tuple(d2.keys())
        assert tuple(d1.values()) == tuple(d2.values())
    z = zip(G.edges(keys=True, data=True), G2.edges(keys=True, data=True))
    for (u1, v1, k1, d1), (u2, v2, k2, d2) in z:
        assert u1 == u2
        assert v1 == v2
        assert k1 == k2
        assert tuple(d1.keys()) == tuple(d2.keys())
        assert tuple(d1.values()) == tuple(d2.values())

    # test custom data types
    nd = {"osmid": str}
    ed = {"length": str, "osmid": float}
    G2 = ox.load_graphml(filepath, node_dtypes=nd, edge_dtypes=ed)
Beispiel #11
0
def load_graph():
    graph = ox.load_graphml(file_name)
    return graph
Beispiel #12
0
import osmnx as ox
import geopandas as gpd
import numpy as np
import networkx as nx
import pandas as pd

fp = 'Sitapura.graphml'
graph = ox.load_graphml(fp)

# train_file_location = 'time_loc_data.csv'
# df = pd.read_csv(train_file_location)
# tim_loc_data = np.array(df)
# service_pos_arr = tim_loc_data[:,1:3]

# path_file_location = 'best_sol.csv'
# dfp = pd.read_csv(path_file_location)                                 # Reading the CSV file
# path_data = np.array(dfp)

# main_lat = 26.7858865
# main_long = 75.8430487
# main_node = ox.get_nearest_node(graph,(main_lat,main_long),method='haversine')
# print(main_node)

# pathIds = path_data[4] - 1
# print(pathIds)
# if np.any(pathIds == -1):
# 	pathIds = pathIds[:-1]

# ids = ox.get_nearest_nodes(graph,service_pos_arr[pathIds,1],service_pos_arr[pathIds,0],method='balltree')
# ids = np.append(ids,main_node)
# ids = np.insert(ids,0,main_node)
def cost_assignment(file_graphml, place_country):
    # these numbers are the speeds on different type of road
    grafo = ox.load_graphml(file_graphml)
    way_dict = {
        "residential": [30, 50, 10],
        "secondary": [40, 90, 30],
        "primary": [50, 70, 20],
        "tertiary": [35, 70, 10],
        "unclassified": [40, 60, 10],
        "secondary_link": [40, 55, 30],
        "trunk": [70, 90, 40],
        "tertiary_link": [35, 50, 30],
        "primary_link": [50, 90, 40],
        "motorway_link": [80, 100, 30],
        "trunk_link": [42, 70, 40],
        "motorway": [110, 130, 40],
        "living_street": [20, 50, 30],
        "road": [30, 30, 30],
        "other": [30, 30, 30]
    }
    # weight/cost assignment
    # u and v are the start and ending point of each edge (== arco).
    for u, v, key, attr in grafo.edges(keys=True, data=True):
        # print(attr["highway"])
        # select first way type from list
        if type(attr["highway"]) is list:
            # verify if the attribute field is a list (it might happen)
            attr["highway"] = str(attr["highway"][0])  # first element of the list
            print(attr["highway"], '=================')
        # verify if the attribute exists, the way type in the dictionary
        if attr["highway"] not in way_dict.keys():
            speedlist = way_dict.get("other")
            speed = speedlist[0] * 1000 / 3600
            # create a new attribute time == "cost" in the field "highway"
            attr['cost'] = float(attr.get("length") / speed)
          #  print(attr.get("highway"), speedlist[0], attr.get("cost"), '^^^^^^^^^^^')
            # add the "attr_dict" to the edge file
            grafo.add_edge(u, v, key, attr_dict=attr)
            continue

        if 'maxspeed' in set(attr.keys()) and len(attr.get("maxspeed")) < 4:
            if type(attr.get("maxspeed")) is list:
                speedList = [int(i) for i in attr.get("maxspeed")]
                speed = np.mean(speedList) * 1000 / 3600
                attr['cost'] = float(attr.get("length") / speed)
            #    print(attr.get("highway"), attr.get("maxspeed"), attr.get("cost"), '========')
            else:
                speed = float(attr.get("maxspeed")) * 1000 / 3600
                attr['cost'] = float(attr.get("length") / speed)
             #   print(attr.get("highway"), attr.get("maxspeed"), attr.get("cost"), '°°°°°°°°°')
            grafo.add_edge(u, v, key, attr_dict=attr)
        else:  # read speed from way class dictionary
            speedlist = way_dict.get(attr["highway"])
            speed = speedlist[0] * 1000 / 3600
            attr['cost'] = float(attr.get("length") / speed)
          #  print(attr.get("highway"), speedlist[0], attr.get("cost"), '-----------')
            grafo.add_edge(u, v, key, attr_dict=attr)
    # save shp file AGAIN street network as ESRI shapefile (includes NODES and EDGES and new attributes)
    name_place_country = re.sub('[/," ",:]', '_', place_country)
    #### !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ##################################
    ### !!! # when I save, the field "cost" becomes a string...wrong!
    ox.save_graphml(grafo, filename=name_place_country + "_cost" + '.graphml')  # when I save, the field "cost" becomes a string...wrong!
def roads_type_folium(file_graphml, road_type, place_country):
    # load grapho
    grafo = ox.load_graphml(file_graphml)
    # adding a new column of edge color to gdf of the graph edges
    gdf_edges = ox.graph_to_gdfs(grafo, nodes=False, fill_edge_geometry=True)
    gdf_nodes = ox.graph_to_gdfs(grafo, edges=False)
    road_type = road_type.replace(' ', '')
    # road = gdf_edges[(gdf_edges.highway.isin( list(road_type.split (",")) ))]

    # make a dictionary for ech color
    road_color_dict = {
        "secondary": "red",
        "primary": "green",
        "tertiary": "blue",
        "motorway_link": "yellow",
        "motorway": "black",
        "residential": "orange",
        "unclassified": "brown",
        "living_street": "orange",
        "trunk": "black",
        "trunk_link": "black"
    }

    # 'living_street',
    # ['tertiary', 'unclassified'],
    # ['tertiary', 'residential', 'unclassified'],
    # ['secondary', 'unclassified']
    # ['secondary', 'tertiary']
    # ['tertiary', 'residential']
    # ['secondary', 'residential']
    # ['residential', 'unclassified']
    # "trunk


    points = []
    # prepare a base_map ###########################################################
    gen_network = gdf_edges[(gdf_edges.highway.isin([road_type.split(",")[0]]))]
    # gen_network = gdf_edges[(gdf_edges.highway.isin(["secondary"]))]
    for i in range(len(gen_network)):
        gen_poly = ox.make_folium_polyline(edge=gen_network.iloc[i], edge_color="black", edge_width=1,
                                           edge_opacity=1, popup_attribute=None)
        points.append(gen_poly.locations)
        gen_poly_unlisted = list(chain.from_iterable(points))
        ave_lat = sum(p[0] for p in gen_poly_unlisted) / len(gen_poly_unlisted)
        ave_lon = sum(p[1] for p in gen_poly_unlisted) / len(gen_poly_unlisted)
    my_map = folium.Map(location=[ave_lat, ave_lon], zoom_start=12, tiles='cartodbpositron') # tiles='cartodbpositron'
    # my_map.save("Catania_motorway.html")
    ##################################################################################

    # add nodes to my_map
    for i in range(len(gdf_nodes)):
        folium.CircleMarker(location=[gdf_nodes.y.iloc[i], gdf_nodes.x.iloc[i]],
                            popup=gdf_nodes.osmid.iloc[i],
                            radius=5,
                            color="red",
                            fill=True,
                            fill_color="yellow",
                            fill_opacity=0.6).add_to(my_map)

    # add edges
    road_type = list(road_type.split(","))
    for road in road_type:
        if road in road_color_dict.keys():
            color_road = road_color_dict.get(road)
        motorway = gdf_edges[(gdf_edges.highway.isin([road]))]
        points = []
        for i in range(len(motorway)):
            motorway_poly = ox.make_folium_polyline(edge=motorway.iloc[i], edge_color="black", edge_width=1,
                                            edge_opacity=1, popup_attribute=None)
            points.append(motorway_poly.locations)
        folium.PolyLine(points, color=color_road, weight=3, opacity=1).add_to(my_map)
        name_place_country = re.sub('[/," ",:]', '_', place_country)
        roadtype = ' '.join([str(elem) for elem in road_type])
        roads = re.sub('[/," ",:]', '_', roadtype)
        my_map.save(name_place_country + "_" + roads + ".html")
    return my_map
Beispiel #15
0
def load_graph_from_disk(src_filepath):
    """ Method to load and return a graph (in graphml) to memory.
    """
    graph = ox.load_graphml(src_filepath)
    return graph
Beispiel #16
0
# python=3.6 requires using Qt4Agg backend for animation saving
import matplotlib
# matplotlib.use('Qt4Agg')
import models
import navigation as nav
from networkx import NetworkXNoPath
import numpy as np
import osmnx as ox
import pandas as pd

"""Piedmont, California"""
G = ox.load_graphml('piedmont.graphml')
G = ox.project_graph(G)
fig, ax = ox.plot_graph(G, node_size=0, edge_linewidth=0.5)

# grab the dimensions of the figure
axis = ax.axis()


def init_custom_agent(n=1, fig_axis=axis, car_id=None, alternate_route=None):
    """
    This function initializes a singular car with custom origin and destination

    :param               n:          int
    :param        fig_axis:         list
    :param          car_id:  None or int
    :param alternate_route: None or list
    :return     cars_frame:    DataFrame
    """

    origin = 53085387
Beispiel #17
0
from collections import OrderedDict as odict
import fiona


def flatten_dict(d, parent_key='', sep='_'):
    items = []
    for k, v in d.items():
        new_key = parent_key + sep + k if parent_key else k
        if isinstance(v, collections.MutableMapping):
            items.extend(flatten(v, new_key, sep=sep).items())
        else:
            items.append((new_key, v))
    return dict(items)


graph = ox.load_graphml(filename="network_de.graphml",
                        folder=baseDir + "gis/graph/")
# graph = graph.to_undirected()
keys = {}
for edge in (graph.edges(data=True)):
    attrs = {}
    if edge[2]['highway'] == 'motorway':
        attrs['weight'] = edge[2]['length'] * 1
    elif edge[2]['highway'] == 'primary':
        attrs['weight'] = edge[2]['length'] * 1.5
    elif edge[2]['highway'] == 'secondary':
        attrs['weight'] = edge[2]['length'] * 1.8
    else:
        attrs['weight'] = edge[2]['length'] * 3
    keyy = (edge[0], edge[1], 0)
    keys[keyy] = attrs
Beispiel #18
0
# In[3]:

# projecting the graph
G_projected = ox.project_graph(G)
ox.plot_graph(G_projected)

# In[4]:

#Save the graph
ox.save_graph_shapefile(G_projected, filename='chandigarh')

# In[5]:

#loading the graph back
G = ox.load_graphml('chandigarh.graphml')
fig, ax = ox.plot_graph(G)
ax.set_title('Chandigarh Map')

# In[7]:

# get the nearest network node to each point
orig_node = ox.get_nearest_node(G, (30.72331046, 76.7449385))
dest_node = ox.get_nearest_node(G, (30.70527403, 76.73457478))

# In[8]:

# find the route between these nodes using dijkstra then plot it
route = nx.shortest_path(G, orig_node, dest_node, weight='length')
fig, ax = ox.plot_graph_route(G, route, node_size=0)
Beispiel #19
0
def LoadSavedGraph(filename):
    return ox.load_graphml(filename=filename, folder=GetNetFolder())
import matplotlib.cm as cm
from matplotlib import pyplot as plt


#finds shortest path between two nodes in an OSMNX graph1
def find_shortest_path(node1, node2, graph):
    return nx.shortest_path(graph, source=node1, target=node2)


if __name__ == "__main__":

    filename = 'grenoble_2500.hraphml'
    path_gpx = 'data/gpx/'
    path_maps = 'data/maps/'

    graph = osmnx.load_graphml(filename=filename, folder=path_maps)

    nodes = list(graph.nodes())

    point1 = (45.20926888100802898406982421875,
              5.76389609836041927337646484375)
    point2 = (45.19823712296783924102783203125,
              5.78004048205912113189697265625)

    node1 = osmnx.get_nearest_node(graph, point1)
    node2 = osmnx.get_nearest_node(graph, point2)

    route = find_shortest_path(node1, node2, graph)
    osmnx.plot.plot_graph_route(graph, route)
    # cmap = plt.cm.plasma
    # route_list = []
Beispiel #21
0
#Routing Distances and Direct distances.
##[Struct.Analysis]=group
##place_name=string gill ludhiana
##filename= string graphml_data
##location=folder

import osmnx as ox, geopandas as gpd, pandas as pd
#matplotlib inline
#ox.config(log_file=True, log_console=True, use_cache=True)
#For converting unicode string of chracters to string
cname = place_name.encode('utf-8')
filename = filename.encode('utf-8')
location = location.encode('utf-8')

# create the street network within the city borders
G = ox.load_graphml(filename, folder=location)
# you can project the network to UTM (zone calculated automatically)
G = ox.project_graph(G)
fig, ax = ox.plot_graph(G)

# get the street network for a place, and its area in square meters
gdf = ox.gdf_from_place(cname)
gdf = ox.project_gdf(gdf)
fig, ax = ox.plot_shape(gdf)
area = ox.project_gdf(gdf).unary_union.area
# calculate basic and extended network stats, merge them together, and display
stats = ox.basic_stats(G, area=area)
df = pd.DataFrame.from_dict(stats, orient="index")
df.to_csv("/home/osmjit/Desktop/testdata/data.csv")

ex_stats = ox.extended_stats(G,
Beispiel #22
0
# 空驶时间阈值 阈值越大连接数越多,车辆逐渐减少,空驶率逐渐增大
# 此处需考虑司机在原地等待的时间
theta = 15
# 预知时长 时长逐渐增长,连接数逐渐增多多,车辆数逐渐减少, 愈加难以等待或者预测
pred_time = 15
# 延误时间阈值
delay = 10

data = pd.read_csv('myCode/data/day_data/sample_data_2.csv')
data['pickup_datetime'] = pd.to_datetime(data['pickup_datetime'])
data['dropoff_datetime'] = pd.to_datetime(data['dropoff_datetime'])
data['pred_time'] = data['pickup_datetime'] + pd.Timedelta(minutes=pred_time)

# 构建图网络
G = ox.load_graphml('myCode/data/Manhattan.graphml')
G_ig = ig.Graph(directed=True)
G_ig.add_vertices(list(G.nodes()))
G_ig.add_edges(list(G.edges()))
travel_time = pd.read_csv('myCode/multi/travel_time.csv')
G_ig.es['travel_time'] = travel_time['travel_time']


def get_timedelta(x):
    try:
        x = round(x)
    except:
        x = 7200
    return pd.Timedelta(seconds=x)

Beispiel #23
0
    foo = BNode()
    bar = BNode()
    problem.add((pddle['edge-computing-problem'], pddl.goal, foo))
    problem.add((foo, RDF.type, pddl.And))
    problem.add((foo, pddl.argument, bar))
    problem.add((bar, RDF.type, pddle.on))
    problem.add((bar, pddle['on-x'], pddle.v))
    problem.add((bar, pddle['on-y'], pddle.dest))

    return problem


def Generator(m, s, e):
    domain = Graph()
    domain.parse('../rdf_generator/domain.ttl', format='turtle')

    problem = problem_generator(m, s, e,
                                '../rdf_generator/problem _template.ttl')
    return domain + problem


if __name__ == '__main__':
    full_map = nx.load_graphml('./demo.graphml')
    problem = problem_generator(full_map, (169822, 1755176087),
                                (1393926005, 172605), 'problem _template.ttl')

    with open('test_out.ttl', 'w') as wfile:
        wfile.write(domain.serialize(format="turtle").decode('utf-8'))
        wfile.write(problem.serialize(format="turtle").decode('utf-8'))
Beispiel #24
0
 def setUp(self): 
     #load graph of nahant, smallest town in MA 
     #generate_model.populate_graph("nahant massachusetts")
     self.graph = ox.load_graphml("nahant-elevation-graph")
     #interconnectivity test only works with undirected graph
     self.undirected = self.graph.to_undirected()
Beispiel #25
0
def initialize_environment(min_request_time: int, max_request_time: int, max_wait_times: List[int],
                           detour_ratios: List[float], vehicle_number: int, vehicle_speed: float) \
        -> Tuple[MultiDiGraph, np.ndarray, np.ndarray, np.ndarray, Dict[int, Set[Order]], List[Vehicle]]:
    """
    初始化环境
    :param max_wait_times: 订单最大等待时间集合
    :param detour_ratios: 订单最大绕路比集合
    :param min_request_time: 提取订单的最小请求时间
    :param max_request_time: 提取订单的最大请求时间
    :param vehicle_number: 生成的车辆数目
    :param vehicle_speed: 车辆的速度
    :return graph: 路网图
    :return shortest_distance:最短路径长度矩阵
    :return shortest_path: 最短路径矩阵
    :return shortest_path_with_minute: 1分钟以内的最短路径矩阵
    :return orders: 各个时刻的订单信息
    :return vehicles: 初始化的车辆列表
    """
    print("read data from disc")
    trip_order_data = pd.read_csv("./order_data/trip_order_data.csv")
    car_fuel_consumption_info = pd.read_csv("car_fuel_consumption_data/car_fuel_consumption_info.csv")
    graph = ox.load_graphml("Manhattan.graphml", folder="./network_data/")
    shortest_distance = np.load("./network_data/shortest_distance.npy")
    shortest_path = np.load("./network_data/shortest_path.npy")
    shortest_path_with_minute = np.load("./network_data/shortest_path_with_minute.npy")

    print("build osm_id to index map")
    osm_id2index = {osm_id: index for index, osm_id in enumerate(graph.nodes)}
    location_map = {osm_id2index[node[0]]: (node[1]['x'], node[1]['y']) for node in graph.nodes(data=True)}
    index2osm_id = {index: osm_id for osm_id, index in osm_id2index.items()}
    GeoLocation.set_location_map(location_map)
    GeoLocation.set_index2osm_id(index2osm_id)

    print("generate order data")
    trip_order_data = trip_order_data[min_request_time <= trip_order_data["time"]]
    trip_order_data = trip_order_data[max_request_time > trip_order_data["time"]]

    order_number = trip_order_data.shape[0]
    pick_up_index_series = trip_order_data["pick_up_index"].values
    drop_off_index_series = trip_order_data["drop_off_index"].values
    request_time_series = trip_order_data["time"].values
    receive_fare_series = (trip_order_data["total_amount"] - trip_order_data["tip_amount"]).values  # 我们不考虑订单的中tip成分
    n_riders_series = trip_order_data["passenger_count"].values

    orders = {}
    for request_time in range(min_request_time, max_request_time):
        orders[request_time] = set()

    for i in range(order_number):
        order_id = i
        start_location = OrderLocation(int(pick_up_index_series[i]), OrderLocation.PICK_UP_TYPE)
        end_location = OrderLocation(int(drop_off_index_series[i]), OrderLocation.DROP_OFF_TYPE)
        request_time = int(request_time_series[i])
        max_wait_time = np.random.choice(max_wait_times)
        order_distance = shortest_distance[start_location.osm_index, end_location.osm_index]
        if order_distance == 0.0:
            continue
        receive_fare = receive_fare_series[i]
        detour_ratio = np.random.choice(detour_ratios)
        n_riders = int(n_riders_series[i])
        order = Order(order_id, start_location, end_location, request_time, max_wait_time,
                      order_distance, receive_fare, detour_ratio, n_riders)
        orders[request_time].add(order)

    print("generate vehicle data")
    Vehicle.set_average_speed(vehicle_speed)
    car_osm_ids = np.random.choice(graph.nodes, size=vehicle_number)
    cars_info = car_fuel_consumption_info.sample(n=vehicle_number)
    vehicles = []
    for i in range(vehicle_number):
        vehicle_id = i
        location = GeoLocation(osm_id2index[int(car_osm_ids[i])])
        car_info = cars_info.iloc[i, :]
        available_seats = int(car_info["seats"])
        cost_per_distance = float(car_info["fuel_consumption"]) / 6.8 * 2.5 / 1.609344
        vehicle = Vehicle(vehicle_id, location, available_seats, cost_per_distance, Vehicle.WITHOUT_MISSION_STATUS)
        vehicles.append(vehicle)

    print("finish generate data")
    return graph, shortest_distance, shortest_path, shortest_path_with_minute, orders, vehicles
                output.write('{}\n'.format(str(trj_data)))
        print(i)


# def plot_route(map_con, matcher, filename="my_plot.png", use_osm=True):
#     mmviz.plot_map(map_con, matcher=matcher[1],
#                 use_osm=use_osm, zoom_path=True,
#                 show_labels=False, show_matching=True, show_graph=False,
#                 filename=filename)

if __name__ == "__main__":
    map_con = InMemMap("osmmap",
                       use_latlon=True,
                       use_rtree=True,
                       index_edges=True)
    graph = ox.load_graphml("mynetwork.graphml")
    graph_proj = ox.project_graph(graph)

    # Create GeoDataFrames
    # Approach 2
    print("start1")
    nodes, edges = ox.graph_to_gdfs(graph_proj, nodes=True, edges=True)

    nodes_proj = nodes.to_crs("EPSG:3414")
    edges_proj = edges.to_crs("EPSG:3414")

    for nid, row in nodes_proj.iterrows():
        map_con.add_node(nid, (row['lat'], row['lon']))

    # adding edges using networkx graph
    for nid1, nid2, _ in graph.edges:
Beispiel #27
0
Created on Wed Sep 19 14:53:30 2018

@author: Valenti
"""

import osmnx as ox
import networkx as nx
import numpy as np
import pandas as pd
import matplotlib.cm as cm
import projections as pj
#import matplotlib as plt
import matplotlib.pyplot as plt


G = ox.load_graphml('networkRM_epgs4326.graphml')
charger1=(41.819110,  12.437210)
charger2=(41.809120, 12.445940)
charger3=(41.826840, 12.454720)

ch1_n = ox.get_nearest_node(G, charger1)
ch2_n = ox.get_nearest_node(G, charger2)
ch3_n = ox.get_nearest_node(G, charger3)
print(ch1_n, ch2_n, ch3_n)
df = pd.read_csv('rob.csv', sep=';')
s = list();
for i in range(len(df)):
   s.append(None)
df['distanza_ch1_mt']=s
df['distanza_ch2_mt']=s
df['distanza_ch3_mt']=s
Beispiel #28
0
                if Time_id["id"][i] in neighbor[j]:
                    TP += 1
    return TP


def getFinalMartrix(test, data, threshold):
    p = np.where(data.values < threshold)
    array = np.zeros((p[0].size, 3))
    Matric = pd.DataFrame(array, columns=["id1", "id2", "time"])
    for i in range(p[0].size):
        Matric["id1"][i] = test[0][p[0][i]]
        Matric["id2"][i] = test[1][p[0][i]]
        Matric["time"][i] = p[1][i] - 1
    return Matric


G = ox.load_graphml('BeijingStreetMap')
test = pd.read_csv("16-28Data/20111128.csv", header=None)  #导入测试矩阵
data = pd.read_csv("Experiment/总得分.csv")  #导入概率矩阵
accident = getAccidentData("Test/事故数据21-28/n_11.28.csv")  #导入真实数据
Time_id = getAllNearestId(accident)  #找到事故发生点的点的ID
predict = getFinalMartrix(test, data, 0.025)  #通过概率函数获得预测矩阵
timeSegment = predict["time"]
neighbor = getAllTimeNeighbor(predict, G)  #找到每一条预测数据的所有的临近点
rightNumber = countPridectRight(neighbor, Time_id, timeSegment)  #预测准确的数据
print(rightNumber)
p = rightNumber / predict.shape[0]
r = rightNumber / accident.shape[0]
print(p)
print(r)
import numpy as np
import osmnx as ox
import pandas as pd

import bmm

np.random.seed(0)

timestamps = 15
n_iter = 200
n_particles = 100

sim_dir = os.getcwd()
graph_path = sim_dir + '/testgraph_portotaxi_graph_portugal-140101.osm._simple.graphml'
graph = ox.load_graphml(graph_path)

train_data_path = sim_dir + '/training_data.csv'

# Load long-lat polylines
polylines_ll = [
    np.array(json.loads(poly))
    for poly in pd.read_csv(train_data_path)['POLYLINE']
]
# Convert to utm
polylines = [bmm.long_lat_to_utm(poly, graph) for poly in polylines_ll]

# Initiate model
mm_model = bmm.ExponentialMapMatchingModel()
mm_model.distance_params[
    'zero_dist_prob_neg_exponent'] = -np.log(0.15) / timestamps
Beispiel #30
0
def project_map():
    ox.config(log_console=True, use_cache=True)
    G = ox.load_graphml('network.graphml')
    G = ox.project_graph(G)
    ox.save_graphml(G, filename='project.graphml')
    return 'Map was successfully projected and saved in project.graphml'
        if i > 0:
            edge = G.get_edge_data(route[i], route[i-1])
            try:
                # Meters to Miles conversion
                edge_length = edge[0]['length'] * 0.00062137   
            except:
                edge_length = 0
            total_length = total_length + edge_length
        i = i + 1
    return(total_length)
    
print('Getting road network')

if os.path.isfile(root+'data/'+filename):
    # Read in the file
    G = ox.load_graphml(root+'data/'+filename)
else:
    # Create file since it doesn't exist
    ox.config(use_cache=True, log_console=False)
    G = ox.graph_from_place('Dutchess County, New York, USA', network_type='drive')
    ox.save_graphml(G, filename=filename)
    
stations = pd.read_excel(root+'Drive Distance Data.xlsx', 'Stations')
events = pd.read_excel(root+'Drive Distance Data.xlsx', 'Events')
length_data = list()

for index, row in stations.iterrows():
    station_name = row['Station']
    station = (row['Lat'], row['Lon'])
    station_node= ox.get_nearest_node(G, station)
    for index, e in events.iterrows():
Beispiel #32
0
def draw_contour(xl, xr, yl, yr, h = 0.1):
	x = np.arange(xl, xr, h)
	y = np.arange(yl, yr, h)
	x, y  = np.meshgrid(x, y)
	z = c(x, y)
	plt.figure()
	cp = plt.contourf(x, y, z)
	plt.colorbar(cp)
	plt.contour(x, y, z)
	plt.savefig('images/Concentration contour.png')
	plt.axes().set_aspect('equal', 'datalim')
	plt.show()

draw_contour(205,235,205,235,0.3)
"""
G = ox.load_graphml('icts1500.graphml')
for u in G.edges():
    print(G[u[0]][u[1]])


@timer
def detect_bld_network_from_point(lat=13.14633,
                                  lon=77.514386,
                                  distance=2000,
                                  filename='icts2000'):
    G = ox.buildings.buildings_from_point((lat, lon), distance=distance)
    for n in G.geometry:
        print(n)
    return G