I have a .csv file with 10 columns. The first column is titled Distance_Between_Points, the second column is titled Time_Between_Points, and the third column is titled Speed_Between_Points and all three do not contain data. The other columns contain other attributes that I want to keep in the output file, including lat/long coordinates and timestamps for each data point:
Distance_Between_Points,Time_Between_Points,Speed_Between_Points,Longitude,Latitude,Point_Index,Timestamp,Name,Number,Place
,,,-80.60483,39.31117,1, 1/29/2000 17:00,name1,number1,place1
,,,-80.58167,39.3045,2, 1/29/2000 18:00,name1,number1,place1
,,,-80.52883,39.24917,3 1/29/2000 19:00,name1,number1,place1
,,,-80.508,39.14917,4 1/29/2000 20:00,name1,number1,place1
,,,-80.49617,39.13117,5, 1/29/2000 21:00,name1,number1,place1
I have wrote the following script and I receive a ValueError: too many values to unpack.
import mathdef distance_on_unit_sphere(lat1, long1, lat2, long2):# Converts lat & long to spherical coordinates in radians.degrees_to_radians = math.pi/180.0# phi = 90 - latitudephi1 = (90.0 - lat1)*degrees_to_radiansphi2 = (90.0 - lat2)*degrees_to_radians# theta = longitudetheta1 = long1*degrees_to_radianstheta2 = long2*degrees_to_radians# Compute the spherical distance from spherical coordinates.# For two locations in spherical coordinates:# (1, theta, phi) and (1, theta', phi')cosine( arc length ) =# sin phi sin phi' cos(theta-theta') + cos phi cos phi' distance = rho * arc lengthcos = (math.sin(phi1)*math.sin(phi2)*math.cos(theta1 - theta2) + math.cos(phi1)*math.cos(phi2))arc = math.acos(cos)*6371.0return arcimport csvimport collectionsfrom datetime import datetimefilein = open('input.csv', 'rb')fileout = open('output.csv', 'wb') fileout.write("Distance_Between_Points,Time_Between_Points,Speed_Between_Points")csvrdr = csv.reader(filein, delimiter=",")linecount = 0todo = collections.deque()lat1 = 39.31117long1 = -80.60483time1 = "1/29/2000 17:00"time2 = "1/29/2000 18:00"datetime.strptime(time1, '%m/%d/%y %H:%M')datetime.strptime(time2, '%m/%d/%y %H:%M')for line in csvrdr: if linecount > 0: # skip header _, Longitude, Latitude = line # _ means discard Longitude = float(Longitude) Latitude = float(Latitude) todo.append((Longitude, Latitude))if len(todo) == 2:# long2, lat2 = todo.popleft() Distance_Between_Points = distance_on_unit_sphere(lat1, long1, lat2, long2) long1 = long2 lat1 = lat2 time1 = time2 Time_Between_Points = time2 - time1 Speed_Between_Points = Distance_Between_Points / Time_Between_Points fileout.write("%2.4f,%2.4f,%2.4f\n" % (Distance_Between_Points, Time_Between_Points, Speed_Between_Points)) #modify compute things first then write themlinecount += 1assert(len(todo) == 1)long2, lat2 = todo.popleft()fileout.write("%2.4f,%2.4f,%2.4f\n" % (0.0, long2, lat2)) #modifyfileout.close()filein.close()I believe the error is related to not using numpy arrays, which I do not have much experience with.
My goal is to populate the empty columns (Distance_Between_Points,Time_Between_Points, and Speed_Between_Points) and also write all the original data from the input.csv to the output.csv using Python.
أكثر...
Distance_Between_Points,Time_Between_Points,Speed_Between_Points,Longitude,Latitude,Point_Index,Timestamp,Name,Number,Place
,,,-80.60483,39.31117,1, 1/29/2000 17:00,name1,number1,place1
,,,-80.58167,39.3045,2, 1/29/2000 18:00,name1,number1,place1
,,,-80.52883,39.24917,3 1/29/2000 19:00,name1,number1,place1
,,,-80.508,39.14917,4 1/29/2000 20:00,name1,number1,place1
,,,-80.49617,39.13117,5, 1/29/2000 21:00,name1,number1,place1
I have wrote the following script and I receive a ValueError: too many values to unpack.
import mathdef distance_on_unit_sphere(lat1, long1, lat2, long2):# Converts lat & long to spherical coordinates in radians.degrees_to_radians = math.pi/180.0# phi = 90 - latitudephi1 = (90.0 - lat1)*degrees_to_radiansphi2 = (90.0 - lat2)*degrees_to_radians# theta = longitudetheta1 = long1*degrees_to_radianstheta2 = long2*degrees_to_radians# Compute the spherical distance from spherical coordinates.# For two locations in spherical coordinates:# (1, theta, phi) and (1, theta', phi')cosine( arc length ) =# sin phi sin phi' cos(theta-theta') + cos phi cos phi' distance = rho * arc lengthcos = (math.sin(phi1)*math.sin(phi2)*math.cos(theta1 - theta2) + math.cos(phi1)*math.cos(phi2))arc = math.acos(cos)*6371.0return arcimport csvimport collectionsfrom datetime import datetimefilein = open('input.csv', 'rb')fileout = open('output.csv', 'wb') fileout.write("Distance_Between_Points,Time_Between_Points,Speed_Between_Points")csvrdr = csv.reader(filein, delimiter=",")linecount = 0todo = collections.deque()lat1 = 39.31117long1 = -80.60483time1 = "1/29/2000 17:00"time2 = "1/29/2000 18:00"datetime.strptime(time1, '%m/%d/%y %H:%M')datetime.strptime(time2, '%m/%d/%y %H:%M')for line in csvrdr: if linecount > 0: # skip header _, Longitude, Latitude = line # _ means discard Longitude = float(Longitude) Latitude = float(Latitude) todo.append((Longitude, Latitude))if len(todo) == 2:# long2, lat2 = todo.popleft() Distance_Between_Points = distance_on_unit_sphere(lat1, long1, lat2, long2) long1 = long2 lat1 = lat2 time1 = time2 Time_Between_Points = time2 - time1 Speed_Between_Points = Distance_Between_Points / Time_Between_Points fileout.write("%2.4f,%2.4f,%2.4f\n" % (Distance_Between_Points, Time_Between_Points, Speed_Between_Points)) #modify compute things first then write themlinecount += 1assert(len(todo) == 1)long2, lat2 = todo.popleft()fileout.write("%2.4f,%2.4f,%2.4f\n" % (0.0, long2, lat2)) #modifyfileout.close()filein.close()I believe the error is related to not using numpy arrays, which I do not have much experience with.
My goal is to populate the empty columns (Distance_Between_Points,Time_Between_Points, and Speed_Between_Points) and also write all the original data from the input.csv to the output.csv using Python.
أكثر...