Files
gpx_analyzer/script.py

85 lines
2.4 KiB
Python

from matplotlib.path import Path
from datetime import datetime
import gpxpy, gpxpy.gpx, sys, csv, os, os.path
class Region:
def __init__(self, boundary, name, timeSpent=0):
self.boundary = boundary
self.name = name
self.timeSpent = timeSpent
def _checkBoundary(self, point):
boundary = Path(self.boundary)
return boundary.contains_point(point)
def _addTimeSpent(self, timeSpent):
self.timeSpent += timeSpent
class Point:
def __init__(self, coordinates, date):
self.coordinates = coordinates
self.date = date
def importGPX():
def tracksFileImport(filePath):
gpx_file = open(filePath, 'r')
global gpx
gpx = gpxpy.parse(gpx_file)
for files in os.listdir('tracks'):
tracksFileImport(''.join(['tracks/',files]))
global points
points = []
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
point = Point((point.longitude, point.latitude), point.time.timestamp())
points.append(point)
def importRegions():
global regions
regions = []
def regionFileImport(filePath, name):
with open(filePath, mode ='r') as file:
csvFile = csv.reader(file)
next(csvFile, None) # Skip header
boundary = []
for row in csvFile:
# must be long, lat
boundary.extend([[row[2],row[3]]])
region = Region(boundary, name)
regions.append(region)
for files in os.listdir('regions'):
regionFileImport(''.join(['regions/',files]), files[:-4]) # Import every file, and get name from splice filename
def calcTimePerRegion():
# The idea is to iterate through every point, if the point is inside a given region,
# then time has to be added to the total spent time, this is done by subtracting the
# point's time to the last considered.
lastPointDate = points[0].date
for point in points:
for region in regions:
if region._checkBoundary(point.coordinates):
region.timeSpent += (point.date - lastPointDate)
lastPointDate = point.date
def printResults():
for region in regions:
print("Time spent on ", region.name, ":", round(region.timeSpent / 60), "minutes")
def main():
importRegions()
importGPX()
calcTimePerRegion()
printResults()
main()