Python 从具有重复直线的线串创建多边形?
下面的代码显示了输入数据的外观Python 从具有重复直线的线串创建多边形?,python,matplotlib,polygon,shapely,Python,Matplotlib,Polygon,Shapely,下面的代码显示了输入数据的外观 #!/usr/bin/python import matplotlib.pyplot as plt from shapely.geometry import MultiLineString from math import sqrt import random GM = (sqrt(5)-1.0)/2.0 W = 8.0 H = W*GM SIZE = (W, H) dim = (1400,2500) def plot_data(lines,center):
#!/usr/bin/python
import matplotlib.pyplot as plt
from shapely.geometry import MultiLineString
from math import sqrt
import random
GM = (sqrt(5)-1.0)/2.0
W = 8.0
H = W*GM
SIZE = (W, H)
dim = (1400,2500)
def plot_data(lines,center):
fig = plt.figure(1, figsize=SIZE, dpi=90)
ax = fig.add_subplot(121)
mline1 = MultiLineString(lines)
plot_coords(ax, mline1)
plot_lines(ax, mline1)
for i in range(len(center)):
ax.plot(center[i][0],center[i][1],'or')
ax.annotate("P%d"%i,(center[i][0],center[i][1]))
ax.set_title('General Polygon')
xrange = [-1, float(dim[0])]
yrange = [-1, float(dim[1])]
ax.set_xlim(*xrange)
ax.set_ylim(*yrange)
ax.set_aspect(1)
plt.show()
def v_color(ob):
r = lambda: random.randint(0,255)
hex_number = '#%02X%02X%02X' % (r(),r(),r())
return hex_number
def plot_coords(ax, ob):
for line in ob:
x, y = line.xy
ax.plot(x, y, 'o', color='#999999', zorder=1)
def plot_lines(ax, ob):
for line in ob:
x, y = line.xy
ax.plot(x, y, color=v_color(ob), alpha=0.7, linewidth=3, solid_capstyle='round', zorder=2)
linestring = [[(1071.0, 2.0), (1197.0, 2.0), (1197.0, 475.0), (1071.0, 475.0), (1071.0, 2.0)], [(945.0, 2.0), (945.0, 475.0), (1071.0, 475.0), (1071.0, 2.0), (945.0, 2.0)], [(1052.0, 475.0), (1218.0, 475.0), (1218.0, 981.0), (1052.0, 981.0), (1052.0, 475.0)], [(945.0, 475.0), (945.0, 1210.5), (1052.0, 1210.5), (1052.0, 475.0), (945.0, 475.0)], [(1052.0, 981.0), (1208.0, 981.0), (1208.0, 1192.0), (1052.0, 1192.0)], [(1062.0, 1192.0), (1062.0, 1665.0), (1062.0, 1210.5), (945.0, 1210.5), (945.0, 2235.5), (1062.0, 2235.5), (1062.0, 1665.0), (1188.0, 1665.0), (1188.0, 1192.0), (1062.0, 1192.0)], [(1062.0, 2021.0), (1218.0, 2021.0), (1218.0, 2232.0), (1062.0, 2232.0), (1062.0, 2021.0), (1198.0, 2021.0), (1198.0, 1665.0), (1188.0, 1665.0)], [(1132.5, 2235.5), (352.0, 2235.5), (352.0, 2321.5), (1132.5, 2321.5), (1132.5, 2235.5)], [(1132.5, 2321.5), (1132.5, 2407.5), (352.0, 2407.5), (352.0, 2321.5), (1132.5, 2321.5)], [(825.0, 2108.0), (352.0, 2108.0), (352.0, 2234.0), (825.0, 2234.0), (825.0, 2108.0)], [(352.0, 2096.0), (352.0, 2391.0), (2.0, 2391.0), (2.0, 2096.0), (352.0, 2096.0)], [(386.0, 2.0), (386.0, 2108.0), (945.0, 2108.0), (945.0, 2.0), (386.0, 2.0)], [(386.0, 2096.0), (2.0, 2096.0), (2.0, 2.0), (386.0, 2.0), (386.0, 2096.0)]]
center = [(1135.0, 728.0), (1140.0, 2126.5), (1130.0, 1086.5), (1130.0, 1843.0), (1134.0, 238.5), (1125.0, 1428.5), (742.25, 2278.5), (742.25, 2364.5), (1008.0, 238.5), (1003.5, 1723.0), (998.5, 842.75), (665.5, 1055.0), (588.5, 2171.0), (194.0, 1049.0), (177.0, 2243.5)]
plot_data(linestring,center)
数据包括许多行字符串,其中一些具有重复的行。我想结合线来创建矩形(或多边形)。
有两个问题,我不知道如何合并这些线来创建一个大的形状,然后从这个形状创建多边形 我运行了你的代码,它似乎工作得很好。你能修改一下你的问题并澄清到底是什么问题吗?如果你能展示出预期的输出,以及它与你现在得到的不同,这会有所帮助。编辑我的问题,我希望你能理解它,直到不清楚为止。您只是想从
linestring
的每个内部列表中创建一个多边形吗?像polygons=[linestring中x的多边形(x)]
?我也不确定你所说的“大形状”是什么意思,这是我过去的老方法,但是有些线串没有相同的格式(比如使用合并2个线串来创建2个多边形)。如果我使用这种方法,我可以得到这样的结果:看右上角(P6,P7),所以我必须改变方法