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
| import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import ogr
import numpy as np
import xlwt
from tempfile import TemporaryFile
from osgeo.gdalconst import *
import csv
class Example(QWidget):
def __init__(self, parent=None):
super(Example, self).__init__(parent)
self.initUI()
def initUI(self):
self.pbar = QProgressBar(self)
self.pbar.setGeometry(25, 160, 400, 25)
self.Information = QLabel('Donnees en cours de traitement :', self)
self.Information.move(25,40)
self.Information.setFixedWidth(400)
self.Information2 = QLabel('Ouverture des donnees : ', self)
self.Information2.move(25,80)
self.Information2.setFixedWidth(400)
self.timer = QBasicTimer()
self.step = 0
self.setGeometry(300, 300, 500, 200)
self.setWindowTitle('En cours de traitement')
self.show()
self.timer.start(100, self)
def timerEvent(self, e):
if self.step >= 100:
self.timer.stop()
return
driver = ogr.GetDriverByName('ESRI Shapefile')
self.Information2.setText("Ouverture et lecture du fichier CSV")
LISTECOMPLET = []
LISTEPOPULATION = []
with open(CSVput, 'r') as f:
reader = csv.reader(f, delimiter=';')
newliste = list(reader)
for elem in newliste :
NOMVILLE = newliste[3]
DEPARTEMENT = newliste[4]
COMPLET = str(NOMVILLE + DEPARTEMENT)
LISTECOMPLET.append(COMPLET)
POPULATION = newliste[5]
LISTEPOPULATION.append(POPULATION)
self.Information2.setText("Ouverture et Comparaison Shape / CSV")
NEWLISTECLE = []
LISTECOMMUNENOTGOOD = ["COMMUNE"]
LISTETYPEERREUR = ["TYPE ERREUR"]
LISTEERREUR = ["LIBELLE_ERREUR"]
SHAPEST = driver.Open(Input, 0)
SHAPELAYER = SHAPEST.GetLayer()
for index,SHAPEFEATURE in enumerate(SHAPELAYER) :
self.step = 10 + int(round((index/len(SHAPELAYER)*80)))
VILLESHP = str(SHAPEFEATURE.GetField("Commune"))
DEPARTEMENTSHP = str(SHAPEFEATURE.GetField("Departement"))
POPULAIONSHP = str(SHAPEFEATURE.GetField("Population"))
if POPULATIONSHP == 'None' :
POPULATIONSHP = ''
COMPLETSHP = str(VILLESHP + DEPARTEMENTSHP)
try :
POPULATIONCSV = str(LISTEPOPULATION[LISTECOMPLET.index(COMPLETSHP)])
except :
POPULATIONCSV = ""
if POPULATIONCSV :
if POPULATIONSHP :
if POPULAIONSHP != POPULATIONCSV :
if POPULAIONSHP > POPULATIONCSV :
LISTECOMMUNENOTGOOD.append(COMPLETSHP)
LISTETYPEERREUR.append("ERREUR MAJEURE - POPULATION SHP > POPULATION CSV")
LISTEERREUR.append("Pour la commune " + COMPLETSHP + ", Dans le CSV, il y a marque que sa population est de " + POPULATIONCSV + " contre " + POPULATIONSHP + " renseigne ici" )
else :
LISTECOMMUNENOTGOOD.append(COMPLETSHP)
LISTETYPEERREUR.append("ERREUR MAJEURE - POPULATION SHP < POPULATION CSV")
LISTEERREUR.append("Pour la commune " + COMPLETSHP + ", Dans le CSV, il y a marque que sa population est de " + POPULATIONCSV + " contre " + POPULATIONSHP + " renseigne ici" )
else :
LISTECOMMUNENOTGOOD.append(COMPLETSHP)
LISTETYPEERREUR.append("ERREUR MAJEURE - ABSENCE POPULATION POUR COMMUNE PRESENTE DANS LE CSV")
LISTEERREUR.append("La commune " + COMPLETSHP + "n'a pas de population ici, cependant au vu du fichier CSV, la population devrait etre " + POPULATIONCSV)
else :
LISTECOMMUNENOTGOOD.append(COMPLETSHP)
LISTETYPEERREUR.append("ERREUR MAJEURE - ABSENCE DE LA COMMUNE DANS LE FICHIER CSV")
LISTEERREUR.append("Au vu du fichier CSV, la COMMUNE " + COMPLETSHP + " n existe pas")
self.Information2.setText("Export du fichier excel :")
book = xlwt.Workbook()
sheet1 = book.add_sheet('Erreurs')
newliste = [LISTECOMMUNENOTGOOD, LISTETYPEERREUR, LISTEERREUR]
newliste = list(np.transpose(newliste))
for indexoflist,sublist in enumerate(newliste):
for indexofsublist, elemofsublist in enumerate(sublist) :
sheet1.write(indexoflist,indexofsublist,elemofsublist)
OutputFile = os.path.dirname(Input) + "/Erreurs.xls"
book.save(OutputFile)
book.save(TemporaryFile())
self.step = 100
self.pbar.setValue(self.step)
self.Information.setText("Traitement fini")
self.Information2.setText("Votre fichier a ete exporte a l'adresse du shp ")
self.Information2.setWordWrap(True)
class Test(QWidget):
def __init__(self, parent=None):
super(Test, self).__init__(parent)
self.initUI()
def initUI(self):
InputLabel = QLabel('Input Shapefile (.shp)')
CSVLAbel = QLabel('Input CSV (.csv)')
self.InputEdit = QLineEdit(self)
self.CSVEdit = QLineEdit(self)
btn1SearchInput = QPushButton('...', self)
btn1SearchInput.resize(btn1SearchInput.sizeHint())
btn2SearchOutput = QPushButton('...', self)
btn2SearchOutput.resize(btn2SearchOutput.sizeHint())
okButton = QPushButton("OK")
cancelButton = QPushButton("Cancel")
grid = QGridLayout()
grid.setSpacing(10)
grid.addWidget(InputLabel, 1, 0)
grid.addWidget(self.InputEdit, 1, 1)
grid.addWidget(btn1SearchInput, 1, 2)
grid.addWidget(CSVLAbel, 2, 0)
grid.addWidget(self.CSVEdit, 2, 1)
grid.addWidget(btn2SearchOutput, 2, 2)
grid.addWidget(okButton, 3, 2)
grid.addWidget(cancelButton, 3, 3)
btn1SearchInput.clicked.connect(self.showDialogInput)
btn2SearchOutput.clicked.connect(self.showDialogOutput)
okButton.clicked.connect(self.OkDialog)
cancelButton.clicked.connect(self.close)
self.setGeometry(300, 300, 500, 200)
self.setWindowTitle('Compare SHP - CSV')
self.setLayout(grid)
self.show()
def showDialogInput(self):
fname = QFileDialog.getOpenFileName(self, 'Select Input Shapefile','', '*.shp')
if fname:
self.InputEdit.setText(fname[0])
def showDialogOutput(self):
csvname = QFileDialog.getOpenFileName(self, 'Select Input CSV','', '*.csv')
if csvname:
self.CSVEdit.setText(csvname[0])
def OkDialog(self):
global Input
Input = self.InputEdit.text()
global CSVput
CSVput = self.CSVEdit.text()
if os.path.isfile(CSVput) is True :
if os.path.isfile(Input) is True :
self.secondwindow = Example()
self.secondwindow.show()
else :
QMessageBox.information(self, 'Message', 'Un (ou deux) des deux chemins renseignes est faux')
else :
QMessageBox.information(self, 'Message', 'Un (ou deux) des chemins renseignes est faux')
def closeEvent(self, event):
reply = QMessageBox.question(self, 'Message',
"Etes vous sur de vouloir quitter ce programme ?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Test()
sys.exit(app.exec_()) |
Partager