我想识别网络边缘的第K个邻居,特别是大量街道的邻居。例如,我有一条我有兴趣看的街道,称之为焦点街道。对于每个焦点街道,我想找到共享交叉点的街道,这些是第一个邻居。然后,对于那些与焦点街道共享交叉点的街道,我想找到他们的邻居(这些将是二阶邻居),等等......
使用ArcGIS的地理处理库(arcpy)计算一阶邻居需要6个多小时,二阶邻居需要18个多小时。不用说我想找到一个更有效的解决方案。我创建了一个python字典,它键在每条街道上,并包含连接的街道作为值。例如:
st2neighs = {street1: [street2, street3, street5], street2: [street1, street4], ...}.
街道1连接到街道2,3,5;街道2连接到街道1和4;研究区内约有30,000条街道,大多数街道连接数少于7条。 以下代码中使用的数据的腌制版本IS HERE 。
我认为知道一阶邻居会让我有效地追踪更高阶的邻居。但是以下代码提供的结果不正确:
##Select K-order neighbors from a set of sampled streets.
##saves in dictionary format such that
##the key is the sampled street and the neighboring streets are the values
##################
##IMPORT LIBRARIES
##################
import random as random
import pickle
#######################
##LOAD PICKLED DATA
#######################
seg_file = open("seg2st.pkl", "rb")
st_file = open("st2neighs.pkl", "rb")
seg2st = pickle.load(seg_file)
st2neigh = pickle.load(st_file)
##################
##DEF FUNCTIONS
##################
##Takes in a dict of segments (key) and their streets (values).
##returns the desired number of sampled streets per segment
##returns a dict keyed segment containing tlids.
def selectSample(seg2st, nbirths):
randSt = {}
for segK in seg2st.iterkeys():
ranSamp = [int(random.choice(seg2st[segK])) for i in xrange(nbirths)]
randSt[segK] = []
for aSamp in ranSamp:
randSt[segK].append(aSamp)
return randSt
##Takes in a list of all streets (keys) and their first order neighbors (values)
##Takes in a list of sampled streets
##returns a dict of all sampled streets and their neighbors.
##Higher order selections should be possible with findMoreNeighbors
##logic is the same but replacing sample (input) with output from
##findFirstNeighbors
def findFirstNeighbors(st2neigh, sample):
compSts = {}
for samp in sample.iterkeys():
for rSt in sample[samp]:
if rSt not in compSts:
compSts[rSt] = []
for compSt in st2neigh[rSt]:
compSts[rSt].append(compSt)
return compSts
def findMoreNeighbors(st2neigh, compSts):
for aSt in compSts:
for st in compSts[aSt]:
for nSt in st2neigh[st]:
if nSt not in compSts[aSt]:
compSts[aSt].append(nSt)
moreNeighs = compSts
return moreNeighs
#####################
##The nHoods
#####################
samp = selectSample(seg2st, 1)
n1 = findFirstNeighbors(st2neigh, samp)
n2 = findMoreNeighbors(st2neigh, n1)
n3 = findMoreNeighbors(st2neigh, n2)
#####################
##CHECK RESULTS
#####################
def checkResults(neighList):
cntr = {}
for c in neighList.iterkeys():
cntr[c] = 0
for a in neighList[c]:
cntr[c] += 1
return cntr
##There is an error no streets **should** have 2000+ order neighbors
c1 = checkResults(n1)
c2 = checkResults(n2)
c3 = checkResults(n3)
帮助!
答案 0 :(得分:1)
在我看来,您想要实现的内容如下:http://en.wikipedia.org/wiki/Composition_of_relations
它实际上是一种直接的算法。设R是关系“是一阶邻居”,因此如果两个街道x,y在R中,那么x是y的一阶邻居。所以,对于二阶邻居,你要计算用R组成的R。对于三阶邻居(R组成R)组成R等等。