gpt4 book ai didi

python - 必须指向二维图(x,y)坐标networkx python

转载 作者:行者123 更新时间:2023-11-28 23:01:42 26 4
gpt4 key购买 nike

我从空间数据库中获取信息,这些值类似于线字符串(空间)格式,我需要将此信息添加到 networkx 图形中,稍后需要在 matplot 库中绘制图形我写了这段代码

cursor.execute("SELECT AsText(roadstring) FROM road1")
for row in cursor.fetchall():
a=row[0][12:-2]
a=str(a)
a=a.split(",")
for i in a:
i=i.split(" ")
i[0]=float(i[0])
i[1]=float(i[1])
weig=abs(i[0]-i[1])
G.add_node((i[0],i[1]))

我无法了解如何为道路 (x1,y1) 和 (x2,y2) 添加二维边,即使我需要为这些边添加权重以获得它们之间的距离有什么建议吗??

The line string of every road are like these 643715.202,2499149.0506 643752.61523545,2499089.86084203 643773.6038,2499056.6558 643773.73878609,2499056.44011079 643793.20162482,2499025.34111554 643813.55943268,2498992.81212045 643826.6563,2498971.8852

我收到这个错误我安装了 matplotlib 我尝试通过复制你的代码

Traceback (most recent call last): File "D:\python\gis\new.py", line 2, in from matplotlib import pyplot as plt File "C:\Python27\lib\site-packages\matplotlib__init__.py", line 133, in from matplotlib.rcsetup import (defaultParams, File "C:\Python27\lib\site-packages\matplotlib\rcsetup.py", line 19, in from matplotlib.colors import is_color_like File "C:\Python27\lib\site-packages\matplotlib\colors.py", line 54, in import matplotlib.cbook as cbook File "C:\Python27\lib\site-packages\matplotlib\cbook.py", line 15, in import new File "D:\python\gis\new.py", line 2, in from matplotlib import pyplot as plt File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 20, in from matplotlib import _pylab_helpers, interactive ImportError: cannot import name interactive

最佳答案

我不完全确定你想要完成什么,但这是我的解释

您将道路定义为沿路的坐标,并且您想要绘制这些坐标作为节点,它们之间的道路作为边。你也想要优势权重是两个节点之间的距离。

这可以很容易地通过保存前一个节点并使用勾股定理计算距离来实现。这是我是怎么做到的:

import networkx as nx
from matplotlib import pyplot as plt
import math

G = nx.Graph()

row = '643715.202,2499149.0506 643752.61523545,2499089.86084203 ' +\
'643773.6038,2499056.6558 643773.73878609,2499056.44011079 ' +\
'643793.20162482,2499025.34111554 643813.55943268,2498992.81212045 ' +\
'643826.6563,2498971.8852'

a=row.split(" ")
# Saving the previous node to be able to calculate the distance
prev_point = None
# Save the positions in a dictionary to be able to draw
# the nodes at the correct positions
pos = {}
for i in a:
cur_point = tuple([float(x) for x in i.split(',')])
assert len(cur_point) == 2
if prev_point is not None:
# Calculate the distance between the nodes with the Pythagorean
# theorem
b = cur_point[1] - prev_point[1]
c = cur_point[0] - prev_point[0]
a = math.sqrt(b ** 2 + c ** 2)
G.add_edge(cur_point, prev_point, weight=a)
G.add_node(cur_point)
pos[cur_point] = cur_point
prev_point = cur_point
nx.draw(G, pos=pos)
plt.savefig('roads.png')

在这个例子中,我假设一个空格分隔节点位置和每个位置的 x 和 y 坐标以逗号分隔,但这很容易更改。上面的代码将输出如下内容:

这会将节点置于其“正确”位置,但如果道路长度差异很大,可能会导致一些问题。在上面的示例中,您可以看到两个节点或多或少地彼此重叠。但这是一个不同的问题。

关于python - 必须指向二维图(x,y)坐标networkx python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10777009/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com