0

I'm writing write a plugin for QGIS. Its first step is to select an item after having selected 'parents items', in a cascade selection.

It works, but it is getting slower and slower after a few tests. I guess it's a problem of accumulation in the memory, but I can't see how to resolve it.

enter image description here

FILE 1 : plan_culture.py

from PyQt5 import QtWidgets
import os.path
from PyQt5.QtCore import QSettings, QTranslator, qVersion, QCoreApplication
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QAction, QFileDialog
from .resources import *
from .plan_culture_dialog import PlanCultureDialog
from .liaisons_bdd import LiaisonsBDD

class PlanCulture:

def __init__(self, iface):

    # Save reference to the QGIS interface
    self.iface = iface
    # -----------
    self.departements = None
    self.choix_dep = None
    self.sql_com = None
    self.curseur = None
    self.tuples = None
    self.liste_com = None
    self.communes = None
    self.choix_com = None
    self.sql_expl = None
    self.liste_expl = None
    self.exploitations = None
    #------------------------

#[ …… ]

def run(self):

    self.dlg.listExploitations.clear()
    self.dlg.listDepartements.clear()
    self.dlg.listCommunes.clear()
    #------------------remplissage de la liste des departements---------------
    self.departements = LiaisonsBDD.choix_departements(self)
    self.dlg.listDepartements.addItems(self.departements)
    self.dlg.listDepartements.currentTextChanged.connect(self.choix_communes)
    #---------------montrer le dialogue
    self.dlg.show()


def choix_communes(self):

    self.choix_dep = str(self.dlg.listDepartements.currentText())
    self.sql_com = "SELECT communes.nom_com  from referentiel.communes  \
    JOIN exploitations ON communes.insee_com = exploitations.insee_com \
    WHERE communes.nom_dep LIKE '{}' ORDER BY nom_com".format(self.choix_dep)
    #------------------execution du sql---------
    self.curseur = LiaisonsBDD.connexion_BDD(self)
    self.curseur.execute(self.sql_com)
    self.tuples = self.curseur.fetchall()   
    self.liste_com = []
    for x in self.tuples:
        self.liste_com.append(x[0])
    #------------------remplissage de la liste des communes---------------
    self.communes = self.liste_com
    self.dlg.listCommunes.clear()
    self.dlg.listCommunes.addItems(self.communes)
    self.dlg.listCommunes.currentTextChanged.connect(self.choix_exploitations)

def choix_exploitations(self):

    self.choix_com = str(self.dlg.listCommunes.currentText())
    #--chaine sql pour remplissage de la liste avec les exploitations sur la commune choisie
    self.sql_expl = "SELECT exploitations.nom_exploitation  FROM exploitations \
    JOIN referentiel.communes ON communes.insee_com = exploitations.insee_com \
    WHERE communes.nom_com LIKE '{}'  ORDER BY nom_exploitation".format(self.choix_com)
    #------------------execution du sql---------
    self.curseur = LiaisonsBDD.connexion_BDD(self)
    self.curseur.execute(self.sql_expl)
    self.tuples = self.curseur.fetchall()   
    self.liste_expl = []
    for x in self.tuples:
        self.liste_expl.append(x[0])
    #------------------remplissage de la liste des exploitations---------------
    self.exploitations = self.liste_expl
    self.dlg.listExploitations.clear()
    self.dlg.listExploitations.addItems(self.exploitations)

FILE 2 : Liaisons_BDD.py

import os.path
from qgis.core import QgsProject, QgsDataSourceUri, QgsCredentials
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtCore import QMetaObject, pyqtSlot
import psycopg2

class LiaisonsBDD:

def __init__(self, iface):

    super(plan_culture_dialog, self).__init__(parent)
    self.iface = iface
    self.setupUi(self)
    self.iface.addDockWidget(Qt.RightDockWidgetArea, self)

    # database properties
    self.couche = None
    self.connInfo = None
    self.success = None
    self.username1 = None
    self.password1 = None
    self.connexion = None
    self.curseur = None
    # sql ---------
    self.sql_dep = None
    self.tuples = None
    self.liste_dep = None



def connexion_BDD(self):
    """ouvrir connexion  a la BDD pour rapatriement donnees"""
    #############################################################################
    #utilisation de la methode QgsCredentials pour recuperer le password de la session ouverte
    #############################################################################
    self.couche = self.iface.activeLayer()
    self.connInfo = QgsDataSourceUri(self.couche.dataProvider().dataSourceUri()).connectionInfo()
    self.success, self.username1, self.password1 = QgsCredentials.instance().get(self.connInfo, None, None)
    if self.success:
        QgsCredentials.instance().put(self.connInfo, self.username1, self.password1)        

    #############################################################################
    #utilisation des metodes du module psycopg2 pour connexion a BDD avec req SQL
    #############################################################################

    self.connexion = psycopg2.connect(host ="localhost",  port="5432", dbname="pe_v2", user=self.username1, password=self.password1)
    self.curseur = self.connexion.cursor()
    return self.curseur

def choix_departements(self):

    self.sql_dep = "SELECT departements.nom_dep, departements.insee_dep from referentiel.departements \
    JOIN (select count(exploitations.*), insee_dep from exploitations group by insee_dep) AS nbre_exploit \
    ON nbre_exploit.insee_dep = departements.insee_dep ORDER BY nom_dep"

    self.curseur = LiaisonsBDD.connexion_BDD(self)
    self.curseur.execute(self.sql_dep)
    self.tuples = self.curseur.fetchall()   
    self.liste_dep = []
    for x in self.tuples:
        self.liste_dep.append(x[0])
    return self.liste_dep
  • This sounds like more of a programming issue than a GIS issue. You might have better luck posting it on stackoverflow.com. – csk Dec 28 '18 at 19:48

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.