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)
Beispiel #2
0
def nx_shp(shp_pts, shp_lines, site_col='site'):
    """
    Function to import shapefiles into nx networks. The lines become the edges and the points are used as the node (names). The points shapefile should have a site name column that is precisely at the intersection of the shp_lines.
    """

    ## Read in shapefiles
    t2 = nx.read_shp(shp_pts)
    t3 = nx.read_shp(shp_lines)

    ## extract the site names
    sites = [i[1][site_col] for i in t2.nodes(data=True)]

    ## Set up rename dictionaries
    rename1 = {(i[0], i[1]): (round(i[0]), round(i[1])) for i in t3.nodes()}
    rename2 = {(round(i[0][0]), round(i[0][1])): i[1]['site']
               for i in t2.nodes(data=True)}

    ## Rename nodes
    g2 = nx.relabel_nodes(t3, rename1)
    g3 = nx.relabel_nodes(g2, rename2)

    ## Remove unnecessary nodes
    remove1 = [g3.nodes()[i] for i in np.where(~np.in1d(g3.nodes(), sites))[0]]
    g3.remove_nodes_from(remove1)

    return g3
Beispiel #3
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)
Beispiel #4
0
def shortest_distance(rp, calc_path, road_path, communes_TH, places_TH,
                      spatial_places):

    if rp is not 'no_flood':
        # read path to save new shapefile
        flooded_road_path = os.path.join(
            calc_path, 'Thanh_Hoa_river_flooded_roads_%s.shp' % rp)
        # load network
        sg = nx.read_shp(flooded_road_path).to_undirected()

    else:
        g = nx.read_shp(road_path)
        sg = max(nx.connected_component_subgraphs(g.to_undirected()), key=len)

    commune_dist = {}
    for id_, commune in communes_TH.iterrows():

        # find nearest points of interest (places in this case)
        nearest_places = places_TH.iloc[list(
            spatial_places.nearest(commune.geometry.bounds, 3))]

        k = 0
        for idplace, place in nearest_places.iterrows():

            # Compute the shortest path based on travel time
            try:
                path = nx.shortest_path(sg,
                                        source=tuple(commune['nearest_node']),
                                        target=tuple(place['nearest_node']),
                                        weight='t_time')
            except:
                path = []

            # save path
            if len(path) == 1:
                distance = 0
            elif len(path) == 0:
                distance = 9999
            else:
                distance = pd.DataFrame(
                    [sg[path[i]][path[i + 1]] for i in range(len(path) - 1)],
                    columns=['length']).sum()[0]

            if k == 0:
                shortest_distance = distance
            else:
                if distance < shortest_distance:
                    shortest_distance = distance

            k += 1

        commune_dist[commune['commune_id']] = shortest_distance

    # create dataframe of distances
    commune_dist = pd.DataFrame.from_dict(commune_dist, orient='index')
    commune_dist.columns = [rp]

    return commune_dist
Beispiel #5
0
 def testload(self):
     expected = nx.DiGraph()
     for p in self.paths:
         expected.add_path(p)
     actual = nx.read_shp(self.shppath)
     assert_equal(sorted(expected.node), sorted(actual.node))
     assert_equal(sorted(expected.edges()), sorted(actual.edges()))
 def buildNetwork(self):
     '''Method to create NetworkX nodes and edges shapefiles.'''
     try:
         layer = str(self.filelist[self.ui.comboBoxInput.currentIndex()])
         if layer == "Available layers:":
             raise IOError, "Please specify input shapefile layer."
              
         DG1 = nx.read_shp(layer)
         if str(self.ui.lineEditSave.text()) == '':
             raise IOError, "No output directory specified."
              
         WriteNetworkShapefiles(DG1, self.fd, overwrite=True)
         #self.writeNetworkShapefiles(DG1)                        
         if self.ui.checkBoxAdd.isChecked():
             # Get created files
             nodes = self.fd+"/nodes.shp"
             edges = self.fd+"/edges.shp"
             # Add to QGIS instance
             qgis.utils.iface.addVectorLayer(edges, "Network Edges", "ogr")
             qgis.utils.iface.addVectorLayer(nodes, "Network Nodes", "ogr")
          
         self.close()
     except (AttributeError, IOError) as e:
         QtGui.QMessageBox.warning( self, "NetworkX Plugin Error", 
                                          "%s" % e)
def shp_adj(networkShp):
    G = nx.read_shp(networkShp)
    mat = nx.adjacency_matrix(G)
    maxNumConn = max((mat).sum(1))
    connectionMat = np.zeros(shape=(mat.shape[0], maxNumConn))
    for i in xrange(mat.shape[0]):
        noZero = np.nonzero(mat[i])
        z = np.asarray(noZero[1])
        connectionMat[i, 0:z.shape[0]] = (z[0:z.shape[0]] + 1)

    shpAdj = np.zeros(shape=(len(G.nodes()), maxNumConn * 2 + 5))
    Gnodes = np.array(G.nodes())
    shpAdj[:, 0] = range(1, len(G.nodes()) + 1)
    shpAdj[:, 1] = Gnodes[:, 0]
    shpAdj[:, 2] = Gnodes[:, 1]
    for i in xrange(maxNumConn):
        shpAdj[:, 3 + i] = connectionMat[:, i]
        nonZero = np.nonzero(shpAdj[:, 3 + i])
        shpAdj[nonZero[0], 5 + i + maxNumConn] = haversine(
            shpAdj[nonZero[0], 1], shpAdj[nonZero[0], 2],
            shpAdj[(shpAdj[nonZero[0], 3 + i] - 1).astype(int),
                   1], shpAdj[(shpAdj[nonZero[0], 3 + i] - 1).astype(int), 2])

    #np.savetxt(resultsFolder+'network-shpAdj.txt', shpAdj, fmt='%i %f %f %i %i %i %i %i %i %i %f %f %f %f %f', delimiter=',')
    return shpAdj, maxNumConn
def updateContourEdgeInfo(urlShpFile, graphPicklePath):
    roadGraphd = nx.read_shp(urlShpFile)
    roadGraph = roadGraphd.to_undirected()
    
    _edgeList = nx.to_edgelist(roadGraph)
    contourAttribute = dict()
    
    # spline parameters
    s=0.0 # smoothness parameter
    k=4 # spline order
    
    for _edge in _edgeList:
        _edgePts = getEdgePoints(_edge)
        _r = [item[0] for item in _edgePts]
        _c = [item[1] for item in _edgePts]
        
#         _r2,_c2 = getUniformSampledPoints(_r,_c)
        _r2,_c2 = getMeasuredSamplePoints(_r,_c)
        
        x = _r2
        y = _c2
#         M = 2*( len(x) + k)
        M = len(x)
        
        
        if M <= k:
            M = 2*k
        
        t = np.linspace(0, len(x), M)
        x = np.interp(t, np.arange(len(x)), x)
        y = np.interp(t, np.arange(len(y)), y)
        z = t
        
        
        # find the knot points
        tckp,u = splprep([x,y,z],s=s,k=k,nest=-1)
        
        # evaluate spline, including interpolated points
        xnew,ynew,znew = splev(linspace(0,1,M),tckp)
        dx,dy,dz = splev(linspace(0,1,M), tckp, der=1)
        
        slp = []
        cnv = 180/ np.pi
        for i in xrange(len(dx)):
            slp.append(np.arctan((dy[i]/dx[i]))*cnv )
            pass
        
        # quantize the slope and assign to edge descriptor list
        roadLetFeat = []
        for elem in slp:
            feat = genFeat(elem, _alphabetSize)
            roadLetFeat.append(feat)
            pass
        
        contourAttribute[(_edge[0],_edge[1])] = roadLetFeat
        pass
    
    nx.set_edge_attributes(roadGraph, 'contour', contourAttribute)
    nx.write_gpickle(roadGraph, graphPicklePath)
    pass
Beispiel #9
0
def shp_to_G(shp_file):
    '''Ingest G from shapefile
    DOES NOT APPEAR TO WORK CORRECTLY'''
    
    G = nx.read_shp(shp_file)
    
    return G
Beispiel #10
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)
Beispiel #11
0
def showShpFileData(shpFileURL):
    shpData = nx.read_shp(shpFileURL)
    shpData = shpData.to_undirected()

    print shpData.nodes(data=True)
    for edge in shpData.edges(data=True):
        print edge

    nodeList = shpData.nodes(data=True)
    nNode = len(nodeList)

    pos = []
    for i in xrange(nNode):
        pos.append(nodeList[i][0])
        pass

    shpLayout = dict(zip(shpData, pos))

    plt.figure(1)
    nx.draw_networkx_edges(shpData,
                           pos=shpLayout,
                           edgelist=None,
                           width=1,
                           edge_color='b',
                           style='-',
                           alpha=0.5)
    nx.draw_networkx_nodes(shpData, pos=shpLayout, nodelist=None, node_size=2)

    print len(shpData.nodes(data=False))
    print len(shpData.edges(data=False))

    plt.show()
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"
def randomWalk(urlShpFile):
    roadGraph = nx.read_shp(urlShpFile)
    # use two random nodes in the graph and generate shortest path between them as vehicle trace

    nodeList = roadGraph.nodes(data=False)
    nNode = len(nodeList)

    trace = []

    _node = random.choice(nodeList)
    visited = []
    count = 0
    while count < 10:
        _neighbors = [x for x in nx.all_neighbors(roadGraph, _node)]
        _neighbor = random.choice(_neighbors)
        while _neighbor in visited:

            pass

        if not _neighbor in trace:
            trace.append(_neighbor)
            count += 1
            _node = _neighbor

    print trace

    pass
Beispiel #14
0
def mysubgraph(_urlShpFile):
    roadGraphd = nx.read_shp(_urlShpFile)
    roadGraph = roadGraphd.to_undirected()
    nodeList = roadGraph.nodes(data=True)

    _nodeList = roadGraph.nodes(data=False)
    nNode = len(nodeList)
    pos = []
    for i in xrange(nNode):
        pos.append(nodeList[i][0])
        pass
    shpLayout = dict(zip(roadGraph, pos))
    _node1 = random.choice(_nodeList)
    _node2 = random.choice(_nodeList)
    _path = nx.astar_path(roadGraph, _node1, _node2, None)

    subG = nx.subgraph(roadGraph, _path)
    plt.figure(1, figsize=(12, 12))
    nx.draw_networkx(subG,
                     pos=shpLayout,
                     edgelist=None,
                     node_size=40,
                     node_color='r',
                     node_shape='d',
                     edgewidth=10,
                     edge_color='r',
                     with_labels=False)
    plt.show()
Beispiel #15
0
def roadNetStats(_urlShpFile):
    roadGraphd = nx.read_shp(_urlShpFile)
    roadGraph = roadGraphd.to_undirected()
    print "graph has been read"
    nodeList = roadGraph.nodes(data=True)
    _nodeList = roadGraph.nodes(data=False)
    nNode = len(nodeList)

    pos = []
    for i in xrange(nNode):
        pos.append(nodeList[i][0])
        pass

    shpLayout = dict(zip(roadGraph, pos))
    print "number of nodes: " + str(nx.number_of_nodes(roadGraph))
    print "number of edges: " + str(nx.number_of_edges(roadGraph))

    neighborCount = {}
    for node in _nodeList:
        neighbors = list(nx.all_neighbors(roadGraph, node))
        n = len(neighbors)

        if neighborCount.has_key(n):
            neighborCount[n] += 1
            pass
        else:
            neighborCount[n] = 1
        pass

    for item in neighborCount.keys():
        print item, ":", neighborCount[item]
    pass
Beispiel #16
0
 def testload(self):
     expected = nx.DiGraph()
     for p in self.paths:
         expected.add_path(p)
     actual = nx.read_shp(self.shppath)
     assert_equal(sorted(expected.node), sorted(actual.node))
     assert_equal(sorted(expected.edges()), sorted(actual.edges()))
def getNetworkGraph(shapefile, segmentlengths):
    g = nx.read_shp(shapefile)
    sg = g.to_undirected()
    for n0, n1 in sg.edges_iter():
        oid = int(sg[n0][n1]["OBJECTID"])
        sg.edge[n0][n1]['length'] = segmentlengths[oid]
    return sg
Beispiel #18
0
def shp2rnet(args):
    graph = networkx.read_shp(args["shape_file"])
    edges = list(graph.edges)
    nodes = {}
    node_id = 0
    edge_id = 0

    with open(args["out"], 'w') as f_out:
        for edge in edges:
            from_lng = edge[0][0]
            from_lat = edge[0][1]
            to_lng = edge[1][0]
            to_lat = edge[1][1]
            from_key = str(from_lng) + "-" + str(from_lat)
            to_key = str(to_lng) + "-" + str(to_lat)
            if from_key not in nodes:
                nodes[from_key] = node_id
                node_id+=1
            if to_key not in nodes:
                nodes[to_key] = node_id
                node_id+=1
            from_id = nodes[from_key]
            to_id = nodes[to_key]
            row = "{} {} {} {} {} {} {}\n".format(edge_id, from_id, to_id,
                    from_lng, from_lat, to_lng, to_lat)
            f_out.write(row)
            edge_id+=1
Beispiel #19
0
def load_shp(shp_path):
    """ loads a shapefile into a networkx based GeoGraph object

    Args:
        shp_path:  string path to a line or point shapefile

    Returns:
        geograph:  GeoGraph

    """

    # NOTE:  if shp_path is unicode io doesn't work for some reason
    shp_path = shp_path.encode('ascii', 'ignore')
    g = nx.read_shp(shp_path)
    coords = dict(enumerate(g.nodes()))

    driver = ogr.GetDriverByName('ESRI Shapefile')
    shp = driver.Open(shp_path)
    layer = shp.GetLayer()

    spatial_ref = layer.GetSpatialRef()
    proj4 = None
    if not spatial_ref:
        if gm.is_in_lon_lat(coords):
            proj4 = gm.PROJ4_LATLONG
        else:
            warnings.warn("Spatial Reference could not be set for {}".
                format(shp_path))

    else:
        proj4 = spatial_ref.ExportToProj4()

    g = nx.convert_node_labels_to_integers(g)

    return GeoGraph(srs=proj4, coords=coords, data=g)
def neighorCount(urlShpFile):
    roadGraph = nx.read_shp(urlShpFile)
    nodeList = roadGraph.nodes(data=False)
    nNode = len(nodeList)
    print nNode
    edgesLst = nx.to_edgelist(roadGraph)
    print len(edgesLst)
    nodeNeighorhood = dict()
    neighborhoodSize = dict()
    nsize = [x for x in xrange(15)]
    for i in nsize:
        neighborhoodSize[i] = 0
        pass

    singleNeighborNodeList = []

    for _node in nodeList:
        _neighbors = [x for x in nx.all_neighbors(roadGraph, _node)]
        if not _node in nodeNeighorhood:
            nNeighbor = len(_neighbors)
            nodeNeighorhood[_node] = nNeighbor
            neighborhoodSize[nNeighbor] += 1
            pass
        pass

    for key in neighborhoodSize.keys():
        print key, '\t:', neighborhoodSize[key]
        pass
    pass
Beispiel #21
0
def save_graph_pickle(shp_file, pickle_name):
    G = nt.read_shp(shp_file)
    G_un = G.to_undirected()

    with open(pickle_name, 'wb') as file:
        pickle.dump(G_un, file)
    return
Beispiel #22
0
def roadData2():
    roadGraph = nx.read_shp(urlShpFile)
    nodeList = roadGraph.nodes(data=False)
    nNode = len(nodeList)

    _node = random.choice(nodeList)
    _nodeEdgeList = nx.edges(roadGraph, _node)
    count = 0
    while (len(_nodeEdgeList) < 2):
        _node = random.choice(nodeList)
        _nodeEdgeList = nx.edges(roadGraph, _node)
        count += 1

    print _nodeEdgeList
    _nodeEdgeLst = nx.to_edgelist(roadGraph, _node)
    for _edge in _nodeEdgeLst:
        _edgePts = getEdgePoints(_edge)
        print _edgePts

    edgePointsLst = []
    for roadEdge in _nodeEdgeLst:

        edgePointPairLst = getEdgePoints(roadEdge)
        edgePointsLst.append(edgePointPairLst)

    dispNodeEdgeGraph(edgePointsLst)
    pass
Beispiel #23
0
def roadData1():
    roadGraph = nx.read_shp(urlShpFile)
    roadEdgeList = nx.to_edgelist(roadGraph)
    _flag = True
    for roadEdge in roadEdgeList:
        while _flag:
            print roadEdge[0]
            print roadEdge[1]
            roadEdgeData = roadEdge[2]
            edgeKeyList = roadEdgeData.keys()
            for edgeKey in edgeKeyList:
                print edgeKey
                print roadEdgeData[edgeKey]
                pass
            edgePointStr = roadEdgeData['Wkt']
            b1 = edgePointStr.find('(')
            b2 = edgePointStr.find(')')
            edgePointsSubStr = edgePointStr[b1 + 1:b2]
            print edgePointsSubStr

            edgePointPairStrLst = edgePointsSubStr.split(',')
            edgePointPairLst = []
            for edgePointPairStr in edgePointPairStrLst:
                edgePointPair = [float(x) for x in edgePointPairStr.split(' ')]
                edgePointPairLst.append(edgePointPair)
                pass


#             coordinates of points in an edge
            for _item in edgePointPairLst:
                print _item
                pass
            _flag = False
    pass
Beispiel #24
0
def roadData():
    roadGraph = nx.read_shp(urlShpFile)
    nodeList = roadGraph.nodes(data=False)
    nNode = len(nodeList)
    qnodeList = []
    for i in xrange(1):
        qnodeList.append(random.choice(nodeList))
        pass
    print 'qnodelist', qnodeList
    roadEdgeList = nx.to_edgelist(roadGraph, qnodeList)
    _flag = 10
    for roadEdge in roadEdgeList:
        if _flag:
            edgePointPairLst = getEdgePoints(roadEdge)

            nAlpha = [float(x) for x in roadEdge[0]]
            nOmega = [float(x) for x in roadEdge[1]]
            #             print nAlpha
            #             print nOmega
            #             for edgePointPair in edgePointPairLst:
            #                 print edgePointPair
            #                 pass
            edgePoints = []
            edgePoints.append(nAlpha)
            for edgePointPair in edgePointPairLst:
                edgePoints.append(edgePointPair)
                pass
            edgePoints.append(nOmega)

            dispEdgeGraph(edgePoints)

            _flag -= 1
            pass

    pass
Beispiel #25
0
def graph_from_shp(pth=r'test_processed_01',
                   idcol='facilityid',
                   crs={'init': 'epsg:4326'}):

    #import well-known-text load func
    from shapely import wkt

    G = nx.read_shp(pth)
    G.graph['crs'] = crs
    G = nx.convert_node_labels_to_integers(G, label_attribute='coords')

    for u, v, d in G.edges(data=True):

        #create a shapely line geometry object
        d['geometry'] = wkt.loads(d['Wkt'])
        d['length'] = d['geometry'].length

        #get rid of other geom formats
        del d['Wkb'], d['Wkt'], d['Json']

        #generate a uniq id if necessary
        if idcol not in d:
            d[idcol] = generate_facility_id()

    return G
Beispiel #26
0
    def __init__(self, file_path, coastline_shp=None, calc_dist_weights=True):
        """
        Build a graph from a shapefile and provide methods to prune, or read
        a gpickle file and convert it to a `RiverGraph`.
        """
        if os.path.exists(file_path) == True and os.path.isfile(
                file_path) == True:
            self.coast_fn = coastline_shp
            if os.path.splitext(file_path)[1] == '.shp':
                g = nx.read_shp(file_path)
            elif os.path.splitext(file_path)[1] == '.graphml':
                g = nx.read_graphml(file_path)
            elif os.path.splitext(file_path)[1] == '.gpickle':
                g = nx.read_gpickle(file_path)
            else:
                #assert False, "path does not have a valid extention (.shp, .graphml, .gpickle): %s" % file_path
                print "path does not have a valid extension (.shp, .graphml, .gpickle)"

            self.graph = RiverGraph(data=g, coastline_shp=self.coast_fn)
            if calc_dist_weights:
                print "Weighting Edges with Distances"
                self.graph = self.graph.weight_edges()
        else:
            if os.path.exists(file_path) == False:
                print "file does not exist"
            if os.path.isfile(file_path) == False:
                print "path is not a file or does not exist: %s" % file_path
Beispiel #27
0
def preprocessRoutes(routeFile):
    edges = nx.read_shp(routeFile)
    posNodes = pre.getPositionOfNodesFromEdges(edges)
    posEdges = pre.getPositionOfEdges(edges)
    edg = pre.getEdges(posEdges,posNodes)   
    network = pre.createNetwork(posNodes,edg)
    return network
def readGraph(csadir, method):
    """
    Load the graph network and line shapefiles and combine both to construct a graph data model.
    First, nodes are loaded from the network shp, then the relations between the nodes is extracted from the lines shp.
    :param csadir: folder to find shapefiles
    :param method: subfolder indicating which method to use. One of ['Addnode', 'Steiner-like', 'Closestnode']
    :return: graph
    """
    dir = '{}{}/'.format(csadir, method)
    graph = loadFiles(dir, 'NWB')['NWB']
    atts = graph[1]

    # Read the network shp and convert to undirected graph
    print ">> Reading graph..."
    G = nx.read_shp('{}network.shp'.format(dir), simplify=True)
    SG = G.to_undirected()

    # Create mapping dictionary for relabeling of nodes
    mapping = {node: int(a['Id']) for (node, a) in SG.nodes(data=True)}

    # Relabel nodes with IDs from shapefile for adding edges
    print ">> Relabeling nodes..."
    nx.relabel_nodes(SG, mapping, copy=False)
    ebunch = [(int(u), int(v), float(w)) for i, u, v, w in atts]

    print ">> Adding edges to graph..."
    SG.add_weighted_edges_from(ebunch)

    # nx.write_gml(SG, '{}test.gml'.format(dir), stringify)
    return SG
Beispiel #29
0
def parseGraph(urlShpFile):
    roadGraphd = nx.read_shp(urlShpFile)
    roadGraph = roadGraphd.to_undirected()
    #     roadGraph = nx.read_shp(urlShpFile)

    #     roadGraph = list(nx.connected_component_subgraphs(roadGraph.to_undirected()))[0]

    nodeLst = roadGraph.nodes(data=False)
    roadEdgeList = nx.to_edgelist(roadGraph)

    weightAttribute = dict()
    for roadEdge in roadEdgeList:
        weightAttribute[(roadEdge[0], roadEdge[1])] = 1
        pass

    nx.set_edge_attributes(roadGraph, 'weight', weightAttribute)

    for node in nodeLst:
        _n = roadGraph.neighbors(node)
        print len(_n), node
        if (len(_n) > 4):
            findTree(roadGraph, node)


#             findmst(roadGraph, node)
    pass
Beispiel #30
0
def test():
    import networkx as nx
    import pandas as pd

    bj_roads = r'data/test/bj_small_roads_sample/bj_small_roads_sample.shp'
    bj_speeds = r'data/roads-20180801-20180831.parquet'

    road_net = nx.read_shp(bj_roads)
    edges = EdgesLookup(
        [(GeneralNode(None, o, None, None), GeneralNode(
            None, d, None, None), float(road_net.edges[(o, d)]['length']))
         for o, d in road_net.edges], lambda a, _: a)
    since = time.perf_counter()
    res = edges.k_shortest_path(
        GeneralNode(None, (116.331194, 39.957388), None, None),
        GeneralNode(None, (116.417038, 40.002861), None, None), 0)
    print('since', time.perf_counter() - since)
    print(*map(lambda i: (i[0], i[1][:5]), res), sep='\n')

    edges = gen_dynamic_edges(road_net, pd.read_parquet(bj_speeds))
    for _ in range(3):
        ts = random.randint(0, 24 * 60 * 60)
        since = time.perf_counter()
        res = edges.k_shortest_path(
            GeneralNode(None, (116.331194, 39.957388), None, None),
            GeneralNode(None, (116.417038, 40.002861), None, None), ts)
        print('since', time.perf_counter() - since)
        print(*map(lambda i: (i[0], i[1][:5]), res), sep='\n')
Beispiel #31
0
def convert_shp_to_graph(input_shp, directed, multigraph, parallel_edges_attribute):
    """Converts a shapefile to networkx graph object in accordance to the given parameters.
        It can directed or undirected, simple graph or multigraph

        Parameters
        ----------
        input_shp: shapefile path

        directed: 'true' or 'false'
            If value is true – directed graph will be created.
            If value is false - undirected graph will be created

        multigraph: 'true' or 'false'
            If value is true – multigraph will be created
            If value is false – simple graph will be created

        parallel_edges_attribute: string
            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
        -------
        Graph
        """
    if multigraph == 'true':
        G = nx_multi_shp.read_shp(r'{0}'.format(input_shp), parallel_edges_attribute, simplify=True,
                                  geom_attrs=True, strict=True)
    else:
        G = nx.read_shp(r'{0}'.format(input_shp))
    if directed == 'true':
        graph = G
    else:
        graph = G.to_undirected()
    return graph
Beispiel #32
0
    def read_network_graph_from_file(filename, is_directed=False):
        """Get network graph from filename.

        Args:
            filename (str):  A name of a geo dataset resource recognized by Fiona package.
            is_directed (bool, optional (Defaults to False)): Graph type. True for directed Graph,
                False for Graph.

        Returns:
            obj, dict: Graph and node coordinates.

        """
        geom = nx.read_shp(filename)
        node_coords = {k: v for k, v in enumerate(geom.nodes())}
        # create graph
        graph = None
        if is_directed:
            graph = nx.DiGraph()
        else:
            graph = nx.Graph()

        graph.add_nodes_from(node_coords.keys())
        l = [set(x) for x in geom.edges()]
        edg = [tuple(k for k, v in node_coords.items() if v in sl) for sl in l]

        graph.add_edges_from(edg)

        return graph, node_coords
Beispiel #33
0
def init(shp_path, civici_tpn_path, coords_path):

    G = nt.read_shp(shp_path)
    G_un = G.to_undirected()
    civici_tpn = np.loadtxt(civici_tpn_path, delimiter=";", dtype='str')
    coords = np.loadtxt(coords_path)
    return G_un, civici_tpn, coords
Beispiel #34
0
    def from_files(cls, shp, csv, **kwargs):
        """
        Parameters
        ----------
        shp : file or string (File, directory, or filename to read).
        csv : string or file handle / StringIO.
        
        Example
        ----------
        NetworkPlan.from_files('networks-proposed.shp', 
                               'metrics-local.csv')
        """

        # Only supports longlat format for now
        # with fiona.open(shp) as shapefile:
        #     # Pass along the projection
        #     if 'proj' in shapefile.crs:
        #         kwargs['proj'] = shapefile.crs['proj']
 
        # Ignore the PROJ.4 header if there
        skip_rows = 0
        with open(csv) as csv_stream:
            if csv_stream.readline().startswith('PROJ.4'):
                skip_rows = 1

        # networkx read_shp fails on unicode paths, so try ascii
        if isinstance(shp, unicode):
            shp = shp.encode("ascii")

        return cls(nx.read_shp(shp), pd.read_csv(csv, skiprows=skip_rows), **kwargs)
Beispiel #35
0
 def testload(self):
     expected = nx.DiGraph()
     map(expected.add_path, self.paths)
     G = nx.read_shp(self.shppath)
     assert_equal(sorted(expected.node), sorted(G.node))
     assert_equal(sorted(expected.edges()), sorted(G.edges()))
     names = [G.get_edge_data(s,e)['Name'] for s,e in G.edges()]
     assert_equal(self.names, sorted(names))
Beispiel #36
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)
Beispiel #37
0
    def testload(self):

        def compare_graph_paths_names(g, paths, names):
            expected = nx.DiGraph()
            for p in paths:
                expected.add_path(p)
            assert_equal(sorted(expected.node), sorted(g.node))
            assert_equal(sorted(expected.edges()), sorted(g.edges()))
            g_names = [g.get_edge_data(s, e)['Name'] for s, e in g.edges()]
            assert_equal(names, sorted(g_names))
                
        # simplified
        G = nx.read_shp(self.shppath)
        compare_graph_paths_names(G, self.simplified_paths, \
                                    self.simplified_names)
       
        # unsimplified
        G = nx.read_shp(self.shppath, simplify=False)
        compare_graph_paths_names(G, self.paths, self.names)
def shp_to_graph(shp_path):
    graph_shp = nx.read_shp(str(shp_path))
    shp = QgsVectorLayer(shp_path, "network", "ogr")
    # parallel edges are excluded of the graph because read_shp does not return a multi-graph, self-loops are included
    #self_loops = [[feat.id(), feat.geometry().asPolyline()[0],feat.attributes()]for feat in shp.getFeatures() if feat.geometry().asPolyline()[0] == feat.geometry().asPolyline()[-1] ]
    # parallel_edges =
    graph = graph_shp.to_undirected(reciprocal=False)
    #column_names = [i.name() for i in shp.pendingFields()]
    #for i in self_loops:
    #    graph.add_edge(i[1],i[1],dict(zip(column_names,i[2])))
    return graph
 def __init__(self, shapefile, edge_weighted_by_distance=True):
     g = nx.read_shp(shapefile)
     mg = max(nx.connected_component_subgraphs(g.to_undirected()), key=len)
     if edge_weighted_by_distance:
         for n0, n1 in mg.edges_iter():
             path = np.array(json.loads(mg[n0][n1]['Json'])['coordinates'])
             distance = np.sum(
                 greate_circle_distance(path[1:,0],path[1:,1], path[:-1,0], path[:-1,1])
             )
             mg.edge[n0][n1]['distance'] = distance
     self.graph = mg
     self._cache = {}
     self._cache_nn = {}
Beispiel #40
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")
Beispiel #41
0
def mappingOneDay():
	for i in range(13340):
		try:
			g = nx.read_shp('./Graph/edges.shp')
			G = nx.Graph()
			G.add_edges_from(g.edges())
			filename = './data/' + str(i) + '.txt'
			Graph = MapReader().mapGraph
			mappingOneTaxi(filename, Graph, G)
			del g
			del G
		except Exception,e:
			fp = open('log_taxi.txt','a')
			fp.write(str(e)+'\n'+str(i)+'.txt'+'\n')
			fp.close()
def read_shp_to_graph(shp_path):
    graph_shp = nx.read_shp(str(shp_path), simplify=True)
    shp = QgsVectorLayer(shp_path, "network", "ogr")
    graph = nx.MultiGraph(graph_shp.to_undirected(reciprocal=False))
    # parallel edges are excluded of the graph because read_shp does not return a multi-graph, self-loops are included
    all_ids = [i.id() for i in shp.getFeatures()]
    ids_incl = [i[2]['feat_id'] for i in graph.edges(data=True)]
    ids_excl = set(all_ids) - set(ids_incl)
    request = QgsFeatureRequest().setFilterFids(list(ids_excl))
    excl_features = [feat for feat in shp.getFeatures(request)]
    ids_excl_attr = [[i.geometry().asPolyline()[0], i.geometry().asPolyline()[-1], i.attributes()] for i in excl_features]
    column_names = [i.name() for i in shp.dataProvider().fields()]
    for i in ids_excl_attr:
        graph.add_edge(i[0], i[1], attr_dict=dict(zip(column_names,i[2])))
    return graph
Beispiel #43
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)
Beispiel #44
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)
Beispiel #45
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)
Beispiel #46
0
    def __init__(self, shp, csv, **kwargs):
        self.shp_p, self.csv_p = shp, csv
        self.priority_metric = kwargs['prioritize'] if 'prioritize' in kwargs else 'population'

        # logger.info('Asserting Input Projections Match')
        # self._assert_proj_match(shp, csv)

        from geometryIO import load
        proj4 = load(shp)[0]
        self.measure = 'haversine' if 'longlat' in proj4 else 'euclidean'

        # Load in and align input data
        logger.info('Aligning Network Nodes With Input Metrics')
        self._network, self._metrics = prep_data( nx.read_shp(shp),
                                                  # pd.read_csv(csv, header=1),
                                                  pd.read_csv(csv),
                                                  loc_tol = self.TOL)

        logger.info('Computing Pairwise Distances')
        self.distance_matrix = self._distance_matrix()
         
        if len(self.distance_matrix[(self.distance_matrix > 0) & (self.distance_matrix < self.TOL)]) > 0:
            logger.error("""Dataset Contains Edges, Less Than {tolerance} Meters! 
                          This can result in incorrect alignment of metrics and network, 
                          where fake nodes are incorrectly assigned metrics. 
                          This error is resolved by buffering your input data.""".format(tolerance=self.TOL))

        # Set the edge weight to the distance between those nodes
        self._weight_edges()
        
        logger.info('Directing Network Away From Roots')
        # Transform edges to a rooted graph
        self.direct_network()

        # Assert that the netork is a tree
        self.assert_is_tree()

        # Build list of fake nodes
        self.fake_nodes = self.fakes(self.metrics.index)

        #Fillna values with Zero
        self._metrics = self.metrics.fillna(0)
Beispiel #47
0
	def __init__(self):
		self.MapData = nx.read_shp('./map/111.shp')
Beispiel #48
0
import networkx as nx
import numpy as np
import pandas as pd
import json
import smopy
import matplotlib.pyplot as plt

import matplotlib as mpl
mpl.rcParams['figure.dpi'] = mpl.rcParams['savefig.dpi'] = 300
# g = nx.read_shp("eotroads_49/eotroads_49.shp")
g = nx.read_shp("data/tl_2013_06_prisecroads.shp")
print len(g.edges())
# print g.adjacency_list()
sgs = list(nx.connected_component_subgraphs(g.to_undirected()))
sg = sgs[0]

for element in sgs:
	if element.order() > sg.order():
		sg = element

print sg.order()
print len(sg.edges())
print sg.adjacency_list()

# pos2 = (36.6026, -121.9026)
# pos1 = (34.0569, -118.2427)
#Silicon Valley
# pos0 = (37.3627, -122.0323)
#LA
pos1 = (34.045536, -118.446065)
    min_distance = None
    for node in graph.nodes():
        distance = calc_distance(node[1], node[0], latitude, longitude)
        if closest_node == None:
            closest_node = node
            min_distance = distance
        elif distance < min_distance:
            closest_node = node
            min_distance = distance
    return closest_node

#############################################################################

print "Loading road network into memory..."

graph = networkx.read_shp("split_roads/split_roads.shp")
print "graph has %d nodes and %d edges" % (len(graph.nodes()),
                                           len(graph.edges()))

graph = networkx.connected_component_subgraphs(graph.to_undirected()).next()

print "Calculating road lengths..."

num_roads = len(graph.edges())
num_done = 0
for node1,node2 in graph.edges():
    if num_done % 1000 == 0:
        print "  %d%% done" % int(100 * float(num_done) / float(num_roads))
    num_done = num_done + 1

    wkt = graph[node1][node2]['Wkt']
Beispiel #50
0
def test_read_shp_nofile():
    try:
        from osgeo import ogr
    except ImportError:
        raise SkipTest('ogr not available.')
    G = nx.read_shp("hopefully_this_file_will_not_be_available")
Beispiel #51
0
    def find_connected_components(self):
        """finds "islands" in the network """
        index = self.dlg.ui.layerCombo.currentIndex() 
        if index < 0: 
            # it may occur if there's no layer in the combo/legend 
            pass
        else: 
            layer = self.dlg.ui.layerCombo.itemData(index) 
        # layer = QgsVectorLayer(self.fileName, "layer_name", "ogr")


        index = self.dlg.ui.lts_combo.currentIndex() 
        if index < 0: 
            # it may occur if there's no layer in the combo/legend 
            pass
        else: 
            lts_column = self.dlg.ui.lts_combo.itemData(index) 
        # with open("C:\Users\Peyman.n\Dropbox\Boulder\Plugin\LTS\log.txt","w")as file:
        #     file.write(lts_column +"\n")
            
        lts1_existed = self.make_column(layer,"_isl_lts1")
        lts2_existed = self.make_column(layer,"_isl_lts2")
        lts3_existed = self.make_column(layer,"_isl_lts3")
        lts4_existed = self.make_column(layer,"_isl_lts4")
        # path = "C:/Users/Peyman.n/Dropbox/Boulder/BoulderStreetsRating_20140407_Peter/for_test.shp"
        # out_path = "C:/Users/Peyman.n/Dropbox/Boulder/BoulderStreetsRating_20140407_Peter"
        # get the path from selected layer
        myfilepath= os.path.dirname( unicode( layer.dataProvider().dataSourceUri() ) ) ;
        layer_name = layer.name()
        path2 = myfilepath +"/"+layer_name+".shp"
        out_path = myfilepath
        # with open("C:\Users\Peyman.n\Dropbox\Boulder\Plugin\LTS\log.txt","a")as file:
        #     file.write(path2 +"\n")
        # ##
        # path3="C:/Users/Peyman.n/Dropbox/Boulder/BoulderStreetsRating_20140407_Peter/BoulderStreetsWProjection_20140407_Joined.shp"
        layer2 = nx.read_shp(str(path2))
        self.dlg.ui.progressBar.setValue(5)
        G=layer2.to_undirected()
        self.dlg.ui.progressBar.setValue(10)
        lts_threshs = [(1,"_isl_lts1"),(2,"_isl_lts2"),(3,"_isl_lts3"),(4,"_isl_lts4")]
        field = str(lts_column)
        # with open("C:\Users\Peyman.n\Dropbox\Boulder\Plugin\LTS\log.txt","a")as file:
        #     file.write(field +"\n")
        prog =0
        for lts_thresh,attr in (lts_threshs):
            prog +=1
            temp = [(u,v,d) for u,v,d in G.edges_iter(data=True) if d[field] <= lts_thresh]  # set the edges numbers to zero
            g2 = nx.Graph(temp)
            H=nx.connected_component_subgraphs(g2)

            for idx, cc in enumerate(H):
                for edge in cc.edges(data=True):
                    G[edge[0]][edge[1]][attr]=idx+1 # zero means it was filtered out
            j= prog * 20
            self.dlg.ui.progressBar.setValue(j)

        # order attributes table
        for index, edge in enumerate (G.edges(data=True)):
            edge = list(edge)
            edge[2] = OrderedDict(sorted(edge[2].items()))
            edge=tuple(edge)
            G[edge[0]][edge[1]] = edge[2] 


        self.remove_column(layer,"_isl_lts1",lts1_existed)
        self.remove_column(layer,"_isl_lts2",lts2_existed)
        self.remove_column(layer,"_isl_lts3",lts3_existed)
        self.remove_column(layer,"_isl_lts4",lts4_existed)


        out_name =str(layer_name+"_with islands")
        write_shp(G,out_path,out_name)
        self.dlg.ui.progressBar.setValue(100)
        QMessageBox.information(self.dlg, ("Successful"), ("A new shapefile "+ out_name+" has been created in your folder"))  
        # Add to TOC
        vlayer = QgsVectorLayer(out_path +"/"+out_name+".shp",out_name,"ogr")
        #get crs of project
        actual_crs = iface.mapCanvas().mapRenderer().destinationCrs()
        #change crs of layer
        vlayer.setCrs(actual_crs)

        QgsMapLayerRegistry.instance().addMapLayer(vlayer)
        
        self.dlg.close()
Beispiel #52
0
import arcpy
import networkx as nx


#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()

#Draw the Graph
networkx.draw(G)
plt.savefig("path.png")
plt.show()
networkx.draw(G,pos=nx.spectral_layout(G), nodecolor='r',edge_color='b')
def main(path_nodes, path_edges, gid_nodes, gid_edges, weight_edges):
    # ----------------------------------------------------------
    # NetworkX
    # ----------------------------------------------------------
    # Import des données SHP à grapher:

    G = nx.Graph(name="Floyd_Warshall_script", date=str(datetime.datetime.now()))
    E = nx.read_shp(str(path_edges))
    N = nx.read_shp(str(path_nodes))

    G.add_nodes_from(N.nodes(data=True))
    G.add_edges_from(E.edges(data=True))

    # Changement de leur nom (pour l'instant, chaque valeur a le nom de ses coordonnées géographiques, c'est nul):
    # ----------------------------------------------------------
    # D'abord pour les edges:

    x = 0
    dict_edges = {}
    total_edges = G.number_of_edges()
    print("Processing Edges")
    while x < total_edges:
        try:  # En raison de la structure possible des fichiers, on place un try qui propose [1] ou [2], histoire de parer à toute éventualité.
            dict_edges[G.edges(data=True)[x][0]] = G.edges(data=True)[x][2][str(gid_edges)]
            x = x + 1
        except:
            dict_edges[G.edges(data=True)[x][0]] = G.edges(data=True)[x][1][str(gid_edges)]
            x = x + 1
    # Maintenant pour les nodes:

    x = 0
    dict_nodes = {}
    count_nodes = {}
    total_nodes = G.number_of_nodes()
    print("Processing Nodes")
    while x < total_nodes:
        try:  # En raison de la structure possible des fichiers, on place un try qui propose [1] ou [2], histoire de parer à toute éventualité.
            dict_nodes[G.nodes(data=True)[x][0]] = G.nodes(data=True)[x][1][str(gid_nodes)]
            count_nodes[x] = G.nodes(data=True)[x][1][str(gid_nodes)]
            x = x + 1
        except:
            dict_nodes[G.nodes(data=True)[x][0]] = G.nodes(data=True)[x][2][str(gid_nodes)]
            count_nodes[x] = G.nodes(data=True)[x][1][str(gid_nodes)]
            x = x + 1

    # On renomme les nodes/edges par leur id/gid originels:
    G = nx.relabel_nodes(G, dict_nodes)
    print("Processing Graph")
    # G=nx.relabel_edges(G,dict_edges) # Il semblerait que cette fonction n'existe pas... A rajouter manuellement, cela ne doit pas être bien dur.

    # ----------------------------------------------------------
    # On lance maintenant le calcul d'itinéraire pour l'ensemble des paires de noeuds du réseau:
    starting_point = datetime.datetime.now()
    results = nx.floyd_warshall(G, weight=str(weight_edges))
    print(starting_point)
    print(datetime.datetime.now())
    print("Il aura fallu:  " + str(datetime.datetime.now() - starting_point) + " pour effectuer le calcul")

    text_file = open("Shitty_output.txt", "w")
    text_file.write(str(results))
    text_file.close()

    open(
        "Output.txt", "w"
    ).close()  # On efface Output.txt avant de lancer des opérations "append" dessus, histoire d'eviter d'avoir un mélange de résultats de plusieurs fichiers.
    with open("Output.txt", "a") as text_file:

        gid = 0
        i = 0
        text_file.writelines("gid" + ";" + "Origin" + ";" + "Destination" + ";" + "FloydWarshall_Cost" + "\n")
        while i < total_nodes:

            origins = results[str(count_nodes[i])]
            i1 = 0
            for origin in origins:
                while i1 < total_nodes:
                    gid = gid + 1
                    destinations = origins[str(count_nodes[i1])]
                    text_file.writelines(
                        str(gid)
                        + ";"
                        + str(count_nodes[i] + ";" + str(count_nodes[i1]) + ";" + str(destinations))
                        + "\n"
                    )
                    i1 = i1 + 1
            i = i + 1
    text_file.close()
    print(
        'Un fichier .TXT nommé "Output" a maintenant du apparaitre dans le dossier depuis lequel nous avons chargé le shapefile'
    )
def merge_lines(network_input):

    #create a copy of the input network as a memory layer
    crs=network_input.crs()
    network_input_filepath=network_input.dataProvider().dataSourceUri()
    network_input_dir=os.path.dirname(network_input_filepath)
    network_input_basename=QFileInfo(network_input_filepath).baseName()
    network_input_path= network_input_dir + "/"+ network_input_basename +".shp"
    networ_expl_path= network_input_dir + "/"+ network_input_basename +"_exploded.shp"
    network_input=QgsVectorLayer(network_input_path, "network_input", "ogr")
    temp_network=QgsVectorLayer('LineString?crs='+crs.toWkt(), "temporary_network", "memory")

    processing.runalg("qgis:explodelines",network_input,networ_expl_path)

    expl_network=QgsVectorLayer(networ_expl_path,"network_exploded","ogr")

    #add a column in the network_input file with feature ID
    expl_network.startEditing()
    expl_network.dataProvider().addAttributes([QgsField("feat_id_", QVariant.Int)])
    expl_network.commitChanges()

    fieldIdx = expl_network.dataProvider().fields().indexFromName("feat_id_")
    updateMap = {}

    for f in expl_network.getFeatures():
        fid=f.id()
        updateMap[fid] = { fieldIdx: fid}

    expl_network.dataProvider().changeAttributeValues( updateMap )

    QgsMapLayerRegistry.instance().addMapLayer(temp_network)

    #iface.mapCanvas().refresh()

    temp_network.dataProvider().addAttributes([y for y in expl_network.dataProvider().fields()])
    temp_network.updateFields()
    temp_network.startEditing()
    temp_network.addFeatures([x for x in expl_network.getFeatures()])
    temp_network.commitChanges()
    temp_network.removeSelection()

    """01: Merge lines from intersection to intersection"""
    #make a graph of the network_input_exploded layer
    G_shp = nx.read_shp(str(networ_expl_path))
    #parallel lines are excluded of the graph because it is not a multigraph, self loops are included
    G=G_shp.to_undirected(reciprocal=False)

    Dual_G=nx.MultiGraph()
    for e in G.edges_iter(data='feat_id_'):
        Dual_G.add_node(e[2])

    for i,j in G.adjacency_iter():
        if len(j)==2:
            values=[]
            for k,v in j.items():
                values.append(v['feat_id_'])
            #print values
            Dual_G.add_edge(values[0],values[1],data=None)

    #lines with three connections have been included, breaks at intresections
    #set also include single edges
    sets=[]
    for j in connected_components(Dual_G):
        sets.append(list(j))

    #make a dictionary of all feature ids and corresponding geometry
    D={}
    for f in temp_network.getFeatures():
        fid=f.attribute('feat_id_')
        #careful! you need AndOwnership otherwise you get a C++ error
        f_geom=f.geometryAndOwnership()
        D[fid]=f_geom

    #make a dictionary of sets of geometries to be combined and sets of ids to be combined
    Geom_sets={}
    for m in sets:
        Geom_sets[tuple(m)]=[]

    for k,v in Geom_sets.items():
        geoms=[]
        for i in k:
            #print i
            i_geom=D[i]
            #print i_geom
            geoms.append(i_geom)
        Geom_sets[k]=tuple(geoms)

    #make adjacency dictionary for nodes of Dual Graph (=edges)
    AdjD={}
    #returns an iterator of (node, adjacency dict) tuples for all nodes
    for (i, v) in Dual_G.adjacency_iter():
        AdjD[i]=v

    sets_in_order=[]
    for f in sets:
        ord_set=[]
        nodes_passed=[]
        if len(f)==2 or len(f)==1:
            ord_set=f
            sets_in_order.append(ord_set)
        else:
            for n in f:
                if len(AdjD[n])==1 or len(AdjD[n])>2:
                    first_line=n
                else:
                    pass
            ord_set=[]
            nodes_passed.append(first_line)
            ord_set.append(first_line)
            for n in ord_set:
                nodes=AdjD[n].keys()
                for node in nodes:
                    if node in nodes_passed:
                        pass
                    else:
                        nodes_passed.append(node)
                        ord_set.append(node)
            sets_in_order.append(ord_set)

    #make a dictionary of all feature ids and corresponding geometry
    A={}
    for f in temp_network.getFeatures():
        fid=f.attribute('feat_id_')
        A[fid]=f.attributes()

    #include in sets ord the geometry of the feature
    for s in sets_in_order:
        for indx,i in enumerate(s):
            ind=indx
            line=i
            s[indx]= [line,D[line]]

    #combine geometries
    New_geoms=[]
    for h in sets_in_order:
        new_geom=None
        if len(h)==1:
            new_geom=h[0][1]
            new_attr=A[h[0][0]]
        elif len(h)==2 :
            line1_geom=h[0][1]
            line2_geom=h[1][1]
            new_geom=line1_geom.combine(line2_geom)
            new_attr=A[h[0][0]]
        else:
            new_attr=A[h[0][0]]
            for i,line in enumerate(h):
                ind=i
                l=line
                if ind==(len(h)-1):
                    pass
                else:
                    l_geom=h[ind][1]
                    next_l=h[(ind+1)%len(h)]
                    next_l_geom=h[(ind+1)%len(h)][1]
                    new_geom=l_geom.combine(next_l_geom)
                    h[(ind+1)%len(h)][1]=new_geom
        #print new_geom
        New_geoms.append([new_geom,new_attr])

    #delete all features and recreate memory layer with new geometries
    temp_network.removeSelection()
    temp_network.startEditing()
    temp_network.selectAll()
    temp_network.deleteSelectedFeatures()
    temp_network.commitChanges()

    New_feat=[]
    #reconstruct new geometries
    for i in New_geoms:
        feature = QgsFeature()
        feature.setGeometry(i[0])
        feature.setAttributes(i[1])
        New_feat.append(feature)

    temp_network.startEditing()
    temp_network.addFeatures(New_feat)
    temp_network.commitChanges()
    temp_network.removeSelection()

    #break lines if they are Multilines
    feat_to_del=[]
    New_feat=[]
    for f in temp_network.getFeatures():
        f_geom_type = f.geometry().wkbType()
        f_id = f.id()
        attr=f.attributes()
        if f_geom_type == 5:
            new_geoms = f.geometry().asGeometryCollection()
            for i in new_geoms:
                new_feat = QgsFeature()
                new_feat.setGeometry(i)
                new_feat.setAttributes(attr)
                New_feat.append(new_feat)
            feat_to_del.append(f_id)

    temp_network.startEditing()
    temp_network.dataProvider().deleteFeatures(feat_to_del)
    temp_network.addFeatures(New_feat)
    temp_network.commitChanges()
    temp_network.removeSelection()

    return temp_network
Beispiel #55
0
def read_shp(path):
    """ Active development for shp read is now done in networkx. This function left in for compatibilty purposes.
    """
    return nx.read_shp(path)
Beispiel #56
0
 def test_missing_geometry(self):
     G = nx.read_shp(self.path)
Beispiel #57
0
def read_shp_to_graph(shp_path):
    graph_shp = nx.read_shp(str(shp_path), simplify=True)
    graph = nx.MultiGraph(graph_shp.to_undirected(reciprocal=False))
    return graph
# -*- coding: utf-8 -*-
import networkx as nx
import numpy as np
import json
from shapely.geometry import asLineString, asMultiPoint


def write_geojson(outfilename, indata):
    with open(outfilename, "w") as file_out:
        file_out.write(json.dumps(indata))

# use Networkx to load a Noded shapefile
# returns a graph where each node is a coordinate pair
# and the edge is the line connecting the two nodes

nx_load_shp = nx.read_shp("../geodata/shp/e01_network_lines_3857.shp")

# A graph is not always connected, so we take the largest connected subgraph
# by using the connected_component_subgraphs function.
nx_list_subgraph = list(nx.connected_component_subgraphs(nx_load_shp.to_undirected()))[0]


# get all the nodes in the network
nx_nodes = np.array(nx_list_subgraph.nodes())

# output the nodes to a GeoJSON file to view in QGIS
network_nodes = asMultiPoint(nx_nodes)
write_geojson("../geodata/ch08_final_netx_nodes.geojson", network_nodes.__geo_interface__ )


# this number represents the nodes position
Beispiel #59
0
import sys
import networkx as nx
import matplotlib.pyplot as plt
import math
import random
from heapq import heappop, heappush



G =  nx.DiGraph.to_undirected(nx.read_shp('./aa_scratch0/axons_larissa_with_buildings6_sub0.shp'))
for x in G:
    print (x)

print sys.argv[1] # first parameter