def test_reclassify_raster(self): """Test we can reclassify a raster layer.""" layer = load_test_raster_layer('hazard', 'continuous_flood_20_20.asc') classes = { 'low': [None, 0.2], # value <= 0.2 'medium': [0.2, 1], # 0.2 < value <= 1 'high': [1, None], # 1 < value } ranges = { exposure_structure['key']: { generic_hazard_classes['key']: { 'active': True, 'classes': classes } } } layer.keywords['thresholds'] = ranges expected_keywords = layer.keywords.copy() title = reclassify_raster_steps['output_layer_name'] % ( layer.keywords['layer_purpose']) expected_keywords['layer_mode'] = 'classified' expected_keywords['value_map'] = { 'high': [3], 'low': [1], 'medium': [2] } expected_keywords['title'] = title expected_keywords['classification'] = generic_hazard_classes['key'] expected_keywords['thresholds'] = classes reclassified = reclassify(layer, exposure_structure['key']) self.assertDictEqual(reclassified.keywords, expected_keywords) stats = reclassified.dataProvider().bandStatistics( 1, QgsRasterBandStats.Min | QgsRasterBandStats.Max) self.assertEqual(stats.minimumValue, 1.0) self.assertEqual(stats.maximumValue, 3.0)
def accept(self): """Handler for when OK is clicked.""" input_path = self.input_path.text() input_title = self.line_edit_title.text() input_source = self.line_edit_source.text() output_path = self.output_path.text() if not output_path.endswith('.tif'): # noinspection PyArgumentList,PyCallByClass,PyTypeChecker QMessageBox.warning( self, self.tr('InaSAFE'), (self.tr('Output file name must be tif file'))) if not os.path.exists(input_path): # noinspection PyArgumentList,PyCallByClass,PyTypeChecker QMessageBox.warning( self, self.tr('InaSAFE'), (self.tr('Input file does not exist'))) return if self.nearest_mode.isChecked(): algorithm = 'nearest' else: algorithm = 'invdist' QtGui.qApp.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor)) file_name = convert_mmi_data( input_path, input_title, input_source, output_path, algorithm=algorithm, algorithm_filename_flag=True) # reclassify raster file_info = QFileInfo(file_name) base_name = file_info.baseName() self.output_layer = QgsRasterLayer(file_name, base_name) self.output_layer.keywords = KeywordIO.read_keywords(self.output_layer) self.output_layer.keywords['classification'] = ( earthquake_mmi_scale['key']) keywords = self.output_layer.keywords if self.output_layer.isValid(): self.output_layer = reclassify( self.output_layer, overwrite_input=True) KeywordIO.write_keywords(self.output_layer, keywords) else: LOGGER.debug("Failed to load") QtGui.qApp.restoreOverrideCursor() if self.load_result.isChecked(): # noinspection PyTypeChecker mmi_ramp_roman(self.output_layer) self.output_layer.saveDefaultStyle() if not self.output_layer.isValid(): LOGGER.debug("Failed to load") else: # noinspection PyArgumentList QgsMapLayerRegistry.instance().addMapLayer(self.output_layer) iface.zoomToActiveLayer() if (self.keyword_wizard_checkbox.isChecked() and self.keyword_wizard_checkbox.isEnabled()): self.launch_keyword_wizard() self.done(self.Accepted)