bm1*_*563 5 python geometry numpy
我有两个多边形P和Q,其中多边形的外部线性环由两个封闭的点集(存储为numpy数组)定义,它们以逆时针方向连接。P和Q的格式如下:
P['x_coords'] = [299398.56 299402.16 299410.25 299419.7 299434.97 299443.75 299454.1 299465.3 299477. 299488.25 299496.8 299499.5 299501.28 299504. 299511.62 299520.62 299527.8 299530.06 299530.06 299525.12 299520.2 299513.88 299508.5 299500.84 299487.34 299474.78 299458.6 299444.66 299429.8 299415.4 299404.84 299399.47 299398.56 299398.56]
P['y_coords'] = [822975.2 822989.56 823001.25 823005.3 823006.7 823005.06 823001.06 822993.4 822977.2 822961. 822943.94 822933.6 822925.06 822919.7 822916.94 822912.94 822906.6 822897.6 822886.8 822869.75 822860.75 822855.8 822855.4 822857.2 822863.44 822866.6 822870.6 822876.94 822886.8 822903. 822920.3 822937.44 822954.94 822975.2]
Q['x_coords'] = [292316.94 292317.94 292319.44 292322.47 292327.47 292337.72 292345.75 292350. 292352.75 292353.5 292352.25 292348.75 292345.75 292342.5 292338.97 292335.97 292333.22 292331.22 292329.72 292324.72 292319.44 292317.2 292316.2 292316.94]
Q['y_coords'] = [663781. 663788.25 663794. 663798.06 663800.06 663799.3 663796.56 663792.75 663788.5 663782. 663773.25 663766. 663762. 663758.25 663756.5 663756.25 663757.5 663761. 663763.75 663767.5 663769.5 663772.25 663777.5 663781. ]
## SIMPLIFIED AND FORMATTED FOR EASY TESTING:
import numpy as np
px_coords = np.array([299398,299402,299410.25,299419.7,299398])
py_coords = np.array([822975.2,822920.3,822937.44,822954.94,822975.2])
qx_coords = np.array([292316,292331.22,292329.72,292324.72,292319.44,292317.2,292316])
qy_coords = np.array([663781,663788.25,663794,663798.06,663800.06,663799.3,663781])
Run Code Online (Sandbox Code Playgroud)
P的外环是通过连接P['x_coords'][0], P['y_coords'][0] -> P['x_coords'][1], P['y_coords'][1]等形成的。每个数组的最后一个坐标与第一个相同,表示形状在拓扑上是封闭的。
是否可以使用numpy计算出P和Q外圈之间的简单最小距离?我一直在搜索SO上下两步,而没有找到任何明确的内容,因此我怀疑这可能是一个非常复杂的问题的过度简化。我知道可以使用现成的空间库(例如GDAL或Shapely)来完成距离计算,但是我很想通过从numpy中重新构建一些东西来理解这些工作原理。
我已经考虑或尝试过的一些事情:
scipy.spatial的问题相同。有没有更好的方法来解决此问题?
k - d树有很多 变体,用于存储具有范围的对象,例如多边形的边缘。我最熟悉的方法(但没有链接)涉及将轴对齐的边界框与每个节点相关联;叶子对应于对象,内部节点\xe2\x80\x99s盒子是包围其两个子节点\xe2\x80\x99s(通常重叠)的最小盒子。通常的中值切割方法应用于对象\xe2\x80\x99s 框的中点(对于线段,这是它们的中点)。
\n\n为每个多边形构建这些后,以下双重递归会找到最接近的方法:
\n\ndef closest(k1,k2,true_dist):\n return _closest(k1,0,k2,0,true_dist,float("inf"))\n\ndef _closest(k1,i1,k2,i2,true_dist,lim):\n b1=k1.bbox[i1]\n b2=k2.bbox[i2]\n # Call leaves their own single children:\n cc1=k1.child[i1] or (i1,)\n cc2=k2.child[i2] or (i2,)\n if len(cc1)==1 and len(cc2)==1:\n return min(lim,true_dist(i1,i2))\n # Consider 2 or 4 pairs of children, possibly-closest first:\n for md,c1,c2 in sorted((min_dist(k1.bbox[c1],k2.bbox[c2]),c1,c2)\n for c1 in cc1 for c2 in cc2):\n if md>=lim: break\n lim=min(lim,_closest(k1,c1,k2,c2,true_dist,lim)\n return lim\nRun Code Online (Sandbox Code Playgroud)\n\n笔记:
\n\ntrue_dist必须至少有一个端点。min_dist可能重叠,在这种情况下它必须返回 0。