Exemple #1
0
def wkt_to_G(wkt_list,
             weight_list=[],
             im_file=None,
             prop_subgraph_filter_weight='length_pix',
             min_subgraph_length=10,
             node_iter=0,
             edge_iter=0,
             simplify_graph=True,
             verbose=False):
    '''Execute all functions'''

    t0 = time.time()
    print("Running wkt_list_to_nodes_edges()...")
    node_loc_dic, edge_dic = wkt_list_to_nodes_edges(wkt_list,
                                                     weight_list=weight_list,
                                                     node_iter=node_iter,
                                                     edge_iter=edge_iter)
    t1 = time.time()
    print("Time to run wkt_list_to_nodes_egdes():", t1 - t0, "seconds")

    #print ("node_loc_dic:", node_loc_dic)
    #print ("edge_dic:", edge_dic)

    print("Creating G...")
    G0 = nodes_edges_to_G(node_loc_dic, edge_dic)
    print("  len(G.nodes():", len(G0.nodes()))
    print("  len(G.edges():", len(G0.edges()))
    # for edge_tmp in G0.edges():
    #    print ("\n 0 wtk_to_G():", edge_tmp, G0.edge[edge_tmp[0]][edge_tmp[1]])

    t2 = time.time()
    print("Time to run nodes_edges_to_G():", t2 - t1, "seconds")

    print("Clean out short subgraphs")
    G0 = apls._clean_sub_graphs(G0,
                                min_length=min_subgraph_length,
                                weight=prop_subgraph_filter_weight,
                                max_nodes_to_skip=30,
                                verbose=True,
                                super_verbose=False)
    t3 = time.time()
    print("Time to run clean_sub_graphs():", t3 - t2, "seconds")

    #    print ("Simplifying graph")
    #    G0 = osmnx_funcs.simplify_graph(G0.to_directed())
    #    G0 = G0.to_undirected()
    #    #G0 = osmnx_funcs.project_graph(G0)
    #    #G_p_init = create_edge_linestrings(G_p_init, remove_redundant=True, verbose=False)
    #    t3 = time.time()
    #    print ("  len(G.nodes():", len(G0.nodes()))
    #    print ("  len(G.edges():", len(G0.edges()))
    #    print ("Time to run simplify graph:", t30 - t3, "seconds")

    # for edge_tmp in G0.edges():
    #    print ("\n 1 wtk_to_G():", edge_tmp, G0.edge[edge_tmp[0]][edge_tmp[1]])

    #edge_tmp = G0.edges()[5]
    #print (edge_tmp, "G0.edge props:", G0.edge[edge_tmp[0]][edge_tmp[1]])

    # geo coords
    if im_file:
        print("Running get_node_geo_coords()...")
        G1 = get_node_geo_coords(G0, im_file, verbose=verbose)
        t4 = time.time()
        print("Time to run get_node_geo_coords():", t4 - t3, "seconds")

        print("Running get_edge_geo_coords()...")
        G1 = get_edge_geo_coords(G1, im_file, verbose=verbose)
        t5 = time.time()
        print("Time to run get_edge_geo_coords():", t5 - t4, "seconds")

        print("projecting graph...")
        G_projected = osmnx_funcs.project_graph(G1)
        t6 = time.time()
        print("Time to project graph:", t6 - t5, "seconds")

        # simplify
        #G_simp = osmnx_funcs.simplify_graph(G_projected.to_directed())
        # osmnx_funcs.plot_graph(G_projected)
        # G1.edge[19][22]

        Gout = G_projected  # G_simp

    else:
        Gout = G0

    if simplify_graph:
        print("Simplifying graph")
        t7 = time.time()
        G0 = osmnx_funcs.simplify_graph(Gout.to_directed())
        G0 = G0.to_undirected()
        Gout = osmnx_funcs.project_graph(G0)
        t8 = time.time()
        print("Time to run simplify graph:", t8 - t7, "seconds")
        # When the simplify funciton combines edges, it concats multiple
        #  edge properties into a list.  This means that 'geometry_pix' is now
        #  a list of geoms.  Convert this to a linestring with
        #   shaply.ops.linemergeconcats
        print("Merge 'geometry' linestrings...")
        keys_tmp = ['geometry_pix', 'geometry_latlon_wkt', 'geometry_utm_wkt']
        for key_tmp in keys_tmp:
            print("Merge", key_tmp, "...")
            for i, (u, v, attr_dict) in enumerate(Gout.edges(data=True)):
                if (i % 10000) == 0:
                    print(i, u, v)
                geom_pix = attr_dict[key_tmp]
                # print (i, u, v, "geom_pix:", geom_pix)
                # print ("  type(geom_pix):", type(geom_pix))

                if type(geom_pix) == list:
                    # check if the list items are wkt strings, if so, create
                    #   linestrigs
                    # or (type(geom_pix[0]) == unicode):
                    if (type(geom_pix[0]) == str):
                        geom_pix = [
                            shapely.wkt.loads(ztmp) for ztmp in geom_pix
                        ]
                    # merge geoms
                    attr_dict[key_tmp] = shapely.ops.linemerge(geom_pix)

        # assign 'geometry' tag to geometry_utm_wkt
        for i, (u, v, attr_dict) in enumerate(Gout.edges(data=True)):
            if verbose:
                print("Create 'geometry' field in edges...")
            # geom_pix = attr_dict[key_tmp]
            line = attr_dict['geometry_utm_wkt']
            if type(line) == str:  # or type(line) == unicode:
                attr_dict['geometry'] = shapely.wkt.loads(line)
            else:
                attr_dict['geometry'] = attr_dict['geometry_utm_wkt']
            # attr_dict['geometry'] = attr_dict['geometry_utm_wkt']

        Gout = osmnx_funcs.project_graph(Gout)

    # get a few stats (and set to graph properties)
    print("Number of nodes:", len(Gout.nodes()))
    print("Number of edges:", len(Gout.edges()))
    Gout.graph['N_nodes'] = len(Gout.nodes())
    Gout.graph['N_edges'] = len(Gout.edges())

    # get total length of edges
    tot_meters = 0
    for i, (u, v, attr_dict) in enumerate(Gout.edges(data=True)):
        tot_meters += attr_dict['length']
    print("Length of edges (km):", tot_meters / 1000)
    Gout.graph['Tot_edge_km'] = tot_meters / 1000

    print("G.graph:", Gout.graph)

    t7 = time.time()
    print("Total time to run wkt_to_G():", t7 - t0, "seconds")

    # for edge_tmp in Gout.edges():
    #   print ("\n 2 wtk_to_G():", edge_tmp, Gout.edge[edge_tmp[0]][edge_tmp[1]])

    return Gout
Exemple #2
0
    # ground truth graph
    G_gt_init = nx.read_gpickle(gt_file)
    # G_gt_init1 = osmnx_funcs.simplify_graph(G_gt_init0.to_directed()).to_undirected()
    # G_gt_init = osmnx_funcs.project_graph(G_gt_init1)
    G_gt_init = apls.create_edge_linestrings(
        G_gt_init, remove_redundant=True, verbose=False)

    print(("G_gt_init.nodes():", G_gt_init.nodes()))
    # define ground truth kdtree
    # kd_idx_dic, kdtree, pos_arr = G_to_kdtree(G_gt_init)

    # proposal graph
    # G_p_ = nx.read_gpickle(prop_file)
    G_p_init0 = nx.read_gpickle(prop_file)
    G_p_init1 = osmnx_funcs.simplify_graph(G_p_init0.to_directed()).to_undirected()
    G_p_init = osmnx_funcs.project_graph(G_p_init1)
    G_p_init = apls.create_edge_linestrings(
        G_p_init, remove_redundant=True, verbose=False)
    # kd_idx_dic_p, kdtree_p, pos_arr_p = G_to_kdtree(G_p_init)

    tp_tot, fp_tot, fn_tot, precision, recall, f1 = \
        compute_topo(G_gt_init, G_p_init, subgraph_radius=subgraph_radius,
                     interval=interval, hole_size=hole_size,
                     n_measurement_nodes=n_measurement_nodes,
                     x_coord=x_coord, y_coord=y_coord,
                     allow_multi_hole=allow_multi_hole,
                     make_plots=make_plots, verbose=verbose)

    ############
    # also compute total topo metric for entire folder
Exemple #3
0
def gt_geojson_to_wkt(geojson_path,
                      im_path,
                      weight_keys=['length', 'travel_time_s'],
                      subgraph_filter_weight='length',
                      min_subgraph_length=5,
                      travel_time_key='travel_time_s',
                      speed_key='inferred_speed_mps',
                      use_pix_coords=False,
                      verbose=False,
                      simplify=False,
                      refine=True,
                      super_verbose=False):
    '''
    Create wkt list of pixel coords in ground truth graph, for use in SpaceNet
    TopCoder competition.
    im_path has name like: ...SN5_roads_train_AOI_7_Moscow_PS-RGB_chip996.tif
    if weight == [], output list of [name_root, geom_pix_wkt]
    else:  output list of [name_root, geom_pix_wkt, weight1, weight2]
    '''

    # linestring = "LINESTRING {}"
    im_name = os.path.basename(im_path)

    # get name_root of image file
    name_root = im_name.split('.')[0].replace('PS-RGB_',
                                              '').replace('PS-MS_', '')
    # # v0
    # AOI_root = 'AOI' + im_name.split('AOI')[-1]
    # name_root = AOI_root.split('.')[0].replace('PS-RGB_', '').replace('PS-MS_', '')
    print("name_root:", name_root)
    print("im_path:", im_path)

    G_gt, _ = apls._create_gt_graph(
        geojson_path,
        im_path,
        subgraph_filter_weight=subgraph_filter_weight,
        min_subgraph_length=min_subgraph_length,
        travel_time_key=travel_time_key,
        speed_key=speed_key,
        use_pix_coords=use_pix_coords,
        refine_graph=refine,
        simplify_graph=simplify,
        verbose=verbose,
        super_verbose=super_verbose)

    # simplify and turn to undirected
    if simplify:
        try:
            G_gt = osmnx_funcs.simplify_graph(G_gt).to_undirected()
        except:
            G_gt = G_gt.to_undirected()
    else:
        G_gt = G_gt.to_undirected()

    # return  [name_root, "LINESTRING EMPTY"] if no edges
    if (len(G_gt.nodes()) == 0) or (len(G_gt.edges()) == 0):
        print("  Empty graph")
        if len(weight_keys) > 0:
            return [[name_root, "LINESTRING EMPTY"] + [0] * len(weight_keys)]
        else:
            return [[name_root, "LINESTRING EMPTY"]]

    # extract geometry pix wkt, save to list
    wkt_list = []
    for i, (u, v, attr_dict) in enumerate(G_gt.edges(data=True)):
        print("attr_dict:", attr_dict)
        geom_pix_wkt = attr_dict['geometry_pix'].wkt
        if verbose:
            print(i, "/", len(G_gt.edges()), "u, v:", u, v)
            # print("  attr_dict:", attr_dict)
            print("  geom_pix_wkt:", geom_pix_wkt)

        wkt_item_root = [name_root, geom_pix_wkt]
        if len(weight_keys) > 0:
            weights = [attr_dict[w] for w in weight_keys]
            if verbose:
                print("  weights:", weights)
            wkt_list.append(wkt_item_root +
                            weights)  # [name_root, geom_pix_wkt, weight])
        else:
            wkt_list.append(wkt_item_root)

    if verbose:
        print("wkt_list:", wkt_list)

    return wkt_list