Example #1
0
def lis2graph(layer):
    """Maakt Graph met LIS-netwerk en bepaalt onderbemalingen.
       Vult [ONTV_VAN] en [X_OBEMAL].
       Gebruikt [LOOST_OP] en [VAN_KNOOPN] als edge (relation) en VAN_KNOOPN als node"""
    # graph aanmaken
    graph = Graph()
    graph_rev = Graph()
    d_K_ONTV_VAN = {}  # alle onderliggende gemalen
    d_K_ONTV_VAN_n1 = {
    }  # alle onderliggende gemalen op 1 niveau diep ivm optellen overcapaciteit

    print_log("netwerk opslaan als graph...", "i")
    for feature in layer.getFeatures():  # .getFeatures()
        VAN_KNOOPN = feature["VAN_KNOOPN"]
        LOOST_OP = feature["K_LOOST_OP"]
        graph.add_node(VAN_KNOOPN)
        graph_rev.add_node(VAN_KNOOPN)
        if LOOST_OP != None:
            graph.add_edge(VAN_KNOOPN, LOOST_OP,
                           1)  # richting behouden voor bovenliggende gebied
            graph_rev.add_edge(
                LOOST_OP, VAN_KNOOPN,
                1)  # richting omdraaien voor onderliggende gebied
    edges_as_tuple = list(
        graph.distances)  # lijst met tuples: [('A', 'B'), ('C', 'B')]

    print_log("onderbemaling bepalen voor rioolgemalen en zuiveringen...", "i")
    where_clause = "Join_Count > 0"
    layer.startEditing()
    for i, feature in enumerate(layer.getFeatures()):  # .getFeatures()
        if not feature["count"] >= 1: continue
        VAN_KNOOPN = feature["VAN_KNOOPN"]
        nodes = dijkstra(graph, VAN_KNOOPN)[0]
        print_log("nodes for {}: {}".format(VAN_KNOOPN, nodes), 'd')
        K_KNP_EIND, X_OPPOMP = [(key, value) for key, value in sorted(
            nodes.iteritems(), key=lambda (k, v): (v, k))][-1]
        print_log(
            "endnode for {}: {},{}".format(VAN_KNOOPN, K_KNP_EIND, X_OPPOMP),
            'd')
        d_edges = dijkstra(graph_rev,
                           VAN_KNOOPN)[1]  # {'B': 'A', 'C': 'B', 'D': 'C'}
        l_onderliggende_gemalen = str(list(d_edges))  # [u'ZRE-123',u'ZRE-234']
        l_onderliggende_gemalen = l_onderliggende_gemalen.replace(
            "u'", "'").replace("[", "").replace("]", "")
        layer.changeAttributeValue(
            feature.id(), layer.fieldNameIndex("K_ONTV_VAN"),
            l_onderliggende_gemalen)  # K_ONTV_VAN = 'ZRE-1','ZRE-2'
        layer.changeAttributeValue(
            feature.id(), layer.fieldNameIndex("X_OBEMAL"),
            len(list(d_edges)))  # X_OBEMAL = 2 (aantal onderbemalingen)
        layer.changeAttributeValue(
            feature.id(), layer.fieldNameIndex("X_OPPOMP"), X_OPPOMP + 1
        )  # X_OPPOMP = 1 (aantal keer oppompen tot rwzi) met shortestPath ('RWZI','ZRE-4')
        layer.changeAttributeValue(
            feature.id(), layer.fieldNameIndex("K_KNP_EIND"), K_KNP_EIND
        )  # eindbemalingsgebied / overnamepunt. bepaald uit netwerk.
        d_K_ONTV_VAN[VAN_KNOOPN] = l_onderliggende_gemalen

        # onderbemalingen 1 niveau diep
        l_onderliggende_gemalen_n1 = [
            start for start, end in edges_as_tuple if end == VAN_KNOOPN
        ]  # dus start['A', 'C'] uit tuples[('A', 'B'),('C', 'B')] als end == 'B'
        d_K_ONTV_VAN_n1[VAN_KNOOPN] = str(l_onderliggende_gemalen_n1).replace(
            "u'", "'").replace("[", "").replace(
                "]", "")  # naar str() en verwijder u'tjes en haken
    layer.commitChanges()

    return [d_K_ONTV_VAN, d_K_ONTV_VAN_n1]
def getGraphe(jsonmap):

    print("in getGraphe")

    themap = jsonmap
    #pprint(themap)
    pprint("---------------------")
    # pprint(themap['areas'])
    # for area in themap['areas']:
    #    pprint("---------------------")
    #    pprint(area)
    for map in themap['areas']:
        for point in (map['map']['vertices']):
            verticeList.append(
                [map['name'] + '.' + point['name'], point['x'], point['y']])
        for arc in (map['map']['streets']):
            for point in verticeList:
                if point[0] == (map['name'] + '.' + arc['path'][0]):
                    x1 = point[1]
                    y1 = point[2]
                if point[0] == (map['name'] + '.' + arc['path'][1]):
                    x2 = point[1]
                    y2 = point[2]
            edgeList.append([
                unicode(map['name'] + '.' + arc['path'][0]),
                unicode(map['name'] + '.' + arc['path'][1]),
                math.sqrt(math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2))
            ])
            #print([map['name'] + '.' + arc['path'][0], map['name'] + '.' + arc['path'][1],
            #       math.sqrt(math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2))])

        for arc in (map['map']['bridges']):
            for point in verticeList:
                if point[0] == (map['name'] + '.' + arc['from']):
                    x1 = point[1]
                    y1 = point[2]
                if point[0] == (arc['to']['area'] + '.' + arc['to']['vertex']):
                    x2 = point[1]
                    y2 = point[2]
            edgeList.append([
                unicode(map['name'] + '.' + arc['from']),
                unicode(arc['to']['area'] + '.' + arc['to']['vertex']),
                arc['weight']
            ])
            #print([map['name'] + '.' + arc['from'], arc['to']['area'] + '.' + arc['to']['vertex'], arc['weight']])

    g = Graph()

    print("vertices")
    for vertex in verticeList:

        print(vertex)

        g.add_vertex(vertex[0])

        pass

    print("edges")
    for edge in edgeList:

        print(edge)

        g.add_edge(edge[0], edge[1], edge[2])

        pass

    return g
def getGraphe(jsonmap):

    print("in getGraphe")

    themap = jsonmap
    #pprint(themap)
    pprint("---------------------")
    # pprint(themap['areas'])
    # for area in themap['areas']:
    #    pprint("---------------------")
    #    pprint(area)
    for map in themap['areas']:
        for point in (map['map']['vertices']):
            verticeList.append([map['name'] + '.' + point['name'], point['x'], point['y']])
        for arc in (map['map']['streets']):
            for point in verticeList:
                if point[0] == (map['name'] + '.' + arc['path'][0]):
                    x1 = point[1]
                    y1 = point[2]
                if point[0] == (map['name'] + '.' + arc['path'][1]):
                    x2 = point[1]
                    y2 = point[2]
            edgeList.append([unicode(map['name'] + '.' + arc['path'][0]),
                             unicode(map['name'] + '.' + arc['path'][1]),
                             math.sqrt(math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2))])
            #print([map['name'] + '.' + arc['path'][0], map['name'] + '.' + arc['path'][1],
            #       math.sqrt(math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2))])

        for arc in (map['map']['bridges']):
            for point in verticeList:
                if point[0] == (map['name'] + '.' + arc['from']):
                    x1 = point[1]
                    y1 = point[2]
                if point[0] == (arc['to']['area'] + '.' + arc['to']['vertex']):
                    x2 = point[1]
                    y2 = point[2]
            edgeList.append(
                [unicode(map['name'] + '.' + arc['from']),
                 unicode(arc['to']['area'] + '.' + arc['to']['vertex']),
                 arc['weight']])
            #print([map['name'] + '.' + arc['from'], arc['to']['area'] + '.' + arc['to']['vertex'], arc['weight']])


    g = Graph()

    print("vertices")
    for vertex in verticeList:

        print (vertex)

        g.add_vertex(vertex[0])

        pass

    print("edges")
    for edge in edgeList:

        print (edge)

        g.add_edge(edge[0], edge[1], edge[2])

        pass

    return g