尝试计算.txt文件中的点之间的距离

时间:2016-10-03 15:41:18

标签: python loops for-loop coordinates gis

我想弄清楚如何使用文本文件中的坐标来计算线段的长度:

  

X,Y格式(x,y,x,y,x,y等......)

     

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

这是我到目前为止所做的:

  

的Python

   # -*- 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
  

当我在PyhthonWin中运行时:

    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 <module>
    for r in coordList(len(xy)-1):
TypeError: 'list' object is not callable

计算两点之间距离的公式为: http://i.stack.imgur.com/vOcu0.png

然后我需要将它们全部添加起来!

1 个答案:

答案 0 :(得分:0)

从你提供的内容来看,我认为不需要过度复杂化。假设您可以正确导入文本文件数据并将其拆分,您可以这样做:

open class func creationRequestForAsset(from image: UIImage) -> Self
open class func creationRequestForAssetFromImage(atFileURL fileURL: URL) -> Self?
open class func creationRequestForAssetFromVideo(atFileURL fileURL: URL) -> Self?

我认为沿着这些方向的东西会运行良好(你甚至不需要将坐标放在元组中)。这是否符合你的需要?