def GetCrossPoint(infc,NeedIndex): #生成曲线 Curve=LineString(GetCurveLinecoordinates(infc)) #读取文本文件,获得总体最小二乘压缩点坐标数组 TLSTxtpath= infc[0:-4]+"D.txt" TLSfp= open(TLSTxtpath,"r") TLSLine=[] for line in TLSfp.readlines(): x0=float(line.replace("\n", "").split("\t")[0]) y0=float(line.replace("\n", "").split("\t")[1]) TLSLine.append((x0,y0)) TLSfp.close() #求交点 for i in range(0,len(TLSLine)-1,1): if i == NeedIndex: ##后交点 #生成直线 LineA=LineString(GetStraightLinecoordinates(TLSLine[i][0],TLSLine[i][1],TLSLine[i+1][0],TLSLine[i+1][1])) #获得交点 CrossPoint=LineA.intersection(Curve) #存放交点 CrossPointArrays=[] #判断交点类型 if str(type(CrossPoint))== "<class 'shapely.geometry.multipoint.MultiPoint'>": for j in range(0,len(list(CrossPoint)),1): CrossPointArrays.append([list(CrossPoint)[j].x,list(CrossPoint)[j].y]) elif str(type(CrossPoint))== "<class 'shapely.geometry.point.Point'>": CrossPointArrays.append([CrossPoint.x,CrossPoint.y]) #前交点 LineB=LineString(GetStraightLinecoordinates(TLSLine[i-1][0],TLSLine[i-1][1],TLSLine[i][0],TLSLine[i][1])) print "前直线",TLSLine[i-1][0],TLSLine[i-1][1],TLSLine[i][0],TLSLine[i][1] CrossPoint2=LineB.intersection(Curve) CrossPointArraysBefore=[] if str(type(CrossPoint2))== "<class 'shapely.geometry.multipoint.MultiPoint'>": for k in range(0,len(list(CrossPoint2)),1): CrossPointArraysBefore.append([list(CrossPoint2)[k].x,list(CrossPoint2)[k].y]) elif str(type(CrossPoint2))== "<class 'shapely.geometry.point.Point'>": CrossPointArraysBefore.append([CrossPoint2.x,CrossPoint2.y]) return CrossPointArraysBefore,CrossPointArrays
def make_point_link_data(fig): n = len(fig) links = dict([(i, {(i + 1) % n, (i - n - 1) % n}) for i in range(n)]) cross_points = [] cross_vertexes = {} crossed_lines = [] cp_idx = n # find cross-points for i in range(len(fig) - 1): for j in range(i + 1, len(fig)): edge_vertexes = [i % n, (i + 1) % n, j % n, (j + 1) % n] i1, i2, j1, j2 = edge_vertexes edge_i = LineString([fig[i1], fig[i2]]) edge_j = LineString([fig[j1], fig[j2]]) if edge_i.crosses(edge_j): ip = edge_i.intersection(edge_j) cross_points.append((ip.x, ip.y)) links[cp_idx] = set() cr_line1 = {i1, i2} cr_line2 = {j1, j2} cross_vertexes[cp_idx] = (cr_line1, cr_line2) if cr_line1 not in crossed_lines: crossed_lines.append(cr_line1) if cr_line2 not in crossed_lines: crossed_lines.append(cr_line2) cp_idx += 1 points = fig + cross_points # add cross-point links for base_line in crossed_lines: inner = [] # inline vertexes for vx, lines in cross_vertexes.items(): if base_line in lines: inner.append(vx) if len(inner) > 0: bs1, bs2 = list(base_line) if len(inner) >= 2: inner.sort(key=lambda x: Point(points[x]).distance(Point(points[bs1]))) links[bs1].remove(bs2) links[bs2].remove(bs1) vxs = [bs1] + inner + [bs2] for i in range(1, len(vxs) - 1): links[vxs[i]].add(vxs[i-1]) links[vxs[i-1]].add(vxs[i]) links[vxs[i]].add(vxs[i+1]) links[vxs[i+1]].add(vxs[i]) return points, links
""" import matplotlib.pyplot as plt ## test shapel intersection from shapely.geometry.polygon import LinearRing, LineString contour = LinearRing([(0, 0), (2, 0), (2,2), (1,2)]); line = LineString([(0,0), (3,4)]); plt.figure(100); plt.clf(); x, y = contour.xy plt.plot(x, y, color='r', alpha=0.7, linewidth=3, solid_capstyle='round', zorder=2) x,y = line.xy; plt.plot(x, y, color='b', alpha=0.7, linewidth=3, solid_capstyle='round', zorder=2) x = line.intersection(contour); for ob in x: x, y = ob.xy if len(x) == 1: plt.plot(x, y, 'o', color='m', zorder=2) else: plt.plot(x, y, color='m', alpha=0.7, linewidth=3, solid_capstyle='round', zorder=2) # test erosion of polygon from shapely.geometry.polygon import Polygon from shapely.geometry import Polygon
y, color='r', alpha=0.7, linewidth=3, solid_capstyle='round', zorder=2) x, y = line.xy plt.plot(x, y, color='b', alpha=0.7, linewidth=3, solid_capstyle='round', zorder=2) x = line.intersection(contour) for ob in x: x, y = ob.xy if len(x) == 1: plt.plot(x, y, 'o', color='m', zorder=2) else: plt.plot(x, y, color='m', alpha=0.7, linewidth=3, solid_capstyle='round', zorder=2) # test erosion of polygon
i_nodes = [] # interior node indices for which the distance is < 0 for i in range(0, np.size(idx)-1): print("* Shape %d/%d" %(i+1, np.size(idx)-1)) d = dscaled[idx[i]:idx[i+1]] if_nodes = [(d[j][0], d[j][1]) for j in range(0, len(d))] # Add the first node to the end to create a closed loop for intersecting: if_loop = if_nodes if_loop.append(if_nodes[0]) interface = LineString(if_loop) # Intersect edges_close with the interface; construct interface nodes. for j in range(0, len(edges_close)): n1 = edges[edges_close[j], 0] n2 = edges[edges_close[j], 1] o = LineString( [p[n1,:], p[n2,:]] ) x = interface.intersection(o) if (np.size(x) == 2): ifp.append( [x.x, x.y] ) if (np.size(x) > 2): # print("\nWarning: mesh edge crosses multiple interface edges! " # "Using the centroid for interface node position.") ifp.append( [x[:].centroid.x, x[:].centroid.y] ) if ((j+1) % 1000 == 0 or (j+1) == len(edges_close)): print("\r- Processing edge %d / %d" %(j+1,len(edges_close)), end='') print("") # Determine if a node n is enclosed by the interface: Assuming r = (-1,-1) # is 'outside', then if the number of interface intersections with edge # (r, n) is odd, n is enclosed. for j in range(0, len(p)):