Improvements all over the place, now input is controlled by files in regions and tracks, algorithm has to improve to get better performance, it is too slow as it currently stands to manage all geometry checks currently done.
This commit is contained in:
73
script.py
73
script.py
@@ -1,12 +1,7 @@
|
||||
from matplotlib.path import Path
|
||||
from datetime import datetime
|
||||
|
||||
import gpxpy
|
||||
import gpxpy.gpx
|
||||
|
||||
import geopandas as gpd
|
||||
|
||||
import sys
|
||||
import gpxpy, gpxpy.gpx, sys, csv, os, os.path
|
||||
|
||||
class Region:
|
||||
def __init__(self, boundary, name, timeSpent=0):
|
||||
@@ -22,39 +17,21 @@ class Region:
|
||||
self.timeSpent += timeSpent
|
||||
|
||||
class Point:
|
||||
def __init__(self, coordinates, date, name='', region=0):
|
||||
def __init__(self, coordinates, date):
|
||||
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 tracksFileImport(filePath):
|
||||
gpx_file = open(filePath, 'r')
|
||||
global gpx
|
||||
gpx = gpxpy.parse(gpx_file)
|
||||
|
||||
def importRegions():
|
||||
areas = gpd.read_file('regions.gpkg')
|
||||
areas.head()
|
||||
for files in os.listdir('tracks'):
|
||||
tracksFileImport(''.join(['tracks/',files]))
|
||||
|
||||
|
||||
|
||||
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:
|
||||
@@ -62,12 +39,25 @@ def initVariables():
|
||||
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 importRegions():
|
||||
global regions
|
||||
regions = []
|
||||
|
||||
def addTimeToRegion(region, time=0):
|
||||
region.timeSpent += time
|
||||
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,
|
||||
@@ -77,23 +67,18 @@ def calcTimePerRegion():
|
||||
for point in points:
|
||||
for region in regions:
|
||||
if region._checkBoundary(point.coordinates):
|
||||
addTimeToRegion(region, (point.date - lastPointDate))
|
||||
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():
|
||||
initVariables()
|
||||
importRegions()
|
||||
importGPX()
|
||||
calcTimePerRegion()
|
||||
printResults()
|
||||
|
||||
main()
|
||||
|
||||
|
||||
# print(a._checkBoundary(point.coordinates))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user