Scarygami / location-history-json-converter

Convert the Location History JSON File from Google Takeout into a useable format
Apache License 2.0
481 stars 118 forks source link

New format? Some items don't have Lat/Long info #20

Closed milklineep closed 5 years ago

milklineep commented 5 years ago

Example data: (I edited out the lat long data for my privacy, in mine it has actual values)

{
  "locations" : [ {
    "timestampMs" : "1509317655000",
    "latitudeE7" : 000000000,
    "longitudeE7" : 00000000,
    "accuracy" : 47,
    "velocity" : 0,
    "heading" : 244,
    "altitude" : 139,
    "activity" : [ {
      "timestampMs" : "1509317648536",
      "activity" : [ {
        "type" : "STILL",
        "confidence" : 85
      }, {
        "type" : "IN_VEHICLE",
        "confidence" : 5
      }, {
        "type" : "ON_FOOT",
        "confidence" : 5
      }, {
        "type" : "WALKING",
        "confidence" : 5
      }, {
        "type" : "ON_BICYCLE",
        "confidence" : 3
      }, {
        "type" : "UNKNOWN",
        "confidence" : 3
      } ]
    },

A couple thousand more, then

{
      "timestampMs" : "1500967359397",
      "activity" : [ {
        "type" : "IN_VEHICLE",
        "confidence" : 41
      }, {
        "type" : "UNKNOWN",
        "confidence" : 35
      }, {
        "type" : "STILL",
        "confidence" : 22
      }, {
        "type" : "ON_BICYCLE",
        "confidence" : 2
      } ]
    } ]
  } ]
}

So, now, files have entries that don't have lat/long data. Quite strange, huh? So I made a really simple fix:

        if args.format == "kml":
            f_out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
            f_out.write("<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n")
            f_out.write("  <Document>\n")
            f_out.write("    <name>Location History</name>\n")
            for item in items:
                if 'longitudeE7' in item: # here is the line i added
                    f_out.write("    <Placemark>\n")
                    # Order of these tags is important to make valid KML: TimeStamp, ExtendedData, then Point
                    f_out.write("      <TimeStamp><when>")
                    f_out.write(datetime.utcfromtimestamp(int(item["timestampMs"]) / 1000).strftime("%Y-%m-%dT%H:%M:%SZ"))
                    f_out.write("</when></TimeStamp>\n")
                    if "accuracy" in item or "speed" in item or "altitude" in item:
                        f_out.write("      <ExtendedData>\n")
                        if "accuracy" in item:
                            f_out.write("        <Data name=\"accuracy\">\n")
                            f_out.write("          <value>%d</value>\n" % item["accuracy"])
                            f_out.write("        </Data>\n")
                        if "speed" in item:
                            f_out.write("        <Data name=\"speed\">\n")
                            f_out.write("          <value>%d</value>\n" % item["speed"])
                            f_out.write("        </Data>\n")
                        if "altitude" in item:
                            f_out.write("        <Data name=\"altitude\">\n")
                            f_out.write("          <value>%d</value>\n" % item["altitude"])
                            f_out.write("        </Data>\n")
                        f_out.write("      </ExtendedData>\n")
                    f_out.write("      <Point><coordinates>%s,%s</coordinates></Point>\n" % (item["longitudeE7"] / 10000000, item["latitudeE7"] / 10000000))
                    f_out.write("    </Placemark>\n")
            f_out.write("  </Document>\n</kml>\n")
milklineep commented 5 years ago

See #21 for my pull request. I'm so dumb, this is my first time using git, hopefully i did it all right sorry for bad style, it's probably a bad idea to have both an issue and a pull request but i have no idea how to github so i'm sorry ^^

Scarygami commented 5 years ago

Hi, having an issue to discuss the problem and a PR to fix the issue is quite normal on Github, so what you did was quite alright. Thanks for the fix :)

sbenton1534 commented 5 years ago

Kkkkkkk