-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpx_to_csv.py
64 lines (52 loc) · 1.85 KB
/
gpx_to_csv.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
'''
Convert a directory of gpx files to csv.
The script captures trek points with coordinates,
elevation and time stamp as trek name and weather conditions.
The script requires an input directory.
'''
from bs4 import BeautifulSoup
import csv
import sys
import pathlib
import os
if len(sys.argv) < 2:
sys.exit("Must supply an input directory")
inPath = sys.argv[1]
def getFiles(inPath):
return [os.path.join(inPath, f) for f in os.listdir(inPath) if f.endswith(".gpx")]
files = getFiles(inPath)
cfile = str(os.path.join(inPath, 'combined.csv'))
print(cfile, type(cfile))
with open(cfile, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Id', 'Name', 'Lat', 'Lon', 'Elev', 'Time',
'Temp', 'Weather'])
for gpx_file in files:
with open(gpx_file, 'r') as f:
contents = f.read()
soup = BeautifulSoup(contents, 'xml')
tracks = soup.find_all('trkpt')
elevations = soup.find_all('ele')
times = soup.find_all('time')
temp = soup.find('s2t:temperature').text
weather = soup.find('s2t:weather').text
name = soup.find('name').text
sf_name = os.path.splitext(gpx_file)[0]
id = os.path.split(sf_name)[1]
csv_file = sf_name + '.csv'
data = []
for track, elevation, time in zip(tracks, elevations, times):
latitude = track['lat']
longitude = track['lon']
elevation_value = elevation.text
time = time.text
data.append([id, name, latitude, longitude, elevation_value,
time, temp, weather])
with open(csv_file, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['Id', 'Name', 'Lat', 'Lon', 'Elev', 'Time',
'Temp', 'Weather'])
writer.writerows(data)
with open(cfile, 'a', newline='') as f:
writer = csv.writer(f)
writer.writerows(data)