geoconv.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env python3
  2. """
  3. # Geoconv - Coordinate converter
  4. # Copyright (c) 2009-2022 Michael Buesch <m@bues.ch>
  5. # Licensed under the GNU/GPL version 2 or later.
  6. """
  7. import sys
  8. def usage():
  9. print("Usage: %s 00 00 00, 00 00.000, 00.000, ..." % sys.argv[0])
  10. def main(argv):
  11. args = " ".join(argv[1:])
  12. args = args.split(',')
  13. if not args:
  14. usage()
  15. return 1
  16. for arg in args:
  17. arg = arg.replace('N', '')
  18. arg = arg.replace('E', '')
  19. arg = arg.replace('O', '')
  20. arg = arg.replace('*', '')
  21. arg = arg.replace('\'', '')
  22. arg = arg.replace('\"', '')
  23. try:
  24. # Parse the input value and convert to fractional degrees
  25. values = arg.split()
  26. if len(values) == 0:
  27. print("") # empty line
  28. continue
  29. elif len(values) == 3:
  30. # 00 00 00 (degree, minutes, seconds)
  31. degree = float(values[0])
  32. minutes = float(values[1])
  33. seconds = float(values[2])
  34. degree = degree + (minutes / 60) + (seconds / 3600)
  35. elif len(values) == 2:
  36. # 00 00.000 (degree, fractional minutes)
  37. degree = float(values[0])
  38. minutes = float(values[1])
  39. degree = degree + (minutes / 60)
  40. elif len(values) == 1:
  41. # 00.000 (factional degrees)
  42. degree = float(values[0])
  43. else:
  44. usage()
  45. return 1
  46. except ValueError:
  47. usage()
  48. return 1
  49. # Print the result in various formats
  50. minutes = (degree - int(degree)) * 60
  51. seconds = (minutes - int(minutes)) * 60
  52. print("%08.4f* | %03.0f* %06.3f' | %03.0f* %02.0f' %02.0f\"" %\
  53. (degree,
  54. float(int(degree)), minutes,
  55. float(int(degree)), float(int(minutes)), seconds))
  56. return 0
  57. if __name__ == "__main__":
  58. sys.exit(main(sys.argv))
  59. # vim: ts=4 sw=4 expandtab