def remove_bands(args): for filename in args.raster: raster = Raster(filename) raster.remove_bands(*args.idxs, out_filename=args.out_file)
class TestOtbFunctions(unittest.TestCase): def setUp(self): self.raster = Raster('data/l8_20130714.tif') def test_should_remove_band(self): out_file = tempfile.NamedTemporaryFile(suffix='.tif') result = self.raster.remove_bands(6, out_filename=out_file.name) self.assertEqual(result.meta['count'], self.raster.meta['count'] - 1) _check_image(tester=self, filename=out_file.name, driver=u'GTiff', width=self.raster.meta['width'], height=self.raster.meta['height'], number_bands=self.raster.meta['count'] - 1, dtype=self.raster.meta['dtype'].ustr_dtype, proj=self.raster.meta['srs'].ExportToProj4()) def test_should_compute_ndvi(self): out_file = tempfile.NamedTemporaryFile(suffix='.tif') self.raster.ndvi(red_idx=4, nir_idx=5, out_filename=out_file.name) _check_image(tester=self, filename=out_file.name, driver=u'GTiff', width=self.raster.meta['width'], height=self.raster.meta['height'], number_bands=1, dtype='Float32', date_time=self.raster.meta['date_time'], proj=self.raster.meta['srs'].ExportToProj4()) def test_should_compute_ndwi(self): out_file = tempfile.NamedTemporaryFile(suffix='.tif') self.raster.ndwi(nir_idx=4, mir_idx=5, out_filename=out_file.name) _check_image(tester=self, filename=out_file.name, driver=u'GTiff', width=self.raster.meta['width'], height=self.raster.meta['height'], number_bands=1, dtype='Float32', date_time=self.raster.meta['date_time'], proj=self.raster.meta['srs'].ExportToProj4()) def test_should_compute_mndwi(self): out_file = tempfile.NamedTemporaryFile(suffix='.tif') self.raster.mndwi(green_idx=4, mir_idx=5, out_filename=out_file.name) _check_image(tester=self, filename=out_file.name, driver=u'GTiff', width=self.raster.meta['width'], height=self.raster.meta['height'], number_bands=1, dtype='Float32', date_time=self.raster.meta['date_time'], proj=self.raster.meta['srs'].ExportToProj4()) def test_lsms_segmentation_should_compute_segmented_image(self): out_file = tempfile.NamedTemporaryFile(suffix='.tif') out_vector_filename = os.path.join( tempfile.gettempdir(), 'labels.shp') out_raster = self.raster.lsms_segmentation( spatialr=5, ranger=15, thres=0.1, rangeramp=0, maxiter=5, object_minsize=10, out_vector_filename=out_vector_filename, out_filename=out_file.name) # Output raster should have same size, same proj, 1 band _check_image(tester=self, filename=out_file.name, driver=u'GTiff', width=self.raster.meta['width'], height=self.raster.meta['height'], number_bands=1, dtype='Float32', proj=self.raster.meta['srs'].ExportToProj4()) # Output vector should have same number of polygon than label array = out_raster.array_from_bands() number_labels = len(np.unique(array)) ds = ogr.Open(out_vector_filename) layer = ds.GetLayer(0) self.assertEqual(layer.GetFeatureCount(), number_labels) def tearDown(self): tmpdir = tempfile.gettempdir() tmpfilenames = [filename for filename in os.listdir(tmpdir) if filename.endswith('.tif.aux.xml') or filename.startswith('segmented_merged.')] for filename in tmpfilenames: os.remove(os.path.join(tmpdir, filename))