本文整理汇总了Python中networkx.Graph.neighbors方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.neighbors方法的具体用法?Python Graph.neighbors怎么用?Python Graph.neighbors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.Graph
的用法示例。
在下文中一共展示了Graph.neighbors方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: polygon_skeleton
# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import neighbors [as 别名]
def polygon_skeleton(polygon, density=10):
""" Given a buffer polygon, return a skeleton graph.
"""
skeleton = Graph()
points = []
for ring in polygon_rings(polygon):
points.extend(densify_line(list(ring.coords), density))
if len(points) <= 4:
# don't bother with this one
return skeleton
print >> stderr, ' ', len(points), 'perimeter points',
rbox = '\n'.join( ['2', str(len(points))] + ['%.2f %.2f' % (x, y) for (x, y) in points] + [''] )
qvoronoi = Popen('qvoronoi o'.split(), stdin=PIPE, stdout=PIPE)
output, error = qvoronoi.communicate(rbox)
voronoi_lines = output.splitlines()
if qvoronoi.returncode:
raise _QHullFailure('Failed with code %s' % qvoronoi.returncode)
vert_count, poly_count = map(int, voronoi_lines[1].split()[:2])
for (index, line) in enumerate(voronoi_lines[2:2+vert_count]):
point = Point(*map(float, line.split()[:2]))
if point.within(polygon):
skeleton.add_node(index, dict(point=point))
for line in voronoi_lines[2+vert_count:2+vert_count+poly_count]:
indexes = map(int, line.split()[1:])
for (v, w) in zip(indexes, indexes[1:] + indexes[:1]):
if v not in skeleton.node or w not in skeleton.node:
continue
v1, v2 = skeleton.node[v]['point'], skeleton.node[w]['point']
line = LineString([(v1.x, v1.y), (v2.x, v2.y)])
if line.within(polygon):
skeleton.add_edge(v, w, dict(line=line, length=line.length))
removing = True
while removing:
removing = False
for index in skeleton.nodes():
if skeleton.degree(index) == 1:
depth = skeleton.node[index].get('depth', 0)
if depth < 20:
other = skeleton.neighbors(index)[0]
skeleton.node[other]['depth'] = depth + skeleton.edge[index][other]['line'].length
skeleton.remove_node(index)
removing = True
print >> stderr, 'contain', len(skeleton.edge), 'internal edges.'
return skeleton
示例2: polygon_dots_skeleton
# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import neighbors [as 别名]
def polygon_dots_skeleton(polygon, points):
'''
'''
skeleton = Graph()
rbox = '\n'.join( ['2', str(len(points))] + ['%.2f %.2f' % (x, y) for (x, y) in points] + [''] )
qvoronoi = Popen('qvoronoi o'.split(), stdin=PIPE, stdout=PIPE)
output, error = qvoronoi.communicate(rbox)
voronoi_lines = output.splitlines()
if qvoronoi.returncode:
raise _QHullFailure('Failed with code %s' % qvoronoi.returncode)
vert_count, poly_count = map(int, voronoi_lines[1].split()[:2])
for (index, line) in enumerate(voronoi_lines[2:2+vert_count]):
point = Point(*map(float, line.split()[:2]))
if point.within(polygon):
skeleton.add_node(index, dict(point=point))
for line in voronoi_lines[2+vert_count:2+vert_count+poly_count]:
indexes = map(int, line.split()[1:])
for (v, w) in zip(indexes, indexes[1:] + indexes[:1]):
if v not in skeleton.node or w not in skeleton.node:
continue
v1, v2 = skeleton.node[v]['point'], skeleton.node[w]['point']
line = LineString([(v1.x, v1.y), (v2.x, v2.y)])
if line.within(polygon):
skeleton.add_edge(v, w, dict(line=line, length=line.length))
removing = True
while removing:
removing = False
for index in skeleton.nodes():
if skeleton.degree(index) == 1:
depth = skeleton.node[index].get('depth', 0)
if depth < 20:
other = skeleton.neighbors(index)[0]
skeleton.node[other]['depth'] = depth + skeleton.edge[index][other]['line'].length
skeleton.remove_node(index)
removing = True
logging.debug('found %d skeleton edges' % len(skeleton.edge))
return skeleton