2016-09-26 3 views
-2

私はまだPythonを初めて使っています。私は単純な距離計算でポリラインの長さを計算するのに役立つ必要があります:csvからポリラインの長さを計算する

distance = sqrt((x1 - x2)**2 + (y1 - y2)**2) 

たとえば、私の入力csvは次のようになります。

id x  y  sequence 
1 1.5 2.5  0 
1 3.2 4.9  1 
1 3.6 6.6  2 
1 4.4 5.0  3 
2 2.0 4.5  0 
2 3.5 6.0  1 

私は 'id'と 'sequence'(行の頂点のシーケンス番号)を持っています。どのようにcsvファイルを読む?現在の「id」が前の行「id」と同じ値を持つ場合、距離計算を行います。sqrt((x [i] - x [i-1])** 2 +(y [i] -y [i -1])** 2)。その後、 'id'でグループ化し、 'distance'値を合計します。

出力CSVは次のようになります。

id distance 
1  ? 
2  ? 

感謝を事前に。

+2

これは一種のあります質問は通常閉鎖されます。一度に1つの質問を行う方法がわからない場合や正確に何が動作していないのかを説明すると、役に立つ回答を得る可能性が高くなります。 – m0s

+2

関数のパラメータリストには、y2という名前の2つの名前とy1という名前の名前のない名前の2つのパラメータがあります。 – barny

答えて

1

ポリライン:詳細情報をご覧ください

def lineMagnitude (x1, y1, x2, y2): 
    lineMagnitude = math.sqrt(math.pow((x2 - x1), 2)+ math.pow((y2 - y1), 2)) 
    return lineMagnitude 

#Calc minimum distance from a point and a line segment (i.e. consecutive vertices in a polyline). 
def DistancePointLine (px, py, x1, y1, x2, y2): 
    #http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/source.vba 
    LineMag = lineMagnitude(x1, y1, x2, y2) 

    if LineMag < 0.00000001: 
     DistancePointLine = 9999 
     return DistancePointLine 

    u1 = (((px - x1) * (x2 - x1)) + ((py - y1) * (y2 - y1))) 
    u = u1/(LineMag * LineMag) 

    if (u < 0.00001) or (u > 1): 
     #// closest point does not fall within the line segment, take the shorter distance 
     #// to an endpoint 
     ix = lineMagnitude(px, py, x1, y1) 
     iy = lineMagnitude(px, py, x2, y2) 
     if ix > iy: 
      DistancePointLine = iy 
     else: 
      DistancePointLine = ix 
    else: 
     # Intersecting point is on the line, use the formula 
     ix = x1 + u * (x2 - x1) 
     iy = y1 + u * (y2 - y1) 
     DistancePointLine = lineMagnitude(px, py, ix, iy) 

    return DistancePointLine 

:二つの点と点とポリラインの間の距離と

enter image description here

距離http://www.maprantala.com/2010/05/16/measuring-distance-from-a-point-to-a-line-segment/

関連する問題