-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_gpx.py
93 lines (78 loc) · 2.93 KB
/
process_gpx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def getKey(customobj):
return customobj.getKey()
def copyDirectory(src, dest):
try:
shutil.copytree(src, dest)
# Directories are the same
except shutil.Error as e:
print('Directory not copied. Error: %s' % e)
# Any error saying that the directory doesn't exist
except OSError, e:
print('Directory not copied. Error: %s' % e)
source_folder = "MyTracks/ProcessedTracks"
destination_folder = "../angular-tracemysteps/ProcessedTracks"
if __name__ == '__main__':
import shutil
import gpxpy
import gpxpy.gpx
import glob
import psycopg2
import ppygis
import os
from time import mktime
import StringIO
import datetime
import gpxpy
import gpxpy.gpx
import glob
import os
import time
import sys
print "STARTING..."
directory_name = 'MyTracks/'
saving_name = 'ProcessedTracks/'
saving_directory = os.path.join(directory_name, saving_name)
DATE_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
print datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
start_date = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
file_count = 0;
total_size = 0;
if not os.path.exists(saving_directory):
os.makedirs(saving_directory)
current_day = ""
for entry in sorted(glob.glob(directory_name + "*.[gG][pP][xX]")):
file = open(entry, 'rb')
gpx_xml = file.read()
file.close()
gpx = gpxpy.parse(gpx_xml)
for track in gpx.tracks:
for segment in sorted(track.segments):
if segment.points[0].time.strftime("%Y-%m-%d") != current_day:
name = 0
else:
name += 1
segment.simplify(5) # RDP
gpx_write = gpxpy.gpx.GPX()
# Create first track in our GPX:
gpx_track = gpxpy.gpx.GPXTrack()
# Create first segment in our GPX track:
gpx_segment = gpxpy.gpx.GPXTrackSegment()
gpx_track.segments.append(segment)
gpx_write.tracks.append(gpx_track)
current_day = segment.points[0].time.strftime("%Y-%m-%d")
fo = open("{}-part{}.gpx".format(os.path.join(saving_directory,current_day),name), "wb")
gpx_string = gpx_write.to_xml()
file_count+=1
total_size += sys.getsizeof(gpx_string) # bytes
fo.write(gpx_string)
fo.close()
print "generated nr files " , file_count
print "total size " , total_size
print datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
end_date = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
shutil.rmtree(destination_folder)
copyDirectory(source_folder, destination_folder)
print "ALL DATA IS PROCESSED AND EXPORTED.\nYOU CAN NOW CLOSE THIS."
print start_date
print "-------------------"
print end_date