Python Puzzle - measuring velocity | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python Puzzle - measuring velocity

I have a csv with the following columns: 1. ID 2. Time - includes a date and exact time (minutes and such) 3. Latitude 4. Longitude Now, the data is from a mobile phone and represents movement. I am writing a script, an algorithm to solve the following puzzle. I want to calculate the velocity of the movement. To do that, I need to calculate the distance according to the lat and longitude. Each ID represents a mobile phone but it stays the same during different activities. By activity I mean a movement. So, let's say you had a walk in the morning and a run in the evening. They are under the same ID but different times. So, in my loop, I need to also separate according to time. The issue with the date column is that I probably have to separate it into two rows or maybe I don't have to? So, I know how to calculate the distance between two points (and velocity is easy) but I need to add it up according to the ID and the timing. So, in order to solve the issue with the different activities by the same ID (walk in the morning and afternoon, evening, etc.), I need to add a condition that time in row += 1 should be between 1 minute and 5 minutes difference (or something like that). I think my best option is to groupby id and time via pandas. import geopy.distance import pandas as pd import matplotlib.pyplot as plt # coords_1 = (52.2296756, 21.0122287) # coords_2 = (52.406374, 16.9251681) def calc_minutes(time_end, time_start): duration = (time_end - time_start).seconds minutes = duration/60 return minutes def calc_velocity(dist_km, time_start, time_end): """Return 0 if time_start == time_end, avoid dividing by 0""" if time_end > time_start: duration = (time_end - time_start).seconds return dist_km / duration else: return 0 def calculate_distance(lat1, lon1, lat2, lon2): coords_1 = (lat1, lon1) coords_2 = (lat2, lon2) d = geopy.distance.vincenty(coords_1, coords_2).km return d *Continuation

10th Jun 2019, 10:18 PM
Matija Kordic
Matija Kordic - avatar
2 Answers
0
*Continuation headers = ['time', 'id', 'lat', 'lon'] dtypes = {'time': 'str', 'id': 'str', 'lat': 'float', 'lon': 'float'} parse_dates = ['time'] # df = pd.read_csv('C:\AR\matija\python_velocityO.csv') df = pd.read_csv('C:\AR\matija\python_velocity.csv', header=None, names=headers, dtype=dtypes, parse_dates=parse_dates) df = df.sort_values(by=['id', 'time']) # # Group the sorted dataframe by ID, and grab the initial value for lat, lon, and time. df['lat0'] = df.groupby('id')['lat'].transform(lambda x: x.iat[0]) df['lon0'] = df.groupby('id')['lon'].transform(lambda x: x.iat[0]) df['t0'] = df.groupby('id')['time'].transform(lambda x: x.iat[0]) # create a new column for distance df['dist_km'] = df.apply( lambda row: calculate_distance( lat1=row['lat'], lon1=row['lon'], lat2=row['lat0'], lon2=row['lon0'] ), axis=1 ) df['velocity_kmps'] = df.apply( lambda row: calc_velocity( dist_km=row['dist_km'], time_start=row['t0'], time_end=row['time'] ), axis=1 ) df['minutes'] = df.apply( lambda row: calc_minutes( time_start=row['t0'], time_end=row['time'] ), axis=1 ) df['t_time'] = df.groupby(['id'])['minutes'].sum() print(df) # plt.plot(df['id'], df['dist_km']) # plt.ylabel('some numbers') # plt.show() # print (CalculateDistance(coords_1, coords_2)) Also this: while ID == ID in row+=1 and 1>t<5 row =1 calculate distance add distance calculate time difference calculate velocity row += 1
10th Jun 2019, 10:21 PM
Matija Kordic
Matija Kordic - avatar
0
Any ideas on how do I proceed?
10th Jun 2019, 10:27 PM
Matija Kordic
Matija Kordic - avatar