Skip to content

Commit

Permalink
adding script to load layer and script to load styles
Browse files Browse the repository at this point in the history
  • Loading branch information
pdrillin committed Dec 17, 2019
1 parent 9d931a8 commit af62954
Show file tree
Hide file tree
Showing 16 changed files with 7,178 additions and 461 deletions.
127 changes: 127 additions & 0 deletions processing/algorithms/add_styles.py
@@ -0,0 +1,127 @@
"""
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""

__author__ = '3liz'
__date__ = '2019-02-15'
__copyright__ = '(C) 2019 by 3liz'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

from qgis.core import (
QgsProcessingAlgorithm,
QgsProcessingParameterString,
QgsProcessingOutputString,
QgsProcessingOutputNumber,
QgsExpressionContextUtils,
QgsVectorLayer,
QgsProcessingOutputMultipleLayers,
QgsProcessingContext,
QgsApplication,
QgsProject
)
from qgis.PyQt.QtSql import *
from .tools import *
from processing.tools.postgis import uri_from_name, GeoDB
import os

class AddStyles(QgsProcessingAlgorithm):
"""
"""

# Constants used to refer to parameters and outputs. They will be
# used when calling the algorithm from another algorithm, or when
# calling from the QGIS console.

INPUT_SQL = 'INPUT_SQL'

OUTPUT_LAYERS = 'OUTPUT_LAYERS'
OUTPUT_STRING = 'OUTPUT_STRING'

def name(self):
return 'add_styles'

def displayName(self):
return self.tr('Load styles on layers')

def group(self):
return self.tr('Configuration')

def groupId(self):
return 'raepa_configuration'

def tr(self, string):
return QCoreApplication.translate('Processing', string)

def createInstance(self):
return self.__class__()

def initAlgorithm(self, config):
"""
Here we define the inputs and output of the algorithm, along
with some other properties.
"""
# INPUTS
self.addParameter(
QgsProcessingParameterString(
self.INPUT_SQL, 'INPUT_SQL',
optional=True
)
)


# OUTPUTS
# Add output for message
self.addOutput(
QgsProcessingOutputString(
self.OUTPUT_STRING,
self.tr('Output message')
)
)

def processAlgorithm(self, parameters, context, feedback):
"""
Here is where the processing itself takes place.
"""
msg = ''
msgl = ''
Sdir_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),'..','..','qgis', 'qml')
layers = QgsProject.instance().mapLayers()
for lyr in layers:
layer = context.project().mapLayer(lyr)
if layer.isValid():
name = layer.name()
fileName= ''
if name == "raepa_canalaep_l":
fileName = "canalisations_AEP.qml"
if name == "raepa_canalass_l":
fileName = "canalisations_ASS.qml"
if name == "raepa_ouvraep_p":
fileName = "ouvrages_AEP.qml"
if name == "raepa_ouvrass_p":
fileName = "ouvrages_ASS.qml"
if name == "raepa_apparaep_p":
fileName = "appareils_AEP.qml"
if name == "raepa_apparass_p":
fileName = "appareils_ASS.qml"
if fileName != '':
layer.loadNamedStyle(os.path.join(Sdir_path, fileName))
layer.triggerRepaint()
msg +=' path : '+ Sdir_path + fileName +' style: '+ layer.name()+ ' is load'
msg = os.path.join(Sdir_path, fileName)

else:
msgl += "!! "+layer.name()+" style not load !!"
return {
self.OUTPUT_STRING: msg
}
178 changes: 178 additions & 0 deletions processing/algorithms/load_project.py
@@ -0,0 +1,178 @@
"""
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""

__author__ = '3liz'
__date__ = '2019-02-15'
__copyright__ = '(C) 2019 by 3liz'

# This will get replaced with a git SHA1 when you do a git archive

__revision__ = '$Format:%H$'

from qgis.core import (
QgsProcessingAlgorithm,
QgsProcessingParameterString,
QgsProcessingOutputString,
QgsProcessingOutputNumber,
QgsExpressionContextUtils,
QgsVectorLayer,
QgsProcessingOutputMultipleLayers,
QgsProcessingContext,
QgsApplication,
QgsProject
)
from qgis.PyQt.QtSql import *
from .tools import *
from processing.tools.postgis import uri_from_name, GeoDB
import os

class LoadProject(QgsProcessingAlgorithm):
"""
"""

# Constants used to refer to parameters and outputs. They will be
# used when calling the algorithm from another algorithm, or when
# calling from the QGIS console.

INPUT_SQL = 'INPUT_SQL'

OUTPUT_LAYERS = 'OUTPUT_LAYERS'
OUTPUT_STRING = 'OUTPUT_STRING'

def name(self):
return 'load_project'

def displayName(self):
return self.tr('Load project after generated database')

def group(self):
return self.tr('Configuration')

def groupId(self):
return 'raepa_configuration'

def tr(self, string):
return QCoreApplication.translate('Processing', string)

def createInstance(self):
return self.__class__()

def initAlgorithm(self, config):
"""
Here we define the inputs and output of the algorithm, along
with some other properties.
"""
# INPUTS
self.addParameter(
QgsProcessingParameterString(
self.INPUT_SQL, 'INPUT_SQL',
optional=True
)
)


# OUTPUTS
# Add output for message
self.addOutput(
QgsProcessingOutputString(
self.OUTPUT_STRING,
self.tr('Output message')
)
)

self.addOutput(
QgsProcessingOutputMultipleLayers(
self.OUTPUT_LAYERS,
self.tr('Output message')
)
)

def processAlgorithm(self, parameters, context, feedback):
"""
Here is where the processing itself takes place.
"""
uri = uri_from_name(QgsExpressionContextUtils.globalScope().variable('raepa_connection_name'))
def initLayer(schema, table, geom, sql, id):
layer = None
uri.setDataSource(schema, table, geom, sql, id)
layer = QgsVectorLayer(uri.uri(), table, "postgres")
if not layer.isValid():
msgl = 'tt'
context.temporaryLayerStore().addMapLayer(layer)
context.addLayerToLoadOnCompletion(
layer.id(),
QgsProcessingContext.LayerDetails(table,
context.project(),
self.OUTPUT_LAYERS
)
)
return layer

layers_name = {
"sys_structure_metadonnee": {'geomfield': None, 'pk': 'id'},
"sys_liste_table": {'geomfield': None, 'pk': 'id'},
"sys_organisme_gestionnaire": {'geomfield': None, 'pk': 'id'},
"val_raepa_cat_canal_ae": {'geomfield': None, 'pk': 'id'},
"val_raepa_cat_canal_ass": {'geomfield': None, 'pk': 'id'},
"val_raepa_fonc_app_ae":{'geomfield': None, 'pk': 'id'},
"val_raepa_fonc_app_ass":{'geomfield': None, 'pk': 'id'},
"val_raepa_fonc_canal_ae":{'geomfield': None, 'pk': 'id'},
"val_raepa_fonc_canal_ass":{'geomfield': None, 'pk': 'id'},
"val_raepa_fonc_ouv_ae":{'geomfield': None, 'pk': 'id'},
"commune": {'geomfield': "geom", 'pk': 'id'},
"_val_raepa_precision_annee":{'geomfield': None, 'pk': 'id'},
"_val_raepa_type_intervention_ass":{'geomfield': None, 'pk': 'id'},
"raepa_apparaep_p": {'geomfield': "geom", 'pk': 'id'},
"raepa_apparass_p": {'geomfield': "geom", 'pk': 'id'},
"affleurant_pcrs": {'geomfield': "geom", 'pk': 'id'},
"_val_raepa_etat_canal_ass":{'geomfield': None, 'pk': 'id'},
"_val_raepa_forme_canal_ass":{'geomfield': None, 'pk': 'id'},
"raepa_canalaep_l": {'geomfield': "geom", 'pk': 'id'},
"raepa_reparaep_p": {'geomfield': "geom", 'pk': 'id'},
"raepa_reparass_p": {'geomfield': "geom", 'pk': 'id'},
"raepa_ouvraep_p": {'geomfield': "geom", 'pk': 'id'},
"raepa_ouvrass_p": {'geomfield': "geom", 'pk': 'id'},
"raepa_canalass_l": {'geomfield': "geom", 'pk': 'id'},
"v_canalisation_avec_z_manquant":{'geomfield': "geom", 'pk': 'id'},
"v_canalisation_avec_zaval_manquant":{'geomfield': "geom", 'pk': 'id'},
"v_canalisation_branchement":{'geomfield': "geom", 'pk': 'id'},
"v_canalisation_sans_ouvrage":{'geomfield': "geom", 'pk': 'id'},
"val_raepa_qualite_geoloc":{'geomfield': None, 'pk': 'id'},
"val_raepa_qualite_anpose":{'geomfield': None, 'pk': 'id'},
"val_raepa_typ_reseau_ass":{'geomfield': None, 'pk': 'id'},
"val_raepa_materiau":{'geomfield': None, 'pk': 'id'},
"val_raepa_mode_circulation":{'geomfield': None, 'pk': 'id'},
"val_raepa_fonc_ouv_ass":{'geomfield': None, 'pk': 'id'},
"val_raepa_type_defaillance":{'geomfield': None, 'pk': 'id'},
"val_raepa_support_reparation":{'geomfield': None, 'pk': 'id'}
}
output_layers = []
msg = ''
msgl = ''

for e in layers_name:
layer = initLayer("raepa", e, layers_name[e]['geomfield'], "", layers_name[e]['pk'])
if layer.isValid():
output_layers.append(layer)
msgl += " ## "+layer.name()+" loaded ##"

else:
msgl += " !! "+layer.name()+" did not load !!"


if len(output_layers) > 0:
msg = 'Import de couche ok ! '
msg += msgl
return {
self.OUTPUT_LAYERS: output_layers,
self.OUTPUT_STRING: msg
}
4 changes: 4 additions & 0 deletions processing/provider.py
Expand Up @@ -44,6 +44,8 @@
from .algorithms.upgrade_database_structure import UpgradeDatabaseStructure
from .algorithms.get_orientation_appareil import GetOrientationAppareil
from .algorithms.get_network_to_vanne import GetNetworkToVanne
from .algorithms.load_project import LoadProject
from .algorithms.add_styles import AddStyles


class RaepaProvider(QgsProcessingProvider):
Expand All @@ -70,6 +72,8 @@ def loadAlgorithms(self):
self.addAlgorithm(CancelLastModification())
self.addAlgorithm(GetOrientationAppareil())
self.addAlgorithm(GetNetworkToVanne())
self.addAlgorithm(LoadProject())
self.addAlgorithm(AddStyles())

def id(self):
return 'raepa'
Expand Down
17 changes: 17 additions & 0 deletions qgis/qml/actions_appareils_AEP.qml
@@ -0,0 +1,17 @@
<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
<qgis version="3.4.13-Madeira" styleCategories="Actions">
<attributeactions>
<defaultAction key="Canvas" value="{00000000-0000-0000-0000-000000000000}"/>
<actionsetting action="from qgis.utils import plugins&#xa;plugins['raepa'].run_action(&#xa; 'parcourir_reseau_depuis_cet_objet',&#xa; '[% idappareil %]',&#xa; 0&#xa;)" capture="0" isEnabledOnlyWhenEditable="0" type="1" id="{b3bb6295-70ae-44c6-97f7-bbc71a51d4ee}" name="Parcourir le réseau depuis cet objet" shortTitle="" icon="" notificationMessage="">
<actionScope id="Feature"/>
<actionScope id="Field"/>
<actionScope id="Canvas"/>
</actionsetting>
<actionsetting action="from qgis.utils import plugins&#xa;plugins['raepa'].run_action(&#xa; 'calcul_orientation_appareil',&#xa; '[% idappareil %]',&#xa;)" capture="0" isEnabledOnlyWhenEditable="0" type="1" id="{efbd9a79-f784-43b0-876a-c3254754d686}" name="Calcul de l'orientation de l'appareil" shortTitle="" icon="" notificationMessage="">
<actionScope id="Feature"/>
<actionScope id="Field"/>
<actionScope id="Canvas"/>
</actionsetting>
</attributeactions>
<layerGeometryType>0</layerGeometryType>
</qgis>
12 changes: 12 additions & 0 deletions qgis/qml/actions_appareils_ASS.qml
@@ -0,0 +1,12 @@
<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
<qgis version="3.4.13-Madeira" styleCategories="Actions">
<attributeactions>
<defaultAction key="Canvas" value="{00000000-0000-0000-0000-000000000000}"/>
<actionsetting action="from qgis.utils import plugins&#xa;plugins['raepa'].run_action(&#xa; 'parcourir_reseau_depuis_cet_objet',&#xa; '[% idappareil %]',&#xa; 0&#xa;)" capture="0" isEnabledOnlyWhenEditable="0" type="1" id="{c540b6cc-c2a0-4402-9a85-9e666ea76ba7}" name="Parcourir le réseau depuis cet objet" shortTitle="" icon="" notificationMessage="">
<actionScope id="Feature"/>
<actionScope id="Field"/>
<actionScope id="Canvas"/>
</actionsetting>
</attributeactions>
<layerGeometryType>0</layerGeometryType>
</qgis>
21 changes: 21 additions & 0 deletions qgis/qml/actions_canalisations_AEP.qml
@@ -0,0 +1,21 @@
<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
<qgis version="3.4.13-Madeira" styleCategories="Actions">
<attributeactions>
<defaultAction key="Canvas" value="{00000000-0000-0000-0000-000000000000}"/>
<actionsetting action="from qgis.utils import plugins&#xa;plugins['raepa'].run_action(&#xa; 'inverser_canalisation',&#xa; '[% id %]',&#xa; '[% @layer_id %]'&#xa;)" capture="0" isEnabledOnlyWhenEditable="0" type="1" id="{b661253b-1f14-4077-a7e2-8375868562db}" name="Inverser la canalisation" shortTitle="Inverser la canalisation" icon="" notificationMessage="">
<actionScope id="Feature"/>
<actionScope id="Field"/>
</actionsetting>
<actionsetting action="from qgis.utils import plugins&#xa;plugins['raepa'].run_action(&#xa; 'parcourir_reseau_depuis_cet_objet',&#xa; '[% idcana %]',&#xa; 0&#xa;)" capture="0" isEnabledOnlyWhenEditable="0" type="1" id="{2a9144ec-949e-42c0-89d3-9fe9020b32dd}" name="Parcourir le réseau depuis cet objet" shortTitle="" icon="" notificationMessage="">
<actionScope id="Feature"/>
<actionScope id="Field"/>
<actionScope id="Canvas"/>
</actionsetting>
<actionsetting action="from qgis.utils import plugins&#xa;plugins['raepa'].run_action(&#xa; 'network_to_vanne',&#xa; '[% idcana %]'&#xa;)" capture="0" isEnabledOnlyWhenEditable="0" type="1" id="{1249ed38-1d64-4036-a8e3-bdf8a363898a}" name="Parcourir le sous réseau jusqu'à une vanne" shortTitle="Parcours réseau vanne" icon="" notificationMessage="">
<actionScope id="Feature"/>
<actionScope id="Field"/>
<actionScope id="Canvas"/>
</actionsetting>
</attributeactions>
<layerGeometryType>1</layerGeometryType>
</qgis>
17 changes: 17 additions & 0 deletions qgis/qml/actions_canalisations_ASS.qml
@@ -0,0 +1,17 @@
<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
<qgis version="3.4.13-Madeira" styleCategories="Actions">
<attributeactions>
<defaultAction key="Canvas" value="{00000000-0000-0000-0000-000000000000}"/>
<actionsetting action="from qgis.utils import plugins&#xa;plugins['raepa'].run_action(&#xa; 'inverser_canalisation',&#xa; '[% id %]',&#xa; '[% @layer_id %]'&#xa;)" capture="0" isEnabledOnlyWhenEditable="0" type="1" id="{5fbb59c5-2b60-4b1f-a2ce-cc5d45cf3aba}" name="Inverser la canalisation" shortTitle="Inverser la canalisation" icon="" notificationMessage="">
<actionScope id="Feature"/>
<actionScope id="Field"/>
<actionScope id="Canvas"/>
</actionsetting>
<actionsetting action="from qgis.utils import plugins&#xa;plugins['raepa'].run_action(&#xa; 'parcourir_reseau_depuis_cet_objet',&#xa; '[% idcana %]',&#xa; 0&#xa;)" capture="0" isEnabledOnlyWhenEditable="0" type="1" id="{f4852f46-9647-4400-9fde-d1975d6ce816}" name="Parcourir le réseau depuis cet objet" shortTitle="" icon="" notificationMessage="">
<actionScope id="Feature"/>
<actionScope id="Field"/>
<actionScope id="Canvas"/>
</actionsetting>
</attributeactions>
<layerGeometryType>1</layerGeometryType>
</qgis>

0 comments on commit af62954

Please sign in to comment.