# setup geoprocessor GP = cm.gp_init() # G: = \\nicknet.env.duke.edu\network\research\lel\GraphGroup # G:\chapel_hill\network\full_lc\network_lc.txt # easy_install -U networkx # got networkx-0.35.1 # E:\code\connmod\branches\ch\data\net2010\network_euc_na.txt # get arguments in_network_txt = sys.argv[1] # initialize logging log = '%s/log_centrality.txt' % os.path.dirname(in_network_txt) cm.log_init(log) cm.log('Reading network configuration') F = cm.read_network(in_network_txt) # read in full network nodes_shp = F.config[('shapefile', 'nodes')] lcpaths_txt = F.config[('leastcostpaths', 'txt')] G = NX.Graph() # create fresh graph cm.log('Reading least-cost paths into fresh graph with only centroids') rdr = csv.reader(open(lcpaths_txt, 'r')) headers = rdr.next() for row in rdr: c1, c2, w = row[0:3] G.add_edge(int(c1), int(c2), weight=int(w)) cm.log( 'Calculating centrality metrics (only works with NetworkX 0.35.1 and higher)' )
# E:\code\connmod\branches\ch\data\net2010\network_trunc_lc.txt "Feature Set" "2010\truncated\edges - trunc lc" # E:\code\connmod\branches\ch\data\net2010c\network_trunc_lc.txt "2010\truncated\edges - trunc lc" import sys, string, os, arcgisscripting, csv, networkx as NX, cm # Create the Geoprocessor object GP = cm.gp_init() log = '%s/log.txt' % os.path.dirname(sys.argv[0]) cm.log_init(log, 'debug') # Script arguments... network_txt = sys.argv[1] edges_lyr = sys.argv[2] # read in network cm.log('Reading input network') G = cm.read_network(network_txt) edges_shp = G.config[('shapefile', 'edges')] lc_txt = G.config[('leastcostpaths', 'txt')] ## debug ##edges_lyr = 'edges_lyr'; GP.MakeFeatureLayer(edges_shp, edges_lyr) ##GP.SelectLayerByAttribute_management(edges_lyr, "NEW_SELECTION", "\"EdgeID\" IN (6693)") # get selected edges cur = GP.SearchCursor(edges_lyr) row = cur.Next() e_selected = [] while row: e_selected.append(row.GetValue('EdgeID')) row = cur.Next()
# network derivative data files network = os.path.splitext(network_txt)[0] network_edgelist = network + '_edgelist.txt' network_nodeattr = network + '_nodeattr.csv' network_edgeattr = network + '_edgeattr.csv' # initialize output folder GP.CreateFolder_management(os.path.dirname(wd), os.path.basename(wd)) # logging log = '%s/log.txt' % wd cm.log_init(log, 'debug') # NetworkX version report and check cm.log('Using NetworkX %s' % NX.__version__) if NX.__version__ < '1.0': cm.log('MUST FIX: Need NetworkX version 1.0 or higher', type='error') try: cm.log('Creating folder and geodatabase') if not GP.Exists(gdb): GP.CreateFileGDB_management(os.path.dirname(gdb), os.path.basename(gdb)) GP.workspace = wd cm.log('Copying patches') GP.CopyFeatures(patches_in, patches) flds = GP.ListFields(patches) if 'PATCHID' in [f.Name.upper() for f in flds]: cm.log('The PatchID field name is reserved. Please remove this field from the input patches.', type='error') GP.AddField(patches, 'PatchID', 'LONG')
# setup geoprocessor GP = cm.gp_init() # Get arguments: in_network_txt = sys.argv[1] # the textfile that locates all the graph stuff wt_min = sys.argv[2] # minimum edge weight wt_max = sys.argv[3] # max weight wt_inc = sys.argv[4] # increment for thresholding sequence (loop) out_csv = sys.argv[5] # output CSV file (generates a plot, not an Arc object) # initialize logging log = '%s/log_thresholding.txt' % os.path.dirname(in_network_txt) cm.log_init(log) cm.log('Reading network configuration') F = cm.read_network(in_network_txt) # read in full network #nodes_shp = F.config[('shapefile', 'nodes')] lcpaths_txt = F.config[('leastcostpaths', 'txt')] G = NX.XGraph() # create fresh graph cm.log('Reading least-cost paths into fresh graph with only centroids') rdr = csv.reader(open(lcpaths_txt,'r')) headers = rdr.next() for row in rdr: c1,c2,w = row[0:3] G.add_edge(int(c1), int(c2), int(w)) # Process and write output: cm.log('Creating sequence of thresholded networks')
import sys, string, os, arcgisscripting import networkx as NX import cm # Create the Geoprocessor object GP = cm.gp_init() log = '%s/log.txt' % os.path.dirname(sys.argv[0]) cm.log_init(log, 'debug') # Script arguments... network_txt = sys.argv[1] pts_inx = sys.argv[2] edges_lyr = sys.argv[3] # read in network cm.log('Reading input network') G = cm.read_network(network_txt) nodes_shp = G.config[('shapefile', 'nodes')] edges_shp = G.config[('shapefile', 'edges')] # Local variables... centroids_near_pts = 'in_memory\\centroids_near_pts' ##centroids_near_pts = "E:\\code\\connmod\\branches\\ch\\data\\net2010\\centroids_near.dbf" if GP.exists(centroids_near_pts): GP.delete(centroids_near_pts) #debug #GP.CopyFeatures(pts_inx, r'C:\temp\connectivity\network_cost\geodb.gdb\pts_inx') GP.MakeFeatureLayer(nodes_shp, 'nodes_lyr') GP.SelectLayerByAttribute_management('nodes_lyr', 'NEW_SELECTION', "\"NodeType\" = 'centroid'")
# arguments in in_network_txt = sys.argv[1] out_network_fld = sys.argv[2] out_network_txt = sys.argv[3] # setup vars for out_network config out_network = os.path.splitext(out_network_txt)[0] out_edgelist = out_network + '_edgelist.txt' out_nodeattr = out_network + '_nodeattr.csv' out_edgeattr = out_network + '_edgeattr.csv' # logging log = '%s/log.txt' % os.path.dirname(out_network) cm.log_init(log, 'debug') cm.log('Reading input network') G = cm.read_network(in_network_txt) # PROBLEM: patches sharing same node, ie edge #Identity_analysis full\nodes patches D:\code\connmod\branches\ch\data\network\full_2\nodes_identity.shp ALL "" KEEP_RELATIONSHIPS cm.log('Removing existing internal nodes (%d) and edges (%d)' % (len(G.nbytype['internal']), len(G.ebytype['internal']))) H = G.copy() H.delete_edges_from(G.ebytype['internal']) H.delete_nodes_from(G.nbytype['internal']) cm.copy_g_attr(G, H) cm.log('Adding edges from centroids (%d) to perimeter nodes (%d)' % (len(G.nbytype['centroid']), len(G.nbytype['perimeter']))) centroids = G.nbytype['centroid']
else: costthreshold = int(sys.argv[2].split()[0]) out_network_fld = sys.argv[3] out_network_txt = sys.argv[4] # setup vars for out_network config out_network = os.path.splitext(out_network_txt)[0] out_edgelist = out_network + '_edgelist.txt' out_nodeattr = out_network + '_nodeattr.csv' out_edgeattr = out_network + '_edgeattr.csv' # logging log = '%s/log.txt' % os.path.dirname(out_network) cm.log_init(log, 'debug') cm.log('Reading input network') G = cm.read_network(in_network_txt) cm.log('Trimming graph based on threshold cumulative cost %g' % costthreshold) H = G.copy() nlist = [] for n, z in G.nz.iteritems(): if z >= costthreshold: H.remove_edges_from(G.edges(n)) H.remove_node(n) edges = H.edges(data=True) for (u, v, w) in edges: if w['weight'] >= costthreshold: H.remove_edge(u, v) cm.log('Trimming isolated nodes with 1 or 0 neighbors')
2] == '#': # if no linear unit specified in optional eucthreshold parameter eucthreshold = '' else: eucthreshold = int(sys.argv[2].split()[0]) out_network = os.path.splitext(out_network_txt)[0] out_lcpaths = out_network + '_lcpaths.txt' out_edgelist = out_network + '_edgelist.txt' out_nodeattr = out_network + '_nodeattr.csv' out_edgeattr = out_network + '_edgeattr.csv' paths_tbl = '%s/geodb.gdb/paths_%s' % (os.path.dirname(in_network_txt), net) log = '%s/log_%s.txt' % (os.path.dirname(wd), net) cm.log_init(log) cm.log('Reading in network') G = cm.read_network(in_network_txt) cm.log('Converting network edge weights to integer') for (u, v, d) in G.edges_iter(data=True): G.remove_edge(u, v) G.add_edge(u, v, weight=int(d['weight'])) H = NX.Graph() # setup empty subgraph to populate with edges cm.log( 'Building centroid-to-centroid paths restricted by component using bidirectional_dijkstra' ) Cs = NX.connected_component_subgraphs(G) nC = len(Cs) paths = {}