gegps.py.in 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #!@PYSHEBANG@
  2. # -*- coding: utf-8 -*-
  3. # @GENERATED@
  4. # This file is Copyright 2010 by the GPSD project
  5. # SPDX-License-Identifier: BSD-2-clause
  6. # This code runs compatibly under Python 2 and 3.x for x >= 2.
  7. # Preserve this property!
  8. """Feed location data from a running GPSD to a Google Earth instance.
  9. This code originally by Jaroslaw Zachwieja and a guy referring
  10. to himself/herself as TJ(http://tjworld.net)
  11. Modified by Chen Wei <weichen302@aol.com> for use with gpsd
  12. Cleaned up and adapted for the GPSD project by Eric S. Raymond.
  13. """
  14. from __future__ import absolute_import, print_function, division
  15. import argparse
  16. import os
  17. import socket # for socket.error
  18. import sys
  19. # pylint wants local modules last
  20. try:
  21. import gps
  22. except ImportError as e:
  23. sys.stderr.write(
  24. "gegps: can't load Python gps libraries -- check PYTHONPATH.\n")
  25. sys.stderr.write("%s\n" % e)
  26. sys.exit(1)
  27. gps_version = '@VERSION@'
  28. if gps.__version__ != gps_version:
  29. sys.stderr.write("gegps: ERROR: need gps module version %s, got %s\n" %
  30. (gps_version, gps.__version__))
  31. sys.exit(1)
  32. KML_OPEN_IN_GE = '''\
  33. <?xml version="1.0" encoding="UTF-8"?>
  34. <kml xmlns="http://earth.google.com/kml/2.2">
  35. <NetworkLink>
  36. <name>Realtime GPS</name>
  37. <open>1</open>
  38. <Link>
  39. <href>Realtime_GPS.kml</href>
  40. <refreshMode>onInterval</refreshMode>
  41. </Link>
  42. </NetworkLink>
  43. </kml>
  44. '''
  45. def kmlize(tpv):
  46. """Kmlize.
  47. http://code.google.com/apis/kml/documentation/kmlreference.html
  48. for official kml document.
  49. """
  50. latitude = tpv['lat']
  51. longitude = tpv['lon']
  52. # not all TPV includes speed, like when acquiring fix
  53. if 'speed' in tpv:
  54. speed_in = tpv['speed'] # meter/second
  55. speed = speed_in * gps.MPS_TO_KPH # Km/h
  56. else:
  57. speed = 0
  58. # not all TPV includes heading, like when acquiring fix
  59. if speed >= 1 and 'track' in tpv:
  60. heading = int(round(tpv['track'], 0))
  61. else:
  62. heading = 0
  63. # not all TPV includes altitude
  64. # like ublox8 in fixed position (time) mode
  65. if 'alt' in tpv:
  66. altitude = tpv['alt']
  67. else:
  68. altitude = 0
  69. return """<?xml version="1.0" encoding="UTF-8"?>
  70. <kml xmlns="http://earth.google.com/kml/2.0">
  71. <Placemark>
  72. <name>%s km/h,heading %s</name>
  73. <description>Realtime GPS feeding</description>
  74. <LookAt>
  75. <longitude>%s</longitude>
  76. <latitude>%s</latitude>
  77. </LookAt>
  78. <Point>
  79. <coordinates>%s,%s,%s</coordinates>
  80. </Point>
  81. </Placemark>
  82. </kml>""" % (speed, heading, longitude, latitude, longitude,
  83. latitude, altitude)
  84. if __name__ == "__main__":
  85. description = 'Feed location data from GPSD to Google Earth.'
  86. usage = '%(prog)s [OPTIONS] [host[:port[:device]]]'
  87. epilog = ('BSD terms apply: see the file COPYING in the distribution root'
  88. ' for details.')
  89. parser = argparse.ArgumentParser(
  90. description=description,
  91. epilog=epilog,
  92. formatter_class=argparse.RawDescriptionHelpFormatter,
  93. usage=usage)
  94. parser.add_argument(
  95. '-?',
  96. action="help",
  97. help='show this help message and exit'
  98. )
  99. parser.add_argument(
  100. '-d', '--kmldir',
  101. default='.',
  102. dest='kmldir',
  103. help='Google Earth kml directory. [Default %(default)s]',
  104. )
  105. parser.add_argument(
  106. '-D',
  107. '--debug',
  108. default=0,
  109. dest='debug',
  110. help='Set level of debug. Must be integer. [Default %(default)s]',
  111. type=int,
  112. )
  113. parser.add_argument(
  114. '--device',
  115. default='',
  116. dest='device',
  117. help='The device to connect. [Default %(default)s]',
  118. )
  119. parser.add_argument(
  120. '--host',
  121. default='localhost',
  122. dest='host',
  123. help='The host to connect. [Default %(default)s]',
  124. )
  125. parser.add_argument(
  126. '-i', '--initialize',
  127. action='store_true',
  128. default=False,
  129. dest='initialize',
  130. help='Google Earth kml directory. [Default %(default)s]',
  131. )
  132. parser.add_argument(
  133. '--port',
  134. default=gps.GPSD_PORT,
  135. dest='port',
  136. help='The port to connect. [Default %(default)s]',
  137. )
  138. parser.add_argument(
  139. '-V', '--version',
  140. action='version',
  141. help='Output version to stderr, then exit',
  142. version="%(prog)s: Version " + gps_version + "\n",
  143. )
  144. parser.add_argument(
  145. 'target',
  146. help='[host[:port[:device]]]',
  147. nargs='?',
  148. )
  149. options = parser.parse_args()
  150. # the options host, port, device are set by the defaults
  151. if options.target:
  152. # override host, port and device with target
  153. arg = options.target.split(':')
  154. len_arg = len(arg)
  155. if len_arg == 1:
  156. (options.host,) = arg
  157. elif len_arg == 2:
  158. (options.host, options.port) = arg
  159. elif len_arg == 3:
  160. (options.host, options.port, options.device) = arg
  161. else:
  162. parser.print_help()
  163. sys.exit(0)
  164. try:
  165. session = gps.gps(host=options.host, port=options.port,
  166. verbose=options.debug)
  167. except socket.error:
  168. sys.stderr.write("gegps: Could not connect to gpsd daemon\n")
  169. sys.exit(1)
  170. session.stream(gps.WATCH_ENABLE)
  171. if options.initialize:
  172. f = open(os.path.join(options.kmldir,
  173. 'Open_in_Google_Earth_RT_GPS.kml'), 'w')
  174. f.write(KML_OPEN_IN_GE)
  175. f.close()
  176. else:
  177. try:
  178. while True:
  179. report = session.next()
  180. if report['class'] == 'TPV':
  181. f = open(os.path.join(options.kmldir,
  182. 'Realtime_GPS.kml'), 'w')
  183. f.write(kmlize(report))
  184. f.close()
  185. except StopIteration:
  186. pass
  187. except KeyboardInterrupt:
  188. print('gegps stopped ')
  189. # end
  190. # vim: set expandtab shiftwidth=4