def clean_edges(Rlinelist,elect0,elect1):
plot = False
Electrodes = shapely_geom.MultiLineString([elect0,elect1])
Rline_cleaned = [[],[]]
for polarity in range(len(Rlinelist)):
#Rline = shapely_geom.collection.GeometryCollection(Rlinelist[polarity])
for n in range(len(Rlinelist[polarity])):
Rline = Rlinelist[polarity][n]
Rsegments = Rline.difference(Electrodes)
check_linear_ring = Rsegments.geoms[0].coords[0]==Rsegments.geoms[-1].coords[-1]
check_no_intersection = not shapely_geom.Point(Rsegments.geoms[0].coords[0]).buffer(1e-9).intersects(Electrodes)
if check_linear_ring and check_no_intersection: #there seems to be a bug in the linear ring intersection of shapely
Rsegments_geoms_0 = shapely_ops.linemerge([Rsegments.geoms[0],Rsegments.geoms[-1]])
Rsegments_temp = [Rsegments_geoms_0]
for i in range(len(Rsegments.geoms)-2):
Rsegments_temp.append(Rsegments[i+1])
Rsegments = shapely_geom.MultiLineString(Rsegments_temp)
for segment in Rsegments.geoms:
if plot:
ax = plt.subplot(111)
segmentarray = numpy.array(segment.coords)
elect0array = numpy.array(elect0.coords)
elect1array = numpy.array(elect1.coords)
ax.plot(segmentarray[:,0].tolist(), segmentarray[:,1].tolist(), color='k', linewidth=3)
ax.plot(elect0array[:,0].tolist(), elect0array[:,1].tolist(), color='b', linewidth=3)
ax.plot(elect1array[:,0].tolist(), elect1array[:,1].tolist(), color='r', linewidth=3)
ax.axis([min(segmentarray[:,0]),max(segmentarray[:,0]),min(segmentarray[:,1]), max(segmentarray[:,1])])
ax.set_aspect(1)
ax.grid(True)
plt.show()
if segment.buffer(1e-9).intersects(elect0) and segment.buffer(1e-9).intersects(elect1): #there seems to be a numerical issue with small segments
Rline_cleaned[polarity].append(segment)
return Rline_cleaned
评论列表
文章目录