Skip to content

Instantly share code, notes, and snippets.

@bwreilly
Created February 7, 2012 23:27
Show Gist options
  • Save bwreilly/1762958 to your computer and use it in GitHub Desktop.
Save bwreilly/1762958 to your computer and use it in GitHub Desktop.
nx_spatial, networkx for trivial spatial graph processing
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])
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