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
import re | |
import csv | |
import ssl | |
import urllib.request | |
ssl._create_default_https_context = ssl._create_unverified_context | |
baseUrl = 'https://geoservices.ign.fr/' | |
url_page = baseUrl + 'documentation/services' | |
with urllib.request.urlopen(url_page) as req: | |
content = req.read().decode('utf-8') | |
regex = r"sites/default/files/[0-9-]*/Ressources_de_services_web_[0-9-]*.csv" | |
match = re.findall(regex, content) | |
URL = baseUrl + match[0] | |
with urllib.request.urlopen(URL) as req: | |
content = req.read().decode('windows-1252') | |
services = csv.DictReader(content.split('\n') , fieldnames=['service', 'cle', 'nom_commercial', 'nom_technique', 'url'], delimiter=';') | |
next(services, None) | |
infos_wms = [] | |
infos_wmts = [] | |
infos_wfs = [] | |
for service in services: | |
if 'WMS' in service.get('service') and 'personnelle' not in service.get('cle'): | |
infos_wms.append([f"{service.get('service')} {service.get('cle').capitalize()}", service.get('url').replace('?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities', '')]) | |
if 'WMTS' in service.get('service') and 'personnelle' not in service.get('cle'): | |
infos_wmts.append([f"{service.get('service')} {service.get('cle').capitalize()}", service.get('url')]) | |
if 'WFS' in service.get('service') and 'personnelle' not in service.get('cle'): | |
infos_wfs.append([f"{service.get('service')} {service.get('cle').capitalize()}", service.get('url').replace('?SERVICE=WFS&VERSION=2.0.0&REQUEST=GetCapabilities', '')]) | |
dict_tuple_wms = {tuple(item): index for index, item in enumerate(infos_wms)} | |
infos_wms = [list(itm) for itm in dict_tuple_wms.keys()] | |
print(infos_wms) | |
dict_tuple_wmts = {tuple(item): index for index, item in enumerate(infos_wmts)} | |
infos_wmts = [list(itm) for itm in dict_tuple_wmts.keys()] | |
print(infos_wmts) | |
dict_tuple_wfs = {tuple(item): index for index, item in enumerate(infos_wfs)} | |
infos_wfs = [list(itm) for itm in dict_tuple_wfs.keys()] | |
def addWmtsWmsConnection(name, url, dpiMode=7, ignoreAxisOrientation=False, ignoreGetFeatureInfoURI=False, ignoreGetMapURI=False, ignoreReportedLayerExtents=False, invertAxisOrientation=False, referer='', smoothPixmapTransform=False): | |
settings = QgsSettings() | |
key = f'qgis/connections-wms/{name}' | |
settings.setValue(key + '/url', url) | |
settings.setValue(key + '/dpiMode', dpiMode) | |
settings.setValue(key + '/ignoreAxisOrientation', ignoreAxisOrientation) | |
settings.setValue(key + '/ignoreGetFeatureInfoURI', ignoreGetFeatureInfoURI) | |
settings.setValue(key + '/ignoreGetMapURI', ignoreGetMapURI) | |
settings.setValue(key + '/ignoreReportedLayerExtents', ignoreReportedLayerExtents) | |
settings.setValue(key + '/invertAxisOrientation', invertAxisOrientation) | |
settings.setValue(key + '/referer', referer) | |
settings.setValue(key + '/smoothPixmapTransform', smoothPixmapTransform) | |
def removeWmtsWmsConnection(name): | |
settings = QgsSettings() | |
settings.remove(f'qgis/connections-wms/{name}') | |
settings.remove(f'qgis/WMS/{name}') | |
def addWfsConnection(name, url, version='auto', maxnumfeatures=None, pagesize=None, pagingenabled=True, ignoreAxisOrientation=False, invertAxisOrientation=False): | |
settings = QgsSettings() | |
key = f'qgis/connections-wfs/{name}' | |
settings.setValue(key + '/url', url) | |
settings.setValue(key + '/version', version) | |
settings.setValue(key + '/maxnumfeatures', maxnumfeatures) | |
settings.setValue(key + '/pagesize', pagesize) | |
settings.setValue(key + '/pagingenabled', pagingenabled) | |
settings.setValue(key + '/ignoreAxisOrientation', ignoreAxisOrientation) | |
settings.setValue(key + '/invertAxisOrientation', invertAxisOrientation) | |
def removeWfsConnection(name): | |
settings = QgsSettings() | |
settings.remove(f'qgis/connections-wfs/{name}') | |
settings.remove(f'qgis/WFS/{name}') | |
settings.beginGroup( QStringLiteral( "/qgis/connections-wfs" ) ); | |
settings.setValue( QString( '/' + connectionName + "/url" ), child.attribute( QStringLiteral( "url" ) ) ); | |
settings.setValue( QString( '/' + connectionName + "/version" ), child.attribute( QStringLiteral( "version" ) ) ); | |
settings.setValue( QString( '/' + connectionName + "/maxnumfeatures" ), child.attribute( QStringLiteral( "maxnumfeatures" ) ) ); | |
settings.setValue( QString( '/' + connectionName + "/pagesize" ), child.attribute( QStringLiteral( "pagesize" ) ) ); | |
settings.setValue( QString( '/' + connectionName + "/pagingenabled" ), child.attribute( QStringLiteral( "pagingenabled" ) ) ); | |
settings.setValue( QString( '/' + connectionName + "/ignoreAxisOrientation" ), child.attribute( QStringLiteral( "ignoreAxisOrientation" ) ) ); | |
settings.setValue( QString( '/' + connectionName + "/invertAxisOrientation" ), child.attribute( QStringLiteral( "invertAxisOrientation" ) ) ); | |
settings.endGroup(); | |
for wms in infos_wms: | |
addWmtsWmsConnection(wms[0], wms[1]) | |
for wmts in infos_wmts: | |
addWmtsWmsConnection(wmts[0], wmts[1]) | |
for wfs in infos_wfs: | |
addWfsConnection(wfs[0], wfs[1]) | |
iface.reloadConnections() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment