我有擴展多邊形的代碼,它的工作原理是將xs和ys乘以一個系數,然后將所得的polyon重新居中在原始中心。給定多邊形需要達到的一點,我也有代碼來找到膨脹系數的值:import numpy as npimport itertools as ITimport copyfrom shapely.geometry import LineString, Pointdef getPolyCenter(points): """ http://stackoverflow.com/a/14115494/190597 (mgamba) """ area = area_of_polygon(*zip(*points)) result_x = 0 result_y = 0 N = len(points) points = IT.cycle(points) x1, y1 = next(points) for i in range(N): x0, y0 = x1, y1 x1, y1 = next(points) cross = (x0 * y1) - (x1 * y0) result_x += (x0 + x1) * cross result_y += (y0 + y1) * cross result_x /= (area * 6.0) result_y /= (area * 6.0) return (result_x, result_y)def expandPoly(points, factor): points = np.array(points, dtype=np.float64) expandedPoly = points*factor expandedPoly -= getPolyCenter(expandedPoly) expandedPoly += getPolyCenter(points) return np.array(expandedPoly, dtype=np.int64)def distanceLine2Point(points, point): points = np.array(points, dtype=np.float64) point = np.array(point, dtype=np.float64) points = LineString(points) point = Point(point) return points.distance(point)def distancePolygon2Point(points, point): distances = [] for i in range(len(points)): if i==len(points)-1: j = 0 else: j = i+1 line = [points[i], points[j]] distances.append(distanceLine2Point(line, point)) minDistance = np.min(distances) #index = np.where(distances==minDistance)[0][0] return minDistance """ Returns the distance from a point to the nearest line of the polygon, AND the distance from where the normal to the line (to reach the point) intersets the line to the center of the polygon."""僅當點與多條線中的一條直接相對時,此代碼才有效。
添加回答
舉報
0/150
提交
取消