In [1]:
'''
Import modules
'''
import pandas as pd
import os
from bs4 import BeautifulSoup
In [2]:
'''
Create a list of document names. In my case, they are six documents, which I named 'g1.kml', 'g2.kml' etc.
'''
docs = ['g1.kml','g2.kml','g3.kml','g4.kml','g5.kml','g6.kml','g7.kml']
In [6]:
'''
Open a file on which to write
'''
g = open('location_data.txt', 'w')
g.write("Datestamp" + ";" + "Timestamp" + ";" + "latitude" + ";" + "longitude" + "\n")
'''
Take out coordinates in loop
'''
for n in docs:
# Open file
f = open(n,"r")
# Read
data = f.read()
soup = BeautifulSoup(data, "html.parser")
# Date/Time
when = soup.findAll('when')
# Lat/lon coordinates
where = soup.findAll('gx:coord')
# Iteration counter
b = 0
# Loop data extraction
for z in when:
if b == len(where)-1:
continue
else:
when_pos = when[b].contents[0]
when_date = when_pos.split('T')[0]
when_time = when_pos.split('T')[1].split('.')[0]
y = where[b].contents[0].split(" ")[0]
x = where[b].contents[0].split(" ")[1]
# add 1
b = b + 1
g.write(when_date.encode('utf8') + ";" + when_time.encode('utf8') + ";" + x.encode("utf8") + ";" + y.encode('utf8') + "\n")
g.close()