Exemplo n.º 1
0
 def select_categorical_raster_StraRS(self, layer):
     # first unselect/clear sampling method
     self.QCBox_StraRS_Method.setCurrentIndex(-1)
     # check
     if not valid_file_selected_in(self.QCBox_CategRaster_StraRS,
                                   "categorical raster"):
         self.QCBox_band_CategRaster_StraRS.clear()
         self.nodata_CategRaster_StraRS.setValue(-1)
         self.QGBox_Sampling_Method.setEnabled(False)
         return
     # check if categorical raster data type is integer or byte
     if layer.dataProvider().dataType(1) not in [1, 2, 3, 4, 5]:
         self.QCBox_CategRaster_StraRS.setCurrentIndex(-1)
         self.QCBox_band_CategRaster_StraRS.clear()
         self.nodata_CategRaster_StraRS.setValue(-1)
         self.QGBox_Sampling_Method.setEnabled(False)
         iface.messageBar().pushMessage(
             "AcATaMa",
             "Error, categorical raster must be byte or integer as data type.",
             level=Qgis.Warning)
         return
     # set band count
     self.QCBox_band_CategRaster_StraRS.clear()
     self.QCBox_band_CategRaster_StraRS.addItems(
         [str(x) for x in range(1,
                                layer.bandCount() + 1)])
     self.QGBox_Sampling_Method.setEnabled(True)
     # set the same nodata value if select the thematic raster
     if layer == self.QCBox_ThematicRaster.currentLayer():
         self.nodata_CategRaster_StraRS.setValue(
             self.nodata_ThematicRaster.value())
         return
     self.nodata_CategRaster_StraRS.setValue(get_nodata_value(layer))
Exemplo n.º 2
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)
Exemplo n.º 3
0
    def select_thematic_raster(self, layer):
        def clear_and_unset_the_thematic_raster():
            with block_signals_to(self.QCBox_ThematicRaster):
                self.QCBox_ThematicRaster.setCurrentIndex(-1)
            self.QCBox_band_ThematicRaster.clear()
            self.nodata_ThematicRaster.setValue(-1)
            # SimpRS
            self.minDistance_SimpRS.setSuffix("")
            self.minDistance_SimpRS.setToolTip("")
            self.minDistance_SimpRS.setValue(0)
            # StraRS
            self.minDistance_StraRS.setSuffix("")
            self.minDistance_StraRS.setToolTip("")
            self.minDistance_StraRS.setValue(0)
            # disable sampling tab
            self.scrollAreaWidgetContents_S.setDisabled(True)
            # unset the thematic classes in classification instance
            sampling_layer = self.QCBox_SamplingFile.currentLayer()
            if sampling_layer and sampling_layer in Classification.instances:
                Classification.instances[
                    sampling_layer].with_thematic_classes = False
            # updated state of sampling file selected for accuracy assessment tab
            self.set_sampling_file_accuracy_assessment()

        # first check
        if not layer or not valid_file_selected_in(self.QCBox_ThematicRaster,
                                                   "thematic raster"):
            clear_and_unset_the_thematic_raster()
            return
        # check if thematic raster data type is integer or byte
        if layer.dataProvider().dataType(1) not in [1, 2, 3, 4, 5]:
            clear_and_unset_the_thematic_raster()
            iface.messageBar().pushMessage(
                "AcATaMa",
                "Error, thematic raster must be byte or integer as data type.",
                level=Qgis.Warning)
            return
        # set band count
        self.QCBox_band_ThematicRaster.clear()
        self.QCBox_band_ThematicRaster.addItems(
            [str(x) for x in range(1,
                                   layer.bandCount() + 1)])
        # set nodata value of thematic raster in nodata field
        self.nodata_ThematicRaster.setValue(get_nodata_value(layer))
        # set/update the units in minimum distance items in sampling tab
        layer_dist_unit = layer.crs().mapUnits()
        str_unit = QgsUnitTypes.toString(layer_dist_unit)
        abbr_unit = QgsUnitTypes.toAbbreviatedString(layer_dist_unit)
        # Set the properties of the QdoubleSpinBox based on the QgsUnitTypes of the thematic raster
        # https://qgis.org/api/classQgsUnitTypes.html
        # SimpRS
        self.minDistance_SimpRS.setSuffix(" {}".format(abbr_unit))
        self.minDistance_SimpRS.setToolTip(
            "Minimum distance in {} (units based on thematic raster selected)".
            format(str_unit))
        self.minDistance_SimpRS.setRange(
            0,
            360 if layer_dist_unit == QgsUnitTypes.DistanceDegrees else 10e6)
        self.minDistance_SimpRS.setDecimals(4 if layer_dist_unit in [
            QgsUnitTypes.DistanceKilometers, QgsUnitTypes.
            DistanceNauticalMiles, QgsUnitTypes.DistanceMiles, QgsUnitTypes.
            DistanceDegrees
        ] else 1)
        self.minDistance_SimpRS.setSingleStep(0.0001 if layer_dist_unit in [
            QgsUnitTypes.DistanceKilometers, QgsUnitTypes.
            DistanceNauticalMiles, QgsUnitTypes.DistanceMiles, QgsUnitTypes.
            DistanceDegrees
        ] else 1)
        self.minDistance_SimpRS.setValue(0)
        # StraRS
        self.minDistance_StraRS.setSuffix(" {}".format(abbr_unit))
        self.minDistance_StraRS.setToolTip(
            "Minimum distance in {} (units based on thematic raster selected)".
            format(str_unit))
        self.minDistance_StraRS.setRange(
            0,
            360 if layer_dist_unit == QgsUnitTypes.DistanceDegrees else 10e6)
        self.minDistance_StraRS.setDecimals(4 if layer_dist_unit in [
            QgsUnitTypes.DistanceKilometers, QgsUnitTypes.
            DistanceNauticalMiles, QgsUnitTypes.DistanceMiles, QgsUnitTypes.
            DistanceDegrees
        ] else 1)
        self.minDistance_StraRS.setSingleStep(0.0001 if layer_dist_unit in [
            QgsUnitTypes.DistanceKilometers, QgsUnitTypes.
            DistanceNauticalMiles, QgsUnitTypes.DistanceMiles, QgsUnitTypes.
            DistanceDegrees
        ] else 1)
        self.minDistance_StraRS.setValue(0)
        # enable sampling tab
        self.scrollAreaWidgetContents_S.setEnabled(True)