-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleExport.py
More file actions
1119 lines (804 loc) · 43 KB
/
ModuleExport.py
File metadata and controls
1119 lines (804 loc) · 43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
/***************************************************************************
ModuleExport
A QGIS plugin
export
-------------------
begin : 2016-04-14
git sha : $Format:%H$
copyright : (C) 2016 by Loic Martel
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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 PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication
from PyQt4.QtGui import QAction, QIcon, QMessageBox, QTableWidgetItem
# Initialize Qt resources from file resources.py
import resources
# Import the code for the dialog
from ModuleExport_dialog import ModuleExportDialog
from DialogSelectFaune import DialogSelectFaune
from DialogSelectFlore import DialogSelectFlore
import os.path
import time
import psycopg2
from PyQt4.Qt import QPushButton, QSqlDatabase, QSqlTableModel
from xml.dom import minidom #Pour pouvoir générer notre fichier de metadata.
from subprocess import call
class ModuleExport:
"""QGIS Plugin Implementation."""
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]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'ModuleExport_{}.qm'.format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
# Create the dialogs (after translation) and keep reference
self.dlg = ModuleExportDialog(self.iface)
self.dlg.setWindowTitle("module d'export")
self.dlg_F = DialogSelectFaune(self.dlg)
self.dlg_Flo = DialogSelectFlore(self.dlg)
#Fonctions esthétiques: ajout d icones.
self.dlg.ButtonFaune.setIcon(QIcon(self.plugin_dir+"/turtle-128.png"))
self.dlg.ButtonFaune.setIcon(QIcon(self.plugin_dir+"/turtle-128.png"))
self.dlg.ButtonFlore.setIcon(QIcon(self.plugin_dir+"/flower.png"))
self.dlg_F.vider_faune.clicked.connect(lambda:self.vider_la_table('fa'))
self.dlg_Flo.vider_flore.clicked.connect(lambda:self.vider_la_table('fl'))
self.dlg.ButtonFaune.clicked.connect(self.fenetre_select_faune)
self.dlg.ButtonFlore.clicked.connect(self.fenetre_select_flore)
#Relier le bouton d'ajout d especefaune à mon SLOT dans DialogSelectFaune transvas_espece.
self.dlg_F.btn_ajoutfaune.clicked.connect(lambda: self.dlg_F.transvasajout__espece_faune())
self.dlg_F.btn_rmfaune.clicked.connect(lambda: self.dlg_F.transvasretrait_espece_faune())
self.dlg_Flo.btn_ajoutflore.clicked.connect(lambda: self.dlg_Flo.transvasajout__espece_flore())
self.dlg_Flo.btn_rmflore.clicked.connect(lambda: self.dlg_Flo.transvasretrait_espece_flore())
self.dlg_Flo.btn_visible.clicked.connect(lambda : self.dlg_Flo.visibilite(True))
self.dlg_Flo.btn_invisible.clicked.connect(lambda : self.dlg_Flo.visibilite(False))
self.dlg_F.btn_visible.clicked.connect(lambda : self.dlg_F.visibilite(True))
self.dlg_F.btn_invisible.clicked.connect(lambda : self.dlg_F.visibilite(False))
# Declare instance attributes
self.actions = []
self.menu = self.tr(u'&ModuleExport')
# TODO: We are going to let the user set this up in a future iteration
self.toolbar = self.iface.addToolBar(u'ModuleExport')
self.toolbar.setObjectName(u'ModuleExport')
#MEP du signal pour vider les selections.
self.dlg_F.unselect.clicked.connect(lambda: self.dlg_F.clear())
self.dlg_Flo.unselect.clicked.connect(lambda: self.dlg_Flo.clear())
#MEP du signal pour la fonction de recherche.
self.dlg_F.searchButton.clicked.connect(lambda: self.va_chercher('fa'))
self.dlg_Flo.searchButton.clicked.connect(lambda: self.va_chercher('fl'))
#Connexion des bouton de lancement et nettoyage de table
self.dlg.buttonLaunch.clicked.connect(lambda : self.lancement_des_operations())
self.dlg.killButton.clicked.connect(lambda : self.nettoyage_des_tables())
self.dlg.buttonCleanExport.clicked.connect(lambda : self.nettoyage_dossier_export())
# noinspection PyMethodMayBeStatic
def tr(self, message):
# noinspection PyTypeChecker,PyArgumentList,PyCallByClass
return QCoreApplication.translate('ModuleExport', message)
def add_action(
self,
icon_path,
text,
callback,
enabled_flag=True,
add_to_menu=True,
add_to_toolbar=True,
status_tip=None,
whats_this=None,
parent=None):
icon = QIcon(self.plugin_dir+"/icon.png")
action = QAction(icon, "Module d'export", parent)
action.triggered.connect(callback)
action.setEnabled(enabled_flag)
if status_tip is not None:
action.setStatusTip(status_tip)
if whats_this is not None:
action.setWhatsThis(whats_this)
if add_to_toolbar:
self.toolbar.addAction(action)
if add_to_menu:
self.iface.addPluginToMenu(
self.menu,
action)
self.actions.append(action)
#Ajout de notre action a la liste d actions.
return action
def initGui(self):
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
icon_path = self.plugin_dir+"/icon.png"
self.add_action(
icon_path,
text=self.tr(u'Module d export'),
callback=self.run,
whats_this = self.tr(u'Module permettant d\'exporter des données Faune / Flore vers une Shapefile ou un CSV.'),
parent=self.iface.mainWindow())
def unload(self):
"""Removes the plugin menu item and icon from QGIS GUI."""
for action in self.actions:
self.iface.removePluginMenu(
self.tr(u'&ModuleExport'),
action)
self.iface.removeToolBarIcon(action)
# remove the toolbar
del self.toolbar
def peuplerComboExport(self):
#creation connexion PGQT pour peupler la liste des type_export.
self.QConn = QSqlDatabase("QPSQL")
self.QConn.setHostName(self.get_host_ip())
self.QConn.setDatabaseName(self.get_db_name())
self.QConn.setUserName(self.get_dbuser_name())
self.QConn.setPassword(self.get_pswrd())
self.QConn.open()
#Creation du modele de donnée pour peupler laQcombobox.
self.Model_export = QSqlTableModel(db = self.QConn)
self.Model_export.setTable("export.w_type_export")
self.Model_export.select()
#Peupler la Qcombobox
self.dlg.comboExport.setModel(self.Model_export)
self.dlg.comboExport.setModelColumn(self.Model_export.fieldIndex("nom"))
####################################################################################
####################################################################################
############## ###################
############## ###################
############## FONCTIONS DE RECUP DE DONNEES ###################
############## DANS PGSQL ###################
############## ###################
####################################################################################
####################################################################################
def get_table_name(self):
#Fonction pour obtenir le nom réel de la couche selectionnée. "Schema.table"
table_name = str(self.iface.activeLayer().source())
table_name = table_name.replace(table_name[0:table_name.find('table=')+6], "").replace('"','').split()
return table_name[0]
def get_db_name(self):
#Fonction pour obtenir le nom de la base de données source
madb = str(self.iface.activeLayer().source())
madb = madb.replace(madb[0:madb.find('dbname=')+7], "").replace('"','').replace("'","").split()
return madb[0]
def get_host_ip(self):
#Fonction pour obtenir le contenu de Host
hname = str(self.iface.activeLayer().source())
hname = hname.replace(hname[0:hname.find('host=')+5], "").replace('"','').replace("'","").split()
return hname[0]
def get_dbuser_name(self):
#Fonction pour obtenir le nom du User
usname = str(self.iface.activeLayer().source())
usname = usname.replace(usname[0:usname.find('user=')+5], "").replace('"','').replace("'","").split()
return usname[0]
def get_pswrd(self):
#Fonction pour obtenir le mot de passe
pwrd = str(self.iface.activeLayer().source())
pwrd = pwrd.replace(pwrd[0:pwrd.find('password=')+9], "").replace('"','').replace("'","").split()
return pwrd[0]
def recup_id(self):
Mesobjet = self.iface.activeLayer().selectedFeatures ()
Mesid = ', '.join([unicode(f['id']) for f in Mesobjet ])
return Mesid
####################################################################################
####################################################################################
############## ###################
############## ###################
############## FENETRES DE FAUNE FLORE ###################
############## ###################
############## ###################
####################################################################################
####################################################################################
def fenetre_select_faune(self):
self.dlg_F.combosearch.addItems([self.tr('taxon'), self.tr('nomcommun'), self.tr('classe'), self.tr('ordre'), self.tr('espece_id'),
self.tr('cd_ref'), self.tr('cd_nom'), self.tr('rarepic'), self.tr('menapic')])
idconnexion = ("dbname=%s host=%s user=%s password=%s") % (self.get_db_name(), self.get_host_ip(), self.get_dbuser_name(), self.get_pswrd())
conn = psycopg2.connect(idconnexion)
cur = conn.cursor()
#fourniture d un echantillon basique et léger.
marequete = """
select w.espece_id, w.taxon, w.nomcommun, w.classe, w.ordre, w.cd_nom, w.cd_ref ,w.rarepic ,w.menapic
from bdfauneflore.w_ref_faune w
limit 100
"""
cur.execute(marequete)
for i in cur:
self.dlg_F.peupler_table_from_base(self.dlg_F.matablewidget, i)
cur.close()
self.dlg_F.matablewidget.resizeColumnToContents(1)
cur = conn.cursor()
marequete2 = """
select w.espece_id, w.taxon, w.nomcommun
from export.w_faune_select w
"""
cur.execute(marequete2)
for i in cur:
self.dlg_F.peupler_table_from_base_selected(self.dlg_F.tab_faune_selected,i)
cur.close()
conn.close()
#Si on appuie sur ok ou si l'utilisateur ferme la fenetre alors les listes sont vidées graphiquement.
#si l'utilisateur clique sur OK alors la liste d esepce selectionnee est transferee dans la table sleect_faune--/--flore.
if self.dlg_F.exec_():
self.ajout_ls_espece_pg('fa')
if self.dlg_F.close():
self.dlg_F.tab_faune_selected.clearContents()
self.dlg_F.matablewidget.clearContents()
self.dlg_F.tab_faune_selected.setRowCount(0)
self.dlg_F.matablewidget.setRowCount(0)
self.dlg_F.combosearch.clear()
def fenetre_select_flore(self):
self.dlg_Flo.combosearch.addItems([self.tr('taxon'), self.tr('nomcommun'), self.tr('classe'), self.tr('ordre'), self.tr('espece_id'),self.tr('cd_ref'), self.tr('cd_nom'), self.tr('rarepic'), self.tr('menapic')])
idconnexion = ("dbname=%s host=%s user=%s password=%s") % (self.get_db_name(), self.get_host_ip(), self.get_dbuser_name(), self.get_pswrd())
conn = psycopg2.connect(idconnexion)
cur = conn.cursor()
#fourniture d un echantillon basique et léger.
marequete = """
select w.espece_id, w.taxon, w.nomcommun, w.classe, w.ordre, w.cd_nom, w.cd_ref ,w.rarepic ,w.menapic
from bdfauneflore.w_ref_flore w
limit 100
"""
cur.execute(marequete)
for i in cur:
self.dlg_Flo.peupler_table_from_base(self.dlg_Flo.matablewidget, i)
cur.close()
self.dlg_Flo.matablewidget.resizeColumnToContents(1)
cur = conn.cursor()
marequete2 = """
select w.espece_id, w.taxon, w.nomcommun
from export.w_flore_select w
"""
cur.execute(marequete2)
for i in cur:
self.dlg_Flo.peupler_table_from_base_selected(self.dlg_Flo.tab_flore_selected,i)
cur.close()
conn.close()
#Si on appuie sur ok ou si l'utilisateur ferme la fenetre alors les listes sont vidées graphiquement.
#si l'utilisateur clique sur OK alors la liste d esepce selectionnee est transferee dans la table sleect_faune--/--flore.
if self.dlg_Flo.exec_():
self.ajout_ls_espece_pg('fl')
if self.dlg_Flo.close():
self.dlg_Flo.tab_flore_selected.clearContents()
self.dlg_Flo.matablewidget.clearContents()
self.dlg_Flo.tab_flore_selected.setRowCount(0)
self.dlg_Flo.matablewidget.setRowCount(0)
self.dlg_Flo.combosearch.clear()
####################################################################################
############## FONCTIONS POUR FAUNE FLORE ###################
####################################################################################
#Fonction pour vider les tables de flore select et faune select.
#Lacement de la suppression en mettant un parametre de type (flore ou faune : fl/fa)
def vider_la_table(self,typesuppr):
#CONTROLE: transformation du parametre en nom de table.
if typesuppr == 'fa':
matable = 'export.w_faune_select'
elif typesuppr =='fl':
matable = 'export.w_flore_select'
else:
print 'erreur sur la table à vider.'
idconnexion = ("dbname=%s host=%s user=%s password=%s") % (self.get_db_name(), self.get_host_ip(), self.get_dbuser_name(), self.get_pswrd())
conn = psycopg2.connect(idconnexion)
cur = conn.cursor()
marequete = """
DELETE FROM %s
""" % self.tr(matable)
print marequete
cur.execute(marequete)
#passage du parametre pour taper dans daune ou flore.
conn.commit()
conn.close()
#On vide la boite de dialogue.
if typesuppr == 'fa':
self.dlg_F.tab_faune_selected.clearContents()
self.dlg_F.tab_faune_selected.setRowCount(0)
if typesuppr == 'fl':
self.dlg_Flo.tab_flore_selected.clearContents()
self.dlg_Flo.tab_flore_selected.setRowCount(0)
def ajout_ls_espece_pg(self,typeadd):
#CONTROLE: transformation du parametre en noms de tables.
if typeadd == 'fa':
matable_select = self.tr('export.w_faune_select')
ma_wref = self.tr('bdfauneflore.w_ref_faune')
#Création d'une liste convertie en string + guillemets + parentheses pour la passer en parametre dans la requeet SQL.
compterow = self.dlg_F.tab_faune_selected.rowCount()
malisteid=[]
for i in xrange(0,compterow):
malisteid.append(self.dlg_F.tab_faune_selected.item(i,0).text())
elif typeadd =='fl':
matable_select = self.tr('export.w_flore_select')
ma_wref = self.tr('bdfauneflore.w_ref_flore')
#Création d'une liste convertie en string + guillemets + parentheses pour la passer en parametre dans la requeet SQL.
compterow = self.dlg_Flo.tab_flore_selected.rowCount()
malisteid=[]
for i in xrange(0,compterow):
malisteid.append(self.dlg_Flo.tab_flore_selected.item(i,0).text())
if malisteid: #Syntaxe pour vérifier que la liste ne soit pas vide.
malisteid = "', '".join(malisteid)
malisteid = self.tr("('" + malisteid + "')")
idconnexion = ("dbname=%s host=%s user=%s password=%s") % (self.get_db_name(),
self.get_host_ip(), self.get_dbuser_name(),
self.get_pswrd())
conn = psycopg2.connect(idconnexion)
cur = conn.cursor()
#On vide la table pour éviter d'insérer des doublons (optimisation)
#TODO: Ajouter un Vaccum pour éviter une surcharge del a table à long terme.
marequete1="""
delete from %s
""" %matable_select
cur.execute(marequete1)
conn.commit()
cur.close()
cur=conn.cursor()
#Envoi de nos parametres dans la liste espece_select
especeid = '(espece_id'
marequete2 ="""
insert into %s %s, taxon, nomcommun%s
select espece_id, taxon, nomcommun
from %s bdf
where bdf.espece_id in %s
""" % (matable_select, self.tr('(espece_id'), self.tr(')'), ma_wref, malisteid)
print marequete2
cur.execute(marequete2)
conn.commit()
conn.close
#Fonction pour lancer la recherche d'éléments dans la liste d'espèce.
def va_chercher(self, typecherche):
if typecherche == 'fa':
matable_select = self.tr('export.w_faune_select')
ma_wref = self.tr('bdfauneflore.w_ref_faune')
maliste_wref = self.dlg_F.matablewidget
marecherche = self.dlg_F.search_bar.text()
moncritere = self.dlg_F.combosearch.currentText()
elif typecherche == 'fl':
matable_select = self.tr('export.w_flore_select')
ma_wref = self.tr('bdfauneflore.w_ref_flore')
maliste_wref = self.dlg_Flo.matablewidget
marecherche = self.dlg_Flo.search_bar.text()
moncritere = self.dlg_Flo.combosearch.currentText()
if len(marecherche)<3:
return
else:
maliste_wref.clearContents()
maliste_wref.setRowCount(0)
marecherche = '%' + marecherche + '%'
idconnexion = ("dbname=%s host=%s user=%s password=%s") % (self.get_db_name(), self.get_host_ip(), self.get_dbuser_name(), self.get_pswrd())
conn = psycopg2.connect(idconnexion)
cur = conn.cursor()
marequete = """
select w.espece_id, w.taxon, w.nomcommun, w.classe, w.ordre, w.cd_nom, w.cd_ref ,w.rarepic ,w.menapic
from %s w
where %s%s%s iLIKE '%s'
""" % (ma_wref, 'CAST(', moncritere, ' AS TEXT)', marecherche)
# le ILIKE permet une recherche insensible à la casse, mais augmente considérablement le temps de requetage.
cur.execute(marequete)
if typecherche == 'fl':
for i in cur:
self.dlg_Flo.peupler_table_from_base(self.dlg_Flo.matablewidget,i)
#Trier les résultats par taxon.
self.dlg_Flo.matablewidget.sortItems(1)
elif typecherche =='fa':
for i in cur:
self.dlg_F.peupler_table_from_base(self.dlg_F.matablewidget,i)
self.dlg_F.matablewidget.sortItems(1)
cur.close()
conn.close()
####################################################################################
####################################################################################
############## ###################
############## ###################
############## LANCE-REQUETE ###################
############## ###################
############## ###################
####################################################################################
####################################################################################
def _1_requeteGenerale(self):
idconnexion = ("dbname=%s host=%s user=%s password=%s") % (self.get_db_name(), self.get_host_ip(), self.get_dbuser_name(), self.get_pswrd())
##########################
#APPEL DE LA PARTIE 1 / 3#
##########################
conn = psycopg2.connect(idconnexion)
cur = conn.cursor()
marequete = """
SELECT export._1_recupdesid('{%s}'::int[]) ;
""" % self.recup_id()
cur.execute(marequete)
conn.commit()
#DEBUG
#Affichage des log PG
#for notice in conn.notices:
#print notice
conn.close()
print marequete
def _2_moissonneuse_bateuse(self):
idconnexion = ("dbname=%s host=%s user=%s password=%s") % (self.get_db_name(), self.get_host_ip(), self.get_dbuser_name(), self.get_pswrd())
#Récupération des DATES à passer en param
#Conservation dun structure lourde en yyyy pour faciliter un eventuel passage en jj.mm.yyyy.
if self.dlg.radioSaisie.isChecked() == True:
self.datemin = self.dlg.date_min_pec.date().toString("dd-MM-yyyy")
self.datemax = self.dlg.date_max_pec.date().toString("dd-MM-yyyy")
self.booldate = 'True'
elif self.dlg.radioObs.isChecked() == True:
self.datemin = self.dlg.date_min_pec.date().toString("yyyy")
self.datemax = self.dlg.date_max_pec.date().toString("yyyy")
self.booldate ='False'
#Récupération des ESPECES en checkant le RadioButton.
if self.dlg.rad_all_espece.isChecked() == True:
self.boolespece = 'False'
elif self.dlg.rad_slc_espece.isChecked()== True:
self.boolespece = 'True'
#Verification de si l on desire toute la flore ou toute la faune.
if self.dlg.checkFlore.isChecked() == True:
self.boolflore = 'True'
else:
self.boolflore = 'False'
if self.dlg.checkFaune.isChecked() == True:
self.boolfaune = 'True'
else:
self.boolfaune = 'False'
#Vérification de la checkbox du parametre pour définir si l'on veut les ESPECES VALIDES.
if self.dlg.obj_valide.isChecked() == True:
self.boolvalid = 'True'
elif self.dlg.obj_valide.isChecked() == False:
self.boolvalid = 'False'
else:
return
#Vérification de la checkbox de parametres pour définir si l'on veut les éspèces PRESENTES.
if self.dlg.obj_present.isChecked() == True:
self.boolpresent = 'True'
elif self.dlg.obj_present.isChecked() == False:
self.boolpresent = 'False'
else:
return
#Vérification de la checkbox de parametres pour définir si l'on veut les données externes.
if self.dlg.radioExt.isChecked() == True:
self.boolexterne = 'True'
elif self.dlg.radioExt.isChecked() == False:
self.boolexterne = 'False'
else:
return
##########################
#APPEL DE LA PARTIE 2 / 3#
##########################
conn = psycopg2.connect(idconnexion)
cur = conn.cursor()
marequete = """
SELECT export._2_filtrage(%s, %s, %s, %s, '%s', '%s', %s, %s, %s) ;
""" % (self.boolespece, self.boolfaune, self.boolflore, self.booldate,
self.datemin, self.datemax, self.boolvalid, self.boolpresent,
self.boolexterne)
cur.execute(marequete)
conn.commit()
#DEBUG
#Affichage des log PG
#for notice in conn.notices:
#print notice
conn.close()
print marequete
#Appeler la fonction d export qui genere TABLES + CSV
def _3_Fichier_CSV(self, typeExport):
#Définition du nom par defaut.
nomCsv = "\\ST_PRINCIPAL.csv"
idconnexion = ("dbname=%s host=%s user=%s password=%s") % (self.get_db_name(), self.get_host_ip(), self.get_dbuser_name(), self.get_pswrd())
conn = psycopg2.connect(idconnexion)
cur = conn.cursor()
#fonction géénrique de recuperation dans notre table de type d export la fonction associée au type.
marequete = """select fonction from export.w_type_export where nom = '%s' """ % typeExport
cur.execute(marequete)
myfunc = cur.fetchone()[0]
#Appel de la fonction en replacant le nom de variable par la variable en parametre.
marequete2 = "SELECT " + str(myfunc)
cur.execute(marequete2)
conn.commit()
conn.close()
#Fonction d Export SIG au format SHP.
def _4_Export_SIG(self, typeExport):
idconnexion = ("dbname=%s host=%s user=%s password=%s") % (self.get_db_name(), self.get_host_ip(), self.get_dbuser_name(), self.get_pswrd())
conn = psycopg2.connect(idconnexion)
cur = conn.cursor()
marequete = """select ogr2ogr from export.w_type_export where nom = '%s' """ % typeExport
cur.execute(marequete)
myOgr = cur.fetchone()[0]
#Chercher le .bat à lancer.
monpath = "S:/00_MODULE_EXPORT_BDCEN/batfiles/"
print os.path.normpath(monpath + myOgr)
call(os.path.normpath(monpath + myOgr),shell=True)
def _5_Metadonne(self):
idconnexion = ("dbname=%s host=%s user=%s password=%s") % (self.get_db_name(), self.get_host_ip(), self.get_dbuser_name(), self.get_pswrd())
conn = psycopg2.connect(idconnexion)
cur = conn.cursor()
#Création du XML avec parametres adéquats.
#Création de mon arbre XML en mémoire VIVE.
modelExport = minidom.Document()
#Création de ma racine. ROOT
newroot = modelExport.createElement('root')
#On ajoute le root a l arbre
modelExport.appendChild(newroot)
#Création de la grande secton de paramexport
paramexport = modelExport.createElement('parametres_d_export')
#On ajoute la paramexport a la root
newroot.appendChild(paramexport)
#Création de OBJETSSIG
objetsig = modelExport.createElement('ObjetsSIG')
paramexport.appendChild(objetsig)
#Création des ENTITE dans objetSIG.
#Remplacer liste par mes id.
self.iface.activeLayer().selectedFeatures ()
for i in self.iface.activeLayer().selectedFeatures():
entite = modelExport.createElement('id')
entite.setAttribute('ID', str(i[0]))
entite.setAttribute('libelle', str(i[1]))
objetsig.appendChild(entite)
#Création de la section de PARAMETRE D OBJETS
paramobjets = modelExport.createElement('paramobjets')
#La donnée est-elle valide?
if self.boolvalid == True:
nodeValid = "Valide uniquement"
else:
nodeValid ="Valide et invalide"
paramobjets.setAttribute('Validite_de_la_donnee', nodeValid)
#La Présence de l espece est-elle une certitude?
if self.boolpresent == True:
nodePresabs = "Présence attestee"
else:
nodePresabs = "Absence"
paramobjets.setAttribute('Certitude_de_presence', nodePresabs)
#La donne externe est elle exclue?
if self.boolexterne == True:
nodeExt = "Inclusion"
else:
nodeExt = "Exclusion"
paramobjets.setAttribute('Donnée_externe', nodeExt)
paramexport.appendChild(paramobjets)
#Création de la DATE.
date = modelExport.createElement('param_date')
#La date prote sur?
if self.booldate == True:
nodeDate = "La saisie"
else:
nodeDate = "La date d'observation"
date.setAttribute('date_portant_sur', nodeDate)
#date min de prise en compte
date.setAttribute('date_minimale_de_prise_en_compte', str(self.datemin))
#date max de prise en compte
date.setAttribute('date_maximale_de_prise_en_compte', str(self.datemax))
paramobjets.appendChild(date)
############################################################################
# RESUME_EXPORT
resumexport = modelExport.createElement("Resume_export")
typeexp = self.dlg.comboExport.currentText()
typeexp = typeexp.encode('UTF-8','ignore')
resumexport.setAttribute('Type_export',typeexp )
newroot.appendChild(resumexport)
#Qui est destinatiare?
ledestina= modelExport.createElement("Destinataire")
#nom export
nomlabel = str(self.dlg.libele.text())
nomlabel= nomlabel.encode('UTF-8', 'ignore')
ledestina.setAttribute('libele_export', nomlabel)
#interlocuteur
nomdest = str(self.dlg.destinataire.text())
nomdest=nomdest.encode('UTF-8', 'ignore')
ledestina.setAttribute('interlocuteur', nomdest)
#Structure
nomstruc = str(self.dlg.structure.text())
nomstruc=nomstruc.encode('UTF-8', 'ignore')
ledestina.setAttribute('structure', nomstruc)
resumexport.appendChild(ledestina)
#STATISTIQUES
#Somme individus
statists = modelExport.createElement("stats_especes")
marequete = "SELECT COUNT(i.id_perm) FROM export.r_id_all i WHERE i.presence IS TRUE"
cur.execute(marequete)
totalent = str(cur.fetchone()[0])
statists.setAttribute("Nombre_total_d_entite",totalent)
#Somme taxons
marequete = """SELECT SUM (count)
FROM
(SELECT COUNT (DISTINCT t.taxon)
FROM
export.r_id_all i, bdfauneflore.t_bilan_faune t
WHERE i.id_perm = t.id_perm
AND i.presence IS TRUE
UNION
SELECT COUNT (DISTINCT t.taxon)
FROM
export.r_id_all i, bdfauneflore.t_bilan_flore t
WHERE i.id_perm = t.id_perm
AND i.presence IS TRUE) AS x"""
cur.execute(marequete)
totalent = str(cur.fetchone()[0])
statists.setAttribute("Nombre_total_de_taxons", totalent)
resumexport.appendChild(statists)
#NB FAUNE EXPORT
#compte d individus
enfa = modelExport.createElement("faune_exportees")
marequete= "SELECT COUNT (i.id_perm) FROM \
export.r_id_all i, bdfauneflore.t_bilan_faune t WHERE i.id_perm = t.id_perm AND i.presence IS TRUE"
cur.execute(marequete)
nbenfa = str(cur.fetchone()[0])
enfa.setAttribute('nombre_d_entites', nbenfa)
# compte d especes.
marequete= "SELECT COUNT (DISTINCT t.taxon) FROM \
export.r_id_all i, bdfauneflore.t_bilan_faune t WHERE i.id_perm = t.id_perm AND i.presence IS TRUE"
cur.execute(marequete)
nbenfa = str(cur.fetchone()[0])
enfa.setAttribute('nombre_d_especes', nbenfa)
statists.appendChild(enfa)
#LISTE TAXONS
if nbenfa !=0:
marequete= "SELECT DISTINCT t.taxon FROM export.r_id_all i,\
bdfauneflore.t_bilan_faune t WHERE i.id_perm = t.id_perm AND i.presence IS TRUE"
cur.execute(marequete)
for i in cur:
nbesfa = i[0]
childEnfa = modelExport.createElement("taxon")
if nbesfa is None:#resolution de bug: si type est null alors valeur arbitraire
childEnfa.setAttribute('Taxon', "NONETYPE")
enfa.appendChild(childEnfa)
else:
nbesfa = nbesfa.encode('UTF-8','ignore')#necessite de reencoder
childEnfa.setAttribute('Taxon', nbesfa)
enfa.appendChild(childEnfa)
#NB FLORE EXPORT
#nb entite flore exportees
efle = modelExport.createElement("flore_exportees")
marequete= "SELECT COUNT (i.id_perm) FROM \
export.r_id_all i, bdfauneflore.t_bilan_flore t WHERE i.id_perm = t.id_perm AND i.presence IS TRUE"
cur.execute(marequete)
nbefle = str(cur.fetchone()[0])
efle.setAttribute('nombre_d_entites', nbefle)
# compte d especes.
marequete= "SELECT COUNT (DISTINCT t.taxon) FROM \
export.r_id_all i, bdfauneflore.t_bilan_flore t WHERE i.id_perm = t.id_perm AND i.presence IS TRUE"
cur.execute(marequete)
nbefle = str(cur.fetchone()[0])
efle.setAttribute('nombre_d_especes', nbefle)
statists.appendChild(efle)
#LISTE TAXON
if nbefle != 0:
marequete= "SELECT DISTINCT t.taxon FROM export.r_id_all i,\
bdfauneflore.t_bilan_flore t WHERE i.id_perm = t.id_perm AND i.presence IS TRUE"
cur.execute(marequete)
for i in cur:
nbefle = i[0]
childEsle = modelExport.createElement("taxon")
if nbefle is None:
childEsle.setAttribute('Taxon', "NONETYPE")
efle.appendChild(childEsle)
else:
nbefle = nbefle.encode('UTF-8','ignore')
childEsle.setAttribute('Taxon', nbefle)
efle.appendChild(childEsle)
conn.commit()
conn.close()
############################################################################
#DESCRIPTION DE LA DONNEE
descdata = modelExport.createElement("Description_des_donnees")
newroot.appendChild(descdata)
# dateexport
# resume des formats
lesformats = modelExport.createElement("formats")
dateExport = time.strftime("%d/%m/%Y")
lesformats.setAttribute('date_export',dateExport)
descdata.appendChild(lesformats)
# Recap du CSV
csv_xml = modelExport.createElement("csv_format")
#Encodage
csv_xml.setAttribute('encodage',"UTF-8")
#separateur
csv_xml.setAttribute('separateur',";")
lesformats.appendChild(csv_xml)
#sig_format
sig_xml = modelExport.createElement("sig_format")
# shapefile?
sig_xml.setAttribute('extension',"Shapefile")
# SCR?
sig_xml.setAttribute('SCR',"Lambert 93")
lesformats.appendChild(sig_xml)
sortieXML = modelExport.toprettyxml()
ecrirexml = open(os.path.normpath('S:/00_MODULE_EXPORT_BDCEN/exports/exportmedatada.xml'), 'w')
ecrirexml.write(sortieXML)
ecrirexml.close()
####################################################################################
######################### FONCTIONS NETTOYAGE ######################################