Exemple #1
0
def graph_scenario(stop_points, geotiff_name, ad_weights, file_name_osm):

    max_lat, min_lat, max_lon, min_lon = Coordinates.create_osmnx(stop_points)

    # Scenario graph (paths are edges and junctions are nodes)
    G = ox.graph_from_bbox(max_lat, min_lat, max_lon, min_lon, network_type='all')
    G, nodes_coordinates, nodes_mass_increment = Graph.configure_graph(G, geotiff_name, stop_points, ad_weights, file_name_osm)
    return G, nodes_coordinates, nodes_mass_increment
def file_osm(directory, coordinates_points):
    """
    This function gets a list with tuples corresponding a geographic
    points. Each tuple has the coordinates (lat, lon) of the point.
    Then, it creates a bounding box which contain all coordinates
    inside. Besides, it checks if exists a Open Street Map '.osm' file
    in a directory. If it not exists, this function
    downloads the file.

    :param file_name:                       String

    :param coordinates_points:              list
                                            the list contains tuples.
                                            Each tuple has the latitude
                                            and the longitude corresponding
                                            to the geographic point.
                                            [(lat, lon)]
    :return:
    """

    coordinates_list = Coordinates.coordinates_list_bbox(
        coordinates_points, margin_value_coordinate=0.0)
    file_name = directory + Coordinates.def_file_name(
        coordinates_list) + '.osm'

    file = Saves.verify_file_exists(coordinates_list, file_name)
    if file is not False:
        print("OSM exists in ", file)
        return MAPS_DIRECTORY + file

    coordinates_list = Coordinates.coordinates_list_bbox(coordinates_points)
    coordinates_osm = Coordinates.coordinates_string(coordinates_list)
    file_name = directory + Coordinates.def_file_name(
        coordinates_list) + '.osm'

    # it just download a new osm file if does not exists a file with
    # the current coordinates
    _osm(coordinates_osm, file_name)
    return file_name
def geotiff(directory, coordinates_stop_points):
    """
    This function downloads the GeoTiff file in
    the Topodata database, according to the input
    coordinates. In other words, it checks which
    geotiff files contain a bounding box, defined
    by the input coordinates.

    The function also verifies if the GeoTiff file
    exists in a directory before download it. If it
    not exists, the function downloads the GeoTiff file.

    :param directory:                   String

    :param coordinates_stop_points:     list
                                        The list should
                                        have tuples with
                                        coordinates of the
                                        points.

    :return:                            String
                                        The name of the GeoTiff
                                        file

    Example of coordinates_stop_points:
    coordinates = [(-22.816008, -47.075614), (-22.816639, -47.074891),
                   (-22.818317, -47.083415), (-22.820244, -47.085422),
                   (-22.816008, -47.075614), (-22.823953, -47.087718)]

    """

    coordinates_osm = Coordinates.coordinates_list_bbox(
        coordinates_stop_points)
    file_names = Topodata.file_names_topodata(coordinates_osm)

    number_of_files = len(file_names)

    geo_directory_and_file_names = set_tif_names(directory, file_names)

    _geotiff(directory, file_names)

    if number_of_files > 1:
        mosaic_name = 'out.tif'
        join_geo(directory, geo_directory_and_file_names, mosaic_name)
        return mosaic_name
    else:
        tif_name = file_names[0]
        return tif_name
Exemple #4
0
def create_route(stop_points, material_weights, json_files, n = None, n_points=10):

    # the desired geotiff name to the mosaic
    mosaic_geotiff_name = 'out.tif'
    geotiff_name_out = MAPS_DIRECTORY + mosaic_geotiff_name

    # creates the geographic rectangular area based on stop points coordinates
    # downloads the Open Street Map (osm) file of the area
    osm_file = OpenSteetMap.file_osm(MAPS_DIRECTORY, stop_points)

    # downloads the GeoTiff file(s) of the area from the Topodata site
    # it returns the geotiff name of the Topodata database
    geotiff_name = GeoTiff.geotiff(MAPS_DIRECTORY, stop_points)
    geotiff_name = MAPS_DIRECTORY + geotiff_name + '.tif'

    # gdal translate of the geotiff file
    geotiff_transformation(geotiff_name, geotiff_name_out)

    # creates a rectangular area based on stop points coordinates
    max_lat, min_lat, max_lon, min_lon = Coordinates.create_osmnx(stop_points)

    # G_file is the name of the graph file (.graphml extension)
    G_file = Saves.def_file_name(MAPS_DIRECTORY, stop_points, '.graphml')

    # defines the json file name of the simulation results
    file_name_json = Saves.def_file_name(RESULTS_DIRECTORY, stop_points, '') + '_' + str(n) + '_' + str(n_points)
    json_files.append(file_name_json)

    # the coordinates of the area is used to verify if the graph exists
    coordinates_list = Coordinates.coordinates_list_bbox(stop_points)
    """
    # if the graph exists, it is not necessary to do all configurations on the graph
    if verify_graph_exists(G_file, stop_points, coordinates_list) is True:

        # creates the .net.xml file (SUMO simulator file)
        netconvert_geotiff(osm_file, geotiff_name_out, NET)

        # configure the elevation and edge weights
        # creates dictionaries with coordinates and mass increment data
        G, nodes_coordinates, nodes_mass_increment = nodes_data(G_file, stop_points, material_weights, osm_file, geotiff_name)

    else:
    """
    # delete collect points added before in the osm file
    Map_Simulation.delete_osm_items(osm_file)

    # if there are ways that can be bidirectional to 'carrinheiro'
    if BIDIRECTIONAL is True:
        Map_Simulation.edit_map(osm_file)

    # creates the osmnx graph based on the geographic area
    # Scenario graph (paths are edges and junctions are nodes)
    G = ox.graph_from_bbox(max_lat, min_lat, max_lon, min_lon, network_type='all')

    # configure the graph
    G, nodes_coordinates, nodes_mass_increment = Graph.configure_graph_simulation(G, geotiff_name, stop_points, material_weights, osm_file, G_file)

    ###########

    # the starting point is the first collect point of the vector
    index_source = list(nodes_coordinates.values()).index(stop_points[0])
    node_source = list(nodes_coordinates.keys())[index_source]

    # the arrival point is the last collect point of the vector
    index_target = list(nodes_coordinates.values()).index(stop_points[-1])
    node_target = list(nodes_coordinates.keys())[index_target]

    # creates the ordering graph
    # it is a complete graph with the number of nodes equivalent to number of collect points
    H = Graph_Collect.create_graph_route(nodes_coordinates, nodes_mass_increment)

    # dictionary with adjacent edges information
    dict_edges_net = Map_Simulation.edges_net(NET)

    # it is necessary configure the edges on simulator to allow the carrinheiro's type of vehicle
    Map_Simulation.allow_vehicle(NET)

    politics = ['weight', 'length']
    heuristics = ['SPFA', 'dijkstra', 'astar']
    """
    # Nearest
    heuristic_combination(nn, G, H, node_source, node_target, politics[0], heuristics[0], stop_points, dict_edges_net, nodes_mass_increment, file_name_json)
    heuristic_combination(nn, G, H, node_source, node_target, politics[0], heuristics[1], stop_points, dict_edges_net,
                          nodes_mass_increment, file_name_json)
    heuristic_combination(nn, G, H, node_source, node_target, politics[0], heuristics[2], stop_points, dict_edges_net,
                          nodes_mass_increment, file_name_json)
    heuristic_combination(nn, G, H, node_source, node_target, politics[1], heuristics[0], stop_points, dict_edges_net,
                          nodes_mass_increment, file_name_json)
    heuristic_combination(nn, G, H, node_source, node_target, politics[1], heuristics[1], stop_points, dict_edges_net,
                          nodes_mass_increment, file_name_json)
    heuristic_combination(nn, G, H, node_source, node_target, politics[1], heuristics[2], stop_points, dict_edges_net,
                          nodes_mass_increment, file_name_json)

    # Closest
    heuristic_combination(ci, G, H, node_source, node_target, politics[0], heuristics[0], stop_points, dict_edges_net,
                          nodes_mass_increment, file_name_json)
    heuristic_combination(ci, G, H, node_source, node_target, politics[0], heuristics[1], stop_points, dict_edges_net,
                          nodes_mass_increment, file_name_json)
    heuristic_combination(ci, G, H, node_source, node_target, politics[0], heuristics[2], stop_points, dict_edges_net,
                          nodes_mass_increment, file_name_json)
    heuristic_combination(ci, G, H, node_source, node_target, politics[1], heuristics[0], stop_points, dict_edges_net,
                          nodes_mass_increment, file_name_json)
    heuristic_combination(ci, G, H, node_source, node_target, politics[1], heuristics[1], stop_points, dict_edges_net,
                          nodes_mass_increment, file_name_json)
    heuristic_combination(ci, G, H, node_source, node_target, politics[1], heuristics[2], stop_points, dict_edges_net,
                          nodes_mass_increment, file_name_json)
    """
    # Further
    heuristic_combination(fi, G, H, node_source, node_target, politics[0], heuristics[0], stop_points, dict_edges_net,
                          nodes_mass_increment, file_name_json)
    heuristic_combination(fi, G, H, node_source, node_target, politics[0], heuristics[1], stop_points, dict_edges_net,
                          nodes_mass_increment, file_name_json)
    heuristic_combination(fi, G, H, node_source, node_target, politics[0], heuristics[2], stop_points, dict_edges_net,
                          nodes_mass_increment, file_name_json)
    heuristic_combination(fi, G, H, node_source, node_target, politics[1], heuristics[0], stop_points, dict_edges_net,
                          nodes_mass_increment, file_name_json)
    heuristic_combination(fi, G, H, node_source, node_target, politics[1], heuristics[1], stop_points, dict_edges_net,
                          nodes_mass_increment, file_name_json)
    heuristic_combination(fi, G, H, node_source, node_target, politics[1], heuristics[2], stop_points, dict_edges_net,
                          nodes_mass_increment, file_name_json)

    return json_files
    save_graph_file(G, G_file)

    return G, nodes_coordinates, nodes_weights


if __name__ == '__main__':
    coords = [(-22.816008, -47.075614), (-22.816639, -47.074891),
              (-22.818317, -47.083415), (-22.820244, -47.085422),
              (-22.823953, -47.087718), (-22.816008, -47.075614),
              (-22.816080, -47.075616), (-22.816000, -47.075697),
              (-22.816081, -47.075677), (-22.816014, -47.075633)]
    osm_file = OpenSteetMap.file_osm('../' + MAPS_DIRECTORY, coords)
    geotiff_name = GeoTiff.geotiff('../' + MAPS_DIRECTORY, coords)
    geotiff_name = '../' + MAPS_DIRECTORY + geotiff_name + '.tif'
    max_lat, min_lat, max_lon, min_lon = Coordinates.create_osmnx(coords)
    G = ox.graph_from_bbox(max_lat,
                           min_lat,
                           max_lon,
                           min_lon,
                           network_type='all')
    G = set_node_elevation(G, geotiff_name)
    G = edge_grades(G)
    G = surface(G, '../' + osm_file)
    if BIDIRECTIONAL is True:
        add_edges_G = [
            (v, u, data) for u, v, k, data in G.edges(keys=True, data=True)
            if G.has_edge(*(v, u)) is False and data['highway'] in TWO_WAY
        ]
        G.add_edges_from(add_edges_G)
    G = maxspeed(G)
Exemple #6
0
def def_file_name(directory, coordinates_points, extension):
    coordinates_list = Coordinates.coordinates_list_bbox(coordinates_points)
    file_name = directory + Coordinates.def_file_name(
        coordinates_list) + extension

    return file_name