Python 如何使用For循环将多边形列表显示为一个图形
因此,我正在编写一个视线脚本,其中多边形充当建筑物,并根据直线是否与多边形相交返回布尔值 它背后的逻辑是可行的,但当我尝试从一个名为polycoords的列表中集成更多多边形并使用For循环以便它可以在图形上实例化它时,它会显示为两个独立的图形,而不是一个单独的图形Python 如何使用For循环将多边形列表显示为一个图形,python,numpy,matplotlib,shapely,descartes,Python,Numpy,Matplotlib,Shapely,Descartes,因此,我正在编写一个视线脚本,其中多边形充当建筑物,并根据直线是否与多边形相交返回布尔值 它背后的逻辑是可行的,但当我尝试从一个名为polycoords的列表中集成更多多边形并使用For循环以便它可以在图形上实例化它时,它会显示为两个独立的图形,而不是一个单独的图形 import numpy as np import matplotlib.pyplot as plt import shapely.geometry from shapely.geometry import LineString f
import numpy as np
import matplotlib.pyplot as plt
import shapely.geometry
from shapely.geometry import LineString
from shapely.geometry import Point, Polygon
import descartes
origin = [-1.0, 0.0] # Set a point to view from
quality = 7 # Number of grid points squared
polycoords = [[[-1, 1], [-1, 0.5], [0, 0.5], [0, 1]],[[1, -1], [1, -0.5], [0, -0.5], [-0, -1]]] # Coordinates of the Polygon
fullresults = []
newbool = []
def los (origin, quality, polycoords):
for buildingpoints in range(len(polycoords)):
x = np.linspace(-1,1,quality)
y = np.linspace(-1,1,quality)
X,Y = np.meshgrid(x,y)
clip_poly = shapely.geometry.Polygon(polycoords[buildingpoints])
fig = plt.figure()
ax = fig.add_subplot(111)
polygonbuilding = ax.add_patch(descartes.PolygonPatch(clip_poly, fc='pink', alpha=0.3))
positions = np.vstack([Y.ravel(), X.ravel()])
for i in range(len(positions)):
for j in range(len(positions[i])):
plt.scatter(*positions[::-1])
x1 = positions[0][j]
y1 = positions[1][j]
line = LineString([origin, (x1, y1)])
if line.intersects(clip_poly) == True:
ax.plot(*np.array(line).T, color='red', linewidth=1, solid_capstyle='round')
else:
ax.plot(*np.array(line).T, color='green', linewidth=1, solid_capstyle='round')
boolintersect = line.intersects(clip_poly)
listresults = origin, [x1,y1],boolintersect
fullresults.append(listresults)
boollist = [x[2] for x in fullresults]
newbool.append(sum(boollist))
return(fullresults, newbool)
def analysis (losresults):
percenteq = round((100-(newbool[-1]/len(fullresults))*100))
print (percenteq,'% of the space is PUBLICALLY visible from point', fullresults[0][0])
print (100-percenteq,'% of the space is PRIVATE/ISNT visible from point', fullresults[0][0])
los(origin, quality, polycoords)
analysis (los)
plt.show()
结果显示了两个单独的图形,而我只需要一个图形,并且两个多边形都存在。我相信这与我的For循环代码的结构有关,但我还是很新,不太确定如何解决这个问题
循环中已解决的网格创建已移出,并按如下所示完成一次:
import numpy as np
import matplotlib.pyplot as plt
import shapely.geometry
from shapely.geometry import LineString
from shapely.geometry import Point, Polygon
import descartes
origin = [-1.0, 0.0] # Set a point to view from
quality = 7 # Number of grid points squared
polycoords = [[[-1, 1], [-1, 0.5], [0, 0.5], [0, 1]],[[1, -1], [1, -0.5], [0, -0.5], [-0, -1]]] # Coordinates of the Polygon
fullresults = []
newbool = []
def init (origin, quality):
global ax, positions
x = np.linspace(-1,1,quality)
y = np.linspace(-1,1,quality)
X,Y = np.meshgrid(x,y)
fig = plt.figure()
ax = fig.add_subplot(111)
positions = np.vstack([Y.ravel(), X.ravel()])
def los (origin, quality, polycoords):
global ax, positions
for buildingpoints in range(len(polycoords)):
clip_poly = shapely.geometry.Polygon(polycoords[buildingpoints])
polygonbuilding = ax.add_patch(descartes.PolygonPatch(clip_poly, fc='pink', alpha=0.3))
for i in range(len(positions)):
for j in range(len(positions[i])):
plt.scatter(*positions[::-1])
x1 = positions[0][j]
y1 = positions[1][j]
line = LineString([origin, (x1, y1)])
if line.intersects(clip_poly) == True:
ax.plot(*np.array(line).T, color='red', linewidth=1, solid_capstyle='round')
else:
ax.plot(*np.array(line).T, color='green', linewidth=1, solid_capstyle='round')
boolintersect = line.intersects(clip_poly)
listresults = origin, [x1,y1],boolintersect
fullresults.append(listresults)
boollist = [x[2] for x in fullresults]
newbool.append(sum(boollist))
return(fullresults, newbool)
def analysis (losresults):
percenteq = round((100-(newbool[-1]/len(fullresults))*100))
print (percenteq,'% of the space is PUBLICALLY visible from point', fullresults[0][0])
print (100-percenteq,'% of the space is PRIVATE/ISNT visible from point', fullresults[0][0])
init (origin, quality)
los(origin, quality, polycoords)
analysis (los)
plt.show()