gegps.in 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!@PYSHEBANG@
  2. # -*- coding: utf-8 -*-
  3. # @GENERATED@
  4. '''
  5. usage: gegps [-i] [-d kmldir]
  6. Feed location data from a running GPSD to a Google Earth instance.
  7. The -d argument is the location of the Google Earth installation
  8. directory. If not specified, it defaults to the current directory.
  9. If you have the free (non-subscription) version, start by running with
  10. the -i option to drop a clue in the Google Earth installation directory,
  11. as 'Open_in_Google_Earth_RT_GPS.kml', then open that file in Places
  12. (File > Open...),
  13. The basic recipe is here:
  14. http://tjworld.net/wiki/Linux/Ubuntu/GoogleEarthPlusRealTimeGPS
  15. This code originally by Jaroslaw Zachwieja and a guy referring
  16. to himself/herself as TJ(http://tjworld.net)
  17. Modified by Chen Wei <weichen302@aol.com> for use with gpsd
  18. Cleaned up and adapted for the GPSD project by Eric S. Raymond.
  19. '''
  20. # This file is Copyright 2010 by the GPSD project
  21. # SPDX-License-Identifier: BSD-2-clause
  22. # This code runs compatibly under Python 2 and 3.x for x >= 2.
  23. # Preserve this property!
  24. from __future__ import absolute_import, print_function, division
  25. import getopt
  26. import os
  27. import socket # for socket.error
  28. import sys
  29. # pylint wants local modules last
  30. try:
  31. import gps
  32. except ImportError as e:
  33. sys.stderr.write(
  34. "gegps: can't load Python gps libraries -- check PYTHONPATH.\n")
  35. sys.stderr.write("%s\n" % e)
  36. sys.exit(1)
  37. gps_version = '@VERSION@'
  38. if gps.__version__ != gps_version:
  39. sys.stderr.write("gegps: ERROR: need gps module version %s, got %s\n" %
  40. (gps_version, gps.__version__))
  41. sys.exit(1)
  42. KML_OPEN_IN_GE = '''\
  43. <?xml version="1.0" encoding="UTF-8"?>
  44. <kml xmlns="http://earth.google.com/kml/2.2">
  45. <NetworkLink>
  46. <name>Realtime GPS</name>
  47. <open>1</open>
  48. <Link>
  49. <href>Realtime_GPS.kml</href>
  50. <refreshMode>onInterval</refreshMode>
  51. </Link>
  52. </NetworkLink>
  53. </kml>
  54. '''
  55. def kmlize(tpv):
  56. '''http://code.google.com/apis/kml/documentation/kmlreference.html
  57. for official kml document'''
  58. latitude = tpv['lat']
  59. longitude = tpv['lon']
  60. # not all TPV includes speed, like when acquiring fix
  61. if 'speed' in tpv:
  62. speed_in = tpv['speed'] # meter/second
  63. speed = speed_in * gps.MPS_TO_KPH # Km/h
  64. else:
  65. speed = 0
  66. # not all TPV includes heading, like when acquiring fix
  67. if speed >= 1 and 'track' in tpv:
  68. heading = int(round(tpv['track'], 0))
  69. else:
  70. heading = 0
  71. # not all TPV includes altitude
  72. # like ublox8 in fixed position (time) mode
  73. if 'alt' in tpv:
  74. altitude = tpv['alt']
  75. else:
  76. altitude = 0
  77. return """<?xml version="1.0" encoding="UTF-8"?>
  78. <kml xmlns="http://earth.google.com/kml/2.0">
  79. <Placemark>
  80. <name>%s km/h,heading %s</name>
  81. <description>Realtime GPS feeding</description>
  82. <LookAt>
  83. <longitude>%s</longitude>
  84. <latitude>%s</latitude>
  85. </LookAt>
  86. <Point>
  87. <coordinates>%s,%s,%s</coordinates>
  88. </Point>
  89. </Placemark>
  90. </kml>""" % (speed, heading, longitude, latitude, longitude,
  91. latitude, altitude)
  92. if __name__ == "__main__":
  93. def usage():
  94. "Print usage and exit"
  95. sys.stderr.write("usage: gegps: [-d] [-h] [-i] [-V]\n\n"
  96. " -d directory Location of Google Earth\n"
  97. " -h Print this help and exit\n"
  98. " -i\n"
  99. " -V Print version and exit\n")
  100. sys.exit(0)
  101. kmldir = "."
  102. initialize = False
  103. (options, arguments) = getopt.getopt(sys.argv[1:], "d:hiV")
  104. for (opt, arg) in options:
  105. if opt == '-d':
  106. kmldir = arg
  107. elif opt == '-h':
  108. usage()
  109. elif opt == '-i':
  110. initialize = True
  111. elif opt == '-V':
  112. sys.stderr.write("gegps: Version %s\n" % gps_version)
  113. sys.exit(0)
  114. try:
  115. session = gps.gps()
  116. except socket.error:
  117. sys.stderr.write("gegps: Could not connect to gpsd daemon\n")
  118. sys.exit(1)
  119. session.stream(gps.WATCH_ENABLE)
  120. if initialize:
  121. f = open(os.path.join(kmldir, 'Open_in_Google_Earth_RT_GPS.kml'), 'w')
  122. f.write(KML_OPEN_IN_GE)
  123. f.close()
  124. else:
  125. try:
  126. while True:
  127. report = session.next()
  128. if report['class'] == 'TPV':
  129. f = open(os.path.join(kmldir, 'Realtime_GPS.kml'), 'w')
  130. f.write(kmlize(report))
  131. f.close()
  132. except StopIteration:
  133. pass
  134. except KeyboardInterrupt:
  135. print('gegps stopped ')
  136. # end
  137. # vim: set expandtab shiftwidth=4