Ejemplo n.º 1
0
    def clipping_thematic_raster(self):
        # first check input files requirements
        if not valid_file_selected_in(self.QCBox_ThematicRaster, "thematic raster"):
            return
        if not valid_file_selected_in(self.QCBox_AreaOfInterest, "area of interest shape"):
            return

        # first select the target dir for save the clipping file
        filename, ext = os.path.splitext(get_current_file_path_in(self.QCBox_ThematicRaster))
        ext = ext if ext in [".tif", ".TIF", ".img", ".IMG"] else ".tif"
        suggested_filename = filename + "_clip" + ext

        file_out, _ = QFileDialog.getSaveFileName(self, self.tr("Select the output file to save the clipping file"),
                                                  suggested_filename,
                                                  self.tr("GeoTiff files (*.tif);;Img files (*.img);;All files (*.*)"))
        if file_out == '':
            return

        # clipping
        clip_file = do_clipping_with_shape(
            self.QCBox_ThematicRaster.currentLayer(),
            self.QCBox_AreaOfInterest.currentLayer(), file_out,
            get_nodata_value(self.QCBox_ThematicRaster.currentLayer()))
        # copy the style
        thematic_basename = os.path.splitext(get_current_file_path_in(self.QCBox_ThematicRaster))[0]
        if os.path.isfile(thematic_basename + ".qml"):
            copyfile(thematic_basename + ".qml", os.path.splitext(file_out)[0] + ".qml")
        # unload old thematic file
        unload_layer(get_current_file_path_in(self.QCBox_ThematicRaster))
        # load to qgis and update combobox list
        load_and_select_filepath_in(self.QCBox_ThematicRaster, clip_file)
        self.select_thematic_raster(self.QCBox_ThematicRaster.currentLayer())

        iface.messageBar().pushMessage("AcATaMa", "Clipping the thematic raster with shape, completed",
                                       level=Qgis.Success)
Ejemplo n.º 2
0
    def removes_temporary_files(self):
        if not self.dockwidget:
            return
        # unload all layers instances from Qgis saved in tmp dir
        try:
            d = self.dockwidget.tmp_dir
            files_in_tmp_dir = [
                os.path.join(d, f) for f in os.listdir(d)
                if os.path.isfile(os.path.join(d, f))
            ]
        except:
            files_in_tmp_dir = []

        for file_tmp in files_in_tmp_dir:
            unload_layer(file_tmp)

        # clear self.dockwidget.tmp_dir
        if self.dockwidget.tmp_dir and os.path.isdir(self.dockwidget.tmp_dir):
            shutil.rmtree(self.dockwidget.tmp_dir, ignore_errors=True)
        self.dockwidget.tmp_dir = None

        # clear qgis main canvas
        self.iface.mapCanvas().clearCache()
        self.iface.mapCanvas().refresh()