This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
forked from galens/playback-gpx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayback-gpx.py
executable file
·107 lines (90 loc) · 4.06 KB
/
playback-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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
import logging
from optparse import OptionParser
import os
import platform
import gpxpy
import gpxpy.gpx
import time
from subprocess import check_output
# python playback-gpx.py -i 1 -r "-r 192.168.56.102" "docs/North On Topanga(1).gpx"
# python playback-gpx.py -i 1 -r "-r 192.168.56.101" "docs/South On Topanga(1).gpx"
# 07/29/2016
# added requirements.txt
# Fix "Returned non-zero exit status" error
# Fix indentation
# 08/21/2014
# added returnDefaultPath function so the script is more os independent
# added -r option so users can pass extra flags to geanyshell like an ip address
# added ability to pause execution of script with keyboard interrupt
def format_decimal(num):
if num is None:
return str(0)
return str(num).replace('.', ',')
def returnDefaultPath():
if(platform.system() == 'Linux'):
return "/usr/bin/genyshell"
elif(platform.system() == 'Windows'):
return "C:\Program Files\Genymobile\Genymotion\genyshell.exe"
elif(platform.system() == 'Darwin'):
return "/Applications/Genymotion Shell.app/Contents/MacOS/genyshell"
def process_file(path, options):
logging.info("processing " + path)
gpx_file = open(path, 'r')
gpx = gpxpy.parse(gpx_file)
for track in gpx.tracks:
print track
for segment in track.segments:
for point in segment.points:
set_point(point, options)
try:
time.sleep(options.interval)
except KeyboardInterrupt:
print '\nPausing... (Hit ENTER to continue, type quit or exit.)'
response = raw_input()
if (response == 'quit') or (response == 'exit'):
exit()
else:
continue
for route in gpx.routes:
for point in route:
set_point(point, options)
time.sleep(options.interval)
def set_point(point, options):
try:
logging.info('Point at ({0},{1},{2})'.format(point.latitude, point.longitude, point.elevation))
logging.debug(check_output([options.command, "-c", "gps setlatitude " + format_decimal(point.latitude), options.ipaddress]))
logging.debug(check_output([options.command, "-c", "gps setlongitude " + format_decimal(point.longitude), options.ipaddress]))
logging.debug(check_output([options.command, "-c", "gps setaltitude " + format_decimal(point.elevation), options.ipaddress]))
except KeyboardInterrupt:
print '\nPausing... (Hit ENTER to continue, type quit or exit.)'
try:
response = raw_input()
if (response == 'quit') or (response == 'exit'):
exit()
print 'Resuming...'
except KeyboardInterrupt:
print 'Resuming...'
if __name__ == '__main__':
usage = "usage: %prog "
parser = OptionParser(usage=usage,
description="Read a gpx file and use it to send points to Genymotion emulator at a fixed interval")
parser.add_option("-d", "--debug", action="store_true", dest="debug")
parser.add_option("-q", "--quiet", action="store_true", dest="quiet")
parser.add_option("-i", "--interval", type="int", dest="interval", default="2",
help="interval between points in seconds, defaults to 2 seconds")
parser.add_option("-r", "--ip_address", dest="ipaddress",
help="any extra flags passed to genymotion shell",
default="")
parser.add_option("-g", "--genymotion-shell", dest="command",
help="path to the genyshell command",
default=returnDefaultPath())
(options, args) = parser.parse_args()
logging.basicConfig(level=logging.DEBUG if options.debug else
(logging.ERROR if options.quiet else logging.INFO))
logging.debug("using genymotion shell at " + options.command)
for path in args:
if not os.path.exists(path):
logging.error(path + " not found")
continue
process_file(path, options)