From d2cc6f16b52bad54c9410048440b715352aed085 Mon Sep 17 00:00:00 2001 From: Tom LAVEILLE Date: Fri, 2 Aug 2024 10:25:19 +0200 Subject: [PATCH] =?UTF-8?q?T=C3=A9l=C3=A9verser=20les=20fichiers=20vers=20?= =?UTF-8?q?"CenRa=5FPOSTGIS"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CenRa_POSTGIS/__init__.py | 28 ++ CenRa_POSTGIS/cenra.png | Bin 0 -> 4977 bytes CenRa_POSTGIS/cenra.py | 925 +++++++++++++++++++++++++++++++++++ CenRa_POSTGIS/cenradialog.py | 37 ++ CenRa_POSTGIS/help.png | Bin 0 -> 2231 bytes 5 files changed, 990 insertions(+) create mode 100644 CenRa_POSTGIS/__init__.py create mode 100644 CenRa_POSTGIS/cenra.png create mode 100644 CenRa_POSTGIS/cenra.py create mode 100644 CenRa_POSTGIS/cenradialog.py create mode 100644 CenRa_POSTGIS/help.png diff --git a/CenRa_POSTGIS/__init__.py b/CenRa_POSTGIS/__init__.py new file mode 100644 index 0000000..c2b6426 --- /dev/null +++ b/CenRa_POSTGIS/__init__.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +""" +/*************************************************************************** + CenRa + A QGIS plugin + Conservatoire d'Espaces Naturels de Rhône-Alpes + ------------------- + begin : 2014-03-27 + copyright : (C) 2014 by Conservatoire d'Espaces Naturels de Rhône-Alpes + email : guillaume.costes@espaces-naturels.fr + ***************************************************************************/ + +/*************************************************************************** + * * + * 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. * + * * + ***************************************************************************/ + This script initializes the plugin, making it known to QGIS. +""" +from __future__ import absolute_import + +def classFactory(iface): + # load CenRa class from file CenRa + from .cenra import CenRa + return CenRa(iface) diff --git a/CenRa_POSTGIS/cenra.png b/CenRa_POSTGIS/cenra.png new file mode 100644 index 0000000000000000000000000000000000000000..d53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ZB_{u?6*$kEHeJoE-w!*k zEWUbT#GI@<@PT*ywWS;QI`Ch>Wxxf%Ix?@L&qT(72RyPmIPp(~|9@%njvssUXuU^V v7n6B8w8$HH8lFRUd!ha{;J}G)`hNoe7QyToB1l_>00000NkvXXu0mjfs=k0$ literal 0 HcmV?d00001 diff --git a/CenRa_POSTGIS/cenra.py b/CenRa_POSTGIS/cenra.py new file mode 100644 index 0000000..9331a43 --- /dev/null +++ b/CenRa_POSTGIS/cenra.py @@ -0,0 +1,925 @@ +# -*- coding: utf-8 -*- +""" +/*************************************************************************** + CenRa + A QGIS plugin + Conservatoire d'Espaces Naturels de Rhône-Alpes + ------------------- + begin : 2014-03-27 + copyright : (C) 2014 by Conservatoire d'Espaces Naturels de Rhône-Alpes + email : guillaume.costes@espaces-naturels.fr + ***************************************************************************/ + +/*************************************************************************** + * * + * 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. * + * * + ***************************************************************************/ +""" +from __future__ import absolute_import +# Import the PyQt and QGIS libraries +from builtins import next +from builtins import str +from builtins import object +from qgis.PyQt.QtCore import QSettings +from qgis.PyQt.QtWidgets import QAction, QMenu, QDialog +from qgis.PyQt.QtGui import QIcon +from PyQt5.QtCore import * +from PyQt5.QtGui import * +from qgis.core import * +from qgis.core import QgsDataSourceUri +# Initialize Qt resources from file resources.py +from . import resources_rc +# Import the code for the dialog +from .cenradialog import CenRaDialog +from .table_postgisdialog import table_postgisDialog +from .tools.PythonSQL import * +from .tools.resources import maj_verif + +import os.path +import webbrowser, os +import psycopg2 +import psycopg2.extras +import base64 + +class CenRa(object): + + def __init__(self, iface): + # Save reference to the QGIS interface + self.iface = iface + # initialize plugin directory + self.plugin_dir = os.path.dirname(__file__) + # initialize locale + locale = QSettings().value("locale/userLocale")[0:2] + localePath = os.path.join(self.plugin_dir, 'i18n', 'cenra_{}.qm'.format(locale)) + + if os.path.exists(localePath): + self.translator = QTranslator() + self.translator.load(localePath) + + if qVersion() > '4.3.3': + QCoreApplication.installTranslator(self.translator) + + # Create the dialog (after translation) and keep reference + self.dlg = CenRaDialog() + self.dlgAjout = table_postgisDialog() + maj_verif('CenRa_POSTGIS') + def initGui(self): + self.toolBar = self.iface.addToolBar("CEN-RA") + self.toolBar.setObjectName("CEN-RA") + + # ***Create action that will start plugin configuration + self.action = QAction( + QIcon(":/plugins/CenRa/page_new.png"), + u"Création d'un dossier", self.iface.mainWindow()) + # connect the action to the run method + self.action.triggered.connect(self.creation) + + # Add toolbar button and menu item + self.toolBar.addAction(self.action) + self.iface.addPluginToMenu(u"CenRa", self.action) + + # ***Create action that will start plugin configuration + self.action = QAction( + QIcon(":/plugins/CenRa/page_ajout.png"), + u"Ajout d'une table", self.iface.mainWindow()) + # connect the action to the run method + self.action.triggered.connect(self.ajout) + + # Add toolbar button and menu item + self.toolBar.addAction(self.action) + self.iface.addPluginToMenu(u"&CenRa", self.action) + + # ***Create action that will start plugin configuration + self.action = QAction( + QIcon(":/plugins/CenRa/help.png"), + u"Aide", self.iface.mainWindow()) + # connect the action to the run method + self.action.triggered.connect(self.doHelp) + + # Add toolbar button and menu item + self.toolBar.addAction(self.action) + self.iface.addPluginToMenu(u"CenRa", self.action) + + self.menu = QMenu() + self.menu.setTitle( QCoreApplication.translate( "CENRA","&CenRa" ) ) + + self.cenra_new = QAction( QIcon(":/plugins/CenRa/page_new.png"), QCoreApplication.translate("CENRA", u"Création d'un dossier" ), self.iface.mainWindow() ) + self.cenra_ajout = QAction( QIcon(":/plugins/CenRa/page_ajout.png"), QCoreApplication.translate("CENRA", "Ajout d'une table" ), self.iface.mainWindow() ) + self.cenra_help = QAction( QIcon(":/plugins/CenRa/help.png"), QCoreApplication.translate("CENRA", "Aide" ), self.iface.mainWindow() ) + + self.menu.addActions( [self.cenra_new, self.cenra_ajout, self.cenra_help] ) + + menu_bar = self.iface.mainWindow().menuBar() + actions = menu_bar.actions() + lastAction = actions[ len( actions ) - 1 ] + menu_bar.insertMenu( lastAction, self.menu ) + + self.cenra_new.triggered.connect(self.creation) + self.cenra_ajout.triggered.connect(self.ajout) + self.cenra_help.triggered.connect(self.doHelp) + + def unload(self): + # Remove the plugin menu item and icon + self.iface.removePluginMenu(u"&CENRA", self.action) + self.iface.removePluginMenu(u"&CenRa", self.action) + self.iface.removeToolBarIcon(self.action) + + # run method that performs all the real work + def creation(self): + # show the dialog + self.dlg.show() + # Run the dialog event loop + result = self.dlg.exec_() + # See if OK was pressed + if result == 1: + +#**********************************Debut_script**************************************** + + ### Creation du schema pour le nouveau site + if self.dlg.at.isChecked(): + schema = "_" + self.dlg.dept.currentText() + "_at_" + self.dlg.nom.text().lower() # Ajout de "_" pour eviter pb de numero en premier caractere + else : + schema = "_" + self.dlg.dept.currentText() + "_" + self.dlg.nom.text().lower() # Ajout de "_" pour eviter pb de numero en premier caractere + + if self.dlg.nom.text() == "" or self.dlg.nom.text() == "NULL": + QMessageBox.warning(None, "Oups :", "Veuillez renseigner un nom de dossier.") + return + + ch = [u"à", u"À", u"â", u"Â", u"ä", u"Ä", u"å", u"Å", u"ç", u"Ç", u"é", u"É", u"è", u"È", u"ê", u"Ê", u"ë", u"Ë", u"î", u"Î", u"ï", u"Ï", u"ô", u"Ô", u"ö", u"Ö", u"ù", u"Ù", u"û", u"Û", u"ü", u"Ü", u"ÿ", u"Ÿ", u"'", u"-", u" "] + for car in ch : + if self.dlg.nom.text().find(car) != -1 : + QMessageBox.warning(None, "Oups :", u"Le nom de dossier ne doit pas comporter de caractères spéciaux, ni d'espaces !\n\n\t" + self.dlg.nom.text().lower() ) + return + + first_conn = psycopg2.connect("host=" + host + " port=" + port + " dbname="+dbname+" user=first_cnx password=" + password) + first_cur = first_conn.cursor(cursor_factory = psycopg2.extras.DictCursor) + first_cur.execute("SELECT mdp_w, login_w FROM pg_catalog.pg_user t1, admin_sig.vm_users_sig t2 WHERE t2.oid = t1.usesysid AND (login_w = '" + os_user + "' OR login_w = '" + os_user + "')") + res_ident = first_cur.fetchone() + mdp = base64.b64decode(str(res_ident[0])).decode('utf-8') + user = res_ident[1] + con = psycopg2.connect("host=" + host + " port=" + port + " dbname="+dbname+" user=" + user + " password=" + mdp) + cur = con.cursor(cursor_factory = psycopg2.extras.DictCursor) + first_conn.close() + + SQL_schema = "CREATE SCHEMA " + schema + ";" + SQL_GRANT_SCHEMA="GRANT CREATE, USAGE ON SCHEMA "+schema+" TO grp_qgis; GRANT CREATE, USAGE ON SCHEMA "+schema+" TO grp_sig;" + + cur.execute(SQL_schema) + cur.execute(SQL_GRANT_SCHEMA) + + ### Creation de la table contour + if self.dlg.couche_contour.isChecked(): # Verifie si la checkbox est cochee + if self.dlg.annee_1.text() == 'aaaa' or self.dlg.annee_1.text() == '': + tablename = schema + "_contour" + else : + tablename = schema + "_contour_" + self.dlg.annee_1.text() + tablename_qgis = tablename[1:] # Permet d'enlever le "_", ajouter a la premiere etape, dans qgis + + SQL_contour = "CREATE TABLE " + schema + "."+ tablename + champ_contour + SQL_pkey = "ALTER TABLE " + schema + "." + tablename + " ADD CONSTRAINT " + tablename + "_pkey" + " PRIMARY KEY (gid)" + SQL_trigger_area_m2 = "CREATE TRIGGER area_m2" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.area_m2();" + SQL_trigger_area_ha = "CREATE TRIGGER area_ha" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.area_ha();" + SQL_trigger_date_creation = "CREATE TRIGGER date_creation" + tablename + " BEFORE INSERT ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_creation();" + SQL_trigger_date_maj = "CREATE TRIGGER date_maj" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_maj();" + + SQL_style = """INSERT INTO layer_styles (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, update_time) + SELECT f_table_catalog, '""" + schema + "', '" + tablename + """', f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, now() + FROM layer_styles + WHERE description = 'contour_modele_v3'""" + SQL_GRANT_TABLE="GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename+" TO grp_qgis;GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename+" TO grp_sig;GRANT ALL ON SEQUENCE "+schema+"."+tablename+"_gid_seq TO grp_qgis;" + + cur.execute(SQL_contour) + cur.execute(SQL_pkey) + cur.execute(SQL_trigger_area_m2) + cur.execute(SQL_trigger_area_ha) + cur.execute(SQL_trigger_date_creation) + cur.execute(SQL_trigger_date_maj) + cur.execute(SQL_style) ## Enregistrement du style (comme style par defaut) dans la table layer_styles + cur.execute(SQL_GRANT_TABLE) + + con.commit() + + ## Affichage de la table + uri = QgsDataSourceUri() + # set host name, port, database name, username and password + uri.setConnection(host ,port ,dbname ,user ,mdp) + # set database schema, table name, geometry column and optionaly subset (WHERE clause) + uri.setDataSource(schema, tablename, geom) + + layer = self.iface.addVectorLayer(uri.uri(), tablename_qgis, "postgres") + + ### Creation de la table habitat + if self.dlg.couche_habitat.isChecked(): + if self.dlg.annee_2.text() == 'aaaa' or self.dlg.annee_2.text() == '': + tablename = schema + "_habitat" + else : + tablename = schema + "_habitat_" + self.dlg.annee_2.text() + tablename_1=tablename+"_points" + tablename_qgis = tablename_1[1:] # Permet d'enlever le "_", ajouter a la premiere etape, dans qgis + SQL_VEGETHAB = "CREATE TABLE "+ schema + "."+ tablename_1 + champ_vegethab_point + SQL_pkey = "ALTER TABLE " + schema + "." + tablename_1 + " ADD CONSTRAINT " + tablename_1 + "_pkey" + " PRIMARY KEY (gid)" + SQL_trigger_coordonnees = "CREATE TRIGGER coordonnees" + tablename_1 + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename_1 + " FOR EACH ROW EXECUTE PROCEDURE ref.coordonnees();" + SQL_style = """INSERT INTO layer_styles (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, update_time) + SELECT f_table_catalog, '""" + schema + "', '" + tablename_1 + """', f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, now() + FROM layer_styles + WHERE description = 'vegethab_points_modele_v3'""" + SQL_GRANT_TABLE="GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename_1+" TO grp_qgis;GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename_1+" TO grp_sig;GRANT ALL ON SEQUENCE "+schema+"."+tablename_1+"_gid_seq TO grp_qgis;" + + cur.execute(SQL_VEGETHAB) + cur.execute(SQL_pkey) + cur.execute(SQL_trigger_coordonnees) + cur.execute(SQL_style) + cur.execute(SQL_GRANT_TABLE) + + con.commit() + ### Affichage de la table + uri = QgsDataSourceUri() + # set host name, port, database name, username and password + uri.setConnection(host ,port ,dbname ,user ,mdp) + # set database schema, table name, geometry column and optionaly subset (WHERE clause) + uri.setDataSource(schema, tablename_1, geom) + + layer = self.iface.addVectorLayer(uri.uri(), tablename_qgis, "postgres") + + tablename_2=tablename+"_lignes" + tablename_qgis = tablename_2[1:] + SQL_VEGETHAB = "CREATE TABLE "+ schema + "."+ tablename_2 + champ_vegethab_multilinestring + SQL_pkey = "ALTER TABLE " + schema + "." + tablename_2 + " ADD CONSTRAINT " + tablename_2 + "_pkey" + " PRIMARY KEY (gid)" + SQL_trigger_length_m = "CREATE TRIGGER length_m" + tablename_2 + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename_2 + " FOR EACH ROW EXECUTE PROCEDURE ref.length_m();" + SQL_trigger_length_km = "CREATE TRIGGER length_km" + tablename_2 + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename_2 + " FOR EACH ROW EXECUTE PROCEDURE ref.length_km();" + SQL_style = """INSERT INTO layer_styles (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, update_time) + SELECT f_table_catalog, '""" + schema + "', '" + tablename_2 + """', f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, now() + FROM layer_styles + WHERE description = 'vegethab_lignes_modele_v3'""" + SQL_GRANT_TABLE="GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename_2+" TO grp_qgis;GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename_2+" TO grp_sig;GRANT ALL ON SEQUENCE "+schema+"."+tablename_2+"_gid_seq TO grp_qgis;" + + cur.execute(SQL_VEGETHAB) + cur.execute(SQL_pkey) + cur.execute(SQL_trigger_length_m) + cur.execute(SQL_trigger_length_km) + cur.execute(SQL_style) + cur.execute(SQL_GRANT_TABLE) + + con.commit() + ### Affichage de la table + uri = QgsDataSourceUri() + # set host name, port, database name, username and password + uri.setConnection(host ,port ,dbname ,user ,mdp) + # set database schema, table name, geometry column and optionaly subset (WHERE clause) + uri.setDataSource(schema, tablename_2, geom) + + layer = self.iface.addVectorLayer(uri.uri(), tablename_qgis, "postgres") + + tablename_3=tablename+"_polygones" + tablename_qgis = tablename_3[1:] + SQL_VEGETHAB = "CREATE TABLE "+ schema + "."+ tablename_3 + champ_vegethab_multipolygon + SQL_pkey = "ALTER TABLE " + schema + "." + tablename_3 + " ADD CONSTRAINT " + tablename_3 + "_pkey" + " PRIMARY KEY (gid)" + SQL_trigger_area_m2 = "CREATE TRIGGER area_m2" + tablename_3 + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename_3 + " FOR EACH ROW EXECUTE PROCEDURE ref.area_m2();" + SQL_trigger_area_ha = "CREATE TRIGGER area_ha" + tablename_3 + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename_3 + " FOR EACH ROW EXECUTE PROCEDURE ref.area_ha();" + SQL_style = """INSERT INTO layer_styles (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, update_time) + SELECT f_table_catalog, '""" + schema + "', '" + tablename_3 + """', f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, now() + FROM layer_styles + WHERE description = 'vegethab_polygones_modele_v3'""" + SQL_GRANT_TABLE="GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename_3+" TO grp_qgis;GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename_3+" TO grp_sig;GRANT ALL ON SEQUENCE "+schema+"."+tablename_3+"_gid_seq TO grp_qgis;" + + cur.execute(SQL_VEGETHAB) + cur.execute(SQL_pkey) + cur.execute(SQL_trigger_area_m2) + cur.execute(SQL_trigger_area_ha) + cur.execute(SQL_style) + cur.execute(SQL_GRANT_TABLE) + + con.commit() + ### Affichage de la table + uri = QgsDataSourceUri() + # set host name, port, database name, username and password + uri.setConnection(host ,port ,dbname ,user ,mdp) + # set database schema, table name, geometry column and optionaly subset (WHERE clause) + uri.setDataSource(schema, tablename_3, geom) + + layer = self.iface.addVectorLayer(uri.uri(), tablename_qgis, "postgres") + ''' + SQL_habitat = "CREATE TABLE " + schema + "."+ tablename + champ_habitat + SQL_pkey = "ALTER TABLE " + schema + "." + tablename + " ADD CONSTRAINT " + tablename + "_pkey" + " PRIMARY KEY (gid)" + SQL_trigger_area_m2 = "CREATE TRIGGER area_m2" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.area_m2();" + SQL_trigger_area_ha = "CREATE TRIGGER area_ha" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.area_ha();" + SQL_trigger_concat_cd_cb = "CREATE TRIGGER concat_cd_cb" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.concat_cd_cb();" + SQL_trigger_date_creation = "CREATE TRIGGER date_creation" + tablename + " BEFORE INSERT ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_creation();" + SQL_trigger_date_maj = "CREATE TRIGGER date_maj" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_maj();" + + SQL_style = """INSERT INTO layer_styles (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, update_time) + SELECT f_table_catalog, '""" + schema + "', '" + tablename + """', f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, now() + FROM layer_styles + WHERE description = 'habitat_modele_v3'""" + + cur.execute(SQL_habitat) + cur.execute(SQL_pkey) + cur.execute(SQL_trigger_area_m2) + cur.execute(SQL_trigger_area_ha) + cur.execute(SQL_trigger_concat_cd_cb) + cur.execute(SQL_trigger_date_creation) + cur.execute(SQL_trigger_date_maj) + cur.execute(SQL_style) ## Enregistrement du style (comme style par defaut) dans la table layer_styles + + con.commit() + + ## Affichage de la table + uri = QgsDataSourceUri() + # set host name, port, database name, username and password + uri.setConnection(host ,port ,dbname ,user ,mdp) + # set database schema, table name, geometry column and optionaly subset (WHERE clause) + uri.setDataSource(schema, tablename, geom) + + layer = self.iface.addVectorLayer(uri.uri(), tablename_qgis, "postgres") + ''' + ### Creation de la table travaux prevus + if self.dlg.couche_travaux_prevus.isChecked(): + #**********Poly + if self.dlg.annee_5.text() == 'aaaa' or self.dlg.annee_5.text() == '': + tablename = schema + "_travaux_prevus_poly" + else : + tablename = schema + "_travaux_prevus_poly_" + self.dlg.annee_5.text() + tablename_qgis = tablename[1:] # Permet d'enlever le "_", ajouter a la premiere etape, dans qgis + + SQL_travaux_prevus = "CREATE TABLE " + schema + "."+ tablename + champ_travaux_prevus_multipolygon + SQL_pkey = "ALTER TABLE " + schema + "." + tablename + " ADD CONSTRAINT " + tablename + "_pkey" + " PRIMARY KEY (gid)" + SQL_trigger_date_creation = "CREATE TRIGGER date_creation" + tablename + " BEFORE INSERT ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_creation();" + SQL_trigger_date_maj = "CREATE TRIGGER date_maj" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_maj();" + SQL_trigger_area_m2 = "CREATE TRIGGER area_m2" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.area_m2();" + SQL_trigger_area_ha = "CREATE TRIGGER area_ha" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.area_ha();" + + SQL_style = """INSERT INTO layer_styles (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, update_time) + SELECT f_table_catalog, '""" + schema + "', '" + tablename + """', f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, now() + FROM layer_styles + WHERE description = 'travaux_prevus_poly_modele_v3'""" + SQL_GRANT_TABLE="GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename+" TO grp_qgis;GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename+" TO grp_sig;GRANT ALL ON SEQUENCE "+schema+"."+tablename+"_gid_seq TO grp_qgis;" + + cur.execute(SQL_travaux_prevus) + cur.execute(SQL_pkey) + cur.execute(SQL_trigger_date_creation) + cur.execute(SQL_trigger_date_maj) + cur.execute(SQL_trigger_area_m2) + cur.execute(SQL_trigger_area_ha) + cur.execute(SQL_style) ## Enregistrement du style (comme style par defaut) dans la table layer_styles + cur.execute(SQL_GRANT_TABLE) + + + con.commit() + + ## Affichage de la table + uri = QgsDataSourceUri() + # set host name, port, database name, username and password + uri.setConnection(host ,port ,dbname ,user ,mdp) + # set database schema, table name, geometry column and optionaly subset (WHERE clause) + uri.setDataSource(schema, tablename, geom) + + layer = self.iface.addVectorLayer(uri.uri(), tablename_qgis, "postgres") + + #**********ligne + if self.dlg.annee_5.text() == 'aaaa' or self.dlg.annee_5.text() == '': + tablename = schema + "_travaux_prevus_ligne" + else : + tablename = schema + "_travaux_prevus_ligne_" + self.dlg.annee_5.text() + tablename_qgis = tablename[1:] # Permet d'enlever le "_", ajouter a la premiere etape, dans qgis + + SQL_travaux_prevus = "CREATE TABLE " + schema + "."+ tablename + champ_travaux_prevus_multilinestring + SQL_pkey = "ALTER TABLE " + schema + "." + tablename + " ADD CONSTRAINT " + tablename + "_pkey" + " PRIMARY KEY (gid)" + SQL_trigger_date_creation = "CREATE TRIGGER date_creation" + tablename + " BEFORE INSERT ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_creation();" + SQL_trigger_date_maj = "CREATE TRIGGER date_maj" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_maj();" + SQL_trigger_length_m = "CREATE TRIGGER length_m" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.length_m();" + SQL_trigger_length_km = "CREATE TRIGGER length_km" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.length_km();" + + SQL_style = """INSERT INTO layer_styles (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, update_time) + SELECT f_table_catalog, '""" + schema + "', '" + tablename + """', f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, now() + FROM layer_styles + WHERE description = 'travaux_prevus_ligne_modele_v3'""" + SQL_GRANT_TABLE="GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename+" TO grp_qgis;GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename+" TO grp_sig;GRANT ALL ON SEQUENCE "+schema+"."+tablename+"_gid_seq TO grp_qgis;" + + cur.execute(SQL_travaux_prevus) + cur.execute(SQL_pkey) + cur.execute(SQL_trigger_date_creation) + cur.execute(SQL_trigger_date_maj) + cur.execute(SQL_trigger_length_m) + cur.execute(SQL_trigger_length_km) + cur.execute(SQL_style) ## Enregistrement du style (comme style par defaut) dans la table layer_styles + cur.execute(SQL_GRANT_TABLE) + + + con.commit() + + ## Affichage de la table + uri = QgsDataSourceUri() + # set host name, port, database name, username and password + uri.setConnection(host ,port ,dbname ,user ,mdp) + # set database schema, table name, geometry column and optionaly subset (WHERE clause) + uri.setDataSource(schema, tablename, geom) + + layer = self.iface.addVectorLayer(uri.uri(), tablename_qgis, "postgres") + + #**********point + if self.dlg.annee_5.text() == 'aaaa' or self.dlg.annee_5.text() == '': + tablename = schema + "_travaux_prevus_point" + else : + tablename = schema + "_travaux_prevus_point_" + self.dlg.annee_5.text() + tablename_qgis = tablename[1:] # Permet d'enlever le "_", ajouter a la premiere etape, dans qgis + + SQL_travaux_prevus = "CREATE TABLE " + schema + "."+ tablename + champ_travaux_prevus_point + SQL_pkey = "ALTER TABLE " + schema + "." + tablename + " ADD CONSTRAINT " + tablename + "_pkey" + " PRIMARY KEY (gid)" + SQL_trigger_date_creation = "CREATE TRIGGER date_creation" + tablename + " BEFORE INSERT ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_creation();" + SQL_trigger_date_maj = "CREATE TRIGGER date_maj" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_maj();" + SQL_trigger_coordonnees = "CREATE TRIGGER coordonnees" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.coordonnees();" + + SQL_style = """INSERT INTO layer_styles (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, update_time) + SELECT f_table_catalog, '""" + schema + "', '" + tablename + """', f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, now() + FROM layer_styles + WHERE description = 'travaux_prevus_point_modele_v3'""" + SQL_GRANT_TABLE="GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename+" TO grp_qgis;GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename+" TO grp_sig;GRANT ALL ON SEQUENCE "+schema+"."+tablename+"_gid_seq TO grp_qgis;" + + cur.execute(SQL_travaux_prevus) + cur.execute(SQL_pkey) + cur.execute(SQL_trigger_date_creation) + cur.execute(SQL_trigger_date_maj) + cur.execute(SQL_trigger_coordonnees) + cur.execute(SQL_style) ## Enregistrement du style (comme style par defaut) dans la table layer_styles + cur.execute(SQL_GRANT_TABLE) + + + con.commit() + + ## Affichage de la table + uri = QgsDataSourceUri() + # set host name, port, database name, username and password + uri.setConnection(host ,port ,dbname ,user ,mdp) + # set database schema, table name, geometry column and optionaly subset (WHERE clause) + uri.setDataSource(schema, tablename, geom) + + layer = self.iface.addVectorLayer(uri.uri(), tablename_qgis, "postgres") + + ### Creation de la table vierge + if self.dlg.couche_vierge.isChecked(): + if self.dlg.annee_4.text() == 'aaaa' or self.dlg.annee_4.text() == '': + tablename = schema + "_" + self.dlg.nom_couche_vierge.text().lower() + else : + tablename = schema + "_" + self.dlg.nom_couche_vierge.text().lower() + "_" + self.dlg.annee_4.text() + tablename_qgis = tablename[1:] # Permet d'enlever le "_", ajouter a la premiere etape, dans qgis + + if self.dlg.couche_vierge_point.isChecked() == 1 : + champ_vierge = champ_viergePoint + + if self.dlg.couche_vierge_ligne.isChecked() == 1 : + champ_vierge = champ_viergeLigne + + if self.dlg.couche_vierge_polygone.isChecked() == 1 : + champ_vierge = champ_viergePolygone + + SQL_vierge = "CREATE TABLE " + schema + "."+ tablename + champ_vierge + SQL_pkey = "ALTER TABLE " + schema + "." + tablename + " ADD CONSTRAINT " + tablename + "_pkey" + " PRIMARY KEY (gid)" + + SQL_trigger_area_m2 = "CREATE TRIGGER area_m2" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.area_m2();" + SQL_trigger_area_ha = "CREATE TRIGGER area_ha" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.area_ha();" + SQL_trigger_length_m = "CREATE TRIGGER length_m" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.length_m();" + SQL_trigger_length_km = "CREATE TRIGGER length_km" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.length_km();" + SQL_trigger_coordonnees = "CREATE TRIGGER coordonnees" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.coordonnees();" + + cur.execute(SQL_vierge) + cur.execute(SQL_pkey) + + if self.dlg.couche_vierge_point.isChecked() == 1 : + cur.execute(SQL_trigger_coordonnees) + + if self.dlg.couche_vierge_ligne.isChecked() == 1 : + cur.execute(SQL_trigger_length_m) + cur.execute(SQL_trigger_length_km) + + if self.dlg.couche_vierge_polygone.isChecked() == 1 : + cur.execute(SQL_trigger_area_m2) + cur.execute(SQL_trigger_area_ha) + + con.commit() + + ### Affichage de la table + uri = QgsDataSourceUri() + # set host name, port, database name, username and password + uri.setConnection(host ,port ,dbname ,user ,mdp) + # set database schema, table name, geometry column and optionaly subset (WHERE clause) + uri.setDataSource(schema, tablename, geom) + + layer = self.iface.addVectorLayer(uri.uri(), tablename_qgis, "postgres") + + else : + con.commit() + + con.close() + pass + + ### Outil Aide + def doHelp(self): + webbrowser.open("http://plateformesig.cenra-outils.org/") + + ### Outil Ajout de nouvelles couche a un dossier + def ajout(self): + first_conn = psycopg2.connect("host=" + host + " port=" + port + " dbname="+dbname+" user=first_cnx password=" + password) + first_cur = first_conn.cursor(cursor_factory = psycopg2.extras.DictCursor) + first_cur.execute("SELECT mdp_w, login_w FROM pg_catalog.pg_user t1, admin_sig.vm_users_sig t2 WHERE t2.oid = t1.usesysid AND (login_w = '" + os_user + "' OR login_w = '" + os_user + "')") + res_ident = first_cur.fetchone() + mdp = base64.b64decode(str(res_ident[0])).decode('utf-8') + user = res_ident[1] + + con = psycopg2.connect("host=" + host + " port=" + port + " dbname="+dbname+" user=" + user + " password=" + mdp) + cur = con.cursor(cursor_factory = psycopg2.extras.DictCursor) + first_conn.close() + # Creation de la liste des schemas de la base de donnees + SQL = """WITH list_schema AS ( + select schemaname,'sig' as group from pg_catalog.pg_tables + where schemaname ~ E'^_(0|1|2|3|4|6|7|f)' + group by schemaname + order by schemaname) + + select string_agg(schemaname,',') + from list_schema + group by "group";""" + + cur.execute(SQL) + + list_brut = str(next(cur)) + list = list_brut [2:-2] + listItems = list.split(",") + + con.close() + + self.dlgAjout.ui.schema.clear() + self.dlgAjout.ui.schema.addItems(listItems) + self.dlgAjout.ui.schema.setCurrentIndex(-1) # Pour ne pas commencer la liste au premier schema + + # show the dialog + self.dlgAjout.show() + # Run the dialog event loop + result = self.dlgAjout.exec_() + # See if OK was pressed + if result == 1: +#******************************debut script********************************* + first_conn = psycopg2.connect("host=" + host + " port=" + port + " dbname="+dbname+" user=first_cnx password=" + password) + first_cur = first_conn.cursor(cursor_factory = psycopg2.extras.DictCursor) + first_cur.execute("SELECT mdp_w, login_w FROM pg_catalog.pg_user t1, admin_sig.vm_users_sig t2 WHERE t2.oid = t1.usesysid AND (login_w = '" + os_user + "' OR login_w = '" + os_user + "')") + res_ident = first_cur.fetchone() + mdp = base64.b64decode(str(res_ident[0])).decode('utf-8') + user = res_ident[1] + con = psycopg2.connect("host=" + host + " port=" + port + " dbname="+dbname+" user=" + user + " password=" + mdp) + cur = con.cursor(cursor_factory = psycopg2.extras.DictCursor) + first_conn.close() + + if self.dlgAjout.ui.schema.currentIndex() == -1 : + QMessageBox.warning(None, "Oups :", "Veuillez choisir un nom de dossier.") + return + + schema = self.dlgAjout.ui.schema.currentText() + + ### Creation de la table contour + if self.dlgAjout.ui.couche_contour.isChecked(): # Verifie si la checkbox est cochee + if self.dlgAjout.ui.annee_1.text() == 'aaaa' or self.dlgAjout.ui.annee_1.text() == '': + tablename = schema + "_contour" + else : + tablename = schema + "_contour_" + self.dlgAjout.ui.annee_1.text() + tablename_qgis = tablename[1:] # Permet d'enlever le "_", ajouter a la premiere etape, dans qgis + + SQL_contour = "CREATE TABLE " + schema + "."+ tablename + champ_contour + SQL_pkey = "ALTER TABLE " + schema + "." + tablename + " ADD CONSTRAINT " + tablename + "_pkey" + " PRIMARY KEY (gid)" + SQL_trigger_area_m2 = "CREATE TRIGGER area_m2" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.area_m2();" + SQL_trigger_area_ha = "CREATE TRIGGER area_ha" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.area_ha();" + SQL_trigger_date_creation = "CREATE TRIGGER date_creation" + tablename + " BEFORE INSERT ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_creation();" + SQL_trigger_date_maj = "CREATE TRIGGER date_maj" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_maj();" + + SQL_style = """INSERT INTO layer_styles (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, update_time) + SELECT f_table_catalog, '""" + schema + "', '" + tablename + """', f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, now() + FROM layer_styles + WHERE description = 'contour_modele_v3'""" + SQL_GRANT_TABLE="GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename+" TO grp_qgis;GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename+" TO grp_sig;GRANT ALL ON SEQUENCE "+schema+"."+tablename+"_gid_seq TO grp_qgis;" + + cur.execute(SQL_contour) + cur.execute(SQL_pkey) + cur.execute(SQL_trigger_area_m2) + cur.execute(SQL_trigger_area_ha) + cur.execute(SQL_trigger_date_creation) + cur.execute(SQL_trigger_date_maj) + cur.execute(SQL_style) ## Enregistrement du style (comme style par defaut) dans la table layer_styles + cur.execute(SQL_GRANT_TABLE) + + con.commit() + + ## Affichage de la table + uri = QgsDataSourceUri() + # set host name, port, database name, username and password + uri.setConnection(host ,port ,dbname ,user ,mdp) + # set database schema, table name, geometry column and optionaly subset (WHERE clause) + uri.setDataSource(schema, tablename, geom) + + layer = self.iface.addVectorLayer(uri.uri(), tablename_qgis, "postgres") + + ### Creation de la table habitat + + if self.dlgAjout.ui.couche_habitat.isChecked(): + if self.dlgAjout.ui.annee_2.text() == 'aaaa' or self.dlgAjout.ui.annee_2.text() == '': + tablename = schema + "_habitat" + else : + tablename = schema + "_habitat_" + self.dlgAjout.ui.annee_2.text() + + tablename_1=tablename+"_points" + tablename_qgis = tablename_1[1:] # Permet d'enlever le "_", ajouter a la premiere etape, dans qgis + SQL_VEGETHAB = "CREATE TABLE "+ schema + "."+ tablename_1 + champ_vegethab_point + SQL_pkey = "ALTER TABLE " + schema + "." + tablename_1 + " ADD CONSTRAINT " + tablename_1 + "_pkey" + " PRIMARY KEY (gid)" + SQL_trigger_coordonnees = "CREATE TRIGGER coordonnees" + tablename_1 + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename_1 + " FOR EACH ROW EXECUTE PROCEDURE ref.coordonnees();" + SQL_style = """INSERT INTO layer_styles (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, update_time) + SELECT f_table_catalog, '""" + schema + "', '" + tablename_1 + """', f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, now() + FROM layer_styles + WHERE description = 'vegethab_points_modele_v3'""" + SQL_GRANT_TABLE="GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename_1+" TO grp_qgis;GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename_1+" TO grp_sig;GRANT ALL ON SEQUENCE "+schema+"."+tablename_1+"_gid_seq TO grp_qgis;" + + cur.execute(SQL_VEGETHAB) + cur.execute(SQL_pkey) + cur.execute(SQL_trigger_coordonnees) + cur.execute(SQL_style) + cur.execute(SQL_GRANT_TABLE) + + con.commit() + ### Affichage de la table + uri = QgsDataSourceUri() + # set host name, port, database name, username and password + uri.setConnection(host ,port ,dbname ,user ,mdp) + # set database schema, table name, geometry column and optionaly subset (WHERE clause) + uri.setDataSource(schema, tablename_1, geom) + + layer = self.iface.addVectorLayer(uri.uri(), tablename_qgis, "postgres") + + tablename_2=tablename+"_lignes" + tablename_qgis = tablename_2[1:] + SQL_VEGETHAB = "CREATE TABLE "+ schema + "."+ tablename_2 + champ_vegethab_multilinestring + SQL_pkey = "ALTER TABLE " + schema + "." + tablename_2 + " ADD CONSTRAINT " + tablename_2 + "_pkey" + " PRIMARY KEY (gid)" + SQL_trigger_length_m = "CREATE TRIGGER length_m" + tablename_2 + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename_2 + " FOR EACH ROW EXECUTE PROCEDURE ref.length_m();" + SQL_trigger_length_km = "CREATE TRIGGER length_km" + tablename_2 + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename_2 + " FOR EACH ROW EXECUTE PROCEDURE ref.length_km();" + SQL_style = """INSERT INTO layer_styles (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, update_time) + SELECT f_table_catalog, '""" + schema + "', '" + tablename_2 + """', f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, now() + FROM layer_styles + WHERE description = 'vegethab_lignes_modele_v3'""" + SQL_GRANT_TABLE="GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename_2+" TO grp_qgis;GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename_2+" TO grp_sig;GRANT ALL ON SEQUENCE "+schema+"."+tablename_2+"_gid_seq TO grp_qgis;" + + cur.execute(SQL_VEGETHAB) + cur.execute(SQL_pkey) + cur.execute(SQL_trigger_length_m) + cur.execute(SQL_trigger_length_km) + cur.execute(SQL_style) + cur.execute(SQL_GRANT_TABLE) + + con.commit() + ### Affichage de la table + uri = QgsDataSourceUri() + # set host name, port, database name, username and password + uri.setConnection(host ,port ,dbname ,user ,mdp) + # set database schema, table name, geometry column and optionaly subset (WHERE clause) + uri.setDataSource(schema, tablename_2, geom) + + layer = self.iface.addVectorLayer(uri.uri(), tablename_qgis, "postgres") + + tablename_3=tablename+"_polygones" + tablename_qgis = tablename_3[1:] + SQL_VEGETHAB = "CREATE TABLE "+ schema + "."+ tablename_3 + champ_vegethab_multipolygon + SQL_pkey = "ALTER TABLE " + schema + "." + tablename_3 + " ADD CONSTRAINT " + tablename_3 + "_pkey" + " PRIMARY KEY (gid)" + SQL_trigger_area_m2 = "CREATE TRIGGER area_m2" + tablename_3 + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename_3 + " FOR EACH ROW EXECUTE PROCEDURE ref.area_m2();" + SQL_trigger_area_ha = "CREATE TRIGGER area_ha" + tablename_3 + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename_3 + " FOR EACH ROW EXECUTE PROCEDURE ref.area_ha();" + SQL_style = """INSERT INTO layer_styles (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, update_time) + SELECT f_table_catalog, '""" + schema + "', '" + tablename_3 + """', f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, now() + FROM layer_styles + WHERE description = 'vegethab_polygones_modele_v3'""" + SQL_GRANT_TABLE="GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename_3+" TO grp_qgis;GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename_3+" TO grp_sig;GRANT ALL ON SEQUENCE "+schema+"."+tablename_3+"_gid_seq TO grp_qgis;" + + cur.execute(SQL_VEGETHAB) + cur.execute(SQL_pkey) + cur.execute(SQL_trigger_area_m2) + cur.execute(SQL_trigger_area_ha) + cur.execute(SQL_style) + cur.execute(SQL_GRANT_TABLE) + + con.commit() + ### Affichage de la table + uri = QgsDataSourceUri() + # set host name, port, database name, username and password + uri.setConnection(host ,port ,dbname ,user ,mdp) + # set database schema, table name, geometry column and optionaly subset (WHERE clause) + uri.setDataSource(schema, tablename_3, geom) + + layer = self.iface.addVectorLayer(uri.uri(), tablename_qgis, "postgres") + ''' + SQL_habitat = "CREATE TABLE " + schema + "."+ tablename + champ_habitat + SQL_pkey = "ALTER TABLE " + schema + "." + tablename + " ADD CONSTRAINT " + tablename + "_pkey" + " PRIMARY KEY (gid)" + SQL_trigger_area_m2 = "CREATE TRIGGER area_m2" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.area_m2();" + SQL_trigger_area_ha = "CREATE TRIGGER area_ha" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.area_ha();" + SQL_trigger_concat_cd_cb = "CREATE TRIGGER concat_cd_cb" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.concat_cd_cb();" + SQL_trigger_date_creation = "CREATE TRIGGER date_creation" + tablename + " BEFORE INSERT ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_creation();" + SQL_trigger_date_maj = "CREATE TRIGGER date_maj" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_maj();" + + SQL_style = """INSERT INTO layer_styles (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, update_time) + SELECT f_table_catalog, '""" + schema + "', '" + tablename + """', f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, now() + FROM layer_styles + WHERE description = 'habitat_modele_v3'""" + SQL_GRANT_TABLE="GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename+" TO grp_qgis;GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename+" TO grp_sig;GRANT ALL ON SEQUENCE "+schema+"."+tablename+"_gid_seq TO grp_qgis;" + + cur.execute(SQL_habitat) + cur.execute(SQL_pkey) + cur.execute(SQL_trigger_area_m2) + cur.execute(SQL_trigger_area_ha) + cur.execute(SQL_trigger_concat_cd_cb) + cur.execute(SQL_trigger_date_creation) + cur.execute(SQL_trigger_date_maj) + cur.execute(SQL_style) ## Enregistrement du style (comme style par defaut) dans la table layer_styles + cur.execute(SQL_GRANT_TABLE) + + con.commit() + + ## Affichage de la table + uri = QgsDataSourceUri() + # set host name, port, database name, username and password + uri.setConnection(host ,port ,dbname ,user ,mdp) + # set database schema, table name, geometry column and optionaly subset (WHERE clause) + uri.setDataSource(schema, tablename, geom) + + layer = self.iface.addVectorLayer(uri.uri(), tablename_qgis, "postgres") + ''' + ### Creation de la table travaux prevus + if self.dlgAjout.ui.couche_travaux_prevus.isChecked(): + #**********Poly + if self.dlgAjout.ui.annee_5.text() == 'aaaa' or self.dlgAjout.ui.annee_5.text() == '': + tablename = schema + "_travaux_prevus_poly" + else : + tablename = schema + "_travaux_prevus_poly_" + self.dlgAjout.ui.annee_5.text() + tablename_qgis = tablename[1:] # Permet d'enlever le "_", ajouter a la premiere etape, dans qgis + + SQL_travaux_prevus = "CREATE TABLE " + schema + "."+ tablename + champ_travaux_prevus_multipolygon + SQL_pkey = "ALTER TABLE " + schema + "." + tablename + " ADD CONSTRAINT " + tablename + "_pkey" + " PRIMARY KEY (gid)" + SQL_trigger_date_creation = "CREATE TRIGGER date_creation" + tablename + " BEFORE INSERT ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_creation();" + SQL_trigger_date_maj = "CREATE TRIGGER date_maj" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_maj();" + SQL_trigger_area_m2 = "CREATE TRIGGER area_m2" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.area_m2();" + SQL_trigger_area_ha = "CREATE TRIGGER area_ha" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.area_ha();" + + SQL_style = """INSERT INTO layer_styles (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, update_time) + SELECT f_table_catalog, '""" + schema + "', '" + tablename + """', f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, now() + FROM layer_styles + WHERE description = 'travaux_prevus_poly_modele_v3'""" + SQL_GRANT_TABLE="GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename+" TO grp_qgis;GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename+" TO grp_sig;GRANT ALL ON SEQUENCE "+schema+"."+tablename+"_gid_seq TO grp_qgis;" + + cur.execute(SQL_travaux_prevus) + cur.execute(SQL_pkey) + cur.execute(SQL_trigger_date_creation) + cur.execute(SQL_trigger_date_maj) + cur.execute(SQL_trigger_area_m2) + cur.execute(SQL_trigger_area_ha) + cur.execute(SQL_style) ## Enregistrement du style (comme style par defaut) dans la table layer_styles + cur.execute(SQL_GRANT_TABLE) + + con.commit() + + ## Affichage de la table + uri = QgsDataSourceUri() + # set host name, port, database name, username and password + uri.setConnection(host ,port ,dbname ,user ,mdp) + # set database schema, table name, geometry column and optionaly subset (WHERE clause) + uri.setDataSource(schema, tablename, geom) + + layer = self.iface.addVectorLayer(uri.uri(), tablename_qgis, "postgres") + + #**********ligne + if self.dlgAjout.ui.annee_5.text() == 'aaaa' or self.dlgAjout.ui.annee_5.text() == '': + tablename = schema + "_travaux_prevus_ligne" + else : + tablename = schema + "_travaux_prevus_ligne_" + self.dlgAjout.ui.annee_5.text() + tablename_qgis = tablename[1:] # Permet d'enlever le "_", ajouter a la premiere etape, dans qgis + + SQL_travaux_prevus = "CREATE TABLE " + schema + "."+ tablename + champ_travaux_prevus_multilinestring + SQL_pkey = "ALTER TABLE " + schema + "." + tablename + " ADD CONSTRAINT " + tablename + "_pkey" + " PRIMARY KEY (gid)" + SQL_trigger_date_creation = "CREATE TRIGGER date_creation" + tablename + " BEFORE INSERT ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_creation();" + SQL_trigger_date_maj = "CREATE TRIGGER date_maj" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_maj();" + SQL_trigger_length_m = "CREATE TRIGGER length_m" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.length_m();" + SQL_trigger_length_km = "CREATE TRIGGER length_km" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.length_km();" + + SQL_style = """INSERT INTO layer_styles (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, update_time) + SELECT f_table_catalog, '""" + schema + "', '" + tablename + """', f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, now() + FROM layer_styles + WHERE description = 'travaux_prevus_ligne_modele_v3'""" + SQL_GRANT_TABLE="GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename+" TO grp_qgis;GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename+" TO grp_sig;GRANT ALL ON SEQUENCE "+schema+"."+tablename+"_gid_seq TO grp_qgis;" + + cur.execute(SQL_travaux_prevus) + cur.execute(SQL_pkey) + cur.execute(SQL_trigger_date_creation) + cur.execute(SQL_trigger_date_maj) + cur.execute(SQL_trigger_length_m) + cur.execute(SQL_trigger_length_km) + cur.execute(SQL_style) ## Enregistrement du style (comme style par defaut) dans la table layer_styles + cur.execute(SQL_GRANT_TABLE) + + con.commit() + + ## Affichage de la table + uri = QgsDataSourceUri() + # set host name, port, database name, username and password + uri.setConnection(host ,port ,dbname ,user ,mdp) + # set database schema, table name, geometry column and optionaly subset (WHERE clause) + uri.setDataSource(schema, tablename, geom) + + layer = self.iface.addVectorLayer(uri.uri(), tablename_qgis, "postgres") + + #**********point + if self.dlgAjout.ui.annee_5.text() == 'aaaa' or self.dlgAjout.ui.annee_5.text() == '': + tablename = schema + "_travaux_prevus_point" + else : + tablename = schema + "_travaux_prevus_point_" + self.dlgAjout.ui.annee_5.text() + tablename_qgis = tablename[1:] # Permet d'enlever le "_", ajouter a la premiere etape, dans qgis + + SQL_travaux_prevus = "CREATE TABLE " + schema + "."+ tablename + champ_travaux_prevus_point + SQL_pkey = "ALTER TABLE " + schema + "." + tablename + " ADD CONSTRAINT " + tablename + "_pkey" + " PRIMARY KEY (gid)" + SQL_trigger_date_creation = "CREATE TRIGGER date_creation" + tablename + " BEFORE INSERT ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_creation();" + SQL_trigger_date_maj = "CREATE TRIGGER date_maj" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.date_maj();" + SQL_trigger_coordonnees = "CREATE TRIGGER coordonnees" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.coordonnees();" + + SQL_style = """INSERT INTO layer_styles (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, update_time) + SELECT f_table_catalog, '""" + schema + "', '" + tablename + """', f_geometry_column, stylename, styleqml, stylesld, useasdefault, "owner", ui, now() + FROM layer_styles + WHERE description = 'travaux_prevus_point_modele_v3'""" + SQL_GRANT_TABLE="GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename+" TO grp_qgis;GRANT DELETE, INSERT, REFERENCES, SELECT, TRIGGER, TRUNCATE, UPDATE ON "+schema+"."+tablename+" TO grp_sig;GRANT ALL ON SEQUENCE "+schema+"."+tablename+"_gid_seq TO grp_qgis;" + + cur.execute(SQL_travaux_prevus) + cur.execute(SQL_pkey) + cur.execute(SQL_trigger_date_creation) + cur.execute(SQL_trigger_date_maj) + cur.execute(SQL_trigger_coordonnees) + cur.execute(SQL_style) ## Enregistrement du style (comme style par defaut) dans la table layer_styles + cur.execute(SQL_GRANT_TABLE) + + con.commit() + + ## Affichage de la table + uri = QgsDataSourceUri() + # set host name, port, database name, username and password + uri.setConnection(host ,port ,dbname ,user ,mdp) + # set database schema, table name, geometry column and optionaly subset (WHERE clause) + uri.setDataSource(schema, tablename, geom) + + layer = self.iface.addVectorLayer(uri.uri(), tablename_qgis, "postgres") + + ### Creation de la table vierge + if self.dlgAjout.ui.couche_vierge.isChecked(): + if self.dlgAjout.ui.annee_4.text() == 'aaaa' or self.dlgAjout.ui.annee_4.text() == '': + tablename = schema + "_" + self.dlgAjout.ui.nom_couche_vierge.text().lower() + else : + tablename = schema + "_" + self.dlgAjout.ui.nom_couche_vierge.text().lower() + "_" + self.dlgAjout.ui.annee_4.text() + tablename_qgis = tablename[1:] # Permet d'enlever le "_", ajouter a la premiere etape, dans qgis + + if self.dlgAjout.ui.couche_vierge_point.isChecked() == 1 : + champ_vierge = champ_viergePoint + + if self.dlgAjout.ui.couche_vierge_ligne.isChecked() == 1 : + champ_vierge = champ_viergeLigne + + if self.dlgAjout.ui.couche_vierge_polygone.isChecked() == 1 : + champ_vierge = champ_viergePolygone + + SQL_vierge = "CREATE TABLE " + schema + "."+ tablename + champ_vierge + SQL_pkey = "ALTER TABLE " + schema + "." + tablename + " ADD CONSTRAINT " + tablename + "_pkey" + " PRIMARY KEY (gid)" + + SQL_trigger_area_m2 = "CREATE TRIGGER area_m2" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.area_m2();" + SQL_trigger_area_ha = "CREATE TRIGGER area_ha" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.area_ha();" + SQL_trigger_length_m = "CREATE TRIGGER length_m" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.length_m();" + SQL_trigger_length_km = "CREATE TRIGGER length_km" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.length_km();" + SQL_trigger_coordonnees = "CREATE TRIGGER coordonnees" + tablename + " BEFORE INSERT OR UPDATE ON " + schema + "." + tablename + " FOR EACH ROW EXECUTE PROCEDURE ref.coordonnees();" + + cur.execute(SQL_vierge) + cur.execute(SQL_pkey) + + if self.dlgAjout.ui.couche_vierge_point.isChecked() == 1 : + cur.execute(SQL_trigger_coordonnees) + + if self.dlgAjout.ui.couche_vierge_ligne.isChecked() == 1 : + cur.execute(SQL_trigger_length_m) + cur.execute(SQL_trigger_length_km) + + if self.dlgAjout.ui.couche_vierge_polygone.isChecked() == 1 : + cur.execute(SQL_trigger_area_m2) + cur.execute(SQL_trigger_area_ha) + + con.commit() + + ### Affichage de la table + uri = QgsDataSourceUri() + # set host name, port, database name, username and password + uri.setConnection(host ,port ,dbname ,user ,mdp) + # set database schema, table name, geometry column and optionaly subset (WHERE clause) + uri.setDataSource(schema, tablename, geom) + + layer = self.iface.addVectorLayer(uri.uri(), tablename_qgis, "postgres") + + else : + con.commit() + + con.close() + pass diff --git a/CenRa_POSTGIS/cenradialog.py b/CenRa_POSTGIS/cenradialog.py new file mode 100644 index 0000000..8a99a57 --- /dev/null +++ b/CenRa_POSTGIS/cenradialog.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +""" +/*************************************************************************** + CenRaDialog + A QGIS plugin + Conservatoire d'Espaces Naturels de Rhône-Alpes + ------------------- + begin : 2014-03-27 + copyright : (C) 2014 by Conservatoire d'Espaces Naturels de Rhône-Alpes + email : guillaume.costes@espaces-naturels.fr + ***************************************************************************/ + +/*************************************************************************** + * * + * 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. * + * * + ***************************************************************************/ +""" + +from qgis.PyQt import QtCore, QtGui +from qgis.PyQt.QtWidgets import QDialog +from .ui_cenra import Ui_cenra +# create the dialog for zoom to point + + +class CenRaDialog(QDialog, Ui_cenra): + def __init__(self): + QDialog.__init__(self) + # Set up the user interface from Designer. + # After setupUI you can access any designer object by doing + # self., and you can use autoconnect slots - see + # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html + # #widgets-and-dialogs-with-auto-connect + self.setupUi(self) diff --git a/CenRa_POSTGIS/help.png b/CenRa_POSTGIS/help.png new file mode 100644 index 0000000000000000000000000000000000000000..d60425f7ec65f748d6e5d2a61d5a3cf6da8d6ee3 GIT binary patch literal 2231 zcmV;o2uSydP)bE=iVgM8EL08?wK{`+&Ooz^__2hYpt{4OJ3qi|F~{lt=W-btr3Xr0*O z(i{&jKb%uP=|_|=Tc;sOwk#52I+v0;`2c*}+0WxozZE{+)>}_F`pwRU+SnHYSiHT~ z6Aq`nzioY0#ZT_v;7Rvbw0;`oxi=bVJll;lB!-j-A&^oa5GgNOYqZuZuP9{4Emu=I zC!bh6!Bfw^9e?G}>2p3`?7HTCwehL(jOpRE$Sgkg>b4uJR_=UgQ<`NOyxwq@C!Two zO9LTH)4-ZyreR>328Jmy42fwNNJDZd7~-wt?PU4Vs4SkrjVnrxzM*j5$(Dg@hFcEq zg(p5AK=BsuqpPkiy7xDa+>&k>lHcz=%AWlnVHgIcX<}J!aLf5^n#QC_1H)yEXd%`@$Nb3@tt<3lY6-}_7o3h&tI&(v1So4@^b{=AvFyi|XZ=ic}b z(=w;Ltz>E043?A>P*D`1AkRl`RyyPH1bu^Hnp*m(Ki)wo8pl=&+g2oO#oe2ia_9OA zE?ypG>%+STM~RnrzxKo6GzR>oH|@IT_T|^#yn3NLbD@u)?>UTV7+5X<%X032{lHgP zG~dsRTpvEK2g8u0c}()MyeyiZ&#J1~oNDhU7PlRp0H@mfs4kmHNnsunw(^`f-CZ!$ za_~(639xuwZKh?)yY9Jdg`v?rz3(_u$Y~TiKum-X2;mSHPblIEg_IKM3Nt^;%l7pP zu`CmhWnx(-hBVl{|06VT_m*o6(=_f{yskDAzyyf+yj#{@T`cpn(>c}FOUoxgJRWn3 z=-f1z1_@hn{Cq#}HTTjx6u}UJ4a?_qdrfHyT;+T}9?K*lZCtE$bPsc`bAZaC0Bf%; zkq3{qZUJ7Fj`sDAo32~nL%@-bx*RbXPTmbS_en6nc<^YkC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