Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bmoregeo
Created August 25, 2014 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmoregeo/9d46bb1d3a84e136b84a to your computer and use it in GitHub Desktop.
Save bmoregeo/9d46bb1d3a84e136b84a to your computer and use it in GitHub Desktop.
Google Personal Location History to Shapefile
from xml.dom import minidom
import fiona
from shapely.geometry import mapping, Point
from dateutil.parser import parse
def xml_to_python(xml_file):
output = []
xmldocument = minidom.parse(xml_file)
placemarks = xmldocument.getElementsByTagName('Placemark')
for placemark in placemarks:
track = placemark.getElementsByTagName('gx:Track')[0]
locations = track.getElementsByTagName('gx:coord')
times = track.getElementsByTagName('when')
for time, location in zip(times,locations):
x,y,z = map(float, location.firstChild.nodeValue.split(' '))
timestamp = parse(time.firstChild.nodeValue)
output.append({'properties':{
'timestring': time.firstChild.nodeValue,
'x': x,
'y':y,
'z':z,
},
'geometry':mapping(Point([x,y,z]))})
return output
def list_of_points_to_shapefile(list_of_points, shapefile):
schema = {
'geometry': 'Point',
'properties': {
'x':'float',
'y':'float',
'z':'float',
'timestring': 'str',
}
}
with fiona.open(shapefile, 'w', 'ESRI Shapefile', schema) as c:
for item in list_of_points:
c.write(item)
if __name__ == '__main__':
kml = r'C:\Users\cfricke\Desktop\history-07-25-2014.kml'
shapefile = r'C:\Users\cfricke\Desktop\history-07-25-2014.shp'
list_of_points = xml_to_python(kml)
list_of_points_to_shapefile(list_of_points, shapefile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment