当它们与多边形相交时,如何使行列表返回布尔值列表,结果是不显示返回True值的线?
当前,我的代码在原点(0,0)到XY笛卡尔平面上的所有点之间绘制了一条线。我希望能够使与多边形相交的线返回布尔值。希望这将使我不显示返回True值的行。
即 IF Lineintersection ==真: 不显示行 其他: 显示行
origin = [0,0]
import numpy as np
import matplotlib.pyplot as plt
from shapely.geometry import LineString
from shapely.geometry import Point, Polygon
import descartes
quality = 7
x = np.linspace(-1,1,quality)
y = np.linspace(-1,1,quality)
X,Y = np.meshgrid(x,y)
polycoords = [[-1, 1], [-1, 0.5], [0, 0.5], [0, 1]]
clip_poly = shapely.geometry.Polygon(polycoords)
positions = np.vstack([Y.ravel(), X.ravel()])
#plt.scatter(*positions[::-1])
fig = plt.figure()
ax = fig.add_subplot(111)
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)])
x2, y2 = line.xy
lines = plt.plot(0, 0, x2, y2, color='green', linewidth=1, solid_capstyle='round')
polygon = Polygon(polycoords)
polygonbuilding = ax.add_patch(descartes.PolygonPatch(clip_poly, fc='pink', alpha=0.3))
plt.show()
答案 0 :(得分:0)
我想您可以通过从两个for
循环中删除多边形创建来简化代码,例如,通过在创建线条之前创建多边形(另请注意,您在原始代码中创建了两次)代码,一次在clip_poly
变量中,一次在polygon
变量中。)
然后,您将能够调用要处理的几何对象的intersects
方法(此处为polygon
和line
)。
我在一个循环中简化了两个for
循环,直接解压缩了x1
和y1
坐标(并且我还将plt.scatter
调用移到了循环之外):
import numpy as np
import matplotlib.pyplot as plt
from shapely.geometry import LineString
from shapely.geometry import Point, Polygon
import descartes
origin = [0,0]
quality = 7
x = np.linspace(-1,1,quality)
y = np.linspace(-1,1,quality)
X,Y = np.meshgrid(x,y)
polycoords = [[-1, 1], [-1, 0.5], [0, 0.5], [0, 1]]
positions = np.vstack([Y.ravel(), X.ravel()])
fig = plt.figure()
ax = fig.add_subplot(111)
polygon = Polygon(polycoords)
polygonbuilding = ax.add_patch(
descartes.PolygonPatch(polygon, fc='pink', alpha=0.3))
plt.scatter(*positions[::-1])
for x1, y1 in positions.T:
line = LineString([origin, (x1, y1)])
if not polygon.intersects(line): # <- the condition you were looking for
x2, y2 = line.xy
plt.plot(0, 0, x2, y2, color='green', linewidth=1, solid_capstyle='round')
plt.show()