Created
February 7, 2012 23:27
-
-
Save bwreilly/1762958 to your computer and use it in GitHub Desktop.
nx_spatial, networkx for trivial spatial graph processing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import arcgisscripting as arc | |
import csv | |
import networkx as nx | |
import nx_spatial as ns | |
gp = arc.create(9.3) | |
features = [ | |
"ServicePoint", | |
"Transformer", | |
"SecUGElectricLineSegment", | |
"SecOHElectricLineSegment" | |
] | |
outcsv = "txservice_relate.csv" | |
# add the fcs to networkx network | |
G = ns.read_fc(workingdir, gp) | |
tx = ns.attr_find(G, FcName="Transformer") | |
servicepoints = ns.attr_find(G, FcName="ServicePoint") | |
# output as csv <accountnumber>, <gridcode> | |
writer = csv.writer(open(outcsv, "wb")) | |
#break up net into connected components | |
components = nx.connected_components(G.to_undirected()) | |
writer.writerow(["sp_acc", "tx_id"]) | |
for c in components: | |
tl = filter(lambda n: n in tx, c) | |
if tl: | |
t = tl[0] | |
for sp in filter(lambda n: n in servicepoints, c): | |
tx_id = G.node[t]['FacilityID'] or "" | |
sp_acc = G.node[sp]['AccountNum'] or "" | |
writer.writerow([sp_acc, tx_id]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import networkx as nx | |
import nx_spatial as ns | |
features = [ | |
"ServicePoint", | |
"Transformer", | |
"SecUGElectricLineSegment", | |
"SecOHElectricLineSegment" | |
] | |
outcsv = "txservice_relate.csv" | |
# add the shapes to networkx network | |
G = ns.read_shp(workingdir) | |
tx = ns.attr_find(G, Name="Transformer") | |
servicepoints = ns.attr_find(G, Name="ServicePoint") | |
# output as csv <accountnumber>, <gridcode> | |
writer = csv.writer(open(outcsv, "wb")) | |
# break up net into connected components | |
components = nx.connected_components(G.to_undirected()) | |
writer.writerow(["sp_acc", "tx_id"]) | |
for c in components: | |
tl = filter(lambda n: n in tx, c) | |
if tl: | |
t = tl[0] | |
for sp in filter(lambda n: n in servicepoints, c): | |
tx_id = G.node[t]['FacilityID'] or "" | |
sp_acc = G.node[sp]['AccountNum'] or "" | |
writer.writerow([sp_acc, tx_id]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment