def mask_by_row(self, path, output): ''' This method replaces the masked value for ones in the target array, one row at a time. ''' outDataset = create_empty_raster_from_reference( output, path, data_type=gdal.GDT_Byte) dataset = gdal.Open(path, gdalconst.GA_ReadOnly) if dataset is None: print "The dataset could not be opened" sys.exit(-1) classification_band = dataset.GetRasterBand(1) rows = classification_band.YSize cols = classification_band.XSize print rows for row in range(rows): outputLine = str('') scanline = classification_band.ReadRaster( 0, row, classification_band.XSize, 1, classification_band.XSize, 1, gdal.GDT_Float32) row_tuple = struct.unpack('f' * classification_band.XSize, scanline) new_row = numpy.zeros(len(row_tuple)) old_row = numpy.array(row_tuple) new_row[old_row == MASK_ARRAY] = 1 new_row.astype('f').tostring() outDataset.GetRasterBand(1).WriteArray( numpy.resize(new_row, (1, cols)), 0, row) del outputLine del old_row del new_row del row_tuple
def mask_by_row(self, path, output): ''' This method replaces the masked value for ones in the target array, one row at a time. ''' outDataset = create_empty_raster_from_reference(output, path, data_type=gdal.GDT_Byte) dataset = gdal.Open(path, gdalconst.GA_ReadOnly) if dataset is None: print "The dataset could not be opened" sys.exit(-1) classification_band = dataset.GetRasterBand(1) rows = classification_band.YSize cols = classification_band.XSize print rows for row in range(rows): outputLine = str('') scanline = classification_band.ReadRaster( 0, row, classification_band.XSize, 1, classification_band.XSize, 1, gdal.GDT_Float32 ) row_tuple = struct.unpack('f' * classification_band.XSize, scanline) new_row = numpy.zeros(len(row_tuple)) old_row = numpy.array(row_tuple) new_row[old_row==MASK_ARRAY] = 1 new_row.astype('f').tostring() outDataset.GetRasterBand(1).WriteArray(numpy.resize(new_row, (1, cols)), 0, row) del outputLine del old_row del new_row del row_tuple
def method_by_block(self, path, output, block_size=8192): ''' Method to create a mask from a given raster writing by block. ''' outDataset = create_empty_raster_from_reference( output, path, data_type=gdal.GDT_Byte) dataset = gdal.Open(path, gdalconst.GA_ReadOnly) if dataset is None: print "The dataset could not be opened" sys.exit(-1) classification_band_a = dataset.GetRasterBand(1) classification_band_b = dataset.GetRasterBand(2) classification_band_c = dataset.GetRasterBand(3) classification_band_d = dataset.GetRasterBand(4) classification_band_e = dataset.GetRasterBand(5) classification_band_f = dataset.GetRasterBand(6) y_size = classification_band_a.YSize x_size = classification_band_a.XSize value_to_mask = 32 total = (y_size + block_size) * (x_size + block_size) / block_size / block_size count = 0 current = -1 for i in range(0, y_size, block_size): if i + block_size < y_size: rows = block_size else: rows = y_size - i for j in range(0, x_size, block_size): if j + block_size < x_size: cols = block_size else: cols = x_size - j A = classification_band_a.ReadAsArray(j, i, cols, rows).astype(numpy.int16) B = classification_band_b.ReadAsArray(j, i, cols, rows).astype(numpy.int16) C = classification_band_c.ReadAsArray(j, i, cols, rows).astype(numpy.int16) D = classification_band_d.ReadAsArray(j, i, cols, rows).astype(numpy.int16) E = classification_band_e.ReadAsArray(j, i, cols, rows).astype(numpy.int16) F = classification_band_f.ReadAsArray(j, i, cols, rows).astype(numpy.int16) res = numpy.maximum.reduce([A, B, C, D, E, F]) count = count + 1 outDataset.GetRasterBand(1).WriteArray(res, j, i) aux = current current = count * 100 / total if aux != current: print current outDataset = None
def method_by_block(self, path, output, block_size=8192): ''' Method to create a mask from a given raster writing by block. ''' outDataset = create_empty_raster_from_reference(output, path, data_type=gdal.GDT_Byte) dataset = gdal.Open(path, gdalconst.GA_ReadOnly) if dataset is None: print "The dataset could not be opened" sys.exit(-1) classification_band_a = dataset.GetRasterBand(1) classification_band_b = dataset.GetRasterBand(2) classification_band_c = dataset.GetRasterBand(3) classification_band_d = dataset.GetRasterBand(4) classification_band_e = dataset.GetRasterBand(5) classification_band_f = dataset.GetRasterBand(6) y_size = classification_band_a.YSize x_size = classification_band_a.XSize value_to_mask = 32 total = (y_size + block_size) * (x_size + block_size) / block_size / block_size count = 0 current = -1 for i in range(0, y_size, block_size): if i + block_size < y_size: rows = block_size else: rows = y_size - i for j in range(0, x_size, block_size): if j + block_size < x_size: cols = block_size else: cols = x_size - j A = classification_band_a.ReadAsArray(j, i, cols, rows).astype(numpy.int16) B = classification_band_b.ReadAsArray(j, i, cols, rows).astype(numpy.int16) C = classification_band_c.ReadAsArray(j, i, cols, rows).astype(numpy.int16) D = classification_band_d.ReadAsArray(j, i, cols, rows).astype(numpy.int16) E = classification_band_e.ReadAsArray(j, i, cols, rows).astype(numpy.int16) F = classification_band_f.ReadAsArray(j, i, cols, rows).astype(numpy.int16) res = numpy.maximum.reduce([A,B,C,D,E,F]) count = count + 1 outDataset.GetRasterBand(1).WriteArray(res,j,i) aux = current current = count * 100 / total if aux != current: print current outDataset = None
def aggregate_by_block(self, path, output, ini_arr, fin_arr, block_size=8192): ''' Method to create a mask from a given raster writing by block. This is attained by reading the dataset in blocks, this is useful when the original raster is really big. ''' outDataset = create_empty_raster_from_reference( output, path, data_type=gdal.GDT_Byte, create_options=['COMPRESS=LZW BIGTIFF=YES']) dataset = gdal.Open(path, gdalconst.GA_ReadOnly) my_dictionary = dictionary_from_list(ini_arr, fin_arr) if dataset is None: print "The dataset could not be opened" sys.exit(-1) classification_band = dataset.GetRasterBand(1) y_size = classification_band.YSize x_size = classification_band.XSize value_to_mask = 32 total = (y_size + block_size) * (x_size + block_size) / block_size / block_size count = 0 current = -1 for i in range(0, y_size, block_size): if i + block_size < y_size: rows = block_size else: rows = y_size - i for j in range(0, x_size, block_size): if j + block_size < x_size: cols = block_size else: cols = x_size - j data_array = classification_band.ReadAsArray( j, i, cols, rows).astype(numpy.int16) to_vector_array = replace_in_array(data_array, my_dictionary) count = count + 1 outDataset.GetRasterBand(1).WriteArray(to_vector_array, j, i) aux = current current = count * 100 / total if aux != current: LOGGER.debug('%s' % current) outDataset = None
def mask_by_block(self, path, output, block_size=8192): ''' Method to create a mask from a given raster writing by block. This is attained by reading the dataset in blocks, this is useful when the original raster is really big. ''' outDataset = create_empty_raster_from_reference( output, path, data_type=gdal.GDT_Byte) dataset = gdal.Open(path, gdalconst.GA_ReadOnly) if dataset is None: print "The dataset could not be opened" sys.exit(-1) classification_band = dataset.GetRasterBand(1) y_size = classification_band.YSize x_size = classification_band.XSize value_to_mask = 32 total = (y_size + block_size) * (x_size + block_size) / block_size / block_size count = 0 current = -1 for i in range(0, y_size, block_size): if i + block_size < y_size: rows = block_size else: rows = y_size - i for j in range(0, x_size, block_size): if j + block_size < x_size: cols = block_size else: cols = x_size - j data_array = classification_band.ReadAsArray( j, i, cols, rows).astype(numpy.int16) data_array[data_array != value_to_mask] = 0 data_array[data_array == value_to_mask] = 1 count = count + 1 outDataset.GetRasterBand(1).WriteArray(data_array, j, i) aux = current current = count * 100 / total if aux != current: print current outDataset = None
def mask_by_block(self, path, output, block_size=8192): ''' Method to create a mask from a given raster writing by block. This is attained by reading the dataset in blocks, this is useful when the original raster is really big. ''' outDataset = create_empty_raster_from_reference(output, path, data_type=gdal.GDT_Byte) dataset = gdal.Open(path, gdalconst.GA_ReadOnly) if dataset is None: print "The dataset could not be opened" sys.exit(-1) classification_band = dataset.GetRasterBand(1) y_size = classification_band.YSize x_size = classification_band.XSize value_to_mask = 32 total = (y_size + block_size) * (x_size + block_size) / block_size / block_size count = 0 current = -1 for i in range(0, y_size, block_size): if i + block_size < y_size: rows = block_size else: rows = y_size - i for j in range(0, x_size, block_size): if j + block_size < x_size: cols = block_size else: cols = x_size - j data_array = classification_band.ReadAsArray(j, i, cols, rows).astype(numpy.int16) data_array[data_array!=value_to_mask] = 0 data_array[data_array==value_to_mask] = 1 count = count + 1 outDataset.GetRasterBand(1).WriteArray(data_array,j,i) aux = current current = count * 100 / total if aux != current: print current outDataset = None