def test_band_mapping(self): with self.assertRaises(AttributeError) as msg: bm = BandMapping(red=4, green=3, blud=2) self.assertEqual("BandMapping has no attribute blud. Allowed keys are " "blue, mask, infrared, rededge, green, red", str(msg.exception)) self.assertDictEqual(BandMapping(), {'blue': 0, 'mask': 0, 'infrared': 0, 'rededge': 0, 'green': 0, 'red': 0}) mydict = {'rededge': 1, 'red': 3, 'infrared': 4, 'mask': 5, } self.assertDictEqual(BandMapping(**mydict), {'blue': 0, 'mask': 5, 'infrared': 4, 'rededge': 1, 'green': 0, 'red': 3}) bm = BandMapping(red=3, green=2) self.assertEqual(bm.allocated_bands(), [2, 3]) bm.update(infrared=4, mask=5) self.assertDictEqual(bm, {'blue': 0, 'mask': 5, 'infrared': 4, 'rededge': 0, 'green': 2, 'red': 3}) del bm['mask'] self.assertDictEqual(bm, {'blue': 0, 'mask': 0, 'infrared': 4, 'rededge': 0, 'green': 2, 'red': 3})
def test_noGroupby(self): out_dir = os.path.join(TmpDir, 'TestCalculateImageIndices', 'no-groupby') if not os.path.exists(out_dir): os.makedirs(out_dir) bm = BandMapping(green=2, infrared=4, rededge=1, mask=5) image_file = os.path.realpath( this_dir + '/data/rasters/area1_rgbi_jan_50cm_84sutm54.tif') poly_shapefile = os.path.realpath( this_dir + '/data/PolyMZ_wgs84_MixedPartFieldsTypes.shp') indices = CalculateIndices(**bm).valid_indices() files = calc_indices_for_block(image_file, 2, BandMapping(red=3, green=2, infrared=4, rededge=1), out_dir, indices=indices, image_epsg=32754, image_nodata=0, polygon_shapefile=poly_shapefile, out_epsg=28354) self.assertEqual(len(indices), 3) self.assertEqual(len(files), len(indices)) with rasterio.open(files[0]) as src: self.assertEqual(src.nodata, -9999) self.assertEqual(src.crs, rasterio.crs.CRS.from_epsg(28354)) self.assertEqual(src.dtypes, ('float32', )) self.assertEqual(src.res, (2.0, 2.0)) self.assertEqual(src.count, 1)
def test_calculateIndices(self): self.assertEqual(CalculateIndices(red=3, infrared=4, mask=5).valid_indices(), ['NDVI', 'PCD']) ci = CalculateIndices() self.assertDictEqual(ci.band_map, {'blue': 0, 'mask': 0, 'infrared': 0, 'rededge': 0, 'green': 0, 'red': 0}) ci.band_map.update({'rededge': 1, 'green': 2, 'mask': 5, 'red': 3, 'infrared': 4}) self.assertDictEqual(ci.band_map, {'blue': 0, 'green': 2, 'infrared': 4, 'mask': 5, 'red': 3, 'rededge': 1}) self.assertEqual(ci.band_map.allocated_bands(), [1, 2, 3, 4, 5]) self.assertEqual(ci.valid_indices(), ['NDVI', 'PCD', 'GNDVI', 'NDRE', 'CHLRE']) bm = BandMapping(red=3, green=2, infrared=4) ci.band_map = bm self.assertEqual(ci.valid_indices(), ['NDVI', 'PCD', 'GNDVI']) file_image = os.path.realpath(this_dir + "/data/rasters/area1_rgbi_jan_50cm_84sutm54.tif") with self.assertRaises(KeyError) as msg: ci.calculate('NDVIa', file_image, src_nodata=0) self.assertEqual("'NDVIA'", str(msg.exception))
def test09_calcImageIndices_allopts(self): out_fold = os.path.join(TmpDir, 'calcindex_allopts') if not os.path.exists(out_fold): os.mkdir(out_fold) bm = BandMapping(green=2, infrared=4, rededge=1, mask=5) indices = CalculateIndices(**bm).valid_indices() files = calc_indices_for_block(fileImage, 2.5, bm, out_fold, indices, image_nodata=0, image_epsg=32754, polygon_shapefile=fileBox, out_epsg=28354) self.gridextract_files += files self.assertEqual(len(files), 3) with rasterio.open(files[0]) as src: self.assertEqual(src.nodata, -9999) self.assertEqual(src.crs, rasterio.crs.CRS.from_epsg(28354)) self.assertEqual(src.meta['dtype'], 'float32') self.assertEqual(src.res, (2.5, 2.5)) self.assertEqual(src.count, 1)
def __init__(self, iface, parent=None): super(CalculateImageIndicesDialog, self).__init__(parent) # Set up the user interface from Designer. self.setupUi(self) self.iface = iface self.DISP_TEMP_LAYERS = read_setting(PLUGIN_NAME + '/DISP_TEMP_LAYERS', bool) self.DEBUG = config.get_debug_mode() # Catch and redirect python errors directed at the log messages python error tab. QgsApplication.messageLog().messageReceived.connect(errorCatcher) if not os.path.exists(TEMPDIR): os.mkdir(TEMPDIR) # Setup for validation messagebar on gui----------------------------- self.messageBar = QgsMessageBar(self) # leave this message bar for bailouts self.validationLayout = QtWidgets.QFormLayout(self) # new layout to gui if isinstance(self.layout(), (QtWidgets.QFormLayout, QtWidgets.QGridLayout)): # create a validation layout so multiple messages can be added and cleaned up. self.layout().insertRow(0, self.validationLayout) self.layout().insertRow(0, self.messageBar) else: self.layout().insertWidget(0, self.messageBar) # for use with Vertical/horizontal layout box self.band_mapping = BandMapping() # GUI Runtime Customisation ----------------------------------------------- self.mcboPolygonLayer.setFilters(QgsMapLayerProxyModel.PolygonLayer) self.mcboPolygonLayer.setExcludedProviders(['wms']) self.mcboPolygonLayer.setLayer(None) self.mcboRasterLayer.setFilters(QgsMapLayerProxyModel.RasterLayer) self.mcboRasterLayer.setExcludedProviders(['wms']) rastlyrs_df = build_layer_table([self.mcboRasterLayer.layer(i) for i in range(self.mcboRasterLayer.count())]) if self.mcboRasterLayer.count() > 0: exc_lyrs = rastlyrs_df[rastlyrs_df['bandcount']<=1] self.mcboRasterLayer.setExceptedLayerList(exc_lyrs['layer'].tolist()) self.updateRaster() # self.chkAddToDisplay.setChecked(False) # self.chkAddToDisplay.hide() self.chkgrpIndices.setExclusive(False) # allow for multi selection self.setWindowIcon(QtGui.QIcon(':/plugins/pat/icons/icon_calcImgIndices.svg'))
def __init__(self, iface, parent=None): super(CalculateImageIndicesDialog, self).__init__(parent) # Set up the user interface from Designer. self.setupUi(self) self.iface = iface self.DISP_TEMP_LAYERS = read_setting(PLUGIN_NAME + '/DISP_TEMP_LAYERS', bool) self.DEBUG = config.get_debug_mode() # Catch and redirect python errors directed at the log messages python error tab. QgsMessageLog.instance().messageReceived.connect(errorCatcher) if not os.path.exists(TEMPDIR): os.mkdir(TEMPDIR) # Setup for validation messagebar on gui----------------------------- self.messageBar = QgsMessageBar( self) # leave this message bar for bailouts self.validationLayout = QtGui.QFormLayout(self) # new layout to gui if isinstance(self.layout(), (QtGui.QFormLayout, QtGui.QGridLayout)): # create a validation layout so multiple messages can be added and cleaned up. self.layout().insertRow(0, self.validationLayout) self.layout().insertRow(0, self.messageBar) else: self.layout().insertWidget( 0, self.messageBar) # for use with Vertical/horizontal layout box self.band_mapping = BandMapping() self.outQgsCRS = None self.chkgrpIndices.setExclusive(False) # allow for multi selection self.exclude_map_layers() self.updateRaster() self.updateUseSelected() self.autoSetCoordinateSystem() # GUI Runtime Customisation ----------------------------------------------- self.chkAddToDisplay.setChecked(False) # self.chkAddToDisplay.hide() self.add_blank_field_to_cbo() self.setWindowIcon( QtGui.QIcon(':/plugins/pat/icons/icon_calcImgIndices.svg'))
def test_noShapefile(self): """ Use Full Image...... No Shapfile, No Non-Vine mask """ out_dir = os.path.join(TmpDir, 'TestCalculateImageIndices', 'no-shapefile') if not os.path.exists(out_dir): os.makedirs(out_dir) image_file = os.path.realpath( this_dir + '/data/rasters/area1_rgbi_jan_50cm_84sutm54.tif') indices = CalculateIndices(red=3, infrared=4, mask=5).valid_indices() files = calc_indices_for_block(image_file, 2, BandMapping(red=3, green=2, infrared=4, rededge=1), out_dir, indices=indices, image_epsg=32754, image_nodata=0, out_epsg=28354) dst_crs = rasterio.crs.CRS.from_epsg(28354) self.assertEqual(len(files), len(indices)) with rasterio.open(files[0]) as src: self.assertEqual(src.count, 1) self.assertEqual(src.crs.wkt, dst_crs.wkt) self.assertEqual(src.tags(1)['name'], indices[0]) self.assertEqual(src.nodata, -9999) self.assertEqual(src.meta['dtype'], 'float32') # test for values at coords row, col = src.index(300725, 6181571) self.assertAlmostEqual( src.read(1)[int(row), int(col)], -0.05087604, 4) row, col = src.index(300647.0, 6181561.0) self.assertAlmostEqual( src.read(1)[int(row), int(col)], 0.02232674, 4) row, col = src.index(300881.342, 6181439.444) self.assertEqual(src.read(1)[int(row), int(col)], -9999)
def test_allOptions(self): """ All Options includes: Use a non-vine mask. Original image nodata is None so set to 0 Reproject Image Use Shapefile AND groupby field """ out_dir = os.path.join(TmpDir, 'TestCalculateImageIndices', 'all-opts') if not os.path.exists(out_dir): os.makedirs(out_dir) image_file = os.path.realpath( this_dir + '/data/rasters/area1_rgbi_jan_50cm_84sutm54.tif') poly_shapefile = os.path.realpath( this_dir + '/data/PolyMZ_wgs84_MixedPartFieldsTypes.shp') indices = CalculateIndices(red=3, infrared=4, mask=5).valid_indices() files = calc_indices_for_block(image_file, 2, BandMapping(red=3, green=2, infrared=4, rededge=1, mask=5), out_dir, indices=indices, image_epsg=32754, image_nodata=0, polygon_shapefile=poly_shapefile, groupby='part_type', out_epsg=28354) dst_crs = rasterio.crs.CRS.from_epsg(28354) self.assertEqual(len(files), 4) with rasterio.open(files[0]) as src: self.assertEqual(src.count, 1) self.assertEqual(src.crs.wkt, dst_crs.wkt) self.assertEqual(src.tags(1)['name'], indices[0]) self.assertEqual(src.nodata, -9999) self.assertEqual(src.meta['dtype'], 'float32') # coords 300725.0, 6181571.0 self.assertEqual(src.read(1)[47, 62], -9999) # coords (300647.0, 6181561.0) self.assertAlmostEqual(src.read(1)[52, 23], 0.20820355, 4)
def test_dontApplyNonVineMask(self): out_dir = os.path.join(TmpDir, 'TestCalculateImageIndices', 'no-nonvine') if not os.path.exists(out_dir): os.makedirs(out_dir) image_file = os.path.realpath( this_dir + '/data/rasters/area1_rgbi_jan_50cm_84sutm54.tif') poly_shapefile = os.path.realpath( this_dir + '/data/PolyMZ_wgs84_MixedPartFieldsTypes.shp') indices = CalculateIndices(red=3, infrared=4, mask=5).valid_indices() files = calc_indices_for_block(image_file, 2, BandMapping(red=3, green=2, infrared=4, rededge=1), out_dir, indices=indices, image_epsg=32754, image_nodata=0, polygon_shapefile=poly_shapefile, groupby='part_type', out_epsg=28354) dst_crs = rasterio.crs.CRS.from_epsg(28354) self.assertEqual(len(files), 4) with rasterio.open(files[0]) as src: self.assertEqual(src.count, 1) self.assertEqual(src.crs.wkt, dst_crs.wkt) self.assertEqual(src.tags(1)['name'], indices[0]) self.assertEqual(src.nodata, -9999) self.assertEqual(src.meta['dtype'], 'float32') # coords 300725.0, 6181571.0 self.assertEqual(src.read(1)[47, 62], -9999) # coords (300647.0, 6181561.0) self.assertAlmostEqual(src.read(1)[52, 23], 0.070868947, 4)