-
Notifications
You must be signed in to change notification settings - Fork 39
/
gegps.in
executable file
·159 lines (136 loc) · 4.61 KB
/
gegps.in
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!@PYSHEBANG@
# -*- coding: utf-8 -*-
# @GENERATED@
'''
usage: gegps [-i] [-d kmldir]
Feed location data from a running GPSD to a Google Earth instance.
The -d argument is the location of the Google Earth installation
directory. If not specified, it defaults to the current directory.
If you have the free (non-subscription) version, start by running with
the -i option to drop a clue in the Google Earth installation directory,
as 'Open_in_Google_Earth_RT_GPS.kml', then open that file in Places
(File > Open...),
The basic recipe is here:
http://tjworld.net/wiki/Linux/Ubuntu/GoogleEarthPlusRealTimeGPS
This code originally by Jaroslaw Zachwieja and a guy referring
to himself/herself as TJ(http://tjworld.net)
Modified by Chen Wei <weichen302@aol.com> for use with gpsd
Cleaned up and adapted for the GPSD project by Eric S. Raymond.
'''
# This file is Copyright (c) 2010-2020 by the GPSD project
# SPDX-License-Identifier: BSD-2-clause
# This code runs compatibly under Python 2 and 3.x for x >= 2.
# Preserve this property!
from __future__ import absolute_import, print_function, division
import getopt
import os
import socket # for socket.error
import sys
# pylint wants local modules last
try:
import gps
except ImportError as e:
sys.stderr.write(
"gegps: can't load Python gps libraries -- check PYTHONPATH.\n")
sys.stderr.write("%s\n" % e)
sys.exit(1)
gps_version = '@VERSION@'
if gps.__version__ != gps_version:
sys.stderr.write("gegps: ERROR: need gps module version %s, got %s\n" %
(gps_version, gps.__version__))
sys.exit(1)
KML_OPEN_IN_GE = '''\
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<NetworkLink>
<name>Realtime GPS</name>
<open>1</open>
<Link>
<href>Realtime_GPS.kml</href>
<refreshMode>onInterval</refreshMode>
</Link>
</NetworkLink>
</kml>
'''
def kmlize(tpv):
'''http://code.google.com/apis/kml/documentation/kmlreference.html
for official kml document'''
latitude = tpv['lat']
longitude = tpv['lon']
# not all TPV includes speed, like when acquiring fix
if 'speed' in tpv:
speed_in = tpv['speed'] # meter/second
speed = speed_in * gps.MPS_TO_KPH # Km/h
else:
speed = 0
# not all TPV includes heading, like when acquiring fix
if speed >= 1 and 'track' in tpv:
heading = int(round(tpv['track'], 0))
else:
heading = 0
# not all TPV includes altitude
# like ublox8 in fixed position (time) mode
if 'alt' in tpv:
altitude = tpv['alt']
else:
altitude = 0
return """<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0">
<Placemark>
<name>%s km/h,heading %s</name>
<description>Realtime GPS feeding</description>
<LookAt>
<longitude>%s</longitude>
<latitude>%s</latitude>
</LookAt>
<Point>
<coordinates>%s,%s,%s</coordinates>
</Point>
</Placemark>
</kml>""" % (speed, heading, longitude, latitude, longitude,
latitude, altitude)
if __name__ == "__main__":
def usage():
"Print usage and exit"
sys.stderr.write("usage: gegps: [-d] [-h] [-i] [-V]\n\n"
" -d directory Location of Google Earth\n"
" -h Print this help and exit\n"
" -i\n"
" -V Print version and exit\n")
sys.exit(0)
kmldir = "."
initialize = False
(options, arguments) = getopt.getopt(sys.argv[1:], "d:hiV")
for (opt, arg) in options:
if opt == '-d':
kmldir = arg
elif opt == '-h':
usage()
elif opt == '-i':
initialize = True
elif opt == '-V':
sys.stderr.write("gegps: Version %s\n" % gps_version)
sys.exit(0)
try:
session = gps.gps()
except socket.error:
sys.stderr.write("gegps: Could not connect to gpsd daemon\n")
sys.exit(1)
session.stream(gps.WATCH_ENABLE)
if initialize:
f = open(os.path.join(kmldir, 'Open_in_Google_Earth_RT_GPS.kml'), 'w')
f.write(KML_OPEN_IN_GE)
f.close()
else:
try:
while True:
report = session.next()
if report['class'] == 'TPV':
f = open(os.path.join(kmldir, 'Realtime_GPS.kml'), 'w')
f.write(kmlize(report))
f.close()
except StopIteration:
pass
except KeyboardInterrupt:
print('gegps stopped ')
# end