이후의 테스트 (정점 당 정점 비교)에는 매우 중요한 제약이 있습니다. 두 정점 모두 정확히 같은 수의 정점이 있어야하며, 그렇게 될 가능성은 거의 없습니다.
분명히 매우 기본적인 광범위한 유사성 검사가 필요하므로 필자는 회선의 주요 특징을 비교하는 것으로 시작합니다.
def are_geometries_similar(geom1,geom2,MAX_ALLOWED_DISTANCE = 100,MAX_ALLOWED_DIFFERENCE_RATIO = 0.1):
"""
Function compares two linestrings' number of vertices, length and basic position.
If they pass all 3 tests within the specified margin of error, it returns true, otherwise it returns false.
"""
# 1. Compare length:
l1 = geom1.length
l2 = geom2.length
if not abs(float(l1) - l2)/max([l1,l2]) < MAX_ALLOWED_DIFFERENCE_RATIO:
return False
# 2. Compare number of vertices:
vert_num1 = len(geom1.coords)
vert_num2 = len(geom2.coords)
if not abs(float(vert_num1) - vert_num2)/max([vert_num1,vert_num2]) < MAX_ALLOWED_DIFFERENCE_RATIO:
return False
# 3. Compare position by calculating the representative point
rp1 = geom1.representative_point()
rp2 = geom2.representative_point()
if rp1.distance(rp2) > MAX_ALLOWED_DISTANCE:
return False
# If all tests passed, return True
return True
다음과 같이
shapely
의 지오메트리 속성을 사용하여이를 달성 할 수 있습니다. 알려 주시면이 주제를 마감 할 수 있는지 알 수 있습니다. –