Meine Klasse Track() errechnet von einem GPS-aufgezeichneten Weg (GPX-file im XML-Format) die Gesamtstrecke, die dazu benötigte Zeit und plottet die jeweiligen Geschwindigkeiten zwischen den waypoints. Ziel nun: Eine Funktion schreiben, die testet, ob die übergebene GPX-Datei evtl. 0 tracks oder 0 waypoints oder 0 routes hat. Wo soll ich diese Funktion ansiedeln? Der Konstruktor __init__(self, gpxdata) ist für das Erstellen eines Dataframes verantwortlich. Wo soll die Funktion aufgerufen werden?
Code: Alles auswählen
from __future__ import print_function, division
import gpxpy
import pandas as pd
from geopy import distance
import matplotlib.pyplot as plt
import os
import numpy as np
class Track():
def __init__(self, gpxdata):
"""
constructor, creates dataframe
"""
data = gpxdata.tracks[0].segments[0].points
df = []
for point in data:
df.append({'lon': point.longitude, 'lat' : point.latitude, 'alt' : point.elevation, 'time' : point.time})
self.df = pd.DataFrame(df, columns=['lon', 'lat', 'alt', 'time'])
# public methods
def total_distance(self):
"""
Calculates the total distance of a list of geographic points
Input: list of geographic points [(lat1,lon1), (lat2, lon2), ..., (lat_n, lon_n)]
Output: total distance in km, i.e. d(p2,p1) + d(p3,p2) + .... + d(p_n,p_(n-1)), where d(x,y) is
the distance of points x and y
"""
df = self.df
geopoints = list(zip(df['lat'], df['lon']))
Npoints = len(geopoints)
total_distance = 0
for i in range(1,Npoints):
total_distance += distance.distance(geopoints[i], geopoints[i-1]).m
return total_distance
def total_elapsed_time(self):
"""
calculates the total time elapsed of the track walked
"""
Npoints = self.df.shape[0]
startTime = self.df['time'][0]
endTime = self.df['time'][Npoints-1]
total_elapsed_time = endTime - startTime
return total_elapsed_time
def plot_velocities(self):
"""
creates a graphical plot of all velocities between each of the two trackpoints
"""
lstVelocities = self.__create_velocities()
lstDistancesAddedTogether = self.__distances_cumul_sum()
plt.plot(lstDistancesAddedTogether, lstVelocities)
plt.xlabel('Distance [km]')
plt.ylabel('Velocity [km/h]')
def print_summary(self, total_distance, total_elapsed_time, plot):
print('Total distance:', total_distance/1000, 'kilometer')
print('Total elapsed time.', total_elapsed_time, 'hh:mm:ss')
print('graph of velocities:')
# private methods
def __create_distances(self):
"""
all single distances are calculated, necessary for velocity-calculation
"""
df = self.df
# lstDistances = []
maxTrkPoints = df.shape[0]
return [distance.distance((df['lat'][i], df['lon'][i]), (df['lat'][i+1], df['lon'][i+1])).km for i in range(1, maxTrkPoints-1)]
def __create_velocities(self):
"""
creates a list of all velocities based on seconds and appropriate distances
"""
lstSeconds = self.__create_seconds()
lstDistances = self.__create_distances()
Npoints = self.df.shape[0]
return [((lstDistances[i]*1000) / lstSeconds[i].total_seconds()*3.6) for i in range(0, Npoints-2)]
def __create_seconds(self):
"""
all single seconds are calculated, necessary for velocity-calculation
"""
maxTrkPoints = self.df.shape[0]
lstTimestamps = [(self.df['time'][i]) for i in range(0, maxTrkPoints)]
return [(lstTimestamps[i+1] - lstTimestamps[i]) for i in range(1, maxTrkPoints-1)]
def __distances_cumul_sum(self):
"""
creates distance values by using the cumulative sum of all distances
"""
lstDistances = self.__create_distances()
return np.cumsum(lstDistances)
def main():
filename, file_extension = os.path.splitext("Aljibe_Picacho_2017-03-08_10-32-52.gpx")
try:
gpx_file = open("Aljibe_Picacho_2017-03-08_10-32-52.gpx", "r") # gpx_file is opened for reading
except file_extension != '.gpx':
raise FileExistsError
gpxdata = gpxpy.parse(gpx_file)
mytrack = Track(gpxdata)
# checkresult = mytrack.check_filetype(gpx_file)
total_distance = mytrack.total_distance()
total_elapsed_time = mytrack.total_elapsed_time()
plot = mytrack.plot_velocities()
mytrack.print_summary(total_distance, total_elapsed_time, plot)
if __name__ == "__main__":
main()
Strawk