Personal intranet/start page where I can view the weather, links, fitbit data, and the news.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.8 KiB

  1. from bs4 import BeautifulSoup
  2. import glob
  3. import os.path
  4. def parseTCX(filename):
  5. file = open(filename)
  6. xml_file = file.read()
  7. soup = BeautifulSoup(xml_file, 'lxml')
  8. id = soup.find("id").text
  9. lats = []
  10. longs = []
  11. for tag in soup.find_all("trackpoint"):
  12. lats.append(tag.find("latitudedegrees").text)
  13. longs.append(tag.find("longitudedegrees").text)
  14. return id[:-10], lats, longs
  15. def single_run(id, lats, longs):
  16. locString = ""
  17. for i in range(0, len(lats)):
  18. locString += longs[i] + "," + lats[i] + " "
  19. value = """
  20. <Placemark>
  21. <name>{0}</name>
  22. <description>xx Miles</description>
  23. <Style>
  24. <LineStyle>
  25. <color>ff0000e6</color>
  26. <width>4</width>
  27. </LineStyle>
  28. </Style>
  29. <LineString>
  30. <tessellate>1</tessellate>
  31. <altitudeMode>clampToGround</altitudeMode>
  32. <coordinates>{1}</coordinates>
  33. </LineString>
  34. </Placemark>
  35. """
  36. return value.format(id, locString)
  37. def convertToKML():
  38. base_path = os.path.dirname(os.path.realpath(__file__))
  39. files = glob.glob(base_path + "/tcx/*.tcx")
  40. header = """<?xml version="1.0" encoding="utf-8" standalone="yes"?>
  41. <kml xmlns="http://www.opengis.net/kml/2.2">
  42. <Document>
  43. <name><![CDATA[38415617200]]></name>
  44. <visibility>1</visibility>
  45. <open>1</open>
  46. <Folder id="Runs">
  47. <name>Tracks</name>
  48. <visibility>1</visibility>
  49. <open>0</open>
  50. """
  51. footer = """
  52. </Folder>
  53. </Document>
  54. </kml>
  55. """
  56. o_file = open("outputKML.kml", "w")
  57. o_file.write(header)
  58. for file in files:
  59. id, lats, longs = parseTCX(file)
  60. o_file.write(single_run(id, lats, longs))
  61. print(files)
  62. o_file.write(footer)
  63. o_file.close()
  64. convertToKML()