Skip to content

Instantly share code, notes, and snippets.

@alpha-beta-soup
Created August 16, 2015 08:54
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 alpha-beta-soup/e674a4e61cd0432ee429 to your computer and use it in GitHub Desktop.
Save alpha-beta-soup/e674a4e61cd0432ee429 to your computer and use it in GitHub Desktop.
Zoom to your current location in QGIS (assuming EPSG 4326)
import qgis.utils
import pycurl
import StringIO
import json
buffer = StringIO.StringIO()
c = pycurl.Curl()
c.setopt(c.URL, 'http://ipinfo.io/')
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()
body = buffer.getvalue()
lat, lon = [float(coord) for coord in json.loads(body)['loc'].split(',')]
mc = qgis.utils.iface.mapCanvas()
scale = 0.01
rect = QgsRectangle(lon-scale,lat-scale,lon+scale,lat+scale)
mc.setExtent(rect)
mc.refresh()
@alpha-beta-soup
Copy link
Author

IP address geolocation is not an exact science.

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