2017-01-25 8 views
2

두 개의 df가 있다고 가정 해 보겠습니다. df1df2입니다. 두 행 모두 선 스트링 유형의 기하학을 나타내는 하나의 열 (geometry_1geometry_2)을가집니다.geopandas로 지오메트리 비교

df1  
    geometry_1 
0 LINESTRING(37.00 59.00, 37.05 59.32) 
.... 


df2 
    geometry_2 
0 LINESTRING(37.89 59.55, 38.05 60.32) 
.... 

두 가지 df에는 더 많은 행이 있지만 지금은 다음 질문에 중점을 두 고자합니다. 두 선이 비슷한지 평가할 방법이 있습니까? 비슷한 말로하면 라인의 각 포인트 사이의 거리가 유효한 값 (예 : 100m)보다 높지 않으면 두 라인이 동일하게 간주됩니다.

답변

0

이후의 테스트 (정점 당 정점 비교)에는 매우 중요한 제약이 있습니다. 두 정점 모두 정확히 같은 수의 정점이 있어야하며, 그렇게 될 가능성은 거의 없습니다.

분명히 매우 기본적인 광범위한 유사성 검사가 필요하므로 필자는 회선의 주요 특징을 비교하는 것으로 시작합니다.

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 
+0

다음과 같이 shapely의 지오메트리 속성을 사용하여이를 달성 할 수 있습니다. 알려 주시면이 주제를 마감 할 수 있는지 알 수 있습니다. –