-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#ponti_new_tmp.py#
85 lines (75 loc) · 2.82 KB
/
#ponti_new_tmp.py#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# %% codecell
# Import all packages
# una libreria per leggere gli shapefile --> info qui: http://geopandas.org/index.html
import geopandas as gpd
# per i grafici
import matplotlib.pyplot as plt
# per il debug
import pdb
# per le cartelle
import os
import sys
sys.path.append("/home/lucatastrophe/Desktop/venessia/Venessia4Working/lib/")
#test per il push
import networkx as nt
from networkx.exception import NetworkXNoPath
import numpy as np
#libreria per leggere gli attributi degli edge
import json
#libreria per convertire da Json a shapely
from shapely.geometry import mapping, shape
#utility per coordinates
from library_coords import civico2coord
# %% codecell
folder = os.getcwd()
ponti = gpd.read_file(folder+"/pontiDivisi_completo/pontiDivisi_solo_venezia_l.shp")
G = nt.read_shp(/pontiDivisi_solo_venezia_l.shp")
# se vogliamo accedere ai nodi con degli indici
#G_2 = nt.convert_node_labels_to_integers(G)
# per renderlo bidirezionale
G_un = G.to_undirected()
G_list = list(G_un.nodes)
# %% codecell
# Plot the shape file
plt.ion()
ponti.plot()
# %% codecell
# crea un dizionario con la corrispondenza nodo-coordinata (se uso grafo senza indici il nodo è identificato proprio dalla coordinata. Viene fuori coordinata:coordinata
pos = dict(zip([v for v in G_un.nodes()], [f for f in G.nodes()]))
#nt.draw_networkx(G_un, pos, node_size = 40, node_color = "y", edge_color = "y", font_size = 8)
# %% codecell
# Read file civico
civico = gpd.read_file(folder + "/pontiDivisi_completo/civico_new.shp")
# %% codecell
starting_address = input('Da dove parti?\n')
coord = civico2coord(G_list, starting_address, civico)
ending_address = input('Dove vai?\n')
coord2 = civico2coord(G_list, ending_address, civico)
# %% codecell
try:
# Dijkstra algorithm, funzione peso lunghezza
#G_un[coord]
path = nt.algorithms.shortest_paths.weighted.dijkstra_path(G_un,coord,coord2, weight="length")
# lista dei nodi attraversati
path_nodes = [n for n in path]
plot_shortest_path(path_nodes,ponti)
plt.xlim(min(coord[0], coord2[0]) - 500, max(coord[0], coord2[0]) + 500)
plt.ylim(min(coord[1], coord2[1]) - 500, max(coord[1], coord2[1]) + 500)
except NetworkXNoPath:
print("Non esiste un percorso tra i due nodi")
ponti.plot()
# %% codecell
# Dijkstra algorithm, funzione peso ponti
try:
length_path, path_nobridges = nt.algorithms.shortest_paths.weighted.single_source_dijkstra(G_un, coord,coord2, weight = weight_bridge)
# lista dei nodi attraversati
path_nodes_nobridges = [n for n in path_nobridges]
plot_shortest_path(path_nodes_nobridges,ponti)
plt.xlim(min(coord[0], coord2[0]) - 500, max(coord[0], coord2[0]) + 500)
plt.ylim(min(coord[1], coord2[1]) - 500, max(coord[1], coord2[1]) + 500)
#print(length_path)
except NetworkXNoPath:
print("Non esiste un percorso tra i due nodi")
ponti.plot()
#coord
#coord2