Skip to content

Instantly share code, notes, and snippets.

@barryrowlingson
Forked from ThomasG77/load_xyz_sources.py
Created February 27, 2018 16:00
Show Gist options
  • Save barryrowlingson/ab51eff9d3a4f62c439d0207033cd17e to your computer and use it in GitHub Desktop.
Save barryrowlingson/ab51eff9d3a4f62c439d0207033cd17e to your computer and use it in GitHub Desktop.
PyQGIS script to save/load/wipe XYZ sources in Browser Panel in QGIS3
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Name : XYZ manager
Description : Script to save/load/wipe WYZ sources in QGIS 3.0
Date : February 27, 2017
copyright : (C) 2017 by Thomas Gratier
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import csv
from PyQt5.QtCore import QSettings
s = QSettings()
xyz = 'qgis/connections-xyz'
keys = ["url", "zmin", "zmax", "authcfg", "username", "password", "referer"]
def getGroupXYZ():
s.beginGroup(xyz)
groupsXYZ = s.childGroups()
s.endGroup()
return groupsXYZ
def connexionsXYZSettings(groups):
connections_xyz = []
for group in groups:
s.beginGroup(xyz + "/" + group)
conn = {}
conn["name"] = group
for key in keys:
if s.value(key):
conn[key] = s.value(key)
else:
conn[key] = ''
s.endGroup()
connections_xyz.append(conn)
return connections_xyz
def writeConnexionsXYZToCSV(dictionnary, filepath):
with open(filepath, 'w', encoding='utf-8') as f: # save to csv
output = csv.writer(f) # create a csv.write
header = dictionnary[0].keys()
output.writerow(header) # header row
for row in dictionnary:
output.writerow(row.values()) # values row
def readConnexionsXYZFromCSV(filepath):
data = []
with open(filepath, encoding='utf-8') as f: # load csv file
for row in csv.DictReader(f):
data.append(row)
return data
def setConnexionXYZ(filepath='/tmp/xyzsources.csv'):
for record in readConnexionsXYZFromCSV(filepath):
s.beginGroup(xyz)
group = record["name"]
for key in keys:
if record[key]:
s.setValue(group + "/" + key, record[key])
s.endGroup()
def removeAllConnexionsXYZ():
for group in getGroupXYZ():
s.beginGroup(xyz)
s.remove("")
s.endGroup()
# If you want to save your current configuration, uncomment and change path
# writeConnexionsXYZToCSV(connexionsXYZSettings(getGroupXYZ()), '/tmp/xyzsources.csv')
# If you want to wipe XYZ configuration, uncomment
# removeAllConnexionsXYZ()
# Adapt path from where your csv is located
# We provide a file to load Mapbox datasources (just replace 'your key' with your own Mapbox account key)
setConnexionXYZ('/tmp/xyzsources.csv')
# You must reload the connection to update Browser Panel content
# otherwise you will not see changes
iface.reloadConnections()
url password authcfg username name referer zmin zmax
http://tile.openstreetmap.org/{z}/{x}/{y}.png OSM default
https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png Wikimedia maps
url password authcfg username name referer zmin zmax
https://api.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}.png?access_token=your_key Mapbox Streets 0 22
https://api.mapbox.com/v4/mapbox.light/{z}/{x}/{y}.png?access_token=your_key Mapbox Light 0 22
https://api.mapbox.com/v4/mapbox.dark/{z}/{x}/{y}.png?access_token=your_key Mapbox Dark 0 22
https://api.mapbox.com/v4/mapbox.satellite/{z}/{x}/{y}.png?access_token=your_key Mapbox Satellite 0 22
https://api.mapbox.com/v4/mapbox.streets-satellite/{z}/{x}/{y}.png?access_token=your_key Mapbox Streets Satellite 0 22
https://api.mapbox.com/v4/mapbox.wheatpaste/{z}/{x}/{y}.png?access_token=your_key Mapbox Wheat Paste 0 22
https://api.mapbox.com/v4/mapbox.streets-basic/{z}/{x}/{y}.png?access_token=your_key Mapbox Streets Basic 0 22
https://api.mapbox.com/v4/mapbox.comic/{z}/{x}/{y}.png?access_token=your_key Mapbox Comic 0 22
https://api.mapbox.com/v4/mapbox.outdoors/{z}/{x}/{y}.png?access_token=your_key Mapbox Outdoors 0 22
https://api.mapbox.com/v4/mapbox.run-bike-hike/{z}/{x}/{y}.png?access_token=your_key Mapbox Run Bike Hike 0 22
https://api.mapbox.com/v4/mapbox.pencil/{z}/{x}/{y}.png?access_token=your_key Mapbox Pencil 0 22
https://api.mapbox.com/v4/mapbox.pirates/{z}/{x}/{y}.png?access_token=your_key Mapbox Pirates 0 22
https://api.mapbox.com/v4/mapbox.emerald/{z}/{x}/{y}.png?access_token=your_key Mapbox Emerald 0 22
https://api.mapbox.com/v4/mapbox.high-contrast/{z}/{x}/{y}.png?access_token=your_key Mapbox High Contrast 0 22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment