From 4a2c404e0fe5bd16a2e8f14f38f518690270cae9 Mon Sep 17 00:00:00 2001 From: Jacky Volpes Date: Tue, 23 Feb 2021 11:01:53 +0100 Subject: [PATCH] Ajout du script principal --- carte-orientation.py | 111 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 carte-orientation.py diff --git a/carte-orientation.py b/carte-orientation.py new file mode 100644 index 0000000..fb3e172 --- /dev/null +++ b/carte-orientation.py @@ -0,0 +1,111 @@ +# 🄯 Association Linux-Alpes +# Contributeurs : Jean-Christophe Becquet (APITUX), Jacky Volpes, Arnaud Champollion +# mercredi 13 janvier 2021 +# licence GPL version 3 ou supĂ©rieure +# ce script fait partie du projet CaliÉc +# voir http://www.linux-alpes.org/caliec/ + +import shutil +import tempfile + +from pathlib import Path + + +def main(): + # ParamĂštres du projet + tempdir = tempfile.TemporaryDirectory() + styles_url = 'http://www.linux-alpes.org/osm/ressources/styles/' + lambert93 = QgsCoordinateReferenceSystem("EPSG:2154") + wgs = QgsCoordinateReferenceSystem("EPSG:4326") + wgspm = QgsCoordinateReferenceSystem("EPSG:3857") + scr = wgspm + project = QgsProject.instance() + project.setCrs(scr) + options = QgsVectorFileWriter.SaveVectorOptions() + options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteFile + options.driverName = "GPKG" + + # Correspondances des noms des couches et des styles: + # "NOM_DE_LA_COUCHE_OSM": ("nom_du_style", "nom_du_layer") + # Commenter les couches non dĂ©sirĂ©es + # L'ordre impacte directement l'ordre final des couches + names = { + #"OUTPUT_OTHER_RELATIONS": (None, "Autres"), + "OUTPUT_MULTIPOLYGONS": ("multipolygon", "Surfaces"), + #"OUTPUT_MULTILINESTRINGS": (None, "Multilignes"), + "OUTPUT_LINES": ("linestring", "Lignes"), + "OUTPUT_POINTS": ("point", "Points"), + } + + # VĂ©rif de l'Ă©chelle + if iface.mapCanvas().scale() > 10000: + if QMessageBox.warning(iface.mainWindow(), "Zone Ă©tendue", + "La zone est Ă©tendue et le tĂ©lĂ©chargement risque de prendre longtemps", + QMessageBox.Ok | QMessageBox.Cancel) == QMessageBox.Cancel: + return + + # ParamĂ©trage du dossier de travail + settings = QSettings() + dir = QFileDialog.getExistingDirectory(iface.mainWindow(), + "SĂ©lectionnez un dossier de travail", + settings.value("caliec/workingDir")) + if dir == "": + return + settings.setValue("caliec/workingDir", dir) + workDir = Path(dir) + + # Construction de la requĂȘte overpass + try: + url = processing.run("quickosm:buildqueryextent", {"EXTENT": iface.mapCanvas().extent()})["OUTPUT_URL"] + except QgsProcessingException: + raise Exception("Vous n'avez pas installĂ© l'extension QuickOSM") + + # Barre de chargement animĂ©e + qApp.setOverrideCursor(Qt.WaitCursor) + message_bar = iface.messageBar() + progress_bar = QProgressBar() + progress_bar.setMaximum(0) + widget = message_bar.createMessage("Chargement", "RĂ©cupĂ©ration des data en ligne") + widget.layout().addWidget(progress_bar) + message_bar.pushWidget(widget) + + # TĂ©lĂ©chargement du fichier osm et crĂ©ation des couches + osmfile = str(Path(tempdir.name) / "osm.xml") + processing.run("native:filedownloader", {"URL": url, "OUTPUT": osmfile}) + layers = processing.run("quickosm:openosmfile", {"FILE": osmfile}) + + for osm_name, style_layer_names in names.items(): + # On enregistre le layer dans le gpkg + options.layerName = style_layer_names[1] + code, error = QgsVectorFileWriter.writeAsVectorFormat(layers[osm_name], str(workDir / "data.gpkg"), options) + if code != 0: + QMessageBox.warning(None, 'Erreur', f"Erreur Ă  l'export de la couche {layer.name()} : \n\n{error}") + return + + # Les layers suivants seront enregistrĂ©s dans le gpkg dĂ©jĂ  existant + options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer + + # On charge le nouveau layer + style_name, layer_name = style_layer_names + new_layer = QgsVectorLayer(str(workDir / f"data.gpkg|layername={layer_name}"), layer_name) + project.addMapLayer(new_layer) + + # On charge le style + if style_name is not None: + stylefile = str((Path(tempdir.name) / style_name).with_suffix('.qml')) + processing.run("native:filedownloader", {"URL": styles_url + f"/{style_name}.qml", "OUTPUT": stylefile}) + new_layer.loadNamedStyle(stylefile) + + try: + project.removeMapLayer(project.mapLayersByName("OpenStreetMap")[0]) + except IndexError: + pass + iface.mapCanvas().refreshAllLayers() + project.write(str(workDir / 'orient.qgs')) + + +try: + main() +finally: + iface.messageBar().popWidget() + qApp.restoreOverrideCursor()