2021-02-23 11:01:53 +01:00
|
|
|
# 🄯 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()
|
2021-02-23 13:36:31 +01:00
|
|
|
styles_url = 'https://forge.chapril.org/linux_alpes/caliec/raw/branch/master/styles/'
|
2021-02-23 11:01:53 +01:00
|
|
|
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()
|