From 683f21be2020e1bda9131c64050e5a5515e03f2e Mon Sep 17 00:00:00 2001 From: masiv_ Date: Tue, 10 Feb 2026 17:21:17 -0300 Subject: [PATCH] first commit --- .gitignore | 6 +++ requirements.txt | 5 +++ script.py | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 .gitignore create mode 100644 requirements.txt create mode 100644 script.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..acc3e2b --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.gpkg +*.gpx +.venv/ +.vscode/ +conf.py +__pycache__ \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..45febcb --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +matplotlib +gpxpy +python-dateutil +datetime +geopandas \ No newline at end of file diff --git a/script.py b/script.py new file mode 100644 index 0000000..cd2bd40 --- /dev/null +++ b/script.py @@ -0,0 +1,99 @@ +from matplotlib.path import Path +from datetime import datetime + +import gpxpy +import gpxpy.gpx + +import geopandas as gpd + +import sys + +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, name='', region=0): + self.coordinates = coordinates + self.date = date + self.name = name + self.region = region + +def importGPX(): + gpx_file = open('thisyear.gpx', 'r') + global gpx + gpx = gpxpy.parse(gpx_file) + +def importRegions(): + areas = gpd.read_file('regions.gpkg') + areas.head() + + + +def initVariables(): + #importRegions() + importGPX() + + # temp import for regions, have to implement gpx eventually, such file currenly contains a series of polygons + from conf import Regions + initRegions = Regions + + global regions + global points + regions = [] + points = [] + + for boundary, name in initRegions: + region = Region(boundary, name) + regions.append(region) + + 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) + + #for coordinates, date in initPoints: + # point = Point(coordinates, date) + # points.append(point) + +def addTimeToRegion(region, time=0): + region.timeSpent += time + +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): + addTimeToRegion(region, (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(): + initVariables() + calcTimePerRegion() + printResults() + +main() + + +# print(a._checkBoundary(point.coordinates)) + +