Trying to calculate distance between points from a .txt file
<p>I am trying to figure out how to calculate the length of a line segment using coordinates that are in a text file:</p>

<blockquote>
  <p>X,Y format (x,y,x,y,x,y,etc...)</p>
  
  <p>4.5,10.0,4.5,5.7,5.5,2.5,6.5,0.3,6.8,0.0,1.0,1.0,3.5,3.5,2,5,6.0,2.0</p>
</blockquote>

<p>This is what I have so far:</p>

<blockquote>
  <p>Python</p>
</blockquote>

<pre><code>   # -*- coding: cp1252 -*-
#Starter code for GIS301 Lab 2
#GIS301Lab2Starter.py

import math

#-----------------------------------------------------------
#Procedure for reading a coordinate text file
#in x1,y1,x2,y2,â¦ xn,yn format
#and resulting in a list as type float

#open file to read
file = open(r'C:\Users\Tristan\Desktop\USB_Backup_10-4-16\2016-2017\Fall2016\SpatialDataStructures\Labs\Lab2\points.txt','r')

#read file to string
coordString = file.read()
#Split coordString into list elements
coordList = coordString.split(',')
#convert from string to float type
for index, item in enumerate(coordList):
    coordList[index] = float(coordList[index])
file.close
#-----------------------------------------------------------
print (coordList)    
#add more code here
numPoints = len(coordList)/2
print("Number of Points")
print (numPoints)


x = [float(r) for r in coordList[0::2]]
y = [float(r) for r in coordList[1::2]]

xy = list(zip(x,y))


# pre-define distance and add subsequent distances
dist = 0
for r in coordList(len(xy)-1):
    dist += ( (xy[r][0]-xy[r+1][0])**2 + (xy[r][1]-xy[r+1][1])**2 )**0.5

print (dist)



#for r in coordList(len(xy)-1):
#    dist += ( (xy[r][0]-xy[r+1][0])**2 + (xy[r][1]-xy[r+1][1])**2 )**0.5
</code></pre>

<blockquote>
  <p>When I run it in PyhthonWin:</p>
</blockquote>

<pre><code>    Traceback (most recent call last):
  File "C:\Users\Tristan\Desktop\USB_Backup_10-4-16\2016-2017\Fall2016\SpatialDataStructures\Labs\Lab2\GOERSLab2.py", line 39, in &lt;module&gt;
    for r in coordList(len(xy)-1):
TypeError: 'list' object is not callable
</code></pre>

<p>The equation for calculating distance between two points is:
<a href="http://i.stack.imgur.com/vOcu0.png" rel="nofollow">http://i.stack.imgur.com/vOcu0.png</a></p>

<p>Then I need to add them all up!</p>

<p>From what you have provided I do not see the need to over complicate this.  Assuming you can properly import your text-file data, and split it, you could do this: </p>

<pre><code>points = ['4.5','10.0','4.5','5.7','5.5','2.5','6.5','0.3','6.8','0.0','1.0','1.0','3.5','3.5','2','5','6.0','2.0']


x = [float(r) for r in points[0::2]]
y = [float(r) for r in points[1::2]]

# put x/y coordinates in tuples
xy = list(zip(x,y))

# pre-define distance and add subsequent distances
dist = 0
for r in xrange(len(xy)-1):
    dist += ( (xy[r][0]-xy[r+1][0])**2 + (xy[r][1]-xy[r+1][1])**2 )**0.5
</code></pre>

<p>I think something along these lines will work well (you don't even need to put the coordinates in tuples, really).  Is this along the lines of what you needed?</p>

python loops for-loop coordinates gis