Example #1
0
def export_problem_to_directory(exit_on_success=False):
    """Populate directory with a shapefile representation of the problem.

       Raises FileNotFoundError
    """
    export_dir = utility.CLI.args().export_dir
    problem = load_problem_file()

    if not os.path.isdir(export_dir):
        os.makedirs(export_dir)

    temp_graph = nx.DiGraph()
    temp_graph.add_node(
        (problem['depot'][0]['longitude'], problem['depot'][0]['latitude']))
    nx.write_shp(temp_graph, os.path.join(export_dir, 'depot.shp'))

    for node_type in ('customer', 'station'):
        temp_graph = nx.DiGraph()
        for node in problem[node_type]:
            temp_graph.add_node((node['longitude'], node['latitude']))
        nx.write_shp(temp_graph, os.path.join(export_dir, node_type + '.shp'))

    for ext in ('dbf', 'shp', 'shx'):
        os.remove(os.path.join(export_dir, 'edges.' + ext))

    Log.info(f'Exported correctly to \'{export_dir}\' '
             'depot.shp, customers.shp and stations.shp\n')
    if exit_on_success:
        raise SystemExit(0)
Example #2
0
def write_shp(g, out_file_path):
    """
    This writes the DiGraph to GIS .shp project files.

    Note 1: You will need to set the CRS manually in a GIS program;
            that is not in this code as the processing can be too costly. 
    Note 2: Will only create one level of non-existent directories, else an OSError.

    """
    #arr = self.splitpath(out_file_path)
    #tmpstr = arr[0] + os.path.splitext(arr[1])[0]
    #os.mkdir(tmpstr)
    (path, filename) = validate_file(out_file_path, mode='write')
    splitname = os.path.splitext(filename)

    #create a directory with that path to avoid overwriting edges.shp
    #when writing multiple networks out to the same directory
    try:
        os.mkdir(path + splitname[0])
        print "made directory"
    except OSError as exc:
        if exc.errno != errno.EEXIST:
            raise
        pass

    path_nofname = path + splitname[0]
    fullpath = path + splitname[0] + '/' + filename
    nx.write_shp(g, fullpath)

    # rename edge files for clarity
    for filename in os.listdir(path_nofname):
        if filename.startswith("edges"):
            os.rename(path_nofname + '/' + filename,
                      path_nofname + '/' + splitname[0] + '_' + filename)
def export_path_to_shp(path_dict, multy, multy_attribute, output_workspace, G):
    new_graph = nx.MultiGraph()
    a = 0
    for node in path_dict:
        path_list = path_dict[node]
        path_list.insert(0, node)
        b = 0
        for edge in G.edges(keys=True):
            c = 0
            for i in range(len(path_list) - 1):
                identifier = str(a) + str(b) + str(c)
                if tuple([tuple(path_list[i]),
                          tuple(path_list[i + 1])
                          ]) == tuple([edge[0], edge[1]]):
                    new_graph.add_edge(edge[0],
                                       edge[1],
                                       identifier,
                                       Name=edge[2],
                                       ident=identifier)
                elif tuple([tuple(path_list[i + 1]),
                            tuple(path_list[i])]) == tuple([edge[0], edge[1]]):
                    new_graph.add_edge(edge[0],
                                       edge[1],
                                       identifier,
                                       Name=edge[2],
                                       ident=identifier)
                c += 1
            b += 1
        a += 1
    if multy == 'true':
        nx_multi_shp.write_shp(new_graph, 'ident', output_workspace)
    else:
        nx.write_shp(new_graph, output_workspace)
Example #4
0
def export_graph_to_shp(G, multy, output_workspace, multy_attribute=None):
    """Export networkx graph object to shapefile

        Parameters
        ----------
        G: networkx graph object

        multy: str
            If value is true – multigraph will be created
            If value is false – simple graph will be created

        output_workspace: str
            path to the folder with output shapefile

        multy_attribute: str
            Field of the shapefile which allows to distinguish parallel edges.
            Note that it could be a field of different types, but all values of this attribute should be filled

        Returns
        -------
        None

    """
    for item in ['edges.shp', 'nodes,shp']:
        filename = os.path.join(output_workspace, item)
        if os.path.exists(filename):
            os.remove(filename)
    if multy == 'true':
        nx_multi_shp.write_shp(G, multy_attribute, output_workspace)
    else:
        nx.write_shp(G, output_workspace)
def edge_representation(row_from_label,
                        col_from_label,
                        row_to_label,
                        col_to_label,
                        distance_matrix,
                        node_label_list,
                        edge_list,
                        gt,
                        outDir,
                        epsg=3035):
    rm_mk_dir(outDir)
    G = nx.Graph()
    x0, y0, w, h = gt[0], gt[3], gt[1], gt[5]
    X0 = x0 + w / 2
    Y0 = y0 + h / 2
    for k in range(edge_list.shape[0]):
        s, t, edge_weight = edge_list[k, :]
        s, t = int(s), int(t)
        py0_ind, px0_ind = row_from_label[s, t], col_from_label[s, t]
        py1_ind, px1_ind = row_to_label[s, t], col_to_label[s, t]
        px0, py0 = X0 + 100 * px0_ind, Y0 - 100 * py0_ind
        px1, py1 = X0 + 100 * px1_ind, Y0 - 100 * py1_ind
        # G.add_edge((px0, py0), (px1, py1), weight=distance_matrix[s, t])
        G.add_edge((px0, py0), (px1, py1), weight=edge_weight)
    nx.write_shp(G, outDir)
    spatialRef = osr.SpatialReference()
    spatialRef.ImportFromEPSG(epsg)
    spatialRef.MorphToESRI()
    for item in ['nodes', 'edges']:
        prj_file = outDir + os.sep + item + '.prj'
        with open(prj_file, 'w') as file:
            file.write(spatialRef.ExportToWkt())
Example #6
0
def disegna_soluzione(percorsi, punti, indici, n_veicoli):
    # per ogni veicolo controllo che ci siano almeno tre elementi in indici altrimenti
    # vuol dire che il veicolo rimane sempre in stazione: ogni percorso inizia
    # con 0 id_clienti visitati 0 (parte e termina nella sorgente)
    # n_veicoli=1
    print n_veicoli
    for i in range(0, n_veicoli):
        if (len(indici[i]) == 2):
            continue
        filename = "./QGIS/output/v" + str(i)
        Gsol = nx.DiGraph()
        prec = 0
        visita = 0
        for j in range(1, len(indici[i])):
            # voglio creare il cammino da punti[precedente] a punti[indici[j]]
            # quindi:
            # - prec corrisponde all'indice del veicolo da cui sto partendo
            # (all'inizio la sorgente - 0)
            # - devo andare al prossimo nodo (che è un cliente) visitato dal mio
            #  veicolo corrente, all'inizio sarà il secondo elemento del mio array
            #  di indici (array di visita del veicolo) recupero il nome completo
            #  da punti
            # - creo la chiave
            key = tuple([punti[prec], punti[indici[i][j]]])
            # devo aggiungere un arco per ogni elenento della lista percorsi con chiave key
            prev_point = percorsi[key][0]
            for k in percorsi[key]:
                if (k == percorsi[key][0]):
                    continue
                Gsol.add_edge(prev_point, k, visita=visita)
                visita = visita + 1
                prev_point = k
            # aggiorno precedente, il percorso continua
            prec = indici[i][j]
        nx.write_shp(Gsol, filename)
Example #7
0
 def test_geometryexport(self):
     def testgeom(lyr, expected):
         feature = lyr.GetNextFeature()
         actualwkt = []
         while feature:
             actualwkt.append(feature.GetGeometryRef().ExportToWkt())
             feature = lyr.GetNextFeature()
         assert_equal(sorted(expected), sorted(actualwkt))
     expectedpoints = (
         "POINT (1 1)",
         "POINT (2 2)",
         "POINT (3 3)",
         "POINT (0.9 0.9)",
         "POINT (4 2)"
     )
     expectedlines = (
         "LINESTRING (1 1,2 2)",
         "LINESTRING (2 2,3 3)",
         "LINESTRING (0.9 0.9,4 2)"
     )
     tpath = os.path.join(tempfile.gettempdir(),'shpdir')
     G = nx.read_shp(self.shppath)
     nx.write_shp(G, tpath)
     shpdir = ogr.Open(tpath)
     testgeom(shpdir.GetLayerByName("nodes"), expectedpoints)
     testgeom(shpdir.GetLayerByName("edges"), expectedlines)
Example #8
0
    def output(self, path):

        out_results = 'sequenced-results.csv'
        out_shp = 'sequenced-network'
        
        # Write the shapefiles to path
        nx.write_shp(self.networkplan.network, os.path.join(path, out_shp))
        # Write the csv to path
        cast = {
                'Sequence..Vertex.id'            : int,
                'Sequence..Root.vertex.id'       : int ,
                'Sequence..Upstream.id'          : int,
                'Sequence..Far.sighted.sequence' : int}
        for k,v in cast.iteritems():
            self.output_frame[k] = self.output_frame[k].fillna(-9223372036854775807).astype(v)        
        self.output_frame.to_csv(os.path.join(path, 'temp.csv'), index=False, na_rep='NaN')
        
        with open(os.path.join(path, 'temp.csv')) as f:
            buff = f.read()
            while True:
                idx = buff.find('-9223372036854775807')
                if idx != -1:
                    buff = buff.replace('-9223372036854775807', 'NaN', idx)
                else:
                    break

        with open(os.path.join(path, out_results), 'w') as f:
            f.write(buff)
        os.remove(os.path.join(path, 'temp.csv'))
        # Trash the node shp files
        [os.remove(os.path.join(os.path.join(path, out_shp), x)) 
                for x in os.listdir(os.path.join(path, out_shp)) if 'node' in x]
    def test_geometryexport(self):
        expectedpoints_simple = ("POINT (1 1)", "POINT (2 2)", "POINT (3 3)",
                                 "POINT (0.9 0.9)", "POINT (4 2)")
        expectedlines_simple = ("LINESTRING (1 1,2 2)", "LINESTRING (2 2,3 3)",
                                "LINESTRING (0.9 0.9,4.0 0.9,4 2)")
        expectedpoints = ("POINT (1 1)", "POINT (2 2)", "POINT (3 3)",
                          "POINT (0.9 0.9)", "POINT (4.0 0.9)", "POINT (4 2)")
        expectedlines = ("LINESTRING (1 1,2 2)", "LINESTRING (2 2,3 3)",
                         "LINESTRING (0.9 0.9,4.0 0.9)",
                         "LINESTRING (4.0 0.9,4 2)")

        tpath = os.path.join(tempfile.gettempdir(), 'shpdir')
        G = nx.read_shp(self.shppath)
        nx.write_shp(G, tpath)
        shpdir = ogr.Open(tpath)
        self.checkgeom(shpdir.GetLayerByName("nodes"), expectedpoints_simple)
        self.checkgeom(shpdir.GetLayerByName("edges"), expectedlines_simple)

        # Test unsimplified
        # Nodes should have additional point,
        # edges should be 'flattened'
        G = nx.read_shp(self.shppath, simplify=False)
        nx.write_shp(G, tpath)
        shpdir = ogr.Open(tpath)
        self.checkgeom(shpdir.GetLayerByName("nodes"), expectedpoints)
        self.checkgeom(shpdir.GetLayerByName("edges"), expectedlines)
Example #10
0
def run_script(iface):

    catchment = []
    r = 2000
    i = 0

    while i > len(cost):
        if cost < r:
            # next node
            if cost(next) < r:
                upperbound.append(i)
        i = i + 1

    G = nx.Graph() # creating an empty graph
    nx.read_shp(str(iface.activeLayer().source()))
    G.add_edge(1, 2, weight=4.7 )

    M = nx.single_source_shortest_path_length(G,0,)




    M = nx.ego_graph(G, (679980.26234586, 6059164.91455736), 1, center=True, undirected=False, distance=2000)
    nx.write_shp()

    layer = iface.mapCanvas().currentLayer()
    for f in layer.getFeatures():
        geom = f.geometry()
        G.add_node(geom)
def shortestpath(STlist, filename, cwd):
    if len(STlist) >= 2:
        #SETup part for this function, it read in the the road shapefile, but only store four bounding
        #box value
        shxFile = open(filename + ".shx", "rb")
        s = shxFile.read(28)
        header = struct.unpack(">iiiiiii", s)  # convert into 7 integers
        s = shxFile.read(72)
        header2 = struct.unpack("<iidddddddd", s)
        minX, minY, maxX, maxY = header2[2], header2[3], header2[4], header2[5]
        Arcxylist = []
        windowWidth, windowHeight = 800, 600  # define window size
        #calculate the ratio, same as 'openFile' function
        ratiox = (maxX - minX) / windowWidth
        ratioy = (maxY - minY) / windowHeight
        ratio = ratiox
        if ratioy > ratio:
            ratio = ratioy
        dsc = arcpy.Describe(filename + ".shp")
        coord_sys = dsc.spatialReference
        #The for loop below will convert Tkinter coordinates into Arcgis-real-world coorinates
        for i in range(1, len(STlist), 2):
            Arcxylist.append([
                minX + STlist[i - 1][0] * ratio,
                maxY - STlist[i - 1][1] * ratio
            ])  #[minX + source.x* ratio, maxY + source.y* ratio]
            Arcxylist.append(
                [minX + STlist[i][0] * ratio, maxY - STlist[i][1] * ratio])
            #set up a relative small distance to compare
            minD = 10000000
            minD2 = 10000000
            G = nx.read_shp("main_road_separte.shp")
            Conlist = list(nx.connected_component_subgraphs(G.to_undirected()))
            g = Conlist[0]
            nodelist = g.nodes(data=True)
            nodelist2 = g.nodes(data=True)
            source = target = ()
            for nodes in nodelist:
                distance = Eucl(nodes[0][0], nodes[0][1], Arcxylist[i - 1][0],
                                Arcxylist[i - 1][1])
                if distance < minD:
                    minD = distance
                    source = nodes[0]
            for nodes2 in nodelist2:
                distance2 = Eucl(nodes2[0][0], nodes2[0][1], Arcxylist[i][0],
                                 Arcxylist[i][1])
                if distance2 < minD2:
                    minD2 = distance2
                    target = nodes2[0]
            result_graph = g.subgraph(nx.shortest_path(g, source, target))
            directory = cwd + "sss" + "a" * i
            if not os.path.exists(directory):
                os.makedirs(directory)
            nx.write_shp(result_graph, directory)
            arcpy.DefineProjection_management(directory + "\\edges.shp",
                                              coord_sys)
            openFile(directory + "\\edges.shx", directory + "\\edges.shp",
                     "black")
    else:
        print "Selected Points not enough"
Example #12
0
def create_island_to_island_graph(g: nx.DiGraph) -> nx.DiGraph:
    GG = nx.DiGraph()
    for node_a, node_b in g.edges_iter():
        if node_a in g.edge[node_b]:
            GG.add_edge((node_a, node_b), (node_b, node_a))
            cab = g.edge[node_a][node_b]['region'].centroid
            GG.node[(node_a, node_b)]['coord'] = (cab.x, cab.y)
    nx.write_shp()
    return GG
def remove_flood_data(graph, output):
    """
        Intersects network routing graph with flood polygons and removes edges and nodes which are affected by the flood.
        :param graph: graph of complete area
        :type graph: osmnx or networkx network routing graph with edge and node file
        :param output: folder path to save output
        :type output: string
        """
    flood_data = gpd.read_file(
        path.join(DATA_DIR, SETTINGS['flood']['preprocessed']))

    edge_data = list(graph.edges(data=True))

    # create an empty spatial index object
    index = rtree.index.Index()
    counter = 0
    for edge in range(len(edge_data)):
        edge_geom = shape(
            LineString([Point(edge_data[edge][0]),
                        Point(edge_data[edge][1])]))
        index.insert(counter, edge_geom.bounds)
        counter += 1

    # check intersection and remove affected nodes
    intersect_counter = 0
    for poly in flood_data['geometry']:
        for fid in list(index.intersection(shape(poly).bounds)):
            edge_geom_shape = shape(
                LineString(
                    [Point(edge_data[fid][0]),
                     Point(edge_data[fid][1])]))
            if edge_geom_shape.intersects(shape(poly)):
                e = edge_data[fid][0]
                e_to = edge_data[fid][1]
                # remove intersected edges
                if graph.has_edge(e, e_to):
                    graph.remove_edge(e, e_to)
                # remove intersected nodes
                if Point(e).intersects(shape(poly)) and graph.has_node(e):
                    graph.remove_node(e)
                    intersect_counter += 1
                if Point(e_to).intersects(
                        shape(poly)) and graph.has_node(e_to):
                    graph.remove_node(e_to)
                    intersect_counter += 1
    print('Amount of intersected and removed nodes:', intersect_counter)

    # save networkx graph with complete graph enum_id
    try:
        nx.write_shp(graph, output)
    except Exception as err:
        print(err)
    print('Intersected networkx graph saved')

    return graph
Example #14
0
def export_path_to_shp(G, multy, output_workspace, path_dict_list):
    """Export of path (list of nodes) through graph to shapefile

        Parameters
        ----------
        G: networkx graph object

        multy: str
            If value is true – multigraph will be created
            If value is false – simple graph will be created

        output_workspace: str
            path to the folder with output shapefile

        path_dict_list: list
            list of dicts kind of {start: [node1, node2, node3]}

        Returns
        -------
        None
    """
    new_graph = nx.MultiGraph(crs=G.graph['crs'])
    e = 0
    for path_dict in path_dict_list:
        a = 0
        for node in path_dict:
            path_list = path_dict[node]
            path_list.insert(0, node)
            b = 0
            for edge in G.edges(keys=True, data=True):
                attribute_data = new_graph.get_edge_data(*edge)
                new_attribute_data = {}
                Wkt = attribute_data['Wkt']
                c = 0
                for i in range(len(path_list) - 1):
                    identifier = str(e) + str(a) + str(b) + str(c)
                    if tuple([tuple(path_list[i]), tuple(path_list[i + 1])]) == tuple(edge[:2])\
                            or tuple([tuple(path_list[i + 1]), tuple(path_list[i])]) == tuple(edge[:2]):
                        new_graph.add_edge(edge[0],
                                           edge[1],
                                           identifier,
                                           Name=edge[2],
                                           ident=identifier,
                                           Wkt=Wkt)
                        new_attribute_data[edge[0], edge[1],
                                           identifier] = attribute_data
                        nx.set_edge_attributes(new_graph, new_attribute_data)
                    c += 1
                b += 1
            a += 1
        e += 1
    if multy == 'true':
        nx_multi_shp.write_shp(new_graph, 'ident', output_workspace)
    else:
        nx.write_shp(new_graph, output_workspace)
def degree_centrality(in_graph, multigraph, multi_attr, output, digraph):
    G = aux_ie.convert_shp_to_graph(in_graph, digraph, multigraph, multi_attr)
    if digraph == 'true':
        in_deg_centrality = nx.in_degree_centrality(G)
        out_deg_centrality = nx.out_degree_centrality(G)
        nx.set_node_attributes(G, in_deg_centrality, 'in_DC')
        nx.set_node_attributes(G, out_deg_centrality, 'out_DC')
    else:
        deg_centrality = nx.degree_centrality(G)
        nx.set_node_attributes(G, deg_centrality, 'DC')
    nx.write_shp(G, output)
 def test_geometryexport(self):
     expectedpoints = ("POINT (1 1)", "POINT (2 2)", "POINT (3 3)",
                       "POINT (0.9 0.9)", "POINT (4 2)")
     expectedlines = ("LINESTRING (1 1,2 2)", "LINESTRING (2 2,3 3)",
                      "LINESTRING (0.9 0.9,4 2)")
     tpath = os.path.join(tempfile.gettempdir(), 'shpdir')
     G = nx.read_shp(self.shppath)
     nx.write_shp(G, tpath)
     shpdir = ogr.Open(tpath)
     self.checkgeom(shpdir.GetLayerByName("nodes"), expectedpoints)
     self.checkgeom(shpdir.GetLayerByName("edges"), expectedlines)
Example #17
0
    def read_shpfile(self,Path):
        #read the shapefile
        nx_load_shp=nx.read_shp('E:/data/bj/road_unsplit.shp')
        self.nx_graph=nx_load_shp.to_undirected()
        #find the largest connected subgraph
        nx_list_subgraph=list(nx.connected_component_subgraphs(nx_load_shp.to_undirected()))[0]
        # self.nx_graph=nx_list_subgraph

        self.nx_nodes=np.array(nx_list_subgraph.nodes())
        self.nx_edges=np.array(nx_list_subgraph.edges())
        nx.write_shp(nx_list_subgraph,'C:/ww')
Example #18
0
def store_shp(rn, target_path):
    ''' nodes: [lng, lat] '''
    rn.remove_nodes_from(list(nx.isolates(rn)))
    print('# of nodes:{}'.format(rn.number_of_nodes()))
    print('# of edges:{}'.format(rn.number_of_edges()))
    for _, _, data in rn.edges(data=True):
        geo_line = ogr.Geometry(ogr.wkbLineString)
        for coord in data['coords']:
            geo_line.AddPoint(coord[0], coord[1])
        data['Wkb'] = geo_line.ExportToWkb()
        del data['coords']
    nx.write_shp(rn, target_path)
 def test_wkt_export(self):
     G = nx.DiGraph()
     tpath = os.path.join(tempfile.gettempdir(), 'shpdir')
     points = ("POINT (0.9 0.9)", "POINT (4 2)")
     line = ("LINESTRING (0.9 0.9,4 2)", )
     G.add_node(1, Wkt=points[0])
     G.add_node(2, Wkt=points[1])
     G.add_edge(1, 2, Wkt=line[0])
     try:
         nx.write_shp(G, tpath)
     except Exception, e:
         assert False, e
def writeGraph(format, outfile):
	if format == 'gexf':
		nx.write_gexf(G, outfile)
	elif format == 'gml':
		nx.write_gml(G, outfile)
	elif format == 'graphml':
		nx.write_graphml(G, outfile)
	elif format == 'json':
		data = nx.node_link_data(G)
		with open(outfile, 'w') as of:
			json.dump(data, of)
	elif format == 'shp':
		nx.write_shp(G, outfile)
Example #21
0
def calc_minimum_spanning_tree(input_network_shp, output_network_folder, building_nodes_shp, output_edges, output_nodes,
                               weight_field, type_mat_default, pipe_diameter_default):
    # read shapefile into networkx format into a directed graph
    graph = nx.read_shp(input_network_shp)

    # transform to an undirected graph
    iterator_edges = graph.edges(data=True)

    G = nx.Graph()
    # plant = (11660.95859999981, 37003.7689999986)
    for (x, y, data) in iterator_edges:
        G.add_edge(x, y, weight=data[weight_field])
    # calculate minimum spanning tree of undirected graph

    mst_non_directed = nx.minimum_spanning_edges(G, data=False)

    # transform back directed graph and save:
    mst_directed = nx.DiGraph()
    mst_directed.add_edges_from(mst_non_directed)
    nx.write_shp(mst_directed, output_network_folder)

    # populate fields Type_mat, Name, Pipe_Dn
    mst_edges = gdf.from_file(output_edges)
    mst_edges['Type_mat'] = type_mat_default
    mst_edges['Pipe_DN'] = pipe_diameter_default
    mst_edges['Name'] = ["PIPE" + str(x) for x in mst_edges['FID']]
    mst_edges.drop("FID", axis=1, inplace=True)
    mst_edges.crs = gdf.from_file(input_network_shp).crs  # to add coordinate system
    mst_edges.to_file(output_edges, driver='ESRI Shapefile')

    # populate fields Building, Type, Name
    mst_nodes = gdf.from_file(output_nodes)

    buiding_nodes_df = gdf.from_file(building_nodes_shp)
    mst_nodes.crs = buiding_nodes_df.crs  # to add same coordinate system
    buiding_nodes_df['coordinates'] = buiding_nodes_df['geometry'].apply(
        lambda x: (round(x.coords[0][0], 4), round(x.coords[0][1], 4)))
    mst_nodes['coordinates'] = mst_nodes['geometry'].apply(
        lambda x: (round(x.coords[0][0], 4), round(x.coords[0][1], 4)))
    names_temporary = ["NODE" + str(x) for x in mst_nodes['FID']]

    new_mst_nodes = mst_nodes.merge(buiding_nodes_df, suffixes=['', '_y'], on="coordinates", how='outer')
    new_mst_nodes.fillna(value="NONE", inplace=True)
    new_mst_nodes['Building'] = new_mst_nodes['Name']

    new_mst_nodes['Name'] = names_temporary
    new_mst_nodes['Type'] = new_mst_nodes['Building'].apply(lambda x: 'CONSUMER' if x != "NONE" else x)
    new_mst_nodes.drop(["FID", "coordinates", 'floors_bg', 'floors_ag', 'height_bg', 'height_ag', 'geometry_y'], axis=1,
                       inplace=True)
    new_mst_nodes.to_file(output_nodes, driver='ESRI Shapefile')
    def test_attributeexport(self):
        def testattributes(lyr, expected):
            feature = lyr.GetNextFeature()
            actualvalues = []
            while feature:
                actualvalues.append(feature.GetGeometryRef().ExportToWkt())
                feature = lyr.GetNextFeature()
            assert_equal(sorted(expected), sorted(actualvalues))

        tpath = os.path.join(tempfile.gettempdir(), 'shpdir')
        G = nx.read_shp(self.shppath)
        nx.write_shp(G, tpath)
        shpdir = ogr.Open(tpath)
        nodes = shpdir.GetLayerByName("nodes")
Example #23
0
    def test_attributeexport(self):
        def testattributes(lyr, expected):
            feature = lyr.GetNextFeature()
            actualvalues = []
            while feature:
                actualvalues.append(feature.GetGeometryRef().ExportToWkt())
                feature = lyr.GetNextFeature()
            assert_equal(sorted(expected), sorted(actualvalues))

        tpath = os.path.join(tempfile.gettempdir(),'shpdir')
        G = nx.read_shp(self.shppath)
        nx.write_shp(G, tpath)
        shpdir = ogr.Open(tpath)
        nodes = shpdir.GetLayerByName("nodes")
 def test_wkt_export(self):
     G = nx.DiGraph()
     tpath = os.path.join(tempfile.gettempdir(), 'shpdir')
     points = ("POINT (0.9 0.9)", "POINT (4 2)")
     line = ("LINESTRING (0.9 0.9,4 2)", )
     G.add_node(1, Wkt=points[0])
     G.add_node(2, Wkt=points[1])
     G.add_edge(1, 2, Wkt=line[0])
     try:
         nx.write_shp(G, tpath)
     except Exception as e:
         assert False, e
     shpdir = ogr.Open(tpath)
     self.checkgeom(shpdir.GetLayerByName("nodes"), points)
     self.checkgeom(shpdir.GetLayerByName("edges"), line)
Example #25
0
    def test_missing_attributes(self):
        G = nx.DiGraph()
        A = (0, 0)
        B = (1, 1)
        C = (2, 2)
        G.add_edge(A, B, foo=100)
        G.add_edge(A, C)

        nx.write_shp(G, self.path)
        H = nx.read_shp(self.path)

        for u, v, d in H.edges(data=True):
            if u == A and v == B:
                assert_equal(d['foo'], 100)
            if u == A and v == C:
                assert_equal(d['foo'], None)
    def test_missing_attributes(self):
        G = nx.DiGraph()
        A = (0, 0)
        B = (1, 1)
        C = (2, 2)
        G.add_edge(A, B, foo=100)
        G.add_edge(A, C)

        nx.write_shp(G, self.path)
        H = nx.read_shp(self.path)

        for u, v, d in H.edges(data=True):
            if u == A and v == B:
                assert_equal(d['foo'], 100)
            if u == A and v == C:
                assert_equal(d['foo'], None)
Example #27
0
def store_rn_shp(rn, target_path):
    print('# of nodes:{}'.format(rn.number_of_nodes()))
    print('# of edges:{}'.format(rn.number_of_edges()))
    for _, data in rn.nodes(data=True):
        if 'pt' in data:
            del data['pt']
    for _, _, data in rn.edges(data=True):
        geo_line = ogr.Geometry(ogr.wkbLineString)
        for coord in data['coords']:
            geo_line.AddPoint(coord.lng, coord.lat)
        data['Wkb'] = geo_line.ExportToWkb()
        del data['coords']
        if 'length' in data:
            del data['length']
    if not rn.is_directed():
        rn = rn.to_directed()
    nx.write_shp(rn, target_path)
Example #28
0
    def test_nodeattributeexport(self):
        tpath = os.path.join(tempfile.gettempdir(), "shpdir")

        G = nx.DiGraph()
        A = (0, 0)
        B = (1, 1)
        C = (2, 2)
        G.add_edge(A, B)
        G.add_edge(A, C)
        label = "node_label"
        for n, d in G.nodes(data=True):
            d["label"] = label
        nx.write_shp(G, tpath)

        H = nx.read_shp(tpath)
        for n, d in H.nodes(data=True):
            assert d["label"] == label
Example #29
0
    def test_nodeattributeexport(self):
        tpath = os.path.join(tempfile.gettempdir(), 'shpdir')

        G = nx.DiGraph()
        A = (0, 0)
        B = (1, 1)
        C = (2, 2)
        G.add_edge(A, B)
        G.add_edge(A, C)
        label = 'node_label'
        for n, d in G.nodes(data=True):
            d['label'] = label
        nx.write_shp(G, tpath)

        H = nx.read_shp(tpath)
        for n, d in H.nodes(data=True):
            assert_equal(d['label'], label)
def edge_representation(row_from_label, col_from_label, row_to_label,
                        col_to_label, distance_matrix, node_label_list,
                        edge_list, gt, out_shp_edges, out_shp_nodes, outDir,
                        epsg=3035):
    G = nx.Graph()
    x0, y0 , w , h = gt[0], gt[3], gt[1], gt[5]
    X0 = x0 + w/2
    Y0 = y0 + h/2
    for k in range(edge_list.shape[0]):
        s, t, edge_weight = edge_list[k, :]
        s, t = int(s), int(t)
        py0_ind, px0_ind = row_from_label[s, t], col_from_label[s, t]
        py1_ind, px1_ind = row_to_label[s, t], col_to_label[s, t]
        px0, py0 = X0 + 100 * px0_ind, Y0 - 100 * py0_ind
        px1, py1 = X0 + 100 * px1_ind, Y0 - 100 * py1_ind
        # G.add_edge((px0, py0), (px1, py1), weight=distance_matrix[s, t])
        G.add_edge((px0, py0), (px1, py1), capacity=str(round(edge_weight, 2)) + " MW")
    nx.write_shp(G, outDir)
    spatialRef = osr.SpatialReference()
    spatialRef.ImportFromEPSG(epsg)
    spatialRef.MorphToESRI()
    for item in ['nodes', 'edges']:
        prj_file = outDir + os.sep + item + '.prj'
        with open(prj_file, 'w') as file:
            file.write(spatialRef.ExportToWkt())
    G = None
    
    gdf = gpd.read_file(outDir + '/edges.shp')

    #gdf['line'] = np.array([1]*edge_list.shape[0])
    #gdf['color'] = np.array(["#ef3b2c"]*edge_list.shape[0])
    #gdf['fillColor'] = np.array(["#ef3b2c"]*edge_list.shape[0])
    #gdf['opacity'] = np.array(["0.7"]*edge_list.shape[0])
    
    gdf.to_file(out_shp_edges)
    gdf = None

    for filename in os.listdir(outDir):
        if filename.startswith("edges"):
            os.remove(outDir + os.sep + filename)
        if filename.startswith("nodes"):
            os.remove(outDir + os.sep + filename)

    '''
Example #31
0
    def test_geometryexport(self):
        expectedpoints_simple = (
            "POINT (1 1)",
            "POINT (2 2)",
            "POINT (3 3)",
            "POINT (0.9 0.9)",
            "POINT (4 2)"
        )
        expectedlines_simple = (
            "LINESTRING (1 1,2 2)",
            "LINESTRING (2 2,3 3)",
            "LINESTRING (0.9 0.9,4.0 0.9,4 2)"
        )
        expectedpoints = (
            "POINT (1 1)",
            "POINT (2 2)",
            "POINT (3 3)",
            "POINT (0.9 0.9)",
            "POINT (4.0 0.9)",
            "POINT (4 2)"
        )
        expectedlines = (
            "LINESTRING (1 1,2 2)",
            "LINESTRING (2 2,3 3)",
            "LINESTRING (0.9 0.9,4.0 0.9)",
            "LINESTRING (4.0 0.9,4 2)"
        )


        tpath = os.path.join(tempfile.gettempdir(), 'shpdir')
        G = nx.read_shp(self.shppath)
        nx.write_shp(G, tpath)
        shpdir = ogr.Open(tpath)
        self.checkgeom(shpdir.GetLayerByName("nodes"), expectedpoints_simple)
        self.checkgeom(shpdir.GetLayerByName("edges"), expectedlines_simple)

        # Test unsimplified 
        # Nodes should have additional point, 
        # edges should be 'flattened'
        G = nx.read_shp(self.shppath, simplify=False)
        nx.write_shp(G, tpath)
        shpdir = ogr.Open(tpath)
        self.checkgeom(shpdir.GetLayerByName("nodes"), expectedpoints)
        self.checkgeom(shpdir.GetLayerByName("edges"), expectedlines)
def edge_representation_old(label_raster,
                            dh_bool_raster,
                            distance_matrix,
                            node_label_list,
                            edge_list,
                            outDir,
                            epsg=3035):
    rm_mk_dir(outDir)
    G = nx.Graph()
    label_ds = gdal.Open(label_raster)
    label_band = label_ds.GetRasterBand(1)
    label_arr = label_band.ReadAsArray()
    bool_ds = gdal.Open(dh_bool_raster)
    bool_band = bool_ds.GetRasterBand(1)
    bool_arr = bool_band.ReadAsArray()
    numLabels = np.max(label_arr)
    coords = measurements.center_of_mass(bool_arr,
                                         label_arr,
                                         index=np.arange(1, numLabels + 1))
    gt = label_ds.GetGeoTransform()
    x0, y0, w, h = gt[0], gt[3], gt[1], gt[5]
    bool_ds = label_ds = gt = None
    X0 = x0 + w / 2
    Y0 = y0 + h / 2
    x = []
    y = []
    for i, item in enumerate(coords):
        x.append(X0 + 100 * item[1])
        y.append(Y0 - 100 * item[0])
        if node_label_list[i]:
            G.add_node((x[i], y[i]))
    for k in range(edge_list.shape[0]):
        s, t, edge_weight = edge_list[k, :]
        s, t = int(s), int(t)
        # G.add_edge((x[s], y[s]), (x[t], y[t]), weight=distance_matrix[s, t])
        G.add_edge((x[s], y[s]), (x[t], y[t]), weight=edge_weight)
    nx.write_shp(G, outDir)
    spatialRef = osr.SpatialReference()
    spatialRef.ImportFromEPSG(epsg)
    spatialRef.MorphToESRI()
    for item in ['nodes', 'edges']:
        prj_file = outDir + os.sep + item + '.prj'
        with open(prj_file, 'w') as file:
            file.write(spatialRef.ExportToWkt())
Example #33
0
 def test_geometryexport(self):
     expectedpoints = (
         "POINT (1 1)",
         "POINT (2 2)",
         "POINT (3 3)",
         "POINT (0.9 0.9)",
         "POINT (4 2)"
     )
     expectedlines = (
         "LINESTRING (1 1,2 2)",
         "LINESTRING (2 2,3 3)",
         "LINESTRING (0.9 0.9,4 2)"
     )
     tpath = os.path.join(tempfile.gettempdir(),'shpdir')
     G = nx.read_shp(self.shppath)
     nx.write_shp(G, tpath)
     shpdir = ogr.Open(tpath)
     self.checkgeom(shpdir.GetLayerByName("nodes"), expectedpoints)
     self.checkgeom(shpdir.GetLayerByName("edges"), expectedlines)
Example #34
0
    def test_attributeexport(self):
        def testattributes(lyr, graph):
            feature = lyr.GetNextFeature()
            while feature:
                coords = []
                ref = feature.GetGeometryRef()
                last = ref.GetPointCount() - 1
                edge_nodes = (ref.GetPoint_2D(0), ref.GetPoint_2D(last))
                name = feature.GetFieldAsString('Name')
                assert_equal(graph.get_edge_data(*edge_nodes)['Name'], name)
                feature = lyr.GetNextFeature()

        tpath = os.path.join(tempfile.gettempdir(), 'shpdir')

        G = nx.read_shp(self.shppath)
        nx.write_shp(G, tpath)
        shpdir = ogr.Open(tpath)
        edges = shpdir.GetLayerByName("edges")
        testattributes(edges, G)
Example #35
0
 def test_wkt_export(self):
     G = nx.DiGraph()
     tpath = os.path.join(tempfile.gettempdir(), 'shpdir')
     points = (
         "POINT (0.9 0.9)",
         "POINT (4 2)"
     )
     line = (
         "LINESTRING (0.9 0.9,4 2)",
     )
     G.add_node(1, Wkt=points[0])
     G.add_node(2, Wkt=points[1])
     G.add_edge(1, 2, Wkt=line[0])
     try:
         nx.write_shp(G, tpath)
     except Exception as e:
         assert False, e
     shpdir = ogr.Open(tpath)
     self.checkgeom(shpdir.GetLayerByName("nodes"), points)
     self.checkgeom(shpdir.GetLayerByName("edges"), line)
Example #36
0
def import_shapefile_to_workspace(exit_on_success=False):
    """Populate workspace with translation of import_shapefile into a graph.

       Raises NameError
    """
    import_file = utility.CLI.args().import_file
    ws = utility.CLI.args().workspace

    imported_graph = nx.read_shp(path=import_file, simplify=True)
    Log.info(f'File \'{import_file}\' imported correctly')

    if not ws:
        raise NameError('Please set workspace dir')

    nx.write_shp(imported_graph, ws)
    Log.info(f'Exported correctly to \'{ws}\' nodes.shp and edges.shp\n')
    Log.info('PLEASE ADD TO \'{}\' ELEVATION '
             'INFORMATION !'.format(os.path.join(ws, 'nodes.shp')))
    Log.info('(Open with QGIS the node.shp and a dem.tif')
    Log.info('then with Point Sampling Tool Plugin create a new node.shp)')
    if exit_on_success:
        raise SystemExit(0)
Example #37
0
def runNetwork(job, options):
    coords = np.load("FNodes.npy")

    labels, cluster_centers = doCluster(coords)
    nodosByLabel = getNodes(coords, labels, 4)

    ###Saving info nodes for QGIS app
    with job.fileStore.writeGlobalFileStream() as (fileHandle, outID):
        fileHandle.write("lat,long,frec\n")
        for idx, key in enumerate(nodosByLabel.keys()):
            fileHandle.write("%s,%s,%s,%i\n" %
                             (idx, cluster_centers[key][0],
                              cluster_centers[key][1], len(nodosByLabel[key])))

    job.fileStore.exportFile(
        outID,
        'file:///Users/isaac/IdeaProjects/Network_Workflow/qgis_stats_nodes.csv'
    )

    seq = getEdges(coords, labels)
    G = createGraph(nodosByLabel, cluster_centers, seq)

    #==============================================================================
    # Save: shaping file
    #==============================================================================
    #    BUG of NX library
    mapping = {}
    counter = 0
    for idx, d in enumerate(G.nodes):
        co_od = (G.node[d]["lat"], G.node[d]["lng"])
        mapping[counter] = co_od
        counter += 1
    G = nx.relabel_nodes(G, mapping)

    with job.fileStore.writeGlobalFileStream() as (fileHandle, outID):
        nx.write_shp(G, fileHandle)

    job.fileStore.exportFile(
        outID, 'file:///Users/isaac/IdeaProjects/Network_Workflow/shape')
Example #38
0
def write_shp(geograph, shp_dir):
    """ writes a shapefile from a networkx based GeoGraph object

    Args:
        geograph: GeoGraph object
        shp_dir:  string path to dir to write shape files

    """

    assert geograph.is_aligned()

    # looks like networkx wants us to relabel nodes by their coords
    tup_map = {i: tuple(coords) for i, coords in geograph.coords.items()}

    # copy geograph to plain networkx graph
    # (relabeling a GeoGraph doesn't seem to work)
    nx_coord_graph = nx.Graph(data=geograph)
    nx.relabel_nodes(nx_coord_graph, tup_map, copy=False)

    nx.write_shp(nx_coord_graph, shp_dir)

    if geograph.srs:
        # write srs info to prj file (nx seems to miss this)
        sr = osr.SpatialReference()
        sr.ImportFromProj4(geograph.srs)
        main_prj_filename = shp_dir + '.prj'
        edge_prj_filename = os.path.join(shp_dir, 'edges.prj')
        node_prj_filename = os.path.join(shp_dir, 'nodes.prj')

        def write_prj(prj_filename):
            out = open(prj_filename, 'w')
            out.write(sr.ExportToWkt())
            out.close()

        write_prj(main_prj_filename)
        write_prj(edge_prj_filename)
        write_prj(node_prj_filename)
Example #39
0
#	for i in EffectivePathIndex:
#		SelectNodesGraph = createSelectNodesGraph(CandidateNodesList, i, ScoreMatrix)
#		MaxScorePath = findMaxScorePath(CandidateNodesList, SelectNodesGraph, i)
#		EffectivePath = findEffectivePath(MaxScorePath, CandidateNodesList, MatchedNodesList, Graph, i)
#		TheLastPath = findTheLastPath(MaxScorePath, EffectivePath)
#		for j in range(len(TheLastPath)-1):
#			Road.append((TheLastPath[j],TheLastPath[j+1]))
#		del SelectNodesGraph
#	return Road

def mappingOneTaxi(filename, Graph, G):
	TaxiDataList = readTaxiData(filename)
	for i in TaxiDataList:
		try:
			mappingOnePath(i, Graph)
#			Road = mappingOnePath(i, Graph)
#			G.add_edges_from(Road)
#			del Road
		except Exception,e:
			fp = open('log.txt','a')
			fp.write(str(e)+'\t'+str(i)+'\n\n')
			fp.close()
	nx.write_shp(G, './Graph')
	return






Example #40
0
#Get shapefile directory
shp = str(arcpy.GetParameterAsText(0))
simplify = False

#Convert shapefile to networkx graph
arcpy.AddMessage("Converting to NetworkX Graph")
G = nx.read_shp(shp, simplify)
undinet = G.to_undirected()

#Clear edge dictionary
arcpy.AddMessage("Clearing attribute dictionary for ~performace~")
for u,v,a in undinet.edges(data=True):
        a.clear()

#Get connected components subgraphs
arcpy.AddMessage("Finding low-stress islands")
concomponents = list(nx.connected_component_subgraphs(undinet))
entries = len(concomponents)
arcpy.AddMessage(entries)

##Loop through subgraphs
arcpy.AddMessage("Exporting to thousands of shapefiles")
for subgraphs in range(0, entries):
    path = "C:\Users\zmer\Documents\Network Wzrd\Scripts\NetworkX\SubNet_Workspace\Export_Shapefiles\\" + str(subgraphs+1) + ".shp"
    Graph = concomponents[subgraphs]
    nx.write_shp(Graph, path)
    arcpy.AddMessage(subgraphs)


Example #41
0
def write_shp(G, outdir):
    """ Active development for shp write is now done in networkx. This function left in for compatibilty purposes.
    """
    nx.write_shp(G, outdir)