hausdorff_distance()
函数返回单个(标量)值。
有没有办法获得两个几何上适合这个距离的/一对点?
类似于我们如何将
nearest_points()
相对于 distance()
。
如果你的多边形不是很大,你可以只测量多边形 1 中每个顶点到多边形 2 中所有顶点之间的距离:
import shapely
import shapely.wkt
import shapely.plotting
from itertools import product
poly1 = r"""Polygon ((407037 6222825, 407049 6222788, 407057 6222764, 407061 6222744,
407061 6222724, 407065 6222703, 407069 6222683, 407065 6222647, 407065 6222623,
407065 6222594, 407065 6222570, 407065 6222542, 407069 6222522, 407073 6222497,
407085 6222473, 407102 6222445, 407126 6222421, 407170 6222380, 407191 6222364,
407211 6222348, 407231 6222340, 407255 6222328, 407275 6222311, 407308 6222295,
407348 6222291, 407389 6222287, 407421 6222287, 407441 6222291, 407461 6222303,
407486 6222324, 407502 6222340, 407530 6222368, 407554 6222392, 407574 6222421,
407587 6222445, 407599 6222481, 407611 6222497, 407611 6222518, 407611 6222542,
407611 6222578, 407595 6222619, 407591 6222639, 407587 6222659, 407574 6222691,
407554 6222724, 407542 6222744, 407522 6222764, 407502 6222788, 407490 6222805,
407465 6222825, 407449 6222837, 407429 6222849, 407417 6222865, 407393 6222877,
407364 6222885, 407348 6222897, 407316 6222906, 407296 6222914, 407259 6222926,
407223 6222934, 407199 6222934, 407178 6222938, 407146 6222938, 407122 6222938,
407089 6222942, 407069 6222942, 407037 6222825))"""
poly2 = r"""Polygon ((406835 6222954, 406847 6222922, 406851 6222889, 406855 6222869,
406855 6222837, 406859 6222817, 406859 6222792, 406855 6222772, 406855 6222748,
406847 6222716, 406843 6222695, 406835 6222667, 406823 6222639, 406815 6222619,
406807 6222598, 406786 6222582, 406782 6222562, 406770 6222546, 406758 6222526,
406730 6222489, 406718 6222465, 406710 6222445, 406693 6222421, 406681 6222396,
406673 6222376, 406665 6222356, 406657 6222332, 406653 6222303, 406653 6222283,
406661 6222263, 406673 6222247, 406689 6222235, 406710 6222223, 406738 6222214,
406762 6222214, 406786 6222227, 406811 6222231, 406831 6222239, 406859 6222243,
406879 6222243, 406900 6222243, 406920 6222243, 406940 6222243, 406972 6222243,
406997 6222239, 407033 6222235, 407102 6222227, 407122 6222223, 407146 6222223,
407170 6222214, 407195 6222214, 407215 6222206, 407239 6222206, 407275 6222198,
407316 6222198, 407372 6222194, 407417 6222194, 407473 6222194, 407522 6222198,
407566 6222198, 407615 6222202, 407663 6222206, 407712 6222214, 407760 6222227,
407793 6222251, 407825 6222275, 407857 6222291, 407882 6222307, 407910 6222324,
407918 6222344, 407930 6222380, 407930 6222417, 407934 6222449, 407938 6222481,
407938 6222501, 407934 6222538, 407926 6222562, 407918 6222586, 407910 6222611,
407906 6222639, 407898 6222663, 407894 6222691, 407882 6222716, 407874 6222736,
407857 6222768, 407845 6222788, 407833 6222805, 407821 6222829, 407805 6222845,
407793 6222865, 407781 6222885, 407760 6222918, 407744 6222934, 407724 6222950,
407704 6222958, 407692 6222974, 407667 6222986, 407635 6223003, 407611 6223011,
407587 6223015, 407550 6223027, 407510 6223031, 407477 6223035, 407441 6223043,
407405 6223047, 407385 6223051, 407348 6223055, 407312 6223059, 407283 6223067,
407255 6223071, 407231 6223075, 407203 6223079, 407170 6223083, 407138 6223083,
407102 6223087, 407081 6223091, 407049 6223095, 407025 6223095, 407005 6223095,
406980 6223087, 406960 6223087, 406940 6223083, 406835 6222954))"""
poly1 = shapely.wkt.loads(poly1)
poly2 = shapely.wkt.loads(poly2)
hdist = poly1.hausdorff_distance(poly2)
print(hdist)
473.1519840387993
p1_coords = list(poly1.boundary.coords)
p2_coords = list(poly2.boundary.coords)
distances = []
for coord1, coord2 in product(p1_coords, p2_coords):
dist = shapely.Point(coord1).distance(shapely.Point(coord2))
distances.append((coord1, coord2, dist))
the_hausdorff_coords = min(distances, key=lambda x: abs(hdist-x[-1]))
print(the_hausdorff_coords)
#((407085.0, 6222473.0), (406661.0, 6222263.0), 473.1553656041533)
hausdorff_line = shapely.LineString([the_hausdorff_coords[0], the_hausdorff_coords[1]])
shapely.plotting.plot_polygon(poly1, color="blue", add_points=False, zorder=1)
shapely.plotting.plot_polygon(poly2, color="green", add_points=False, zorder=2)
shapely.plotting.plot_line(hausdorff_line, color="red", add_points=False, zorder=3)