# 🄯 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 = 'https://forge.chapril.org/linux_alpes/caliec/raw/branch/master/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: QMessageBox.warning(iface.mainWindow(), "Attention", "Vous n'avez pas installĂ© l'extension QuickOSM") return # 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(): style_name, layer_name = style_layer_names # On enregistre le layer dans le gpkg options.layerName = layer_name 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 new_layer = QgsVectorLayer(str(workDir / f"data.gpkg|layername={layer_name}"), layer_name) # Les layers suivants seront enregistrĂ©s dans le gpkg dĂ©jĂ  existant options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer # 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) # On charge le nouveau layer project.addMapLayer(new_layer) 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()