只是一个更新。以下Whuber的意见后,我发现,生成空间权重矩阵简单地使用Python循环和字典来确定邻居。我复制了以下过程。
第一部分遍历每个块组的每个顶点。它创建一个以顶点坐标为键的字典,并创建一个以该坐标为顶点的块组ID列表。请注意,这需要拓扑整齐的数据集,因为只有完美的顶点/顶点重叠才会注册为邻居关系。幸运的是,人口普查局的TIGER块组shapefile在这方面是可以的。
第二部分再次遍历每个块组的每个顶点。它创建一个块组的ID作为键和块组的邻居的ID作为值的字典。
# Create dictionary of vertex coordinate : [...,IDs,...]
BlockGroupVertexDictionary = {}
BlockGroupCursor = arcpy.SearchCursor(BlockGroups.shp)
BlockGroupDescription = arcpy.Describe(BlockGroups.shp)
BlockGroupShapeFieldName = BlockGroupsDescription.ShapeFieldName
#For every block group...
for BlockGroupItem in BlockGroupCursor :
BlockGroupID = BlockGroupItem.getValue("BKGPIDFP00")
BlockGroupFeature = BlockGroupItem.getValue(BlockGroupShapeFieldName)
for BlockGroupPart in BlockGroupFeature:
#For every vertex...
for BlockGroupPoint in BlockGroupPart:
#If it exists (and isnt empty interior hole signifier)...
if BlockGroupPoint:
#Create string version of coordinate
PointText = str(BlockGroupPoint.X)+str(BlockGroupPoint.Y)
#If coordinate is already in dictionary, append this BG's ID
if PointText in BlockGroupVertexDictionary:
BlockGroupVertexDictionary[PointText].append(BlockGroupID)
#If coordinate is not already in dictionary, create new list with this BG's ID
else:
BlockGroupVertexDictionary[PointText] = [BlockGroupID]
del BlockGroupItem
del BlockGroupCursor
#Create dictionary of ID : [...,neighbors,...]
BlockGroupNeighborDictionary = {}
BlockGroupCursor = arcpy.SearchCursor(BlockGroups.shp)
BlockGroupDescription = arcpy.Describe(BlockGroups.shp)
BlockGroupShapeFieldName = BlockGroupDescription.ShapeFieldName
#For every block group
for BlockGroupItem in BlockGroupCursor:
ListOfBlockGroupNeighbors = []
BlockGroupID = BlockGroupItem.getValue("BKGPIDFP00")
BlockGroupFeature = BlockGroupItem.getValue(BlockGroupShapeFieldName)
for BlockGroupPart in BlockGroupFeature:
#For every vertex
for BlockGroupPoint in BlockGroupPart:
#If it exists (and isnt interior hole signifier)...
if BlockGroupPoint:
#Create string version of coordinate
PointText = str(BlockGroupPoint.X)+str(BlockGroupPoint.Y)
if PointText in BlockGroupVertexDictionary:
#Get list of block groups that have this point as a vertex
NeighborIDList = BlockGroupVertexDictionary[PointText]
for NeighborID in NeighborIDList:
#Don't add if this BG already in list of neighbors
if NeighborID in ListOfBGNeighbors:
pass
#Add to list of neighbors (as long as its not itself)
elif NeighborID != BlockGroupID:
ListOfBGNeighbors.append(NeighborID)
#Store list of neighbors in blockgroup object in dictionary
BlockGroupNeighborDictionary[BlockGroupID] = ListOfBGNeighbors
del BlockGroupItem
del BlockGroupCursor
del BlockGroupVertexDictionary
事后看来,我意识到第二部分可以使用其他方法,而无需再次遍历shapefile。但这就是我使用的方法,即使一次用于数千个块组,它也能很好地工作。我还没有尝试过与整个美国做它,但它可以执行对整个状态。