Esempio n. 1
0
    def run(self):

        self.previewLayer = QgsVectorLayer('Polygon', 'ProMaIDes DEM', 'memory')
        QgsMapLayerRegistry.instance().addMapLayer(self.previewLayer)

        self.demExportDialog = PluginDialog(self.iface, self.iface.mainWindow())
        self.demExportDialog.setModal(False)
        self.demExportDialog.setWindowTitle('DEM Export')

        self.demExportDialog.accepted.connect(self.onDialogAccepted)
        self.demExportDialog.rejected.connect(self.onDialogRejected)
        self.demExportDialog.rasterAdded.connect(self.addRasterBounds)
        self.demExportDialog.rasterUpdated.connect(self.updateRasterBounds)
        self.demExportDialog.rasterRemoved.connect(self.removeRasterBounds)

        self.demExportDialog.show()
Esempio n. 2
0
class DEMExport(PluginBase):

    ILM_TMPL_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'resources', 'template.ilm')

    def __init__(self, tid, toolManager, parent=None):
        PluginBase.__init__(self, tid, toolManager, parent)
        self.demExportDialog = None
        self.previewLayer = None

    def run(self):

        self.previewLayer = QgsVectorLayer('Polygon', 'ProMaIDes DEM', 'memory')
        QgsMapLayerRegistry.instance().addMapLayer(self.previewLayer)

        self.demExportDialog = PluginDialog(self.iface, self.iface.mainWindow())
        self.demExportDialog.setModal(False)
        self.demExportDialog.setWindowTitle('DEM Export')

        self.demExportDialog.accepted.connect(self.onDialogAccepted)
        self.demExportDialog.rejected.connect(self.onDialogRejected)
        self.demExportDialog.rasterAdded.connect(self.addRasterBounds)
        self.demExportDialog.rasterUpdated.connect(self.updateRasterBounds)
        self.demExportDialog.rasterRemoved.connect(self.removeRasterBounds)

        self.demExportDialog.show()

    def interpolate(self, baseLayer, band, outRaster, interpolMode):

        interpol = RasterInterpolator(baseLayer, interpolMode, band)
        for i in range(outRaster.numCells()):

            if self.exportCanceled:
                return

            point = outRaster.cellCenter(i)
            value = interpol.interpolate(QgsPoint(point[0], point[1]))

            if value:
                outRaster.set_cell_elevation(i, value)
            else:
                outRaster.set_cell_elevation(i, outRaster.nodata)

            self.progress.setValue(self.progress.value() + 1)

    def addRasterBounds(self, id, polygon):
        dp = self.previewLayer.dataProvider()
        newPoly = QgsFeature(id)
        newPoly.setGeometry(polygon)
        dp.addFeatures([newPoly])

        self.previewLayer.updateExtents()
        self.previewLayer.triggerRepaint()

    def updateRasterBounds(self, id, polygon):
        self.previewLayer.dataProvider().changeGeometryValues({id: polygon})

        self.previewLayer.updateExtents()
        self.previewLayer.triggerRepaint()

    def removeRasterBounds(self, id):
        self.previewLayer.dataProvider().deleteFeatures([id])

        self.previewLayer.updateExtents()
        self.previewLayer.triggerRepaint()

    def onDialogAccepted(self):
        baseLayer = self.demExportDialog.baseLayer()
        band = self.demExportDialog.band()
        rasters = self.demExportDialog.rasters()  # list of tuples (raster, interpolMode, filename)

        num_cells = 0
        for raster, interpolMode, filename in rasters:
            num_cells += raster.numCells()

        # if there are rasters to interpolate and export
        if num_cells > 0:

            text = ''
            if len(rasters) > 1:
                text = 'Interpolating and exporting %d rasters ...' % len(rasters)
            else:
                text = 'Interpolating and exporting raster ...'

            self.progress = QProgressDialog(text, 'Abort', 0, num_cells, self.iface.mainWindow())
            self.progress.setWindowTitle('DEM Export Progress')
            self.progress.canceled.connect(self.cancelExport)
            self.exportCanceled = False

            self.progress.show()
            self.progress.setValue(1)

            for raster, interpolMode, filename in rasters:
                self.interpolate(baseLayer, band, raster, interpolMode)
                if self.exportCanceled:
                    break

                try:
                    raster.save_as_prm(filename)
                except IOError:
                    QMessageBox.critical(self.iface.mainWindow(), 'I/O Error',
                                         'An I/O error occured during\nraster export to file\n\n%s' % filename)

            self.progress.close()
            self.progress = None

            if self.demExportDialog.createIlmFile():
                standardLocation = QDesktopServices.storageLocation(QDesktopServices.DocumentsLocation)
                filename = QFileDialog.getSaveFileName(self.iface.mainWindow(), 'Save .ilm File', standardLocation)
                if filename != '':
                    try:
                        self.writeIlmFile(filename, rasters)
                    except IOError:
                        QMessageBox.critical(self.iface.mainWindow(), 'I/O Error',
                                             'An I/O error occured during\n.ilm export to file\n\n%s' % filename)

        if self.demExportDialog.removeLayersBox.checkState() == QtCore.Qt.Checked:
            QgsMapLayerRegistry.instance().removeMapLayer(self.previewLayer.id())

        del self.demExportDialog
        del self.previewLayer

        self.finished.emit()

    def cancelExport(self):
        self.exportCanceled = True

    def onDialogRejected(self):
        QgsMapLayerRegistry.instance().removeMapLayer(self.previewLayer.id())

        del self.demExportDialog
        del self.previewLayer

        self.finished.emit()

    def writeIlmFile(self, filename, rasters):
        ilm_tmpl_file = open(self.ILM_TMPL_FILE, 'r')
        ilm_tmpl = ilm_tmpl_file.read()
        ilm_tmpl_file.close()

        ilm = open(filename, 'w+')

        # write general settings to ilm file
        ilm.write(ilm_tmpl.format(len(rasters)))

        counter = 0
        for raster, interpolMode, filename in rasters:
            # CAUTION! The positive rotation direction is defined opposite in ProMaIDEs
            angle = -raster.angle / math.pi * 180.0

            ilm.write('!$BEGINFPMODEL = %d "%s"\n' % (counter, raster.name()))
            ilm.write('!GENERAL= <SET> \n')
            ilm.write('  $NX          = %d\n' % raster.nc)
            ilm.write('  $NY          = %d\n' % raster.nr)
            ilm.write('  $LOWLEFTX    = %f\n' % raster.xll)
            ilm.write('  $LOWLEFTY    = %f\n' % raster.yll)
            ilm.write('  $ELEMWIDTH_X = %f\n' % raster.dc)
            ilm.write('  $ELEMWIDTH_Y = %f\n' % raster.dr)
            ilm.write('  $NOINFOVALUE = %f\n' % raster.nodata)
            ilm.write('  $ANGLE       = %f\n' % angle)
            ilm.write('</SET> \n')

            ilm.write('!2DOUTPUT = "./results"\n')
            ilm.write('!FLOODPLAINFILE = "%s"\n' % filename)

            ilm.write('!LIMITS = <SET>  # numerical limits for 2D simulation\n')
            ilm.write('  $RTOL = 5e-7   # relative tolerances   [optional, standard value = 5e-7]\n')
            ilm.write('  $ATOL = 5e-7   # absolute tolerances   [optional, standard value = 5e-7]\n')
            ilm.write('  $WET  = 0.01   # wet and dry parameter [optional, standard value = 1e-2]\n')
            ilm.write('</SET>\n')
            ilm.write('!$ENDFPMODEL\n\n')

            counter += 1

        ilm.write('!$ENDDESCRIPTION\n')
        ilm.close()