def test_raster_should_get_array_block_one_band(self): filename = 'data/RGB.byte.tif' raster = Raster(filename) array = raster.array_from_bands(2, block_win=(256, 256, 128, 128)) self.assertEqual(array.ndim, 2) self.assertEqual(array.shape, (128, 128)) self.assertEqual(array.dtype, 'UInt8')
def test_raster_should_get_array_one_band(self): filename = 'data/RGB.byte.tif' raster = Raster(filename) array = raster.array_from_bands(1) self.assertEqual(array.ndim, 2) self.assertEqual(array.shape, (718, 791)) self.assertEqual(array.dtype, 'UInt8')
def test_raster_should_get_array_block(self): filename = 'data/RGB.byte.tif' raster = Raster(filename) array = raster.array_from_bands(block_win=(256, 256, 300, 300)) self.assertEqual(array.ndim, 3) self.assertEqual(array.shape, (300, 300, 3)) self.assertEqual(array.dtype, 'UInt8')
def apply_mask(args): mask_raster = Raster(args.mask) for filename in args.raster: raster = Raster(filename) raster.apply_mask(mask_raster=mask_raster, mask_value=args.mask_value, set_value=args.set_value, out_filename=args.out_file)
def lsms_segmentation(args): raster = Raster(args.raster) block_size = (args.block_width, args.block_height) \ if args.block_width and args.block_height \ else None raster.lsms_segmentation(args.spatialr, args.spectralr, args.thres, args.rangeramp, args.maxiter, object_minsize=args.minsize, block_size=block_size, out_vector_filename=args.vector_file, out_filename=args.out_file)
def test_raster_should_set_date(self): filename = 'data/RGB_unproj.byte.tif' tmp_file = tempfile.NamedTemporaryFile(suffix='.tif') shutil.copyfile(filename, tmp_file.name) raster = Raster(tmp_file.name) self.assertIsNone(raster.meta['date_time']) dt = datetime(2014, 01, 01) raster.date_time = dt _check_image(self, tmp_file.name, u'GTiff', 791, 718, 3, 'Byte', date_time=dt.strftime('%Y:%m:%d %H:%M:%S')) self.assertEqual(raster.meta['date_time'], dt)
def test_raster_should_set_projection(self): filename = 'data/RGB_unproj.byte.tif' tmp_file = tempfile.NamedTemporaryFile(suffix='.tif') shutil.copyfile(filename, tmp_file.name) raster = Raster(tmp_file.name) self.assertIsNone(raster.srs) sr = osr.SpatialReference() sr.ImportFromEPSG(4326) raster.srs = sr _check_image(self, tmp_file.name, u'GTiff', 791, 718, 3, 'Byte', proj=sr.ExportToProj4()) self.assertTrue(raster.meta['srs'].IsSame(sr))
def test_raster_should_get_block_list(self): filename = 'data/RGB.byte.tif' raster = Raster(filename) blocks = raster.block_windows() self.assertEqual(blocks.next(), (0, 0, raster.meta['block_size'][0], raster.meta['block_size'][1])) last_block = None length = 1 total_height = raster.block_size[1] while True: try: last_block = blocks.next() length += 1 total_height += last_block[3] except StopIteration: break self.assertEqual(last_block, (0, 717, raster.meta['block_size'][0], 1)) self.assertEqual(length, 240) self.assertEqual(total_height, raster.meta['height'])
def snow_brake_date(): # Command-line parameters parser = argparse.ArgumentParser( description="Compute a raster whose value for each pixel is the " "date/time of snow break, given a list of temporally distinct, but " "spatially identical, images.\n\n" "That is, the value of each pixel will be the date/time of the first " "image where the snow disappear on the pixel and does not appear again " "in the following images.\n\n" "Presence or absence of snow is evaluated from the Normalized " "Difference Snow Index (NDSI) which is computed from green and " "mid-infrared band." "Dates will be in numeric format (eg. Apr 25, 2013 (midnight) " "-> 1366840800.0)") parser.add_argument("rasters", nargs='+', help="List of input images") parser.add_argument("-g", "--idx_green", type=int, default=3, help="Index of a green band, common to all images " "(default: 3)") parser.add_argument("-mir", "--idx_mir", type=int, default=6, help="Index of a mid-infrared band, common to all " "images (default: 6)") parser.add_argument("-o", "--out_file", default='./snow_break.tif', help="Path to the output file (default: " "'snow_break.tif' in the current folder)") args = parser.parse_args() # Perform the actual work # Compute all NDSIs, read them as NumPy arrays, and stack them into one # array rasters = [Raster(filename) for filename in args.rasters] rasters.sort(key=lambda raster: raster.meta['datetime']) for raster in rasters: assert raster.meta['datetime'] is not None, "Raster has no " "TIFFTAG_DATETIME metatadata: {}".format(raster.filename) ndsis = [raster.mndwi('{}.mndwi.tif'.format( os.path.basename(os.path.splitext(raster.filename)[0])), idx_green=args.idx_green, idx_mir=args.idx_mir) for raster in rasters] arrays = [ndsi.array() for ndsi in ndsis] stack_array = np.dstack(arrays) # List of dates dates = [ndsi.meta['datetime'] for ndsi in ndsis] # Create an empty array of correct size ndsi0 = ndsis[0] width = ndsi0.meta['width'] height = ndsi0.meta['height'] out_array = np.empty((height, width), dtype=np.float64) # Find the snow-brake date _under_threshold_date(stack_array, dates, out_array)
def compute_temporal_stats(args): rasters = [Raster(filename) for filename in args.raster] kw = {} kw['out_filename'] = args.out_file if args.band_idx is not None: kw['band_idx'] = args.band_idx if args.stat is not None: kw['stats'] = list(args.stat) if args.date_from is not None: kw['date2float'] = partial(_days_between, args.date_from) temporal_stats(*rasters, **kw)
def test_raster_should_get_block_list_with_given_size(self): filename = 'data/RGB.byte.tif' raster = Raster(filename) xsize = 7 ysize = 2 lastx = raster.meta['width'] - xsize lasty = raster.meta['height'] - ysize number_blocks = \ raster.meta['width'] / xsize * raster.meta['height'] / ysize blocks = raster.block_windows(block_size=(xsize, ysize)) self.assertEqual(blocks.next(), (0, 0, xsize, ysize)) last_block = None length = 1 while True: try: last_block = blocks.next() length += 1 except StopIteration: break self.assertEqual(last_block, (lastx, lasty, xsize, ysize)) self.assertEqual(length, number_blocks)
def test_concatenate_should_work_when_same_size_same_proj(self): rasters = [Raster(os.path.join(self.folder, filename)) for filename in os.listdir(self.folder) if filename.startswith('l8_') and filename.endswith('.tif')] out_file = tempfile.NamedTemporaryFile(suffix='.tif') concatenate_rasters(*rasters, out_filename=out_file.name) _check_image(tester=self, filename=out_file.name, driver=u'GTiff', width=rasters[0].meta['width'], height=rasters[0].meta['height'], number_bands=rasters[0].meta['count'] * 8, dtype=rasters[0].meta['dtype'].ustr_dtype, proj=rasters[0].meta['srs'].ExportToProj4())
def stat_to_classification(args): #Get the sample-feature matrix from the roi X, Y = cla.get_samples_from_roi(args.label_file, args.roi_file, args.stat_file) #Get the sample-feature matrix from the labeled raster X_label, reverse = cla.get_samples_from_label_img(args.label_file, args.stat_file) #Split the roi into two dataset X_train, X_test, Y_train, Y_test = train_test_split(X, Y,train_size =\ args.train_size) #set some parameters stat = Raster(args.stat_file) out_filename = os.path.join(args.dir, args.out_file) #Initialize the classifier, train it and perform it Y_predict = cla.decision_tree(X_train, Y_train, X_test, X_label, reverse, stat, out_filename, ext=args.format) #Compute the classification metrics and the confusion matrix cm, report, accuracy = cla.pred_error_metrics(Y_predict, Y_test, target_names = \ args.target_names) #Print the results print "confusion matrix\n", cm print report print "OA :", accuracy # Show confusion matrix in a separate window plt.matshow(cm) plt.title('Confusion matrix') plt.colorbar() plt.ylabel('True label') plt.xlabel('Predicted label') plt.show()
def test_raster_should_get_attr_values(self): filename = 'data/l8_20130425.tif' raster = Raster(filename) self.assertEqual(raster.filename, filename) self.assertEqual(raster.driver.gdal_name, 'GTiff') self.assertEqual(raster.width, 66) self.assertEqual(raster.height, 56) self.assertEqual(raster.count, 7) self.assertEqual(raster.dtype.lstr_dtype, 'int16') self.assertEqual(raster.block_size, (66, 8)) self.assertEqual(raster.date_time, datetime(2013, 04, 25)) self.assertEqual(raster.gdal_extent, ((936306.723651, 6461635.694121), (936306.723651, 6459955.694121), (938286.723651, 6461635.694121), (938286.723651, 6459955.694121))) self.assertEqual( raster.meta['srs'].ExportToWkt(), 'PROJCS["RGF93 / Lambert-93",GEOGCS["RGF93",' 'DATUM["Reseau_Geodesique_Francais_1993",' 'SPHEROID["GRS 1980",6378137,298.2572221010042,' 'AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6171"]],' 'PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],' 'AUTHORITY["EPSG","4171"]],' 'PROJECTION["Lambert_Conformal_Conic_2SP"],' 'PARAMETER["standard_parallel_1",49],' 'PARAMETER["standard_parallel_2",44],' 'PARAMETER["latitude_of_origin",46.5],' 'PARAMETER["central_meridian",3],' 'PARAMETER["false_easting",700000],' 'PARAMETER["false_northing",6600000],UNIT["metre",1,' 'AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","2154"]]') self.assertEqual(raster.meta['transform'], (936306.723651, 30.0, 0.0, 6461635.694121, 0.0, -30.0))
def test_concatenate_should_raise_assertion_error_if_not_same_proj(self): rasters = [Raster(os.path.join(self.folder, 'RGB.byte.tif')), Raster(os.path.join(self.folder, 'RGB_unproj.byte.tif'))] out_file = tempfile.NamedTemporaryFile(suffix='.tif') self.assertRaises(AssertionError, concatenate_rasters, *rasters, out_filename=out_file.name)
def label_stats(args): raster = Raster(args.raster) label_raster = Raster(args.label_file) raster.label_stats(args.stats, label_raster=label_raster, out_filename=args.out_file)
def main(): raster = Raster(IMG) raster.ndvi(4, 5, '/tmp/l8_20130714_ndvi.tif') raster.ndmi(5, 6, '/tmp/l8_20130714_ndwi.tif') raster.ndsi(3, 6, '/tmp/l8_20130714_mndwi.tif',)
def compute_temporal_stats(): # Command-line parameters parser = argparse.ArgumentParser( description="Compute pixel-wise statistics on radiometric indices from " "a given list of multitemporal, but spatially identical, images.\n\n" "Output is a multi-band image where each band contains a result (eg. " "maximum, mean). For results taken from original values (eg. maximum), " "there is an additional band which gives the date/time at which the " "result has been found in numeric format (eg. maximum has occured on " "Apr 25, 2013 (midnight) -> 1366840800.0)") parser.add_argument("rasters", nargs='+', help="List of input images") parser.add_argument("-b", "--idx_blue", type=int, default=2, help="Index of a blue band, common to all images " "(default: 2)") parser.add_argument("-g", "--idx_green", type=int, default=3, help="Index of a green band, common to all images " "(default: 3)") parser.add_argument("-r", "--idx_red", type=int, default=4, help="Index of a red band, common to all images " "(default: 4)") parser.add_argument("-nir", "--idx_nir", type=int, default=5, help="Index of a near-infrared band, common to all " "images (default: 5)") parser.add_argument("-mir", "--idx_mir", type=int, default=6, help="Index of a mid-infrared band, common to all " "images (default: 6)") parser.add_argument("-d", "--date-from", type=valid_date, help="Date from which to compute statis " "the input images and whose values are dates in " "numeric format. If specified, statistical dates are " "computed relative to the dates in this raster") parser.add_argument( "-t", "--time_raster", help="Path to a mono-band raster with same extent than " "the input images and whose values are dates in " "numeric format. If specified, statistical dates are " "computed relative to the dates in this raster") parser.add_argument( "-o", "--out_file", default='./stats.tif', help="Path to the output file (default: 'stats.tif' in " "the current folder)") args = parser.parse_args() # Do the actual work rasters = [Raster(filename) for filename in args.rasters] ndvis = [ raster.ndvi(idx_red=args.idx_red, idx_nir=args.idx_nir, '{}.ndvi.tif'.format( os.path.basename(os.path.splitext( raster.filename)[0]))) for raster in rasters ] ndwis = [ raster.ndwi(idx_nir=args.idx_nir, idx_mir=args.idx_mir, '{}.ndwi.tif'.format( os.path.basename(os.path.splitext( raster.filename)[0]))) for raster in rasters ] mndwis = [ raster.mndwi(idx_green=args.idx_green, idx_mir=args.idx_mir, '{}.mndwi.tif'.format( os.path.basename( os.path.splitext(raster.filename)[0]))) for raster in rasters ] temporal_stats(ndvis, 'stats.ndvi.tif', 'GTiff', stats=['min', 'max', 'range']) temporal_stats(ndwis, 'stats.ndwi.tif', 'GTiff', stats=['min', 'max', 'range']) temporal_stats(mndwis, 'stats.mndwi.tif', 'GTiff', stats=['min', 'max', 'range'])
def main(): raster = Raster(IMG) raster.ndvi(3, 4, '/tmp/spot6_ndvi.tif')
def pan_sharpen(args): raster = Raster(args.raster) pan_raster = Raster(args.pan_file) raster.fusion(pan_raster, out_filename=args.out_file)
def remove_bands(args): for filename in args.raster: raster = Raster(filename) raster.remove_bands(*args.idxs, out_filename=args.out_file)
# -*- coding: utf-8 -*- """ Created on Mon Feb 9 14:13:31 2015 @author: sig """ from ymraster import Raster, write_file, get_samples_from_roi from sklearn import tree from sklearn.externals.six import StringIO import pydot #TODO install the module rst_file = "data/new_set2/Spot6_MS_31072013_masked.tif" rst = Raster(rst_file) rst_array = rst.array() roi_file ="" label_file = "" stat_file = "" statX_array, statY_array = get_samples_from_roi(label_file,roi_file, stat_file ) clf = tree.DecisionTreeClassifier() clf = clf.fit(statX_array, statY_array) classif = clf.predict(rst_array) dot_data = StringIO() tree.export_graphviz(clf, out_file=dot_data) graph = pydot.graph_from_dot_data(dot_data.getvalue())
def setUp(self): self.raster = Raster('data/l8_20130714.tif')
def concatenate(args): rasters = [Raster(raster) for raster in args.raster] concatenate_rasters(*rasters, out_filename=args.out_file)
def ndwi(args): for filename in args.in_list: raster = Raster(filename) raster.ndwi(args.idx_nir, args.idx_mir, out_filename=args.out_file)
def mndwi(args): for filename in args.in_list: raster = Raster(filename) raster.mndwi(args.idx_green, args.idx_mir, 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))
def ndvi(args): for filename in args.in_list: raster = Raster(filename) raster.ndvi(args.idx_red, args.idx_nir, out_filename=args.out_file)