如何使用 For 循环将多边形列表显示为一个图形
How to display Polygon List into one Graph with For Loops
所以我正在编写一个视线脚本,其中多边形充当建筑物,return 一个基于线是否与多边形相交的布尔值。
它背后的逻辑有效,但是当我尝试从名为 polycoords 的列表中集成更多多边形并使用 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 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()
所以我正在编写一个视线脚本,其中多边形充当建筑物,return 一个基于线是否与多边形相交的布尔值。
它背后的逻辑有效,但是当我尝试从名为 polycoords 的列表中集成更多多边形并使用 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 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()