gegps 4.5 KB

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