top of page

Grasshopper Scripts for Network Connectivity

Feb, 2017

Network Connectivity is a method that use Graph Theory to solve spatial problems. I developed a series of Grasshopper tools to calculate Network Connectivity Measures. These measures can be used to find closest routes, most central nodes, most connected routes, least cost path, etc. Since all tools were written as Python language, they are highly customizable and can extend to many other usages. 

job_portfolio-10.jpg

import rhinoscriptsyntax as rs
import ghpythonlib.components as gh

for l in nt_lines:
    #get the start and end point index from nt_pts
    st=rs.CurveStartPoint(l)
    st_index= rs.PointArrayClosestPoint(nt_pts,st)
    ed=rs.CurveEndPoint(l)
    ed_index=rs.PointArrayClosestPoint(nt_pts,ed)
    #add edge based on start/end pts
    G.add_edge(st_index,ed_index)
    #build a library, with a road as key; the start and end index are the values.
    nt_lib[l]=[st_index,ed_index]
    #nt_line[(st_index,ed_index)]=l
    #G[st_index][ed_index]['weight'] = rs.CurveLength(l)

def get_weight(nt_l):
    length=rs.CurveLength(nt_l)
    return length
    
c= list(nt_lib[nt_lines[9]])
for i in nt_lines:
    indexs=list(nt_lib[i])
    st_ind=indexs[0]
    #print indexs
    #print rs.CurveLength(i)
    ed_ind=indexs[1]
    #print ed_ind
    G[st_ind][ed_ind]['weight'] = get_weight(i)
   
    

# convert the input vertex indices and convert them to topology indices
from_v = start_end[0]
to_v = start_end[1]

def get_shortest_path(from_v,to_v):
    topology_list = nx.shortest_path(G, from_v, to_v, True)
    print topology_list
    p_list=[]
    for i in range(1,len(topology_list)):
        try:
            road=nt_lib.keys()[nt_lib.values().index([topology_list[i-1],topology_list[i]])]
        except:
            road=nt_lib.keys()[nt_lib.values().index([topology_list[i],topology_list[i-1]])]
            pass
        p_list.append(road)
    return p_list

a=get_shortest_path(from_v,to_v)


import itertools as it
vet_combinations= list(it.combinations(multi_pts,2))

path_list=[]
for i in vet_combinations:
    from_i = i[0]
    to_i = i[1]
    path_list.append(get_shortest_path(from_i,to_i))

b=path_list
print b

bottom of page