This commit is contained in:
Djedouas 2021-03-03 13:42:42 +01:00
parent 8554e448b2
commit f248b5e128

View File

@ -26,6 +26,8 @@ from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction, qApp, QFileDialog, QProgressBar, QMessageBox from qgis.PyQt.QtWidgets import QAction, qApp, QFileDialog, QProgressBar, QMessageBox
from qgis.core import QgsCoordinateReferenceSystem, QgsProject, QgsVectorFileWriter, QgsVectorLayer, QgsFeatureRequest from qgis.core import QgsCoordinateReferenceSystem, QgsProject, QgsVectorFileWriter, QgsVectorLayer, QgsFeatureRequest
from qgis.utils import OverrideCursor
from processing import QgsProcessingException from processing import QgsProcessingException
@ -70,7 +72,7 @@ class Orientation:
# Declare instance attributes # Declare instance attributes
self.actions = [] self.actions = []
self.menu = self.tr(u'&CaLiÉc') self.menu = self.tr('CaLiEc')
# Check if plugin was started the first time in current QGIS session # Check if plugin was started the first time in current QGIS session
# Must be set in initGui() to survive plugin reloads # Must be set in initGui() to survive plugin reloads
@ -272,14 +274,34 @@ class Orientation:
# On explose les champs # On explose les champs
layer = processing.run("native:explodehstorefield", layer = processing.run("native:explodehstorefield",
parameters={"INPUT": layers[osm_name], parameters={"INPUT": layers[osm_name],
"FIELD": "other_tags", "OUTPUT": "memory:"}, "FIELD": "other_tags",
"OUTPUT": "memory:"},
context=context)["OUTPUT"]
# On enlève les champs en doublons en gardant celui des doublons qui est écrit tout en minuscule
field_names = [a.name() for a in layer.fields()]
field_names_lower = [a.lower() for a in field_names]
expected_fields, deleted_fields = [], []
for field_name, field_name_lower in zip(field_names, field_names_lower):
if field_names_lower.count(field_name_lower) == 1 or field_name.lower() == field_name:
expected_fields.append(field_name)
else:
deleted_fields.append(field_name)
# On réexplose les champs sans doublons
layer = processing.run("native:explodehstorefield",
parameters={"INPUT": layers[osm_name],
"FIELD": "other_tags",
"OUTPUT": "memory:",
"EXPECTED_FIELDS": ",".join(expected_fields)},
context=context)["OUTPUT"] context=context)["OUTPUT"]
# On enregistre le layer dans le gpkg # On enregistre le layer dans le gpkg
options.layerName = layer_name options.layerName = layer_name
code, error = QgsVectorFileWriter.writeAsVectorFormat(layer, str(workDir / "data.gpkg"), options) code, error = QgsVectorFileWriter.writeAsVectorFormat(layer, str(workDir / "data.gpkg"), options)
if code != 0: if code != 0:
QMessageBox.warning(None, 'Erreur', f"Erreur à l'export de la couche {layer_name} : \n\n{error}") with OverrideCursor(Qt.ArrowCursor):
QMessageBox.warning(self.iface.mainWindow(), 'Erreur', f"Erreur à l'export de la couche {layer_name} : \n\n{error[:2000]}")
return return
new_layer = QgsVectorLayer(str(workDir / f"data.gpkg|layername={layer_name}"), layer_name) new_layer = QgsVectorLayer(str(workDir / f"data.gpkg|layername={layer_name}"), layer_name)
@ -303,3 +325,9 @@ class Orientation:
self.iface.mapCanvas().refreshAllLayers() self.iface.mapCanvas().refreshAllLayers()
project.write(str(workDir / 'orient.qgs')) project.write(str(workDir / 'orient.qgs'))
if len(deleted_fields) > 0:
with OverrideCursor(Qt.ArrowCursor):
QMessageBox.warning(self.iface.mainWindow(), "Attention",
"Les champs suivants sont en doublons et ont été supprimés au profit de leur doublon en minuscule :\n- " +
"\n - ".join(deleted_fields))