Last active
April 5, 2022 17:02
-
-
Save jbg/60bf54b8f19c0a42abf7091c486d17fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Export your Google Maps starred places from https://www.google.com/bookmarks/ | |
# Then pipe them through this script to get GeoJSON: | |
# ./gmaps_stars_to_geojson.py < GoogleBookmarks.html > features.json | |
# You could import the resulting GeoJSON file to a PostGIS database with: | |
# ogr2ogr -f PostgreSQL PG:"dbname=DBNAME user=USER password=PASSWORD host=HOST" features.json -nln TABLENAME -append | |
# Depends on 'geojson', 'lxml' and 'requests' from PyPI. | |
# Credit to https://gist.github.com/endolith/3896948 | |
import re | |
import sys | |
import time | |
import geojson | |
import lxml.html | |
import requests | |
coords_regex = re.compile(r'cacheResponse\(\[\[\[[\d\.]+,([\d\.]+),([\d\.]+)\],') | |
doc = lxml.html.parse(sys.stdin).getroot() | |
features = [] | |
for element, attribute, url, pos in doc.body.iterlinks(): | |
if 'maps.google' not in url: | |
continue | |
description = element.text or '' | |
while True: | |
try: | |
response = requests.get(url.replace(' ','+')) | |
except IOError as e: | |
print('Connection problem, retrying:', file=sys.stderr) | |
print(repr(e), file=sys.stderr) | |
time.sleep(2.0) | |
else: | |
break | |
coords = coords_regex.search(response.text) | |
if not coords: | |
print('Coordinates not found', file=sys.stderr) | |
continue | |
longitude = coords.group(1) | |
latitude = coords.group(2) | |
try: | |
features.append(geojson.Feature(geometry=geojson.Point([float(longitude), float(latitude)]), properties={'name': description})) | |
except ValueError: | |
print('Invalid coordinates', file=sys.stderr) | |
time.sleep(2.0) | |
geojson.dump(geojson.FeatureCollection(features), sys.stdout) |
I was just the other day looking at my starred places on Google Maps and realised that a lot of my older ones have disappeared, which reminded me why I wrote this script in the first place - to avoid having to trust Google to look after my information for me.
If I get a chance in the coming days I’ll try to update this to work with whatever changes Google has made in the three years since I wrote it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
doesn't look to be working for me, any updated version?