Calculate the length of polyline from csv
<p>I'm still new to python. I need helps to calculate the length of polyline with simple distance calculation:</p>

<pre><code>    distance = sqrt( (x1 - x2)**2 + (y1 - y2)**2 )
</code></pre>

<p>For instance my input csv looks like this. </p>

<pre><code>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
</code></pre>

<p>I have 'id' and 'sequence' (sequence number of the line vertices). How read the csv file? if current 'id' has the same value as previous row 'id', then perform the distance calculation : sqrt( (x[i] - x[i-1])**2 + (y[i] - y[i-1])**2 ). After that, group by 'id' and sum their 'distance' value.</p>

<p>The output csv would look like this:</p>

<pre><code>id    distance
 1       ?
 2       ?
</code></pre>

<p>Thanks in advance. </p>

<p>Polyline:</p>

<p><a href="http://i.stack.imgur.com/vML5D.gif" rel="nofollow"><img src="http://i.stack.imgur.com/vML5D.gif" alt="enter image description here"></a></p>

<p><strong>Distance between two points and the distance between a point and polyline:</strong></p>

<pre><code>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 &lt; 0.00000001:
        DistancePointLine = 9999
        return DistancePointLine

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

    if (u &lt; 0.00001) or (u &gt; 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 &gt; 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
</code></pre>

<p>For more information visit: <a href="http://www.maprantala.com/2010/05/16/measuring-distance-from-a-point-to-a-line-segment/" rel="nofollow">http://www.maprantala.com/2010/05/16/measuring-distance-from-a-point-to-a-line-segment/</a></p>

python csv distance