Skip to content

Instantly share code, notes, and snippets.

@endolith
Created October 16, 2012 02:29
Show Gist options
  • Select an option

  • Save endolith/3896948 to your computer and use it in GitHub Desktop.

Select an option

Save endolith/3896948 to your computer and use it in GitHub Desktop.
Export Google Maps starred locations
# -*- coding: utf-8 -*-
"""
Go to Google Bookmarks: https://www.google.com/bookmarks/
On the bottom left, click "Export bookmarks": https://www.google.com/bookmarks/bookmarks.html?hl=en
After downloading the html file, run this script on it to generate a KML.
"""
from lxml.html import document_fromstring
import simplekml
from urllib2 import urlopen
import re
import time
filename = r'GoogleBookmarks.html'
with open(filename) as bookmarks_file:
data = bookmarks_file.read()
kml = simplekml.Kml()
# Hacky and doesn't work for all of the stars:
lat_re = re.compile('markers:[^\]]*latlng[^}]*lat:([^,]*)')
lon_re = re.compile('markers:[^\]]*latlng[^}]*lng:([^}]*)')
coords_in_url = re.compile('\?q=(-?\d{,3}\.\d*),\s*(-?\d{,3}\.\d*)')
doc = document_fromstring(data)
for element, attribute, url, pos in doc.body.iterlinks():
if 'maps.google' in url:
description = element.text or ''
print description.encode('UTF8')
print u"URL: {0}".format(url)
if coords_in_url.search(url):
# Coordinates are in URL itself
latitude = coords_in_url.search(url).groups()[0]
longitude = coords_in_url.search(url).groups()[1]
else:
# Load map and find coordinates in source of page
try:
sock = urlopen(url.replace(' ','+').encode('UTF8'))
except Exception, e:
print 'Connection problem:'
print repr(e)
print 'Waiting 2 minutes and trying again'
time.sleep(120)
sock = urlopen(url.replace(' ','+').encode('UTF8'))
content = sock.read()
sock.close()
time.sleep(3) # Don't annoy server
try:
latitude = lat_re.findall(content)[0]
longitude = lon_re.findall(content)[0]
except IndexError:
print '[Coordinates not found]'
print
continue
print latitude, longitude
try:
kml.newpoint(name=description,
coords=[(float(longitude), float(latitude))])
except ValueError:
print '[Invalid coordinates]'
print
kml.save("GoogleBookmarks.kml")
@endolith
Copy link
Copy Markdown
Author

You have explicit permission! :)

@samirkothari89
Copy link
Copy Markdown

How do you run the html file in KML? I did get all the names of my bookmarks (thank you for that)

@iturki
Copy link
Copy Markdown

iturki commented Dec 28, 2015

Or, you can use this online tool to do the html > KML conversion:
http://gexport.somee.com/

@jbg
Copy link
Copy Markdown

jbg commented Apr 5, 2016

I made a Python 3, GeoJSON-exporting version based on this script here:
https://gist.github.com/jbg/60bf54b8f19c0a42abf7091c486d17fc

@ngzhian
Copy link
Copy Markdown

ngzhian commented Jun 28, 2016

I updated the regex for lat, lng based on the most recent google response, here's the gist: https://gist.github.com/ngzhian/73c51bcb44c8bfd8dc02d4aa538fb83f

@3v1n0
Copy link
Copy Markdown

3v1n0 commented Jan 11, 2017

Thanks, I guess you want to merge changes from @ngzhian in order to get this working properly with new APIs

@rrrnld
Copy link
Copy Markdown

rrrnld commented Jan 29, 2017

Hey! Just a quick heads up: I packaged it up a bit with a proper requirements.txt and put it up on https://github.com/heyarne/bookmarks-to-kml. I hope that's fine. If it's ok i'd also like to build a simple web interface around it, but because of the missing license file I didn't want to do it without permission. Any objections?

@dmsza
Copy link
Copy Markdown

dmsza commented Feb 24, 2017

I've made another script that converts Google Maps JSON to KML. It is more complete and it works flawless! Check here: https://github.com/dmrsouza/json2kml

@LaDeeDaDee
Copy link
Copy Markdown

LaDeeDaDee commented Mar 3, 2017

@dmrsouza flawless if you know can run simplekml via a pip...

'pip' is not recognized as an internal or external command,
operable program or batch file.

How do I install simplekml via pip? I have downloaded simplekml 1.3.0 but I'm not sure if it is installed?

@dmsza
Copy link
Copy Markdown

dmsza commented Mar 4, 2017

@LaDeeDaDee At least on Windows pip command is installed with Python. Anyway there are instructions about how to install pip at https://docs.python.org/3/installing/

@rlewandowski1988
Copy link
Copy Markdown

Gentlemen,
I am a beginner with python, but managed to get through the installation, pip.. now I'm stuck here:

C:\Users\Kierownik\Desktop\json2kml>json2kml.py
Opening file "Saved Places.json"
Traceback (most recent call last):
File "C:\Users\Kierownik\Desktop\json2kml\json2kml.py", line 32, in
for place in data["features"]:
KeyError: 'features'

Would you, please, be able to help?

Thanks!

@dmsza
Copy link
Copy Markdown

dmsza commented Mar 19, 2017

@rlewandowski1988, please check your JSON file (open it in a text editor). It should start as:

{ "type" : "FeatureCollection", "features" : [ { "geometry" : { "coordinates" :........

It seems it is missing the "features" section. If your data is not private and if you can share your JSON file, I am willing to help you.

@rlewandowski1988
Copy link
Copy Markdown

@dmrsouza, I previously gave up becaue I realised that google introduced new feature under saving your places - now you can save and give the location one of the categories: Favorites, Want to go, Starred places or New list (you create one). Knowing that I just thought that they probably changed the data structure and that's why json2kml.py stopped working... but it turned out that insted of downloading Maps (your places) I downloaded the Location history ;) Nevertheless your comment helped! thanks very much for help! all the very best! ;)

@blaines
Copy link
Copy Markdown

blaines commented Jul 26, 2017

Google says "No Data Found" for all of my accounts when exporting "Maps (your places)". Boo

Trying the bookmarks approach instead

@andriip
Copy link
Copy Markdown

andriip commented Aug 4, 2017

@rlewandowski1988, Hi did you try this script with your Location History file?
Is it working?

@YvesBas
Copy link
Copy Markdown

YvesBas commented Nov 1, 2017

Thanks for this very useful script! However I would need to get also the custom labels I added to my starred locations to be able to use this data on another GIS. Do you think it would be possible to get the labels??

@eszjay
Copy link
Copy Markdown

eszjay commented Dec 4, 2017

Google has now provided their own tool to export your Map data in JSON format at Google Takeout

@bhatiagagan
Copy link
Copy Markdown

@heyarne I tried using your script as it's exactly what I need in my situation ( don't have access to old account but downloaded the HTML file), but I got this error 'No module named simplekml '
I tried installing simplekml with pip install simplekmp, but got the following error-

Any help would be appreciated, I'm a super noob here...but could really use all my 1000s of bookmarks in the new account...

Collecting simplekml
Using cached simplekml-1.3.0.zip
Installing collected packages: simplekml
Running setup.py install for simplekml ... error
Complete output from command /usr/bin/python -u -c "import setuptools, tokenize;file='/private/var/folders/jk/6y47vn0d2gdcd9v84g51zz8h0000gn/T/pip-build-Vgk14q/simplekml/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /var/folders/jk/6y47vn0d2gdcd9v84g51zz8h0000gn/T/pip-ZB2nDK-record/install-record.txt --single-version-externally-managed --compile:
running install
running build
running build_py
.......
copying simplekml/coordinates.py -> build/lib/simplekml
copying simplekml/schema.py -> build/lib/simplekml
running install_lib
creating /Library/Python/2.7/site-packages/simplekml
error: could not create '/Library/Python/2.7/site-packages/simplekml': Permission denied

@walidvb
Copy link
Copy Markdown

walidvb commented Apr 13, 2018

@bhatiagagan, try to install with sudo

@lsmolic
Copy link
Copy Markdown

lsmolic commented Aug 15, 2018

If you are using Google Takeout, you can change the file path and export

# FROM THIS Google Takeout format:
# {
#   "type" : "FeatureCollection",
#   "features" : [ {
#     "geometry" : {
#       "coordinates" : [ -118.2474094, 34.0527440 ],
#       "type" : "Point"
#     },
#     "properties" : {
#       "Google Maps URL" : "http://maps.google.com/?cid=9214992026148815492",
#       "Location" : {
#         "Address" : "316 West 2nd Street, Los Angeles, CA 90012, United States",
#         "Business Name" : "The Redwood Bar & Grill",
#         "Country Code" : "US",
#         "Geo Coordinates" : {
#           "Latitude" : "34.0527440",
#           "Longitude" : "-118.2474094"
#         }
#       },
#       "Published" : "2018-07-21T02:16:43Z",
#       "Title" : "The Redwood Bar & Grill",
#       "Updated" : "2018-07-21T02:16:43Z"
#     },
#     "type" : "Feature"
#   }]
# }
import csv
import json

# uploading a CSV will let you choose the title column as well. This also pulls out the address correctly.
final_csv_file = './file_to_import_to_google.csv'

google_json_file_path = './Saved Places.json'
with open(google_json_file_path, encoding='utf-8-sig') as google_json_file:
  google_map_json = json.load(google_json_file)

  fieldnames = ['title', 'address', 'latitude', 'longitude', 'link']
  with open(final_csv_file,'w', newline='\n') as csv_file:
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    writer.writeheader()
    for f in google_map_json['features']:
      props = f['properties']

      
      business_name = props.get('Location', {}).get('Business Name', '')
      # default to business name is title doesn't exist
      title = props.get('Title', business_name) 
      link = props.get('Google Maps URL', '')
      
      address = props.get('Location', {}).get('Address', '')
      country_code = props.get('Location', {}).get('Country Code', '')

      try:
        latitude = props.get('Location', {}).get('Geo Coordinates').get('Latitude')
        longitude = props.get('Location', {}).get('Geo Coordinates').get('Longitude')
      except:
        # sometimes lat/long are not encapsulated by 'Geo Coordinates' key
        latitude = props.get('Location', {}).get('Latitude', '')
        longitude = props.get('Location', {}).get('Longitude', '')

      # specify which country to pull from
      writer.writerow({
          'title': props.get('Title', ''), 
          'address': props.get('Location', {}).get('Address', ''),
          'latitude': props.get('Location', {}).get('Geo Coordinates', {}).get('Latitude', ''),
          'longitude': props.get('Location', {}).get('Geo Coordinates', {}).get('Longitude', ''),
          'link': props.get('Google Maps URL', ''),
        })
    

@Ademord
Copy link
Copy Markdown

Ademord commented Nov 12, 2018

As of today google doesnt let me connect to google maps from the script. I am getting

Connection problem:
HTTPError()

I will try the Google Takeout method listed here

@spyhunter99
Copy link
Copy Markdown

what version of python is this for? getting this

 File "export_google_starred_locations.py", line 34
    print description.encode('UTF8')

@gerarnan
Copy link
Copy Markdown

gerarnan commented Dec 6, 2018

@spyhunter99 i am getting the same as you!!

@endolith
Copy link
Copy Markdown
Author

endolith commented Jan 2, 2019

@spyhunter It's for an old Python version. Does Google Takeout not work for you?

@faissalshrf
Copy link
Copy Markdown

I want to transfer all my starred location from one Google account to another. Is there a way to batch-import them to the new account without opening each location and starring it individually?

@kate1212klim
Copy link
Copy Markdown

fantastic! i'm learning python and it's really fascinating how it can help solve problems) Hope I can write such helpful codes soon :) Good luck!

@thany
Copy link
Copy Markdown

thany commented Mar 24, 2022

The idea here is great, but unfortunately Google have been stirring around in their products, and the only way since a little while to export saved places, is via Takeout - select "Saved".

This will produce a file containing all saved places, sure enough, but:

  1. They are in CSV format, not JSON or HTML.
  2. They do not contain lat/lon, but instead a link that presumably be used against the Places API (example)

So this script will have probably lost its usefulness some years ago ๐Ÿ˜ž

@endolith
Copy link
Copy Markdown
Author

endolith commented Mar 24, 2022

@thany From Google Takeout, Saved Places.json and Labeled places.json both contain lat/lon and can be converted using https://mygeodata.cloud/converter/json-to-gpx

I've been using that technique lately, so yeah this script is probably obsolete, it's from 2012.

@jordymeow
Copy link
Copy Markdown

jordymeow commented Dec 8, 2022

Unfortunately, impossible to get the GPS coordinates for anything else than the locations with stars. Personally, I used exportgooglemaps.com; this tool takes the CSV and JSON from Google Takeout and create new files. You get the Place ID, GPS coordinates, and a few more columns, but that's what I needed the most. Not sure why Google doesn't give better files...

@SynapseScribe
Copy link
Copy Markdown

Unfortunately, impossible to get the GPS coordinates for anything else than the locations with stars. Personally, I used exportgooglemaps.com; this tool takes the CSV and JSON from Google Takeout and create new files. You get the Place ID, GPS coordinates, and a few more columns, but that's what I needed the most. Not sure why Google doesn't give better files...

is the code behind https://exportgooglemaps.com open source? what about privacy concerns..?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment