def VediTabellaDanni(self):
     self.NomeTabella = str(self.txtShellFilePath_6.text())
     self.TabView = TableViewer(self.iface, self.NomeTabella)
     self.TabView.show()  # show the dialog
     result = self.TabView.exec_()
class calcolodannoDialog(QtGui.QDialog, Ui_FloodRisk):
    def __init__(self, iface):
        QtGui.QDialog.__init__(self)
        self.setupUi(self)
        self.iface = iface
        self.btnChooseShellFile_3.setIcon(
            QIcon(":/plugins/floodrisk/icons/folder_explore.png"))
        self.btnChooseShellFile_3.setIconSize(QSize(25, 25))
        self.pushButtonView.setIcon(
            QIcon(":/plugins/floodrisk/icons/table_go.png"))
        self.pushButtonView.setIconSize(QSize(25, 25))
        self.pushButton.setIcon(
            QIcon(":/plugins/floodrisk/icons/chart_bar.png"))
        self.pushButton.setIconSize(QSize(25, 25))
        self.buttonGrafici.setIcon(
            QIcon(":/plugins/floodrisk/icons/images.jpg"))
        self.buttonGrafici.setIconSize(QSize(35, 25))
        self.label_red_danno.setPixmap(
            QPixmap(":/plugins/floodrisk/icons/red20.png"))
        self.label_red_vuln.setPixmap(
            QPixmap(":/plugins/floodrisk/icons/red20.png"))
        self.label_green_danno.setPixmap(
            QPixmap(":/plugins/floodrisk/icons/green20.png"))
        self.label_green_vuln.setPixmap(
            QPixmap(":/plugins/floodrisk/icons/green20.png"))

        # initialize actions
        QObject.connect(self.btnChooseShellFile_3, SIGNAL("clicked()"),
                        self.setFileMaxH)
        QObject.connect(self.buttonGrafici, SIGNAL("clicked()"),
                        self.graficoCurve)
        QObject.connect(self.pushButtonView, SIGNAL("clicked()"),
                        self.VediTabellaDanni)
        QObject.connect(self.toolButtonEsegui, SIGNAL("clicked()"),
                        self.EseguiCalcoloDanni)
        QObject.connect(self.pushButtonSalvaProgetto, SIGNAL("clicked()"),
                        self.writexml)
        QObject.connect(self.pushButtonLoadLayer, SIGNAL("clicked()"),
                        self.CaricaLayers)
        QObject.connect(self.pushButton, SIGNAL("clicked()"), self.istogrammi)

        self.dic_TypeId = {}
        self.CurveType = ''
        self.TotalDamage = 0.0
        #self.sep=set_csv_separator()

        # help
        QObject.connect(self.buttonBox, SIGNAL(_fromUtf8("helpRequested()")),
                        self.show_help)

    #------------- Actions -----------------------

    def show_help(self):
        """Load the help text into the system browser."""
        show_context_help(context='include3')

    def setFileMaxH(self):
        message = QtGui.QMessageBox.question(self, self.tr('Attention'),self.tr("Warning you are editing the data input to the project: current data" \
        " of output will be deleted. Are you sure?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
        if message == QtGui.QMessageBox.Yes:
            FileMaxHPath = QFileDialog.getOpenFileName(self, self.tr('Select peak flow depth file'), \
            '.', 'File tif (*.tif);;All files (*.*)')
            self.txtShellFilePath_3.setText(FileMaxHPath)
            msg = FileMaxHPath
            QMessageBox.information(None, "FileMaxHPath", msg)
            msg = str(self.txtShellFilePath.text())
            if msg == "":
                aa = str(FileMaxHPath)
                DirOut = os.path.dirname(aa)
                Name = os.path.basename(aa)
                pp = str.split(Name, '.')
                FileProgettoPath = DirOut + os.sep + pp[0] + '.dmg'
                self.txtShellFilePath.setText(FileProgettoPath)

            msg = str(self.txtShellFilePath_5.text())
            if msg == "":
                aa = str(FileMaxHPath)
                DirOut = os.path.dirname(aa)
                Name = os.path.basename(aa)
                pp = str.split(Name, '.')
                FileDannoPath = DirOut + os.sep + pp[0] + '_dmg.tif'
                self.txtShellFilePath_5.setText(FileDannoPath)

            msg = str(self.txtShellFilePath_6.text())
            if msg == "":
                aa = str(FileMaxHPath)
                DirOut = os.path.dirname(aa)
                Name = os.path.basename(aa)
                pp = str.split(Name, '.')
                FileTabDannoPath = DirOut + os.sep + pp[0] + '_dmg.csv'
                self.txtShellFilePath_6.setText(FileTabDannoPath)

            msg = str(self.txtShellFilePath_vulnerato.text())
            if msg == "":
                aa = str(FileMaxHPath)
                DirOut = os.path.dirname(aa)
                Name = os.path.basename(aa)
                pp = str.split(Name, '.')
                FileVulnPath = DirOut + os.sep + pp[0] + '_vuln.tif'
                self.txtShellFilePath_vulnerato.setText(FileVulnPath)

            #----Deleting output data -----
            self.txtShellFilePath_5.setText("")
            self.txtShellFilePath_vulnerato.setText("")
            self.txtShellFilePath_6.setText("")
            abil = bool("true")
            self.pushButtonSalvaProgetto.setEnabled(abil)

    def graficoCurve(self):
        tipo = self.comboBoxGrafici.currentText()
        try:
            self.idTipo = self.dic_TypeId[tipo]
            from graficofloodriskdialog import graficofloodriskDialog
            gfd = graficofloodriskDialog(self.iface, self.idTipo, tipo)
            geoDataBase = str(self.txtShellFilePath_2.text())
            if geoDataBase != "":
                gfd.lineEdit.setText(geoDataBase)
            gfd.run()
        except:
            txt0 = 'Geodatabase: %s \n\n' % self.txtShellFilePath_2.text()
            txt1 = self.tr("Error in table Vulnerability")
            msg = '%s %s' % (txt0, txt1)
            QMessageBox.information(None, "Graph", msg)

    def EseguiCalcoloDanni(self):

        self.Nome = []
        self.listafiles = []
        # FileDEM1
        self.Nome.append('File Max Water Depth')
        self.listafiles.append(str(self.txtShellFilePath_3.text()))
        # DBfile
        self.Nome.append('File Geodatabase')
        self.listafiles.append(str(self.txtShellFilePath_2.text()))
        # NameFileGridVulnerability
        self.Nome.append('File Grid Vulnerability')
        self.listafiles.append(str(self.txtShellFilePath_vulnerato.text()))
        # NameFileGridDamages
        self.Nome.append('File Grid Damages')
        self.listafiles.append(str(self.txtShellFilePath_5.text()))
        # NameFileTable
        self.Nome.append('File Table 1')
        self.listafiles.append(str(self.txtShellFilePath_6.text()))

        tipo = self.comboBoxGrafici.currentText()
        self.CurveType = tipo

        abil0 = bool("true")
        try:
            self.idTipo = self.dic_TypeId[tipo]
            self.listafiles.append(self.idTipo)
        except:
            txt0 = 'Geodatabase: %s \n\n' % self.txtShellFilePath_2.text()
            txt1 = self.tr('Warning the Depth-Damage Curves Type')
            txt2 = self.tr('does not exists')
            msg = '%s %s %s: %s' % (txt0, txt1, tipo, txt2)
            QMessageBox.information(None, "Input", msg)
            abil0 = bool()
            errMsg = 'Input Error'

        if abil0:
            abil = bool("true")
            for i in range(2):
                if not os.path.exists(self.listafiles[i]):
                    txt1 = self.tr('Warning the file')
                    txt2 = self.tr('does not exists')
                    msg = '%s %s: %s' % (txt1, self.Nome[i], txt2)
                    QMessageBox.information(None, "File input", msg)
                    abil = bool()
                    errMsg = 'Input Error'
            for k in range(3):
                i = k + 2
                if len(self.listafiles[i]) == 0:
                    txt1 = self.tr('Attention assign a name to file')
                    msg = '%s: %s ' % (txt1, self.Nome[i])
                    QMessageBox.information(None, "File output", msg)
                    abil = bool()
                    errMsg = 'Input Error'
        else:
            abil = bool()

        if abil:
            fileprogetto = str(self.txtShellFilePath.text())

            # initializes progressbar
            self.progressBar.setFormat(self.tr('Damage assessment') + ': %p%')

            self.progressBar.setValue(0)
            abil = bool()
            self.buttonBox.setEnabled(abil)

            NotErr, errMsg, TotalDamage = CalcoloDannoInondazione.main(
                self.listafiles, self.progressBar)

            self.TotalDamage = TotalDamage

            self.luci()
            if NotErr:
                msg = self.tr('End of Job')
                QMessageBox.information(None, "FloodRisk", msg)
                self.writexml()

                abil = bool('True')
                self.buttonBox.setEnabled(abil)
                self.progressBar.setFormat(('%p%'))
                self.progressBar.setValue(0)
            else:
                msg = errMsg + " - " + self.tr("Run not executed")
                QMessageBox.information(None, "Run", msg)
                self.luci()
        else:
            msg = errMsg + " - " + self.tr("Run not executed")
            QMessageBox.information(None, "Run", msg)
            self.luci()

    def writexml(self):
        fileprogetto = str(self.txtShellFilePath.text())
        dicProgetto = {}
        dicParameter = {}
        if fileprogetto != "":
            xmlfile = open(fileprogetto)
            dom = minidom.parse(xmlfile)
            for node in dom.getElementsByTagName("General"):
                L = node.getElementsByTagName("File")
                for node2 in L:
                    Button = node2.getAttribute("Button")
                    nome = node2.getAttribute("name")
                    dicProgetto[Button] = nome
            for node in dom.getElementsByTagName("Parameters"):
                L = node.getElementsByTagName("Parameter")
                for node2 in L:
                    Param = node2.getAttribute("Param")
                    Value = node2.getAttribute("Value")
                    dicParameter[Param] = Value

            xmlfile.close()
        # Create the minidom document
        doc = Document()

        # Create the <wml> base element
        wml = doc.createElement("FloodRisk")
        doc.appendChild(wml)

        # Create the main <card> element
        maincard = doc.createElement("General")
        wml.appendChild(maincard)

        # Create a <p> element
        ShellFilePath = str(self.txtShellFilePath_2.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FileGeodatabase')
        paragraph1.setAttribute("name", ShellFilePath)
        maincard.appendChild(paragraph1)

        ShellFilePath = str(self.txtShellFilePath_3.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FilePeakFloodDepth')
        paragraph1.setAttribute("name", ShellFilePath)
        #paragraph1.setAttribute("unit", '[m]')
        maincard.appendChild(paragraph1)

        # Save input file
        for Button in dicProgetto.keys():
            nome = dicProgetto[Button]
            if Button == 'FilePeakFloodVelocity':
                paragraph1 = doc.createElement("File")
                paragraph1.setAttribute("Button", Button)
                paragraph1.setAttribute("name", nome)
                #paragraph1.setAttribute("unit", '[m]')
                maincard.appendChild(paragraph1)
            if Button == 'FileWarningTime':
                paragraph1 = doc.createElement("File")
                paragraph1.setAttribute("Button", Button)
                paragraph1.setAttribute("name", nome)
                #paragraph1.setAttribute("unit", '[m]')
                maincard.appendChild(paragraph1)

        # Create a <p> element
        ShellFilePath = str(self.txtShellFilePath_5.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FileGridDamages')
        paragraph1.setAttribute("name", ShellFilePath)
        #paragraph1.setAttribute("unit", '[kEuro]')
        maincard.appendChild(paragraph1)
        #
        # Create a <p> element
        ShellFilePath = str(self.txtShellFilePath_vulnerato.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FileGridVulnerability')
        paragraph1.setAttribute("name", ShellFilePath)
        maincard.appendChild(paragraph1)
        #
        # Create a <p> element
        ShellFilePath = str(self.txtShellFilePath_6.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FileTable1')
        paragraph1.setAttribute("name", ShellFilePath)
        maincard.appendChild(paragraph1)

        # Save input file
        for Button in dicProgetto.keys():
            nome = dicProgetto[Button]
            if Button == 'FileGridPopRisk':
                paragraph1 = doc.createElement("File")
                paragraph1.setAttribute("Button", Button)
                paragraph1.setAttribute("name", nome)
                maincard.appendChild(paragraph1)
            if Button == 'FileTable2':
                paragraph1 = doc.createElement("File")
                paragraph1.setAttribute("Button", Button)
                paragraph1.setAttribute("name", nome)
                maincard.appendChild(paragraph1)

        # Create the main <card> element
        maincard2 = doc.createElement("Parameters")
        wml.appendChild(maincard2)

        # Create a <p> element
        Param1 = doc.createElement("Parameter")
        Param1.setAttribute("Param", 'CurveType')
        Param1.setAttribute("Value", self.CurveType)
        maincard2.appendChild(Param1)

        Param1 = doc.createElement("Parameter")
        Param1.setAttribute("Param", 'TotalDamage')
        msg = '%.1f' % self.TotalDamage
        Param1.setAttribute("Value", msg)
        maincard2.appendChild(Param1)

        for Param in dicParameter.keys():
            Value = dicParameter[Param]
            if Param == 'Method':
                Param2 = doc.createElement("Parameter")
                Param2.setAttribute("Param", 'Method')
                Param2.setAttribute("Value", Value)
                maincard2.appendChild(Param2)
            if Param == 'Understand':
                Param2 = doc.createElement("Parameter")
                Param2.setAttribute("Param", 'Understand')
                Param2.setAttribute("Value", Value)
                maincard2.appendChild(Param2)
            if Param == 'LOL':
                Param2 = doc.createElement("Parameter")
                Param2.setAttribute("Param", 'LOL')
                Param2.setAttribute("Value", Value)
                maincard2.appendChild(Param2)

        if fileprogetto != "":
            fp = open(fileprogetto, "w")
            # writexml(self, writer, indent='', addindent='', newl='', encoding=None)
            doc.writexml(fp, "", "   ", "\n", "UTF-8")
            self.AutoLoad = fileprogetto
            QMessageBox.information(None, "Info", self.tr("Project Saved"))

    def VediTabellaDanni(self):
        self.NomeTabella = str(self.txtShellFilePath_6.text())
        self.TabView = TableViewer(self.iface, self.NomeTabella)
        self.TabView.show()  # show the dialog
        result = self.TabView.exec_()

    def CaricaLayers(self):
        filePath = str(self.txtShellFilePath_2.text())
        if os.path.exists(filePath):
            # case geodatabase
            tabelle = ['StructurePoly', 'InfrastrLines', 'CensusBlocks']
            for nomelayer in tabelle:
                if not LayerCaricato(self, nomelayer):
                    openFile(self, filePath, nomelayer)

        filePath = str(self.txtShellFilePath_3.text())
        if os.path.exists(filePath):
            if not LayerCaricato(self, filePath):
                openFile(self, filePath, '')

        filePath = str(self.txtShellFilePath_vulnerato.text())
        if os.path.exists(filePath):
            if not LayerCaricato(self, filePath):
                openFile(self, filePath, '')

        filePath = str(self.txtShellFilePath_5.text())
        if os.path.exists(filePath):
            if not LayerCaricato(self, filePath):
                openFile(self, filePath, '')

    def istogrammi(self):
        self.NomeFile = str(self.txtShellFilePath_6.text())
        if os.path.exists(self.NomeFile):
            try:

                import matplotlib

                self.sep = check_csv_separator(self.NomeFile)
                # Reading csv file
                finp = open(self.NomeFile)
                csv_reader = csv.reader(finp,
                                        delimiter=self.sep,
                                        quotechar='"')
                headers = csv_reader.next()

                self.fields = []
                for p in headers:
                    self.fields.append(p)

                progress = unicode(
                    'Reading data '
                )  # As a progress bar is used the main window's status bar, because the own one is not initialized yet

                yEuro1 = []
                yEuro2 = []
                xCodice = []
                for record in csv_reader:
                    for i in range(len(record)):
                        if i == 0:
                            xCodice += [record[i]]
                        if i == 5:
                            yEuro2 += [float(record[i])]
                        if i == 6:
                            yEuro1 += [float(record[i])]

                finp.close()

                #---------------Draw Chart-----------------
                y1 = yEuro1
                y2 = yEuro2
                x1 = xCodice
                width = 0.3
                i = arange(len(y1))
                r1 = bar(i, y1, width, color='r', linewidth=1)
                r2 = bar(i + width, y2, width, color='b', linewidth=1)
                xticks(i + width / 2, x1)
                xlabel('Code')
                ylabel('Euro')
                title(self.tr('Damage assessment results'))
                try:
                    legend((r1[0], r2[0]), (self.tr('Content Damage'),
                                            self.tr('Structure Damage')),
                           'best')
                except:
                    pass
                grid()
                show()
            except:
                QMessageBox.information(
                    None, "Warning",
                    "The current version of QGIS does not allow import matplotlib"
                )

        else:
            txt1 = self.tr('Warning the file')
            txt2 = self.tr('does not exists')
            msg = '%s\n\n %s\n\n %s' % (txt1, self.NomeFile, txt2)
            QMessageBox.information(None, "Input", msg)

    #------------------- Functions ---------------------------
    def startxml(self):
        fileprogetto = str(self.txtShellFilePath.text())
        if fileprogetto != "":
            xmlfile = open(fileprogetto)
            dom = minidom.parse(xmlfile)
            for node in dom.getElementsByTagName("General"):
                L = node.getElementsByTagName("File")
                for node2 in L:
                    Button = node2.getAttribute("Button")
                    nome = node2.getAttribute("name")
                    if Button == 'FileGeodatabase':
                        self.txtShellFilePath_2.setText(nome)
                    elif Button == 'FilePeakFloodDepth':
                        self.txtShellFilePath_3.setText(nome)
                    elif Button == 'FileGridDamages':
                        self.txtShellFilePath_5.setText(nome)
                    elif Button == 'FileGridVulnerability':
                        self.txtShellFilePath_vulnerato.setText(nome)
                    elif Button == 'FileTable1':
                        self.txtShellFilePath_6.setText(nome)

            for node in dom.getElementsByTagName("Parameters"):
                L = node.getElementsByTagName("Parameter")
                for node2 in L:
                    Param = node2.getAttribute("Param")
                    try:
                        Value = node2.getAttribute("Value")
                    except:
                        Value = node2.getAttribute("name")
                    if Param == 'CurveType':
                        self.CurveType = Value
                        self.setCurrentCurveType(Value)

            xmlfile.close()
            abil = bool("true")
            self.pushButtonSalvaProgetto.setEnabled(abil)

    def luci(self):

        FilePath = str(self.txtShellFilePath_6.text())
        #FileGridDamages
        FilePath = str(self.txtShellFilePath_5.text())
        if os.path.exists(FilePath):
            self.label_red_danno.hide()
            self.label_green_danno.show()
        else:
            self.label_red_danno.show()
            self.label_green_danno.hide()

        #FileGridVulnerability
        FilePath = str(self.txtShellFilePath_vulnerato.text())
        if os.path.exists(FilePath):
            self.label_red_vuln.hide()
            self.label_green_vuln.show()
        else:
            self.label_red_vuln.show()
            self.label_green_vuln.hide()

    def setListaTipoCurvaVuln(self):
        FileGDB = str(self.txtShellFilePath_2.text())
        if FileGDB != "":
            if self.CheckGeodatabase():
                conn = sqlite3.connect(FileGDB)
                cursor = conn.cursor()
                testoQuery = 'SELECT VulnID FROM Vulnerability GROUP BY VulnID'
                cursor.execute(testoQuery)
                ListaTipi1 = cursor.fetchall()
                ListaTipi = []
                for row in ListaTipi1:
                    ListaTipi.append(int(row[0]))
                dic_VulnType = {}
                self.dic_TypeId = {}
                testoQuery2 = 'SELECT * FROM VulnType'
                cursor.execute(testoQuery2)
                ListaDescription = cursor.fetchall()
                if len(ListaDescription) > 0:
                    for row in ListaDescription:
                        dic_VulnType[int(row[1])] = str(row[2])
                        self.dic_TypeId[str(row[2])] = int(row[1])
                    ListaDescrizione = []
                    for num in ListaTipi:
                        ListaDescrizione.append(dic_VulnType[num])
                    self.comboBoxGrafici.clear()
                    self.comboBoxGrafici.addItems(ListaDescrizione)

            else:
                QMessageBox.information(
                    None, "FloodRisk",
                    self.tr("You must first create the Geodb.Sqlite"))
        else:
            QMessageBox.information(
                None, "FloodRisk",
                self.tr("You must first create the Geodb.Sqlite"))

    def setCurrentCurveType(self, ItemText):
        # set currentItem CurveType
        self.CurveType = ''
        NumType = self.comboBoxGrafici.count()
        AllItems = [self.comboBoxGrafici.itemText(i) for i in range(NumType)]
        if NumType > 0:
            index = -1
            for ii in range(NumType):
                if ItemText == AllItems[ii]:
                    index = ii
            if index >= 0:
                self.comboBoxGrafici.setCurrentIndex(index)
                self.CurveType = ItemText

    def CheckGeodatabase(self):
        res = bool()
        if os.path.exists(self.txtShellFilePath_2.text()):
            mydb_path = self.txtShellFilePath_2.text()
            try:
                # connecting the db
                conn = sqlite3.connect(mydb_path)
                # creating a Cursor
                cur = conn.cursor()

                TablesList = [
                    'spatial_ref_sys', 'AnalysisArea', 'CensusBlocks',
                    'FatalityRate'
                ]
                TablesList.append('FatalityRate')
                TablesList.append('FloodSeverity')
                TablesList.append('InfrastrLines')
                TablesList.append('VulnType')
                TablesList.append('Vulnerability')
                TablesList.append('StructurePoly')

                for NomeTabella in TablesList:
                    sql = "SELECT sql FROM sqlite_master WHERE type='table' AND name='%s';" % (
                        NomeTabella)
                    cur.execute(sql)
                    Tabella = str(cur.fetchone()[0])

                res = bool('True')
            except:
                res = bool()
        else:
            res = bool()
        return res
 def VediTabellaDanni(self):
     self.NomeTabella=str(self.txtShellFilePath_6.text())
     self.TabView = TableViewer(self.iface,self.NomeTabella)
     self.TabView.show()# show the dialog
     result = self.TabView.exec_()
class calcolodannoDialog(QtGui.QDialog, Ui_FloodRisk):
    def __init__(self,iface):
        QtGui.QDialog.__init__(self)
        self.setupUi(self)
        self.iface=iface
        self.btnChooseShellFile_3.setIcon(QIcon(":/plugins/floodrisk/icons/folder_explore.png"))
        self.btnChooseShellFile_3.setIconSize(QSize(25,25))
        self.pushButtonView.setIcon(QIcon(":/plugins/floodrisk/icons/table_go.png"))
        self.pushButtonView.setIconSize(QSize(25,25))
        self.pushButton.setIcon(QIcon(":/plugins/floodrisk/icons/chart_bar.png"))
        self.pushButton.setIconSize(QSize(25,25))
        self.buttonGrafici.setIcon(QIcon(":/plugins/floodrisk/icons/images.jpg"))
        self.buttonGrafici.setIconSize(QSize(35,25))
        self.label_red_danno.setPixmap(QPixmap(":/plugins/floodrisk/icons/red20.png"))
        self.label_red_vuln.setPixmap(QPixmap(":/plugins/floodrisk/icons/red20.png"))
        self.label_green_danno.setPixmap(QPixmap(":/plugins/floodrisk/icons/green20.png"))
        self.label_green_vuln.setPixmap(QPixmap(":/plugins/floodrisk/icons/green20.png"))

        # initialize actions
        QObject.connect(self.btnChooseShellFile_3, SIGNAL("clicked()"), self.setFileMaxH)
        QObject.connect(self.buttonGrafici, SIGNAL("clicked()"), self.graficoCurve)
        QObject.connect(self.pushButtonView, SIGNAL("clicked()"), self.VediTabellaDanni)
        QObject.connect(self.toolButtonEsegui, SIGNAL("clicked()"), self.EseguiCalcoloDanni)
        QObject.connect(self.pushButtonSalvaProgetto, SIGNAL("clicked()"), self.writexml)
        QObject.connect(self.pushButtonLoadLayer, SIGNAL("clicked()"), self.CaricaLayers)
        QObject.connect(self.pushButton, SIGNAL("clicked()"), self.istogrammi)

        self.dic_TypeId={}
        self.CurveType=''
        self.TotalDamage=0.0

        # help
        QObject.connect(self.buttonBox, SIGNAL(_fromUtf8("helpRequested()")), self.show_help)


    #------------- Actions -----------------------

    def show_help(self):
        """Load the help text into the system browser."""
        show_context_help(context='include3')

    def setFileMaxH(self):
        message = QtGui.QMessageBox.question(self, self.tr('Attention'),self.tr("Warning you are editing the data input to the project: current data" \
        " of output will be deleted. Are you sure?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
        if message == QtGui.QMessageBox.Yes:
            FileMaxHPath = QFileDialog.getOpenFileName(self, self.tr('Select peak flow depth file'), \
            '.', 'File tif (*.tif);;All files (*.*)')
            self.txtShellFilePath_3.setText(FileMaxHPath)
            msg=FileMaxHPath
            QMessageBox.information(None, "FileMaxHPath", msg)
            msg=str(self.txtShellFilePath.text())
            if msg=="":
                aa=str(FileMaxHPath)
                DirOut=os.path.dirname(aa)
                Name=os.path.basename(aa)
                pp=str.split(Name,'.')
                FileProgettoPath=DirOut+os.sep + pp[0]+'.dmg'
                self.txtShellFilePath.setText(FileProgettoPath)

            msg=str(self.txtShellFilePath_5.text())
            if msg=="":
                aa=str(FileMaxHPath)
                DirOut=os.path.dirname(aa)
                Name=os.path.basename(aa)
                pp=str.split(Name,'.')
                FileDannoPath=DirOut+os.sep + pp[0]+'_dmg.tif'
                self.txtShellFilePath_5.setText(FileDannoPath)

            msg=str(self.txtShellFilePath_6.text())
            if msg=="":
                aa=str(FileMaxHPath)
                DirOut=os.path.dirname(aa)
                Name=os.path.basename(aa)
                pp=str.split(Name,'.')
                FileTabDannoPath=DirOut+os.sep + pp[0]+'_dmg.csv'
                self.txtShellFilePath_6.setText(FileTabDannoPath)

            msg=str(self.txtShellFilePath_vulnerato.text())
            if msg=="":
                aa=str(FileMaxHPath)
                DirOut=os.path.dirname(aa)
                Name=os.path.basename(aa)
                pp=str.split(Name,'.')
                FileVulnPath=DirOut+os.sep + pp[0]+'_vuln.tif'
                self.txtShellFilePath_vulnerato.setText(FileVulnPath)

            #----Deleting output data -----
            self.txtShellFilePath_5.setText("")
            self.txtShellFilePath_vulnerato.setText("")
            self.txtShellFilePath_6.setText("")
            abil=bool("true")
            self.pushButtonSalvaProgetto.setEnabled(abil)

    def graficoCurve(self):
        tipo = self.comboBoxGrafici.currentText()
        try:
            self.idTipo = self.dic_TypeId[tipo]
            from graficofloodriskdialog import graficofloodriskDialog
            gfd=graficofloodriskDialog(self.iface, self.idTipo, tipo)
            geoDataBase=str(self.txtShellFilePath_2.text())
            if geoDataBase!="":
                gfd.lineEdit.setText(geoDataBase)
            gfd.run()
        except:
            txt0='Geodatabase: %s \n\n' % self.txtShellFilePath_2.text()
            txt1=self.tr("Error in table Vulnerability")
            msg='%s %s' % (txt0,txt1)
            QMessageBox.information(None, "Graph", msg)

    def EseguiCalcoloDanni(self):

        self.Nome=[]
        self.listafiles=[]
        # FileDEM1
        self.Nome.append('File Max Water Depth')
        self.listafiles.append(str(self.txtShellFilePath_3.text()))
        # DBfile
        self.Nome.append('File Geodatabase')
        self.listafiles.append(str(self.txtShellFilePath_2.text()))
        # NameFileGridVulnerability
        self.Nome.append('File Grid Vulnerability')
        self.listafiles.append(str(self.txtShellFilePath_vulnerato.text()))
        # NameFileGridDamages
        self.Nome.append('File Grid Damages')
        self.listafiles.append(str(self.txtShellFilePath_5.text()))
        # NameFileTable
        self.Nome.append('File Table 1')
        self.listafiles.append(str(self.txtShellFilePath_6.text()))

        tipo = self.comboBoxGrafici.currentText()
        self.CurveType=tipo

        abil0=bool("true")
        try:
            self.idTipo = self.dic_TypeId[tipo]
            self.listafiles.append(self.idTipo)
        except:
                txt0='Geodatabase: %s \n\n' % self.txtShellFilePath_2.text()
                txt1=self.tr('Warning the Depth-Damage Curves Type')
                txt2=self.tr('does not exists')
                msg='%s %s %s: %s' % (txt0,txt1,tipo,txt2)
                QMessageBox.information(None, "Input", msg)
                abil0=bool()
                errMsg='Input Error'

        if abil0:
            abil=bool("true")
            for i in range(2):
                if not os.path.exists(self.listafiles[i]):
                    txt1=self.tr('Warning the file')
                    txt2=self.tr('does not exists')
                    msg='%s %s: %s' % (txt1,self.Nome[i],txt2)
                    QMessageBox.information(None, "File input", msg)
                    abil=bool()
                    errMsg='Input Error'
            for k in range(3):
                i=k+2
                if len(self.listafiles[i])==0:
                    txt1=self.tr('Attention assign a name to file')
                    msg='%s: %s ' % (txt1,self.Nome[i])
                    QMessageBox.information(None, "File output", msg)
                    abil=bool()
                    errMsg='Input Error'
        else:
            abil=bool()

        if abil:
            fileprogetto=str(self.txtShellFilePath.text())

            # initializes progressbar
            self.progressBar.setFormat(self.tr('Damage assessment') +': %p%')

            self.progressBar.setValue(0)
            abil=bool()
            self.buttonBox.setEnabled(abil)

            NotErr, errMsg, TotalDamage = CalcoloDannoInondazione.main(self.listafiles,self.progressBar)

            self.TotalDamage=TotalDamage

            self.luci()
            if NotErr:
                msg=self.tr('End of Job')
                QMessageBox.information(None, "FloodRisk", msg)
                self.writexml()

                abil=bool('True')
                self.buttonBox.setEnabled(abil)
                self.progressBar.setFormat(('%p%'))
                self.progressBar.setValue(0)
            else:
                msg=errMsg + " - " + self.tr("Run not executed")
                QMessageBox.information(None, "Run", msg)
                self.luci()
        else:
            msg=errMsg + " - " + self.tr("Run not executed")
            QMessageBox.information(None, "Run", msg)
            self.luci()

    def writexml (self):
        fileprogetto=str(self.txtShellFilePath.text())
        dicProgetto={}
        dicParameter={}
        if fileprogetto!="":
            xmlfile=open(fileprogetto)
            dom=minidom.parse(xmlfile)
            for node in dom.getElementsByTagName("General"):
                L = node.getElementsByTagName("File")
                for node2 in L:
                    Button = node2.getAttribute("Button")
                    nome = node2.getAttribute("name")
                    dicProgetto[Button] = nome
            for node in dom.getElementsByTagName("Parameters"):
                L = node.getElementsByTagName("Parameter")
                for node2 in L:
                    Param = node2.getAttribute("Param")
                    Value = node2.getAttribute("Value")
                    dicParameter[Param] = Value

            xmlfile.close()
        # Create the minidom document
        doc = Document()

        # Create the <wml> base element
        wml = doc.createElement("FloodRisk")
        doc.appendChild(wml)

        # Create the main <card> element
        maincard = doc.createElement("General")
        wml.appendChild(maincard)

        # Create a <p> element
        ShellFilePath= str(self.txtShellFilePath_2.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FileGeodatabase')
        paragraph1.setAttribute("name", ShellFilePath)
        maincard.appendChild(paragraph1)

        ShellFilePath= str(self.txtShellFilePath_3.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FilePeakFloodDepth')
        paragraph1.setAttribute("name", ShellFilePath)
        #paragraph1.setAttribute("unit", '[m]')
        maincard.appendChild(paragraph1)

        # Save input file
        for Button in dicProgetto.keys():
            nome = dicProgetto[Button]
            if Button == 'FilePeakFloodVelocity':
                paragraph1 = doc.createElement("File")
                paragraph1.setAttribute("Button", Button)
                paragraph1.setAttribute("name", nome)
                #paragraph1.setAttribute("unit", '[m]')
                maincard.appendChild(paragraph1)
            if Button == 'FileWarningTime':
                paragraph1 = doc.createElement("File")
                paragraph1.setAttribute("Button", Button)
                paragraph1.setAttribute("name", nome)
                #paragraph1.setAttribute("unit", '[m]')
                maincard.appendChild(paragraph1)

        # Create a <p> element
        ShellFilePath= str(self.txtShellFilePath_5.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FileGridDamages')
        paragraph1.setAttribute("name", ShellFilePath)
        #paragraph1.setAttribute("unit", '[kEuro]')
        maincard.appendChild(paragraph1)
#
        # Create a <p> element
        ShellFilePath= str(self.txtShellFilePath_vulnerato.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FileGridVulnerability')
        paragraph1.setAttribute("name", ShellFilePath)
        maincard.appendChild(paragraph1)
#
        # Create a <p> element
        ShellFilePath= str(self.txtShellFilePath_6.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FileTable1')
        paragraph1.setAttribute("name", ShellFilePath)
        maincard.appendChild(paragraph1)

        # Save input file
        for Button in dicProgetto.keys():
            nome = dicProgetto[Button]
            if Button == 'FileGridPopRisk':
                paragraph1 = doc.createElement("File")
                paragraph1.setAttribute("Button", Button)
                paragraph1.setAttribute("name", nome)
                maincard.appendChild(paragraph1)
            if Button == 'FileTable2':
                paragraph1 = doc.createElement("File")
                paragraph1.setAttribute("Button", Button)
                paragraph1.setAttribute("name", nome)
                maincard.appendChild(paragraph1)

        # Create the main <card> element
        maincard2 = doc.createElement("Parameters")
        wml.appendChild(maincard2)

        # Create a <p> element
        Param1 = doc.createElement("Parameter")
        Param1.setAttribute("Param", 'CurveType')
        Param1.setAttribute("Value", self.CurveType)
        maincard2.appendChild(Param1)

        Param1 = doc.createElement("Parameter")
        Param1.setAttribute("Param", 'TotalDamage')
        msg='%.1f' % self.TotalDamage
        Param1.setAttribute("Value", msg)
        maincard2.appendChild(Param1)


        for Param in dicParameter.keys():
            Value = dicParameter[Param]
            if Param == 'Method':
                Param2 = doc.createElement("Parameter")
                Param2.setAttribute("Param", 'Method')
                Param2.setAttribute("Value", Value)
                maincard2.appendChild(Param2)
            if Param == 'Understand':
                Param2 = doc.createElement("Parameter")
                Param2.setAttribute("Param", 'Understand')
                Param2.setAttribute("Value", Value)
                maincard2.appendChild(Param2)
            if Param == 'LOL':
                Param2 = doc.createElement("Parameter")
                Param2.setAttribute("Param", 'LOL')
                Param2.setAttribute("Value", Value)
                maincard2.appendChild(Param2)


        if fileprogetto!="":
          fp = open(fileprogetto,"w")
          # writexml(self, writer, indent='', addindent='', newl='', encoding=None)
          doc.writexml(fp, "", "   ", "\n", "UTF-8")
          self.AutoLoad=fileprogetto
          QMessageBox.information(None, "Info", self.tr("Project Saved"))

    def VediTabellaDanni(self):
        self.NomeTabella=str(self.txtShellFilePath_6.text())
        self.TabView = TableViewer(self.iface,self.NomeTabella)
        self.TabView.show()# show the dialog
        result = self.TabView.exec_()

    def CaricaLayers(self):
        filePath=str(self.txtShellFilePath_2.text())
        if os.path.exists(filePath):
            # case geodatabase
            tabelle=['StructurePoly','InfrastrLines','CensusBlocks']
            for nomelayer in tabelle:
                if not LayerCaricato(self,nomelayer):
                    openFile(self,filePath,nomelayer)

        filePath=str(self.txtShellFilePath_3.text())
        if os.path.exists(filePath):
            if not LayerCaricato(self,filePath):
                openFile(self,filePath,'')

        filePath=str(self.txtShellFilePath_vulnerato.text())
        if os.path.exists(filePath):
            if not LayerCaricato(self,filePath):
                openFile(self,filePath,'')

        filePath=str(self.txtShellFilePath_5.text())
        if os.path.exists(filePath):
            if not LayerCaricato(self,filePath):
                openFile(self,filePath,'')

    def istogrammi(self):
        self.NomeFile=str(self.txtShellFilePath_6.text())
        if os.path.exists(self.NomeFile):
            filcsv=open(self.NomeFile,'r')
            riga=filcsv.readline()
            testo=riga[:-1]
            pp=str.split(testo,';')
            self.fields=[]
            yEuro1=[]
            yEuro2=[]
            xCodice=[]
            for p in pp:
                self.fields.append(p)
            self.data = []
            for i in range(len(self.fields)):
              self.data += [[]]
            steps=0
            for rec in filcsv:
                if len(rec)>0:
                    steps=steps+1
            #steps = self.provider.featureCount()
            stepp = steps / 10
            if stepp == 0:
              stepp = 1
            progress = unicode('Reading data ') # As a progress bar is used the main window's status bar, because the own one is not initialized yet
            filcsv.close()
            filcsv=open(self.NomeFile,'r')
            riga=filcsv.readline()
            n = 0
            self.numRows=0
            for kk in range(steps):
                riga=filcsv.readline()[:-1]
                attrs=str.split(riga,';')
                for i in range(len(attrs)):
                    self.data[i] += [attrs[i]]
                    if i == 0:
                        xCodice += [attrs[i]]
                    if i == 4:
                        yEuro2 += [int(attrs[i])]
                    if i == 5:
                        yEuro1 += [int(attrs[i])]

                n += 1
                self.numRows+=1
            #---------------Draw Chart-----------------
            y1=yEuro1
            y2=yEuro2
            x1=xCodice
            width=0.3
            i=arange(len(y1))
            r1=bar(i, y1,width, color='r',linewidth=1)
            r2=bar(i+width,y2,width,color='b',linewidth=1)
            xticks(i+width/2,x1)
            xlabel('Code'); ylabel('Euro'); title(self.tr('Damage assessment results'))
            legend((r1[0],r2[0]),(self.tr('Content Damage'), self.tr('Structure Damage')), 'best')
            grid()
            show()
        else:
            txt1=self.tr('Warning the file')
            txt2=self.tr('does not exists')
            msg='%s\n\n %s\n\n %s' % (txt1,self.NomeFile,txt2)
            QMessageBox.information(None, "Input", msg)


    #------------------- Functions ---------------------------
    def startxml (self):
        fileprogetto=str(self.txtShellFilePath.text())
        if fileprogetto!="":
            xmlfile=open(fileprogetto)
            dom=minidom.parse(xmlfile)
            for node in dom.getElementsByTagName("General"):
                L = node.getElementsByTagName("File")
                for node2 in L:
                    Button = node2.getAttribute("Button")
                    nome = node2.getAttribute("name")
                    if Button=='FileGeodatabase':
                        self.txtShellFilePath_2.setText(nome)
                    elif Button=='FilePeakFloodDepth':
                        self.txtShellFilePath_3.setText(nome)
                    elif Button=='FileGridDamages':
                        self.txtShellFilePath_5.setText(nome)
                    elif Button=='FileGridVulnerability':
                        self.txtShellFilePath_vulnerato.setText(nome)
                    elif Button=='FileTable1':
                        self.txtShellFilePath_6.setText(nome)

            for node in dom.getElementsByTagName("Parameters"):
                L = node.getElementsByTagName("Parameter")
                for node2 in L:
                    Param = node2.getAttribute("Param")
                    try:
                        Value = node2.getAttribute("Value")
                    except:
                        Value = node2.getAttribute("name")
                    if Param=='CurveType':
                        self.CurveType=Value
                        self.setCurrentCurveType(Value)

            xmlfile.close()
            abil=bool("true")
            self.pushButtonSalvaProgetto.setEnabled(abil)

    def luci(self):

        FilePath= str(self.txtShellFilePath_6.text())
        #FileGridDamages
        FilePath= str(self.txtShellFilePath_5.text())
        if os.path.exists(FilePath):
            self.label_red_danno.hide()
            self.label_green_danno.show()
        else:
            self.label_red_danno.show()
            self.label_green_danno.hide()

        #FileGridVulnerability
        FilePath= str(self.txtShellFilePath_vulnerato.text())
        if os.path.exists(FilePath):
            self.label_red_vuln.hide()
            self.label_green_vuln.show()
        else:
            self.label_red_vuln.show()
            self.label_green_vuln.hide()

    def setListaTipoCurvaVuln(self):
        FileGDB = str(self.txtShellFilePath_2.text())
        if FileGDB != "":
            if self.CheckGeodatabase():
                conn = sqlite3.connect(FileGDB)
                cursor = conn.cursor()
                testoQuery='SELECT VulnID FROM Vulnerability GROUP BY VulnID'
                cursor.execute(testoQuery)
                ListaTipi1 = cursor.fetchall()
                ListaTipi = []
                for row in ListaTipi1:
                    ListaTipi.append(int(row[0]))
                dic_VulnType={}
                self.dic_TypeId={}
                testoQuery2='SELECT * FROM VulnType'
                cursor.execute(testoQuery2)
                ListaDescription = cursor.fetchall()
                if len(ListaDescription)>0:
                    for row in ListaDescription:
                        dic_VulnType[int(row[1])] = str(row[2])
                        self.dic_TypeId[str(row[2])] = int(row[1])
                    ListaDescrizione=[]
                    for num in ListaTipi:
                        ListaDescrizione.append(dic_VulnType[num])
                    self.comboBoxGrafici.clear()
                    self.comboBoxGrafici.addItems(ListaDescrizione)

            else:
                 QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))
        else:
             QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))

    def setCurrentCurveType(self,ItemText):
        # set currentItem CurveType
        self.CurveType=''
        NumType=self.comboBoxGrafici.count()
        AllItems = [self.comboBoxGrafici.itemText(i) for i in range(NumType)]
        if NumType>0:
            index=-1
            for ii in range(NumType):
                if ItemText==AllItems[ii]:
                    index=ii
            if index>=0:
                self.comboBoxGrafici.setCurrentIndex(index)
                self.CurveType=ItemText

    def CheckGeodatabase(self):
        res=bool()
        if os.path.exists(self.txtShellFilePath_2.text()):
            mydb_path=self.txtShellFilePath_2.text()
            try:
                # connecting the db
                conn = sqlite3.connect(mydb_path)
                # creating a Cursor
                cur = conn.cursor()

                TablesList=['spatial_ref_sys','AnalysisArea','CensusBlocks','FatalityRate']
                TablesList.append('FatalityRate')
                TablesList.append('FloodSeverity')
                TablesList.append('InfrastrLines')
                TablesList.append('VulnType')
                TablesList.append('Vulnerability')
                TablesList.append('StructurePoly')

                for NomeTabella in TablesList:
                    sql="SELECT sql FROM sqlite_master WHERE type='table' AND name='%s';" % (NomeTabella)
                    cur.execute(sql)
                    Tabella=str(cur.fetchone()[0])

                res=bool('True')
            except:
                res=bool()
        else:
            res=bool()
        return res
class calcolorischiopopolazioneDialog(QtGui.QDialog, Ui_FloodRisk):
    def __init__(self,iface):
        QtGui.QDialog.__init__(self)
        self.setupUi(self)
        self.iface=iface

        self.btnChooseShellFile_3.setIcon(QIcon(":/plugins/floodrisk/icons/folder_explore.png"))
        self.btnChooseShellFile_3.setIconSize(QSize(25,25))
        self.btnChooseShellFile_velocita.setIcon(QIcon(":/plugins/floodrisk/icons/folder_explore.png"))
        self.btnChooseShellFile_velocita.setIconSize(QSize(25,25))
        self.btnChooseShellFile_tempi.setIcon(QIcon(":/plugins/floodrisk/icons/folder_explore.png"))
        self.btnChooseShellFile_tempi.setIconSize(QSize(25,25))
        self.pushButtonView_2.setIcon(QIcon(":/plugins/floodrisk/icons/table_go.png"))
        self.pushButtonView_2.setIconSize(QSize(25,25))
        self.pushButtonIstogrammi.setIcon(QIcon(":/plugins/floodrisk/icons/chart_bar.png"))
        self.pushButtonIstogrammi.setIconSize(QSize(25,25))
        self.label_red_pop.setPixmap(QPixmap(":/plugins/floodrisk/icons/red20.png"))
        self.label_green_pop.setPixmap(QPixmap(":/plugins/floodrisk/icons/green20.png"))

        # initialize actions
        QObject.connect(self.btnChooseShellFile_3, SIGNAL("clicked()"), self.setFileMaxH)
        QObject.connect(self.btnChooseShellFile_velocita, SIGNAL("clicked()"), self.setFileMaxV)
        QObject.connect(self.btnChooseShellFile_tempi, SIGNAL("clicked()"), self.setFileTime)
        QObject.connect(self.toolButtonEsegui_2, SIGNAL("clicked()"), self.EseguiCalcoloPopolazione)
        QObject.connect(self.pushButtonView_2, SIGNAL("clicked()"), self.VediTabellaRischio)
        QObject.connect(self.pushButtonSalvaProgetto, SIGNAL("clicked()"), self.writexml)
        QObject.connect(self.pushButtonLoadLayer, SIGNAL("clicked()"), self.CaricaLayers)
        QObject.connect(self.pushButtonIstogrammi, SIGNAL("clicked()"), self.istogrammi)
        self.radioButton.toggled.connect(self.setUnderstandingDam)
        self.radioButton_2.toggled.connect(self.setUnderstandingSufri)

        # help
        QObject.connect(self.buttonBox, SIGNAL(_fromUtf8("helpRequested()")), self.show_help)

        self.paramMethod=''
        self.paramUnderstanding=''
        self.paramLOL_notRound=0.0

    #------------- Actions -----------------------

    def show_help(self):
        """Load the help text into the system browser."""
        show_context_help(context='include4')


    def setFileMaxV(self):
        message = QtGui.QMessageBox.question(self, self.tr('Attention'),self.tr("Warning you are editing the data input to the project: current data" \
        " of output will be deleted. Are you sure?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
        if message == QtGui.QMessageBox.Yes:
            FileMaxVPath = QFileDialog.getOpenFileName(self, "Select file max water velocity", \
            '.', 'File tif (*.tif);;All files (*.*)')
            self.txtShellFilePath_velocita.setText(FileMaxVPath)
            #----Deleting output data-----
            self.txtShellFilePath_4.setText("")
            self.txtShellFilePath_7.setText("")

    def setFileTime(self):
        message = QtGui.QMessageBox.question(self, self.tr('Attention'),self.tr("Warning you are editing the data input to the project: current data" \
        " of output will be deleted. Are you sure?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
        if message == QtGui.QMessageBox.Yes:
            FileTimePath = QFileDialog.getOpenFileName(self, self.tr("Select warning time shape file"), \
            '.', 'File shp (*.shp);;All files (*.*)')
            self.txtShellFilePath_tempi.setText(FileTimePath)
            #----Deleting output data-----
            self.txtShellFilePath_4.setText("")
            self.txtShellFilePath_7.setText("")

    def setFileMaxH(self):
        message = QtGui.QMessageBox.question(self, self.tr('Attention'),self.tr("Warning you are editing the data input to the project: current data" \
        " of output will be deleted. Are you sure?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
        if message == QtGui.QMessageBox.Yes:
            FileMaxHPath = QFileDialog.getOpenFileName(self, self.tr('Select peak flow depth file'), \
            '.', 'File tif (*.tif);;All files (*.*)')
            self.txtShellFilePath_3.setText(FileMaxHPath)
            msg=FileMaxHPath
            QMessageBox.information(None, "FileMaxHPath", msg)
            msg=str(self.txtShellFilePath.text())
            if msg=="":
                aa=str(FileMaxHPath)
                DirOut=os.path.dirname(aa)
                Name=os.path.basename(aa)
                pp=str.split(Name,'.')
                FileProgettoPath=DirOut+os.sep + pp[0]+'.dmg'
                self.txtShellFilePath.setText(FileProgettoPath)

            msg=str(self.txtShellFilePath_4.text())
            if msg=="":
                aa=str(FileMaxHPath)
                DirOut=os.path.dirname(aa)
                Name=os.path.basename(aa)
                pp=str.split(Name,'.')
                FileGridPopPath=DirOut+os.sep + pp[0]+'_pop.tif'
                self.txtShellFilePath_4.setText(FileGridPopPath)

            msg=str(self.txtShellFilePath_7.text())
            if msg=="":
                aa=str(FileMaxHPath)
                DirOut=os.path.dirname(aa)
                Name=os.path.basename(aa)
                pp=str.split(Name,'.')
                FileTab2DannoPath=DirOut+os.sep + pp[0]+'_pop.csv'
                self.txtShellFilePath_7.setText(FileTab2DannoPath)

            #----Deleting output data-----
            self.txtShellFilePath_4.setText("")
            self.txtShellFilePath_7.setText("")
            abil=bool("true")
            self.pushButtonSalvaProgetto.setEnabled(abil)
            #self.luci()

    def EseguiCalcoloPopolazione(self):

        # ------------------------------------------------------
        # performs the calculation of the risk to the population
        # ------------------------------------------------------
        self.Nome=[]
        self.listafiles=[]
        # FileDEM1
        self.Nome.append('File Max Water Depth')
        self.listafiles.append(str(self.txtShellFilePath_3.text()))
        # file Water Velocity
        self.Nome.append('File Water Velocity')
        self.listafiles.append(str(self.txtShellFilePath_velocita.text()))
        # file Warning Time
        self.Nome.append('File Warning Time')
        self.listafiles.append(str(self.txtShellFilePath_tempi.text()))
        # DBfile
        self.Nome.append('File Geodatabase')
        self.listafiles.append(str(self.txtShellFilePath_2.text()))
        # NameFileGridPop
        self.Nome.append('File Grid population at risk')
        self.listafiles.append(str(self.txtShellFilePath_4.text()))
        # NameFileTable2
        self.Nome.append('File Table 2')
        self.listafiles.append(str(self.txtShellFilePath_7.text()))

        parametroUnderstanding = self.comboBox.currentText()
        self.paramUnderstanding=parametroUnderstanding
        self.listafiles.append(parametroUnderstanding)

        abil=bool("true")
        for i in range(4):
            if not os.path.exists(self.listafiles[i]):
                msg1=self.tr('Error the file')
                msg2=self.tr('does not exist')
                msg='%s %s: %s ' % (msg1,self.Nome[i],msg2)
                QMessageBox.information(None, "Fine input", msg)
                abil=bool()
        for k in range(2):
            i=k+4
            if len(self.listafiles[i])==0:
                txt1=self.tr('Attention assign a name to file')
                msg='%s: %s ' % (txt1,self.Nome[i])
                QMessageBox.information(None, "Fine output", msg)
                abil=bool()
        if not self.CheckFloodSeverity():
            abil=bool()

        if not self.CheckFatalityRate():
            abil=bool()

        if abil:
            fileprogetto=str(self.txtShellFilePath.text())

            if self.CheckGeodatabase():

                # initializes progressbar
                self.progressBar.setFormat(self.tr('Population consequences assessment') +': %p%')

                self.progressBar.setValue(0)
                abil=bool()
                self.buttonBox.setEnabled(abil)

                NotErr, errMsg, NumLoss_notRound = AssessConsequencesPopulation.main(self.listafiles,self.progressBar)

                self.paramLOL_notRound=NumLoss_notRound

                self.luci()

                if NotErr:
                    msg=self.tr('End of Job')
                    QMessageBox.information(None, "FloodRisk", msg)
                    self.writexml()

                    abil=bool('True')
                    self.buttonBox.setEnabled(abil)
                    self.progressBar.setFormat(('%p%'))
                    self.progressBar.setValue(0)
                else:
                    msg=self.tr("Run not executed")
                    msg='%s : %s' % (errMsg,msg)
                    QMessageBox.information(None, "Run", msg)
                    self.luci()

            else:

                QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))

        else:
            msg=self.tr("Run not executed")
            QMessageBox.information(None, "Run", msg)
            self.luci()

    def VediTabellaRischio(self):
        self.NomeTabella=str(self.txtShellFilePath_7.text())
        self.TabView = TableViewer(self.iface,self.NomeTabella)
        self.TabView.show()# show the dialog
        result = self.TabView.exec_()

    def writexml (self):
        fileprogetto=str(self.txtShellFilePath.text())
        dicProgetto={}
        dicParameter={}
        if fileprogetto!="":
            xmlfile=open(fileprogetto)
            dom=minidom.parse(xmlfile)
            for node in dom.getElementsByTagName("General"):
                L = node.getElementsByTagName("File")
                for node2 in L:
                    Button = node2.getAttribute("Button")
                    nome = node2.getAttribute("name")
                    dicProgetto[Button] = nome
            for node in dom.getElementsByTagName("Parameters"):
                L = node.getElementsByTagName("Parameter")
                for node2 in L:
                    Param = node2.getAttribute("Param")
                    Value = node2.getAttribute("Value")
                    dicParameter[Param] = Value

            xmlfile.close()
        # Create the minidom document
        doc = Document()

        # Create the <wml> base element
        wml = doc.createElement("FloodRisk")
        doc.appendChild(wml)

        # Create the main <card> element
        maincard = doc.createElement("General")
        wml.appendChild(maincard)

        # Create a <p> element
        ShellFilePath= str(self.txtShellFilePath_2.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FileGeodatabase')
        paragraph1.setAttribute("name", ShellFilePath)
        maincard.appendChild(paragraph1)

        ShellFilePath= str(self.txtShellFilePath_3.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FilePeakFloodDepth')
        paragraph1.setAttribute("name", ShellFilePath)
        #paragraph1.setAttribute("unit", '[m]')
        maincard.appendChild(paragraph1)

        # Create a <p> element
        ShellFilePath= str(self.txtShellFilePath_velocita.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FilePeakFloodVelocity')
        paragraph1.setAttribute("name", ShellFilePath)
        #paragraph1.setAttribute("unit", '[m]')
        maincard.appendChild(paragraph1)

        # Create a <p> element
        ShellFilePath= str(self.txtShellFilePath_tempi.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FileWarningTime')
        paragraph1.setAttribute("name", ShellFilePath)
        #paragraph1.setAttribute("unit", '[m]')
        maincard.appendChild(paragraph1)

        # Save input files
        for Button in dicProgetto.keys():
            nome = dicProgetto[Button]
            if Button == 'FileGridDamages':
                paragraph1 = doc.createElement("File")
                paragraph1.setAttribute("Button", Button)
                paragraph1.setAttribute("name", nome)
                #paragraph1.setAttribute("unit", '[m]')
                maincard.appendChild(paragraph1)
            if Button == 'FileGridVulnerability':
                paragraph1 = doc.createElement("File")
                paragraph1.setAttribute("Button", Button)
                paragraph1.setAttribute("name", nome)
                #paragraph1.setAttribute("unit", '[m]')
                maincard.appendChild(paragraph1)
            if Button == 'FileTable1':
                paragraph1 = doc.createElement("File")
                paragraph1.setAttribute("Button", Button)
                paragraph1.setAttribute("name", nome)
                #paragraph1.setAttribute("unit", '[m]')
                maincard.appendChild(paragraph1)

        # Create a <p> element
        ShellFilePath= str(self.txtShellFilePath_4.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FileGridPopRisk')
        paragraph1.setAttribute("name", ShellFilePath)
        maincard.appendChild(paragraph1)

        # Create a <p> element
        ShellFilePath= str(self.txtShellFilePath_7.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FileTable2')
        paragraph1.setAttribute("name", ShellFilePath)
        maincard.appendChild(paragraph1)

        # Create the main <card> element
        maincard2 = doc.createElement("Parameters")
        wml.appendChild(maincard2)

        for Param in dicParameter.keys():
            Value = dicParameter[Param]
            if Param == 'CurveType':
                Param2 = doc.createElement("Parameter")
                Param2.setAttribute("Param", 'CurveType')
                Param2.setAttribute("Value", Value)
                maincard2.appendChild(Param2)
            if Param == 'TotalDamage':
                Param2 = doc.createElement("Parameter")
                Param2.setAttribute("Param", 'TotalDamage')
                Param2.setAttribute("Value", Value)
                maincard2.appendChild(Param2)

        Param2 = doc.createElement("Parameter")
        Param2.setAttribute("Param", 'Method')
        Param2.setAttribute("Value", str(self.paramMethod))
        maincard2.appendChild(Param2)

        Param2 = doc.createElement("Parameter")
        Param2.setAttribute("Param", 'Understand')
        Param2.setAttribute("Value", self.paramUnderstanding)
        maincard2.appendChild(Param2)

        Param3 = doc.createElement("Parameter")
        Param3.setAttribute("Param", 'LOL')
        msg='%4f' % self.paramLOL_notRound
        Param3.setAttribute("Value", msg)
        maincard2.appendChild(Param3)


        if fileprogetto!="":
            fp = open(fileprogetto,"w")
            # writexml(self, writer, indent='', addindent='', newl='', encoding=None)
            doc.writexml(fp, "", "   ", "\n", "UTF-8")
            self.AutoLoad=fileprogetto
            QMessageBox.information(None, "Info", self.tr("Project Saved"))

    def CaricaLayers(self):
        filePath=str(self.txtShellFilePath_2.text())
        if os.path.exists(filePath):
            # case geodatabase
            tabelle=['StructurePoly','InfrastrLines','CensusBlocks']
            for nomelayer in tabelle:
                # checks if the layer is already loaded
                if not LayerCaricato(self,nomelayer):
                    openFile(self,filePath,nomelayer)

        filePath=str(self.txtShellFilePath_3.text())
        if os.path.exists(filePath):
            if not LayerCaricato(self,filePath):
                openFile(self,filePath,'')

        filePath=str(self.txtShellFilePath_velocita.text())
        if os.path.exists(filePath):
            if not LayerCaricato(self,filePath):
                openFile(self,filePath,'')

        filePath=str(self.txtShellFilePath_tempi.text())
        if os.path.exists(filePath):
            if not LayerCaricato(self,filePath):
                openFile(self,filePath,'')

        filePath=str(self.txtShellFilePath_4.text())
        if os.path.exists(filePath):
            if not LayerCaricato(self,filePath):
                openFile(self,filePath,'')

    def istogrammi(self):
        self.NomeFile=str(self.txtShellFilePath_7.text())
        if os.path.exists(self.NomeFile):
            filcsv=open(self.NomeFile,'r')
            riga=filcsv.readline()
            testo=riga[:-1]
            pp=str.split(testo,';')
            self.fields=[]
            yPopTotRischio=[]
            yPerViteUman=[]
            xTiranteIdrico=[]
            for p in pp:
                self.fields.append(p)
            self.data = []
            for i in range(len(self.fields)):
                self.data += [[]]
            steps=0
            for rec in filcsv:
                if len(rec)>0:
                    steps=steps+1
            #steps = self.provider.featureCount()
            stepp = steps / 10
            if stepp == 0:
                stepp = 1
            progress = unicode('Reading data ') # As a progress bar is used the main window's status bar, because the own one is not initialized yet
            filcsv.close()
            filcsv=open(self.NomeFile,'r')
            riga=filcsv.readline()
            n = 0
            self.numRows=0
            for kk in range(steps):
                riga=filcsv.readline()[:-1]
                attrs=str.split(riga,';')
                for i in range(len(attrs)):
                    self.data[i] += [attrs[i]]
                    if i == 0:
                        xTiranteIdrico += [attrs[i]]
                    if i == 4:
                        yPopTotRischio += [int(attrs[i])]
                    if i == 5:
                        yPerViteUman += [int(attrs[i])]

                n += 1
                self.numRows+=1

        #---------------Draw Chart-----------------
        y1=yPerViteUman
        y2=yPopTotRischio
        x1=xTiranteIdrico
        width=0.3  # bar width
        i=arange(len(y1))
        r1=bar(i, y1,width, color='r',linewidth=1)
        r2=bar(i+width,y2,width,color='b',linewidth=1)
        xticks(i+width/2,x1)
        xlabel(self.tr('Range water depth (m)')); ylabel(self.tr('Number people')); title(self.tr('Consequences for the population'))
        legend((r1[0],r2[0]),(self.tr('Loss of Life'), self.tr('Total Polpulation at Risk')),'best')
        grid()
        show()

    def setUnderstandingDam(self, enabled):
        if enabled:
            self.paramMethod=0
            FileGDB = str(self.txtShellFilePath_2.text())
            if FileGDB != "":
                if self.CheckGeodatabase():
                    conn = sqlite3.connect(FileGDB)
                    cursor = conn.cursor()
                    testoQuery='SELECT Understanding FROM FatalityRate WHERE FRType=0 GROUP BY Understanding'
                    cursor.execute(testoQuery)
                    ListaDam = cursor.fetchall()
                    if len(ListaDam)>0:
                        Lista=[]
                        for i in range(len(ListaDam)):
                            Lista.append(str(ListaDam[i][0]))
                        self.comboBox.clear()
                        self.comboBox.addItems(Lista)
                    else:
                        self.comboBox.clear()
                        msg0='Geodatabse %s' % FileGDB
                        msg1=self.tr('error table FatalityRate is empty')
                        msg='%s : %s' % (msg0,msg1)
                        QMessageBox.information(None, "FloodRisk", msg)
                else:
                    msg0='Geodatabse %s' % FileGDB
                    msg1=self.tr('error in table FatalityRate')
                    msg='%s : %s' % (msg0,msg1)
                    QMessageBox.information(None, "FloodRisk", msg)
            else:
                QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))

    def setUnderstandingSufri(self, enabled):
        if enabled:
            self.paramMethod=1
            FileGDB = str(self.txtShellFilePath_2.text())
            if FileGDB != "":
                if self.CheckGeodatabase():
                    conn = sqlite3.connect(FileGDB)
                    cursor = conn.cursor()
                    testoQuery='SELECT Understanding FROM FatalityRate WHERE FRType=1 GROUP BY Understanding'
                    cursor.execute(testoQuery)
                    ListaDam = cursor.fetchall()
                    if len(ListaDam)>0:
                        Lista=[]
                        for i in range(len(ListaDam)):
                            Lista.append(str(ListaDam[i][0]))
                        self.comboBox.clear()
                        self.comboBox.addItems(Lista)
                    else:
                        self.comboBox.clear()
                        msg0='Geodatabse %s' % FileGDB
                        msg1=self.tr('error table FatalityRate is empty')
                        msg='%s : %s' % (msg0,msg1)
                        QMessageBox.information(None, "FloodRisk", msg)
                else:
                    msg0='Geodatabse %s' % FileGDB
                    msg1=self.tr('error in table FatalityRate')
                    msg='%s : %s' % (msg0,msg1)
                    QMessageBox.information(None, "FloodRisk", msg)
            else:
                QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))


    def setCurrentUnderstanding(self,ItemText):
        # set currentItem paramUnderstanding
        NumType=self.comboBox.count()
        AllItems = [self.comboBox.itemText(i) for i in range(NumType)]
        if NumType>0:
            index=-1
            for ii in range(NumType):
                if ItemText==AllItems[ii]:
                    index=ii
            if index>=0:
                self.comboBox.setCurrentIndex(index)
                self.paramUnderstanding=ItemText

    #------------------- Functions -------------------
    def luci(self):
        #FileGridPopRisk
        FilePath= str(self.txtShellFilePath_4.text())
        if os.path.exists(FilePath):
            self.label_red_pop.hide()
            self.label_green_pop.show()
        else:
            self.label_red_pop.show()
            self.label_green_pop.hide()

    def preSetUnderstandingDam(self):

            self.radioButton.toggle()
            FileGDB = str(self.txtShellFilePath_2.text())
            if FileGDB != "":
                if self.CheckGeodatabase():
                    conn = sqlite3.connect(FileGDB)
                    cursor = conn.cursor()
                    testoQuery='SELECT Understanding FROM FatalityRate WHERE FRType=0 GROUP BY Understanding'
                    cursor.execute(testoQuery)
                    ListaDam = cursor.fetchall()
                    Lista=[]
                    for i in range(len(ListaDam)):
                        Lista.append(str(ListaDam[i][0]))
                    self.comboBox.clear()
                    self.comboBox.addItems(Lista)
                    self.paramMethod=0
                else:
                    msg0='Geodatabse %s' % FileGDB
                    msg1=self.tr('error in table FatalityRate')
                    msg='%s : %s' % (msg0,msg1)
                    QMessageBox.information(None, "FloodRisk", msg)
            else:
                QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))

    def preSetUnderstandingSufri(self):
            self.radioButton_2.toggle()
            FileGDB = str(self.txtShellFilePath_2.text())
            if FileGDB != "":
                if self.CheckGeodatabase():
                    conn = sqlite3.connect(FileGDB)
                    cursor = conn.cursor()
                    testoQuery='SELECT Understanding FROM FatalityRate WHERE FRType=1 GROUP BY Understanding'
                    cursor.execute(testoQuery)
                    ListaDam = cursor.fetchall()
                    Lista=[]
                    for i in range(len(ListaDam)):
                        Lista.append(str(ListaDam[i][0]))
                    self.comboBox.clear()
                    self.comboBox.addItems(Lista)
                else:
                    msg0='Geodatabse %s' % FileGDB
                    msg1=self.tr('error in table FatalityRate')
                    msg='%s : %s' % (msg0,msg1)
                    QMessageBox.information(None, "FloodRisk", msg)
            else:
                QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))

    def CheckGeodatabase(self):
        res=bool()
        if os.path.exists(self.txtShellFilePath_2.text()):
            mydb_path=self.txtShellFilePath_2.text()
            try:
                # connecting the db
                conn = sqlite3.connect(mydb_path)
                # creating a Cursor
                cur = conn.cursor()

                TablesList=['spatial_ref_sys','AnalysisArea','CensusBlocks','FatalityRate']
                TablesList.append('FatalityRate')
                TablesList.append('FloodSeverity')
                TablesList.append('InfrastrLines')
                TablesList.append('VulnType')
                TablesList.append('Vulnerability')
                TablesList.append('StructurePoly')

                for NomeTabella in TablesList:
                    sql="SELECT sql FROM sqlite_master WHERE type='table' AND name='%s';" % (NomeTabella)
                    cur.execute(sql)
                    Tabella=str(cur.fetchone()[0])

                res=bool('True')
            except:
                res=bool()
        else:
            res=bool()
        return res

    def CheckFloodSeverity(self):
        res=bool()
        FileGDB = str(self.txtShellFilePath_2.text())
        if FileGDB != "":
            if self.CheckGeodatabase():
                try:
                    conn = sqlite3.connect(FileGDB)
                    cursor = conn.cursor()
                    fieldOrder='Num'
                    NomeTabella='FloodSeverity'
                    testoQuery=" SELECT * FROM %s ORDER BY %s;" %(NomeTabella,fieldOrder)
                    cursor.execute(testoQuery)
                    Lista = cursor.fetchall()
                    if len(Lista)>0:
                        res=bool('True')
                    else:
                        msg0='Geodatabse %s' % FileGDB
                        msg1=self.tr('error table FloodSeverity is empty')
                        msg='%s : %s' % (msg0,msg1)
                        QMessageBox.information(None, "FloodRisk", msg)

                except:
                    msg0='Geodatabse %s' % FileGDB
                    msg1=self.tr('error in table FloodSeverity')
                    msg='%s : %s' % (msg0,msg1)
                    QMessageBox.information(None, "FloodRisk", msg)

            else:
                QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))
        else:
            QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))

        return res

    def CheckFatalityRate(self):
        res=bool()
        FileGDB = str(self.txtShellFilePath_2.text())
        if FileGDB != "":
            if self.CheckGeodatabase():
                try:
                    conn = sqlite3.connect(FileGDB)
                    cursor = conn.cursor()
                    fieldOrder='Num'
                    NomeTabella='FatalityRate'
                    testoQuery=" SELECT * FROM %s ORDER BY %s;" %(NomeTabella,fieldOrder)
                    cursor.execute(testoQuery)
                    Lista = cursor.fetchall()
                    if len(Lista)>0:
                        res=bool('True')
                    else:
                        msg0='Geodatabse %s' % FileGDB
                        msg1=self.tr('error table FatalityRate is empty')
                        msg='%s : %s' % (msg0,msg1)
                        QMessageBox.information(None, "FloodRisk", msg)

                except:
                    msg0='Geodatabse %s' % FileGDB
                    msg1=self.tr('error in table FatalityRate')
                    msg='%s : %s' % (msg0,msg1)
                    QMessageBox.information(None, "FloodRisk", msg)

            else:
                QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))
        else:
            QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))

        return res
Beispiel #6
0
class calcolorischiopopolazioneDialog(QtGui.QDialog, Ui_FloodRisk):
    def __init__(self,iface):
        QtGui.QDialog.__init__(self)
        self.setupUi(self)
        self.iface=iface

        self.btnChooseShellFile_3.setIcon(QIcon(":/plugins/floodrisk/icons/folder_explore.png"))
        self.btnChooseShellFile_3.setIconSize(QSize(25,25))
        self.btnChooseShellFile_velocita.setIcon(QIcon(":/plugins/floodrisk/icons/folder_explore.png"))
        self.btnChooseShellFile_velocita.setIconSize(QSize(25,25))
        self.btnChooseShellFile_tempi.setIcon(QIcon(":/plugins/floodrisk/icons/folder_explore.png"))
        self.btnChooseShellFile_tempi.setIconSize(QSize(25,25))
        self.pushButtonView_2.setIcon(QIcon(":/plugins/floodrisk/icons/table_go.png"))
        self.pushButtonView_2.setIconSize(QSize(25,25))
        self.pushButtonIstogrammi.setIcon(QIcon(":/plugins/floodrisk/icons/chart_bar.png"))
        self.pushButtonIstogrammi.setIconSize(QSize(25,25))
        self.label_red_pop.setPixmap(QPixmap(":/plugins/floodrisk/icons/red20.png"))
        self.label_green_pop.setPixmap(QPixmap(":/plugins/floodrisk/icons/green20.png"))

        # initialize actions
        QObject.connect(self.btnChooseShellFile_3, SIGNAL("clicked()"), self.setFileMaxH)
        QObject.connect(self.btnChooseShellFile_velocita, SIGNAL("clicked()"), self.setFileMaxV)
        QObject.connect(self.btnChooseShellFile_tempi, SIGNAL("clicked()"), self.setFileTime)
        QObject.connect(self.toolButtonEsegui_2, SIGNAL("clicked()"), self.EseguiCalcoloPopolazione)
        QObject.connect(self.pushButtonView_2, SIGNAL("clicked()"), self.VediTabellaRischio)
        QObject.connect(self.pushButtonSalvaProgetto, SIGNAL("clicked()"), self.writexml)
        QObject.connect(self.pushButtonLoadLayer, SIGNAL("clicked()"), self.CaricaLayers)
        QObject.connect(self.pushButtonIstogrammi, SIGNAL("clicked()"), self.istogrammi)
        self.radioButton.toggled.connect(self.setUnderstandingDam)
        self.radioButton_2.toggled.connect(self.setUnderstandingSufri)

        # help
        QObject.connect(self.buttonBox, SIGNAL(_fromUtf8("helpRequested()")), self.show_help)

        self.paramMethod=''
        self.paramUnderstanding=''
        self.paramLOL_notRound=0.0

    #------------- Actions -----------------------

    def show_help(self):
        """Load the help text into the system browser."""
        show_context_help(context='include4')


    def setFileMaxV(self):
        message = QtGui.QMessageBox.question(self, self.tr('Attention'),self.tr("Warning you are editing the data input to the project: current data" \
        " of output will be deleted. Are you sure?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
        if message == QtGui.QMessageBox.Yes:
            FileMaxVPath = QFileDialog.getOpenFileName(self, "Select file max water velocity", \
            '.', 'File tif (*.tif);;All files (*.*)')
            self.txtShellFilePath_velocita.setText(FileMaxVPath)
            #----Deleting output data-----
            self.txtShellFilePath_4.setText("")
            self.txtShellFilePath_7.setText("")

    def setFileTime(self):
        message = QtGui.QMessageBox.question(self, self.tr('Attention'),self.tr("Warning you are editing the data input to the project: current data" \
        " of output will be deleted. Are you sure?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
        if message == QtGui.QMessageBox.Yes:
            FileTimePath = QFileDialog.getOpenFileName(self, self.tr("Select warning time shape file"), \
            '.', 'File shp (*.shp);;All files (*.*)')
            self.txtShellFilePath_tempi.setText(FileTimePath)
            #----Deleting output data-----
            self.txtShellFilePath_4.setText("")
            self.txtShellFilePath_7.setText("")

    def setFileMaxH(self):
        message = QtGui.QMessageBox.question(self, self.tr('Attention'),self.tr("Warning you are editing the data input to the project: current data" \
        " of output will be deleted. Are you sure?"), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
        if message == QtGui.QMessageBox.Yes:
            FileMaxHPath = QFileDialog.getOpenFileName(self, self.tr('Select peak flow depth file'), \
            '.', 'File tif (*.tif);;All files (*.*)')
            self.txtShellFilePath_3.setText(FileMaxHPath)
            msg=FileMaxHPath
            QMessageBox.information(None, "FileMaxHPath", msg)
            msg=str(self.txtShellFilePath.text())
            if msg=="":
                aa=str(FileMaxHPath)
                DirOut=os.path.dirname(aa)
                Name=os.path.basename(aa)
                pp=str.split(Name,'.')
                FileProgettoPath=DirOut+os.sep + pp[0]+'.dmg'
                self.txtShellFilePath.setText(FileProgettoPath)

            msg=str(self.txtShellFilePath_4.text())
            if msg=="":
                aa=str(FileMaxHPath)
                DirOut=os.path.dirname(aa)
                Name=os.path.basename(aa)
                pp=str.split(Name,'.')
                FileGridPopPath=DirOut+os.sep + pp[0]+'_pop.tif'
                self.txtShellFilePath_4.setText(FileGridPopPath)

            msg=str(self.txtShellFilePath_7.text())
            if msg=="":
                aa=str(FileMaxHPath)
                DirOut=os.path.dirname(aa)
                Name=os.path.basename(aa)
                pp=str.split(Name,'.')
                FileTab2DannoPath=DirOut+os.sep + pp[0]+'_pop.csv'
                self.txtShellFilePath_7.setText(FileTab2DannoPath)

            #----Deleting output data-----
            self.txtShellFilePath_4.setText("")
            self.txtShellFilePath_7.setText("")
            abil=bool("true")
            self.pushButtonSalvaProgetto.setEnabled(abil)
            #self.luci()

    def EseguiCalcoloPopolazione(self):

        # ------------------------------------------------------
        # performs the calculation of the risk to the population
        # ------------------------------------------------------
        self.Nome=[]
        self.listafiles=[]
        # FileDEM1
        self.Nome.append('File Max Water Depth')
        self.listafiles.append(str(self.txtShellFilePath_3.text()))
        # file Water Velocity
        self.Nome.append('File Water Velocity')
        self.listafiles.append(str(self.txtShellFilePath_velocita.text()))
        # file Warning Time
        self.Nome.append('File Warning Time')
        self.listafiles.append(str(self.txtShellFilePath_tempi.text()))
        # DBfile
        self.Nome.append('File Geodatabase')
        self.listafiles.append(str(self.txtShellFilePath_2.text()))
        # NameFileGridPop
        self.Nome.append('File Grid population at risk')
        self.listafiles.append(str(self.txtShellFilePath_4.text()))
        # NameFileTable2
        self.Nome.append('File Table 2')
        self.listafiles.append(str(self.txtShellFilePath_7.text()))

        parametroUnderstanding = self.comboBox.currentText()
        self.paramUnderstanding=parametroUnderstanding
        self.listafiles.append(parametroUnderstanding)

        abil=bool("true")
        for i in range(4):
            if not os.path.exists(self.listafiles[i]):
                msg1=self.tr('Error the file')
                msg2=self.tr('does not exist')
                msg='%s %s: %s ' % (msg1,self.Nome[i],msg2)
                QMessageBox.information(None, "Fine input", msg)
                abil=bool()
        for k in range(2):
            i=k+4
            if len(self.listafiles[i])==0:
                txt1=self.tr('Attention assign a name to file')
                msg='%s: %s ' % (txt1,self.Nome[i])
                QMessageBox.information(None, "Fine output", msg)
                abil=bool()
        if not self.CheckFloodSeverity():
            abil=bool()

        if not self.CheckFatalityRate():
            abil=bool()

        if abil:
            fileprogetto=str(self.txtShellFilePath.text())

            if self.CheckGeodatabase():

                # initializes progressbar
                self.progressBar.setFormat(self.tr('Population consequences assessment') +': %p%')

                self.progressBar.setValue(0)
                abil=bool()
                self.buttonBox.setEnabled(abil)

                NotErr, errMsg, NumLoss_notRound = AssessConsequencesPopulation.main(self.listafiles,self.progressBar)

                self.paramLOL_notRound=NumLoss_notRound

                self.luci()

                if NotErr:
                    msg=self.tr('End of Job')
                    QMessageBox.information(None, "FloodRisk", msg)
                    self.writexml()

                    abil=bool('True')
                    self.buttonBox.setEnabled(abil)
                    self.progressBar.setFormat(('%p%'))
                    self.progressBar.setValue(0)
                else:
                    msg=self.tr("Run not executed")
                    msg='%s : %s' % (errMsg,msg)
                    QMessageBox.information(None, "Run", msg)
                    self.luci()

            else:

                QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))

        else:
            msg=self.tr("Run not executed")
            QMessageBox.information(None, "Run", msg)
            self.luci()

    def VediTabellaRischio(self):
        self.NomeTabella=str(self.txtShellFilePath_7.text())
        self.TabView = TableViewer(self.iface,self.NomeTabella)
        self.TabView.show()# show the dialog
        result = self.TabView.exec_()

    def writexml (self):
        fileprogetto=str(self.txtShellFilePath.text())
        dicProgetto={}
        dicParameter={}
        if fileprogetto!="":
            xmlfile=open(fileprogetto)
            dom=minidom.parse(xmlfile)
            for node in dom.getElementsByTagName("General"):
                L = node.getElementsByTagName("File")
                for node2 in L:
                    Button = node2.getAttribute("Button")
                    nome = node2.getAttribute("name")
                    dicProgetto[Button] = nome
            for node in dom.getElementsByTagName("Parameters"):
                L = node.getElementsByTagName("Parameter")
                for node2 in L:
                    Param = node2.getAttribute("Param")
                    Value = node2.getAttribute("Value")
                    dicParameter[Param] = Value

            xmlfile.close()
        # Create the minidom document
        doc = Document()

        # Create the <wml> base element
        wml = doc.createElement("FloodRisk")
        doc.appendChild(wml)

        # Create the main <card> element
        maincard = doc.createElement("General")
        wml.appendChild(maincard)

        # Create a <p> element
        ShellFilePath= str(self.txtShellFilePath_2.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FileGeodatabase')
        paragraph1.setAttribute("name", ShellFilePath)
        maincard.appendChild(paragraph1)

        ShellFilePath= str(self.txtShellFilePath_3.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FilePeakFloodDepth')
        paragraph1.setAttribute("name", ShellFilePath)
        #paragraph1.setAttribute("unit", '[m]')
        maincard.appendChild(paragraph1)

        # Create a <p> element
        ShellFilePath= str(self.txtShellFilePath_velocita.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FilePeakFloodVelocity')
        paragraph1.setAttribute("name", ShellFilePath)
        #paragraph1.setAttribute("unit", '[m]')
        maincard.appendChild(paragraph1)

        # Create a <p> element
        ShellFilePath= str(self.txtShellFilePath_tempi.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FileWarningTime')
        paragraph1.setAttribute("name", ShellFilePath)
        #paragraph1.setAttribute("unit", '[m]')
        maincard.appendChild(paragraph1)

        # Save input files
        for Button in dicProgetto.keys():
            nome = dicProgetto[Button]
            if Button == 'FileGridDamages':
                paragraph1 = doc.createElement("File")
                paragraph1.setAttribute("Button", Button)
                paragraph1.setAttribute("name", nome)
                #paragraph1.setAttribute("unit", '[m]')
                maincard.appendChild(paragraph1)
            if Button == 'FileGridVulnerability':
                paragraph1 = doc.createElement("File")
                paragraph1.setAttribute("Button", Button)
                paragraph1.setAttribute("name", nome)
                #paragraph1.setAttribute("unit", '[m]')
                maincard.appendChild(paragraph1)
            if Button == 'FileTable1':
                paragraph1 = doc.createElement("File")
                paragraph1.setAttribute("Button", Button)
                paragraph1.setAttribute("name", nome)
                #paragraph1.setAttribute("unit", '[m]')
                maincard.appendChild(paragraph1)

        # Create a <p> element
        ShellFilePath= str(self.txtShellFilePath_4.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FileGridPopRisk')
        paragraph1.setAttribute("name", ShellFilePath)
        maincard.appendChild(paragraph1)

        # Create a <p> element
        ShellFilePath= str(self.txtShellFilePath_7.text())
        paragraph1 = doc.createElement("File")
        paragraph1.setAttribute("Button", 'FileTable2')
        paragraph1.setAttribute("name", ShellFilePath)
        maincard.appendChild(paragraph1)

        # Create the main <card> element
        maincard2 = doc.createElement("Parameters")
        wml.appendChild(maincard2)

        for Param in dicParameter.keys():
            Value = dicParameter[Param]
            if Param == 'CurveType':
                Param2 = doc.createElement("Parameter")
                Param2.setAttribute("Param", 'CurveType')
                Param2.setAttribute("Value", Value)
                maincard2.appendChild(Param2)
            if Param == 'TotalDamage':
                Param2 = doc.createElement("Parameter")
                Param2.setAttribute("Param", 'TotalDamage')
                Param2.setAttribute("Value", Value)
                maincard2.appendChild(Param2)

        Param2 = doc.createElement("Parameter")
        Param2.setAttribute("Param", 'Method')
        Param2.setAttribute("Value", str(self.paramMethod))
        maincard2.appendChild(Param2)

        Param2 = doc.createElement("Parameter")
        Param2.setAttribute("Param", 'Understand')
        Param2.setAttribute("Value", self.paramUnderstanding)
        maincard2.appendChild(Param2)

        Param3 = doc.createElement("Parameter")
        Param3.setAttribute("Param", 'LOL')
        msg='%4f' % self.paramLOL_notRound
        Param3.setAttribute("Value", msg)
        maincard2.appendChild(Param3)


        if fileprogetto!="":
            fp = open(fileprogetto,"w")
            # writexml(self, writer, indent='', addindent='', newl='', encoding=None)
            doc.writexml(fp, "", "   ", "\n", "UTF-8")
            self.AutoLoad=fileprogetto
            QMessageBox.information(None, "Info", self.tr("Project Saved"))

    def CaricaLayers(self):
        filePath=str(self.txtShellFilePath_2.text())
        if os.path.exists(filePath):
            # case geodatabase
            tabelle=['StructurePoly','InfrastrLines','CensusBlocks']
            for nomelayer in tabelle:
                # checks if the layer is already loaded
                if not LayerCaricato(self,nomelayer):
                    openFile(self,filePath,nomelayer)

        filePath=str(self.txtShellFilePath_3.text())
        if os.path.exists(filePath):
            if not LayerCaricato(self,filePath):
                openFile(self,filePath,'')

        filePath=str(self.txtShellFilePath_velocita.text())
        if os.path.exists(filePath):
            if not LayerCaricato(self,filePath):
                openFile(self,filePath,'')

        filePath=str(self.txtShellFilePath_tempi.text())
        if os.path.exists(filePath):
            if not LayerCaricato(self,filePath):
                openFile(self,filePath,'')

        filePath=str(self.txtShellFilePath_4.text())
        if os.path.exists(filePath):
            if not LayerCaricato(self,filePath):
                openFile(self,filePath,'')

    def istogrammi(self):
        self.NomeFile=str(self.txtShellFilePath_7.text())
        if os.path.exists(self.NomeFile):
            try:
                import matplotlib

                self.sep=check_csv_separator(self.NomeFile)
               # Reading csv file
                finp = open(self.NomeFile)
                csv_reader = csv.reader(finp, delimiter=self.sep, quotechar='"')
                headers = csv_reader.next()

                self.fields=[]
                for p in headers:
                    self.fields.append(p)

                progress = unicode('Reading data ') # As a progress bar is used the main window's status bar, because the own one is not initialized yet

                yPopTotRischio=[]
                yPerViteUman=[]
                xTiranteIdrico=[]
                for record in csv_reader:
                    for i in range(len(record)):

                        if i == 0:
                            xTiranteIdrico += [record[i]]
                        if i == 4:
                            yPopTotRischio += [int(record[i])]
                        if i == 5:
                            yPerViteUman += [int(record[i])]

                finp.close()

                #---------------Draw Chart-----------------
                y1=yPerViteUman
                y2=yPopTotRischio
                x1=xTiranteIdrico
                width=0.3  # bar width
                i=arange(len(y1))
                r1=bar(i, y1,width, color='r',linewidth=1)
                r2=bar(i+width,y2,width,color='b',linewidth=1)
                xticks(i+width/2,x1)
                xlabel(self.tr('Range water depth (m)')); ylabel(self.tr('Number people')); title(self.tr('Consequences for the population'))
                try:
                    legend((r1[0],r2[0]),(self.tr('Loss of Life'), self.tr('Total Polpulation at Risk')),'best')
                except:
                    pass
                grid()
                show()
            except:
                QMessageBox.information(None, "Warning", "The current version of QGIS does not allow import matplotlib")

    def setUnderstandingDam(self, enabled):
        if enabled:
            self.paramMethod=0
            FileGDB = str(self.txtShellFilePath_2.text())
            if FileGDB != "":
                if self.CheckGeodatabase():
                    conn = sqlite3.connect(FileGDB)
                    cursor = conn.cursor()
                    testoQuery='SELECT Understanding FROM FatalityRate WHERE FRType=0 GROUP BY Understanding'
                    cursor.execute(testoQuery)
                    ListaDam = cursor.fetchall()
                    if len(ListaDam)>0:
                        Lista=[]
                        for i in range(len(ListaDam)):
                            Lista.append(str(ListaDam[i][0]))
                        self.comboBox.clear()
                        self.comboBox.addItems(Lista)
                    else:
                        self.comboBox.clear()
                        msg0='Geodatabse %s' % FileGDB
                        msg1=self.tr('error table FatalityRate is empty')
                        msg='%s : %s' % (msg0,msg1)
                        QMessageBox.information(None, "FloodRisk", msg)
                else:
                    msg0='Geodatabse %s' % FileGDB
                    msg1=self.tr('error in table FatalityRate')
                    msg='%s : %s' % (msg0,msg1)
                    QMessageBox.information(None, "FloodRisk", msg)
            else:
                QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))

    def setUnderstandingSufri(self, enabled):
        if enabled:
            self.paramMethod=1
            FileGDB = str(self.txtShellFilePath_2.text())
            if FileGDB != "":
                if self.CheckGeodatabase():
                    conn = sqlite3.connect(FileGDB)
                    cursor = conn.cursor()
                    testoQuery='SELECT Understanding FROM FatalityRate WHERE FRType=1 GROUP BY Understanding'
                    cursor.execute(testoQuery)
                    ListaDam = cursor.fetchall()
                    if len(ListaDam)>0:
                        Lista=[]
                        for i in range(len(ListaDam)):
                            Lista.append(str(ListaDam[i][0]))
                        self.comboBox.clear()
                        self.comboBox.addItems(Lista)
                    else:
                        self.comboBox.clear()
                        msg0='Geodatabse %s' % FileGDB
                        msg1=self.tr('error table FatalityRate is empty')
                        msg='%s : %s' % (msg0,msg1)
                        QMessageBox.information(None, "FloodRisk", msg)
                else:
                    msg0='Geodatabse %s' % FileGDB
                    msg1=self.tr('error in table FatalityRate')
                    msg='%s : %s' % (msg0,msg1)
                    QMessageBox.information(None, "FloodRisk", msg)
            else:
                QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))


    def setCurrentUnderstanding(self,ItemText):
        # set currentItem paramUnderstanding
        NumType=self.comboBox.count()
        AllItems = [self.comboBox.itemText(i) for i in range(NumType)]
        if NumType>0:
            index=-1
            for ii in range(NumType):
                if ItemText==AllItems[ii]:
                    index=ii
            if index>=0:
                self.comboBox.setCurrentIndex(index)
                self.paramUnderstanding=ItemText

    #------------------- Functions -------------------
    def luci(self):
        #FileGridPopRisk
        FilePath= str(self.txtShellFilePath_4.text())
        if os.path.exists(FilePath):
            self.label_red_pop.hide()
            self.label_green_pop.show()
        else:
            self.label_red_pop.show()
            self.label_green_pop.hide()

    def preSetUnderstandingDam(self):

            self.radioButton.toggle()
            FileGDB = str(self.txtShellFilePath_2.text())
            if FileGDB != "":
                if self.CheckGeodatabase():
                    conn = sqlite3.connect(FileGDB)
                    cursor = conn.cursor()
                    testoQuery='SELECT Understanding FROM FatalityRate WHERE FRType=0 GROUP BY Understanding'
                    cursor.execute(testoQuery)
                    ListaDam = cursor.fetchall()
                    Lista=[]
                    for i in range(len(ListaDam)):
                        Lista.append(str(ListaDam[i][0]))
                    self.comboBox.clear()
                    self.comboBox.addItems(Lista)
                    self.paramMethod=0
                else:
                    msg0='Geodatabse %s' % FileGDB
                    msg1=self.tr('error in table FatalityRate')
                    msg='%s : %s' % (msg0,msg1)
                    QMessageBox.information(None, "FloodRisk", msg)
            else:
                QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))

    def preSetUnderstandingSufri(self):
            self.radioButton_2.toggle()
            FileGDB = str(self.txtShellFilePath_2.text())
            if FileGDB != "":
                if self.CheckGeodatabase():
                    conn = sqlite3.connect(FileGDB)
                    cursor = conn.cursor()
                    testoQuery='SELECT Understanding FROM FatalityRate WHERE FRType=1 GROUP BY Understanding'
                    cursor.execute(testoQuery)
                    ListaDam = cursor.fetchall()
                    Lista=[]
                    for i in range(len(ListaDam)):
                        Lista.append(str(ListaDam[i][0]))
                    self.comboBox.clear()
                    self.comboBox.addItems(Lista)
                else:
                    msg0='Geodatabse %s' % FileGDB
                    msg1=self.tr('error in table FatalityRate')
                    msg='%s : %s' % (msg0,msg1)
                    QMessageBox.information(None, "FloodRisk", msg)
            else:
                QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))

    def CheckGeodatabase(self):
        res=bool()
        if os.path.exists(self.txtShellFilePath_2.text()):
            mydb_path=self.txtShellFilePath_2.text()
            try:
                # connecting the db
                conn = sqlite3.connect(mydb_path)
                # creating a Cursor
                cur = conn.cursor()

                TablesList=['spatial_ref_sys','AnalysisArea','CensusBlocks','FatalityRate']
                TablesList.append('FatalityRate')
                TablesList.append('FloodSeverity')
                TablesList.append('InfrastrLines')
                TablesList.append('VulnType')
                TablesList.append('Vulnerability')
                TablesList.append('StructurePoly')

                for NomeTabella in TablesList:
                    sql="SELECT sql FROM sqlite_master WHERE type='table' AND name='%s';" % (NomeTabella)
                    cur.execute(sql)
                    Tabella=str(cur.fetchone()[0])

                res=bool('True')
            except:
                res=bool()
        else:
            res=bool()
        return res

    def CheckFloodSeverity(self):
        res=bool()
        FileGDB = str(self.txtShellFilePath_2.text())
        if FileGDB != "":
            if self.CheckGeodatabase():
                try:
                    conn = sqlite3.connect(FileGDB)
                    cursor = conn.cursor()
                    fieldOrder='Num'
                    NomeTabella='FloodSeverity'
                    testoQuery=" SELECT * FROM %s ORDER BY %s;" %(NomeTabella,fieldOrder)
                    cursor.execute(testoQuery)
                    Lista = cursor.fetchall()
                    if len(Lista)>0:
                        res=bool('True')
                    else:
                        msg0='Geodatabse %s' % FileGDB
                        msg1=self.tr('error table FloodSeverity is empty')
                        msg='%s : %s' % (msg0,msg1)
                        QMessageBox.information(None, "FloodRisk", msg)

                except:
                    msg0='Geodatabse %s' % FileGDB
                    msg1=self.tr('error in table FloodSeverity')
                    msg='%s : %s' % (msg0,msg1)
                    QMessageBox.information(None, "FloodRisk", msg)

            else:
                QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))
        else:
            QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))

        return res

    def CheckFatalityRate(self):
        res=bool()
        FileGDB = str(self.txtShellFilePath_2.text())
        if FileGDB != "":
            if self.CheckGeodatabase():
                try:
                    conn = sqlite3.connect(FileGDB)
                    cursor = conn.cursor()
                    fieldOrder='Num'
                    NomeTabella='FatalityRate'
                    testoQuery=" SELECT * FROM %s ORDER BY %s;" %(NomeTabella,fieldOrder)
                    cursor.execute(testoQuery)
                    Lista = cursor.fetchall()
                    if len(Lista)>0:
                        res=bool('True')
                    else:
                        msg0='Geodatabse %s' % FileGDB
                        msg1=self.tr('error table FatalityRate is empty')
                        msg='%s : %s' % (msg0,msg1)
                        QMessageBox.information(None, "FloodRisk", msg)

                except:
                    msg0='Geodatabse %s' % FileGDB
                    msg1=self.tr('error in table FatalityRate')
                    msg='%s : %s' % (msg0,msg1)
                    QMessageBox.information(None, "FloodRisk", msg)

            else:
                QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))
        else:
            QMessageBox.information(None, "FloodRisk", self.tr("You must first create the Geodb.Sqlite"))

        return res