100 lines
2.4 KiB
Python
100 lines
2.4 KiB
Python
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))
|
|
|
|
|