Skip to content

Commit

Permalink
Algo: Save style in database
Browse files Browse the repository at this point in the history
  • Loading branch information
pdrillin committed Oct 16, 2020
1 parent b12b7ab commit 4ddf3d3
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 18 deletions.
54 changes: 52 additions & 2 deletions veloroutes_voies_vertes/processing/chargement/load_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,25 @@
QgsVectorLayer,
QgsRasterLayer
)

from processing.tools.postgis import uri_from_name

from ...qgis_plugin_tools.tools.algorithm_processing import BaseProcessingAlgorithm
from ...qgis_plugin_tools.tools.i18n import tr
from ...qgis_plugin_tools.tools.resources import resources_path


class LoadLayersAlgorithm(BaseProcessingAlgorithm):
"""
Chargement des couches adresse depuis la base de données
Chargement des couches depuis la base de données
"""
all_layer = None

DATABASE = "DATABASE"
SCHEMA = "SCHEMA"
RASTER = "RASTER"
STYLE = "STYLE"
SAVE_STYLE = "SAVE_STYLE"
OUTPUT = "OUTPUT"
OUTPUT_MSG = "OUTPUT MSG"

Expand Down Expand Up @@ -77,7 +82,25 @@ def initAlgorithm(self, config):
self.addParameter(
QgsProcessingParameterBoolean(
self.RASTER,
tr("Ajouter un fond raster OpenStreetMap?"),
tr("Ajouter un fond raster OpenStreetMap ?"),
defaultValue=False,
optional=False,
)
)

self.addParameter(
QgsProcessingParameterBoolean(
self.STYLE,
tr("Ajouter les styles aux couches ?"),
defaultValue=False,
optional=False,
)
)

self.addParameter(
QgsProcessingParameterBoolean(
self.SAVE_STYLE,
tr("Enregistrer les styles en base ?"),
defaultValue=False,
optional=False,
)
Expand Down Expand Up @@ -122,6 +145,9 @@ def processAlgorithm(self, parameters, context, feedback):
layers_name = ["repere", "poi_tourisme", "poi_service", "liaison", "segment"]
layers_v_name = ["v_portion", "v_itineraire"]
tables_name = ["element", "etape", "portion", "itineraire"]

self.all_layer = layers_name + layers_v_name + tables_name

connection = self.parameterAsString(parameters, self.DATABASE, context)

feedback.pushInfo("## CONNEXION A LA BASE DE DONNEES ##")
Expand Down Expand Up @@ -167,6 +193,9 @@ def processAlgorithm(self, parameters, context, feedback):
result = self.XYZ(context, url_with_params, 'OpenStreetMap')
output_layers.append(result.id())

self.style = self.parameterAsBool(parameters, self.STYLE, context)
self.save_style = self.parameterAsBool(parameters, self.SAVE_STYLE, context)

# add attribute tables
for x in tables_name:
if not context.project().mapLayersByName(x):
Expand All @@ -177,3 +206,24 @@ def processAlgorithm(self, parameters, context, feedback):
output_layers.append(result.id())

return {self.OUTPUT_MSG: msg, self.OUTPUT: output_layers}

def postProcessAlgorithm(self, context, feedback):

if not self.style and self.save_style:
feedback.reportError(
"Le style des couches ne peut pas être chargé en base car aucun "
" style n'est importé"
)
else:
for x in self.all_layer:
layers = context.project().mapLayersByName(x)
if len(layers) >= 1:
layer = layers[0]
if self.style:
layer.loadNamedStyle(resources_path("qml", x + ".qml"))
feedback.pushInfo("Le style de la couche " + x + " a été chargé")
if self.save_style:
layer.saveStyleToDatabase(layer.name(), 'Extension Véloroutes - ' + x, True, '')
feedback.pushInfo("Le style de la couche " + x + " a été enregistré en base")

return {}
29 changes: 21 additions & 8 deletions veloroutes_voies_vertes/processing/chargement/load_styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@

class LoadStylesAlgorithm(BaseProcessingAlgorithm):
"""
Chargement des couches adresse depuis la base de données
Chargement des styles depuis le dossier resources
"""
layers_name = [
"repere", "poi_tourisme", "poi_service", "OpenStreetMap",
"portion", "itineraire", "liaison", "segment", "v_portion",
"v_itineraire", "etape", "element"
]

INPUT = "INPUT"
OUTPUT_MSG = "OUTPUT MSG"
Expand Down Expand Up @@ -99,13 +104,7 @@ def processAlgorithm(self, parameters, context, feedback):
manager.addRelation(rel3)
manager.addRelation(rel4)

layers_name = [
"repere", "poi_tourisme", "poi_service", "OpenStreetMap",
"portion", "itineraire", "liaison", "segment", "v_portion",
"v_itineraire", "etape", "element"
]

for x in layers_name:
for x in self.layers_name:
layers = context.project().mapLayersByName(x)
if layers:
for layer in layers:
Expand All @@ -115,3 +114,17 @@ def processAlgorithm(self, parameters, context, feedback):
msg = msg + " // Style for " + x + " successfully loaded"

return {self.OUTPUT_MSG: msg}

def postProcessAlgorithm(self, context, feedback):

if context.project():

for x in self.layers_name:
layers = context.project().mapLayersByName(x)
if len(layers) >= 1:
lay = layers[0]
# reload provider's data
lay.dataProvider().reloadData()
lay.triggerRepaint()

return {}
41 changes: 34 additions & 7 deletions veloroutes_voies_vertes/processing/config/save_style_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@
__revision__ = '$Format:%H$'

from qgis.core import (
QgsProcessingParameterString,
QgsProcessingParameterBoolean,
QgsProcessingOutputString,
QgsProcessingOutputNumber,
QgsExpressionContextUtils
)

from ...qgis_plugin_tools.tools.algorithm_processing import BaseProcessingAlgorithm


class ConfigurePlugin(BaseProcessingAlgorithm):
class SaveStyleDatabase(BaseProcessingAlgorithm):

CONFIRM_SAVE = 'CONFIRM_SAVE'
OUTPUT_STRING = 'OUTPUT_STRING'

def name(self):
return 'save_style_database'

def displayName(self):
return 'Sauvegerder les styles en base de données'
return 'Sauvegarder les styles en base de données'

def group(self):
return 'Configuration'
Expand All @@ -42,7 +41,9 @@ def initAlgorithm(self, config):
"""
# INPUTS

#Boolean to confirm save
# Boolean to confirm save
input_bool = QgsProcessingParameterBoolean(self.CONFIRM_SAVE, 'Confirmez la sauvegarde des styles')
self.addParameter(input_bool)

# OUTPUTS

Expand All @@ -54,4 +55,30 @@ def initAlgorithm(self, config):
def processAlgorithm(self, parameters, context, feedback):
"""
Here is where the processing itself takes place.
"""
"""
confirm = self.parameterAsBool(parameters, self.CONFIRM_SAVE, context)

if not confirm:
return {
self.OUTPUT_STRING: 'les styles n\'ont pas été sauvegardés'
}

layers_required = [
"repere", "poi_tourisme", "poi_service", "OpenStreetMap",
"portion", "itineraire", "liaison", "segment", "v_portion",
"v_itineraire", "etape", "element"
]

layers = context.project().mapLayers()
if len(layers) < 1:
return {
self.OUTPUT_STRING: 'Aucune couche n\'a été trouvée'
}
for lay in layers:
if lay in layers_required:
layer = layers[lay]
layer.saveStyleToDatabase(layer.name(), 'default description', True, '')

return {
self.OUTPUT_STRING: 'les styles ont été sauvegardés'
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ def initAlgorithm(self, config):
self.PROJECTS_FOLDER,
tr("Chemin de destination"),
'',
False,
False
)
outparam.tooltip_3liz = 'Chemin de destination pour enregistrer les exports Shapefile'
Expand Down
2 changes: 2 additions & 0 deletions veloroutes_voies_vertes/processing/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .chargement.load_styles import LoadStylesAlgorithm
from ..qgis_plugin_tools.tools.resources import resources_path
from .config.configure import ConfigurePlugin
from .config.save_style_database import SaveStyleDatabase
from .covadis.import_covadis import ImportCovadis
from .covadis.export_covadis import ExportCovadis
from .covadis.export_all import ExportCovadisAll
Expand All @@ -26,6 +27,7 @@ def loadAlgorithms(self):
self.addAlgorithm(ImportCovadis())
self.addAlgorithm(ExportCovadis())
self.addAlgorithm(ExportCovadisAll())
self.addAlgorithm(SaveStyleDatabase())

def id(self):
return "veloroutes_vv"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def initAlgorithm(self, config):
optional=False,
)
)

# OUTPUTS
self.addOutput(
QgsProcessingOutputNumber(self.OUTPUT_STATUS, tr("Output status"))
Expand Down

0 comments on commit 4ddf3d3

Please sign in to comment.