0

I am trying to parse a .json file into a .kml file to be used by a plotting program. I am going to give a set a data samples to simplify the issue:

I have a LocationHistory.json file that has the following structure:

{
 "data" : {
   "items" : [ {
     "kind" : "latitude#location",
     "timestampMs" : "1374870896803",
     "latitude" : 34.9482949,
     "longitude" : -85.3245474,
     "accuracy" : 2149
   }, {
     "kind" : "latitude#location",
     "timestampMs" : "1374870711762",
     "latitude" : 34.9857898,
     "longitude" : -85.3526902,
     "accuracy" : 2016"
   }]
  }
}

I define a function that parses the json data and then I want to feed it into a "placemark" string to write (output) into a "location.kml" file.

def parse_jason_data_to_kml_file():
   kml_file = open('location.kml', "r+")

   #Here I parse the info inside the LocationHistory.json file
   json_file = open('LocationHistory.json')
   json_string = json_file.read()
   json_data = json.loads(json_string)

   locations = json_data["data"]["items"]

   # Here I get the placemark string structure
   placemark = ["<Placemark>",
                "<TimeStamp><when>the “timestampMS” value from the JSON data item</when></TimeStamp>",
                "<ExtendedData>",
                "<Data name=”accuracy”>",
                "<value> the “accuracy” value from the JSON data item </value>]",
                "</Data>",
                "</ExtendedData><Point><coordinates>”longitude,latitude”</coordinates></Point>",
                "</Placemark>"]
   placemark = "\n".join(placemark)

   # Now i try to find certain substrings in the "placemark" string that I would like to replace:
   find_timestamp = placemark.find("the “timestampMS” value from the JSON data item")
   find_accuracy = placemark.find("the “accuracy” value from the JSON data item")
   find_longitude = placemark.find("”longitude")
   find_latitude = placemark.find("latitude”")

   # Next, I want to loop through the LocationHistory.json file (the data list above)
   # and replace the strings with the parsed LocationHistory.json data saved as: 
   # location["timestampMs"], location["accuracy"], location["longitude"], location["latitude"]: 
   for location in locations:                
       if find_timestamp != -1:
           placemark.replace('the “timestampMS” value from the JSON data item', location["timestampMs"])
       if find_accuracy != -1:
           placemark.replace('the “accuracy” value from the JSON data item', location["accuracy"])
       if find_longitude != -1:
           placemark.replace('”longitude', location["longitude"])
       if find_latitude != -1:
           placemark.replace('latitude”', location["latitude"])
       kml_file.write(placemark)
       kml_file.write("\n")

    kml_file.close()

The aim of this code is to write the placemark string that contains the .json data, into the location.ml file, from the placemark string that looks like this:

<Placemark>
<TimeStamp><when>the “timestampMS” value from the JSON data item</when></TimeStamp>
<ExtendedData>
<Data name=”accuracy”>
<value> the “accuracy” value from the JSON data item </value>
</Data>
</ExtendedData><Point><coordinates>”longitude,latitude”</coordinates></Point>
</Placemark>

To an output, which should look like this:

<Placemark>
<TimeStamp><when>2013-07-26T22:34:56Z</when></TimeStamp>
<ExtendedData>
<Data name=”accuracy”>
<value>2149</value>
</Data>
</ExtendedData><Point><coordinates>-85.3245474,34.9482949</coordinates></Point>
</Placemark>

<Placemark>
<TimeStamp><when>2013-07-26T22:31:51Z</when></TimeStamp>
<ExtendedData>
<Data name=”accuracy”>
<value>2016</value>
</Data>
</ExtendedData><Point><coordinates>-85.3526902,34.9857898</coordinates></Point>
</Placemark>

If I try to run this code, I get the following error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/Elysium/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "/Users/Elysium/anaconda/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 78, in execfile
builtins.execfile(filename, *where)
File "/Users/Elysium/Dropbox/LeeV/Task 5 - Location Plotting/ParseToKML.py", line 77, in <module>
parse_jason_data_to_kml_file()
File "/Users/Elysium/Dropbox/LeeV/Task 5 - Location Plotting/ParseToKML.py", line 51, in parse_jason_data_to_kml_file
placemark.replace('the “timestampMS” value from the JSON data item', location["timestampMs"])
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 33: ordinal not in range(128)

Is this a problem with my code or a software problem? From various sources I can find that it can be related to PIP or ascii or something, but no answer to help me... I'm sure I have pip installed...

Any help would be appreciated. Any suggestions to improve my code is also welcome :)

Thank You

TrigonaMinima
  • 1,828
  • 1
  • 23
  • 35
Lee Venter
  • 53
  • 1
  • 3
  • 12

1 Answers1

1

You can try this as follows-

placemark = ["<Placemark>",
    "<TimeStamp><when>%(timestampMs)r</when></TimeStamp>",
    "<ExtendedData>",
    "<Data name=\"accuracy\">",
    "<value>%(accuracy)r</value>]",
    "</Data>",
    "</ExtendedData><Point><coordinates>%(longitude)r, %(latitude)r</coordinates></Point>",
    "</Placemark>"]

placemark = "\n".join(placemark)
for location in locations:
    temp = placemark % location
    kml_file.write(temp)
    kml_file.write("\n")

Here, in %(any_dict_key)r, r converts any Python object (here, the value of the any_dict_key) and then inserts it into the string. So, for your timestamps you have to convert it into a datetime object.

You can read this part of the documentation to check out the details - string-formatting-operations

TrigonaMinima
  • 1,828
  • 1
  • 23
  • 35
  • I'm on my phone atm, had to go out of the house for a while. Will confirm the moment I get back if this solves my problem, Thank you TrigonaMinima – Lee Venter Sep 29 '15 at 17:53
  • Wow, works great thanks TrigonaMinima. I thank you for your time and your help, it's very appreciated! :) – Lee Venter Sep 29 '15 at 22:09
  • I'll read the extra documentation link that you provided. I hope I can figure out how to convert the timestamp into a datetime object, but I suppose I'll bother you when I can't :) Thanks again – Lee Venter Sep 29 '15 at 22:27
  • 1
    No problem. May be [this](http://stackoverflow.com/q/3682748/2650427) might help you with timestamps. :) – TrigonaMinima Sep 29 '15 at 23:06
  • Thanks, I'll check it out! – Lee Venter Sep 30 '15 at 10:57