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 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 update_bandlist(self): """update the band list to the drop down box""" if self.mcboRasterLayer.currentLayer() is None: return # list of all bands for image band_list = range(1, self.mcboRasterLayer.currentLayer().bandCount() + 1) for obj in [ self.cboBandRed, self.cboBandGreen, self.cboBandIR, self.cboBandRedEdge, self.cboBandNonVine ]: ''' Hide items from combo which have already been allocated to a band. NOTE: deleting the items changes the current index, and resetting to the corrected index will trigger a change event which turns into an endless loop source https://stackoverflow.com/a/49778675/9567306 ''' for i in band_list: idx = obj.findText('Band {: >2}'.format(i)) if i in self.band_mapping.allocated_bands( ) and 'Band {: >2}'.format(i) != obj.currentText(): obj.view().setRowHidden(idx, True) else: obj.view().setRowHidden(idx, False) indices = CalculateIndices(**self.band_mapping).valid_indices() for x in self.chkgrpIndices.buttons(): if x.text().upper() in indices: x.setEnabled(True) else: x.setEnabled(False) x.setChecked(False)
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)