def copy_geo(array, prototype=None, xoffset=0, yoffset=0): """Copy geotransfrom from prototype dataset to array but account for x, y offset of clipped array.""" ds = gdal.Open(gdal_array.GetArrayFilename(array)) prototype = gdal.Open(prototype) gdal_array.CopyDatasetInfo(prototype, ds, xoff=xoffset, yoff=yoffset) return ds
def create_tif_mask(in_fname, out_fname): # Open the raster file raster = gdal.Open(in_fname, gdal.GA_ReadOnly) band_1 = raster.GetRasterBand(1) # Read the data as a numpy array data_1 = gdal_array.BandReadAsArray(band_1) # Create a boolean band with all 1's mask = (data_1 >= 0).astype(int) assert np.mean(mask) == 1 # Write the output mask driver = gdal.GetDriverByName("GTiff") ds_out = driver.Create(out_fname, raster.RasterXSize, raster.RasterYSize, 1, band_1.DataType) gdal_array.CopyDatasetInfo(raster, ds_out) band_out = ds_out.GetRasterBand(1) gdal_array.BandWriteArray(band_out, mask) # Close the datasets band_1 = None raster = None band_out = None ds_out = None
def convert_file(f: str, output_proj: str) -> None: nodata_val = -32768 in_ds = gdal.Open(f"/vsigs/{f}") big_vals = in_ds.ReadAsArray() * 10000 np.nan_to_num(big_vals, copy=False, nan=nodata_val) out_ds = gdal_array.OpenArray(np.int16(big_vals)) gdal_array.CopyDatasetInfo(in_ds, out_ds) gdal.Warp( # Could also copy directly to vsiaz if you want f"data/hm/{os.path.basename(f)}", out_ds, dstSRS=output_proj, srcNodata=nodata_val, dstNodata=nodata_val, xRes=30, yRes=30, creationOptions=[ "COMPRESS=LZW", "PREDICTOR=2", "BLOCKXSIZE=256", "BLOCKYSIZE=256", "INTERLEAVE=BAND", "TILED=YES", ], )
def OpenArray( array, prototype_ds = None, xoff=0, yoff=0 ): ds =gdal_array.OpenArray(array) if ds is not None and prototype_ds is not None: if type(prototype_ds).__name__ == 'str': prototype_ds = gdal.Open(prototype_ds) if prototype_ds is not None: gdal_array.CopyDatasetInfo( prototype_ds, ds, xoff=xoff, yoff=yoff ) return ds
def RasterDifference(RasterFile1, RasterFile2, raster_band=1, OutFileName="Test.outfile", OutFileType="ENVI"): """ Takes two rasters of same size and subtracts second from first, e.g. Raster1 - Raster2 = raster_of_difference then writes it out to file """ Raster1 = gdal.Open(RasterFile1) Raster2 = gdal.Open(RasterFile2) print("RASTER 1: ") print(Raster1.GetGeoTransform()) print(Raster1.RasterCount) print(Raster1.GetRasterBand(1).XSize) print(Raster1.GetRasterBand(1).YSize) print(Raster1.GetRasterBand(1).DataType) print("RASTER 2: ") print(Raster2.GetGeoTransform()) print(Raster2.RasterCount) print(Raster2.GetRasterBand(1).XSize) print(Raster2.GetRasterBand(1).YSize) print(Raster2.GetRasterBand(1).DataType) raster_array1 = np.array(Raster1.GetRasterBand(raster_band).ReadAsArray()) raster_array2 = np.array(Raster2.GetRasterBand(raster_band).ReadAsArray()) assert (raster_array1.shape == raster_array2.shape) print("Shapes: ", raster_array1.shape, raster_array2.shape) difference_raster_array = raster_array1 - raster_array2 # import matplotlib.pyplot as plt # # plt.imshow(difference_raster_array) # driver = gdal.GetDriverByName(OutFileType) dsOut = driver.Create(OutFileName, Raster1.GetRasterBand(1).XSize, Raster1.GetRasterBand(1).YSize, 1, gdal.GDT_Float32) #Raster1.GetRasterBand(raster_band).DataType) gdal_array.CopyDatasetInfo(Raster1, dsOut) bandOut = dsOut.GetRasterBand(1) gdal_array.BandWriteArray(bandOut, difference_raster_array)
cutlineDSName=cutline_layer, cropToCutline=True, ) # Normalize values and convert to integer for smaller disk size values = cropped_ds.ReadAsArray() # Some very small negatives values[values < 0] = 0 # Dave: sqrt of max normalized values. Here we use 5k as a "theoretical" # max among all years (given max among 2013, 2016, and 2020 as ~3k). # Then multiply by 10000 to save as integer values = np.sqrt(values / 5000.0) * 10000.0 output_ds = gdal_array.OpenArray(np.int16(values)) output_band = output_ds.GetRasterBand(1) output_band.SetScale(0.0001) gdal_array.CopyDatasetInfo(cropped_ds, output_ds) # Reproject to output projection and re-crop, since conversion to/from # ndarray lost that info gdal.Warp( output_file, output_ds, cutlineDSName="data/azml/conus_projected.gpkg", cropToCutline=True, outputBounds=bounds, dstNodata=-32768, dstSRS=crs, creationOptions=[ "COMPRESS=LZW", "PREDICTOR=2", "BLOCKXSIZE=256",
def processQuality(self, bandlayer, qualityInfo): if bandlayer not in self.bandfiles: raise Exception('Given quality band is not available!') qualityFile = self.bandfiles[bandlayer] LOGGER.info('QualityFile '+str(qualityFile)) qualityArray = gdalnumeric.LoadFile(qualityFile) qualityValues = np.unique(qualityArray) qualityBand = self.bands[bandlayer] if not os.path.exists(self.publishPath+'/data/mask'): os.makedirs(self.publishPath+'/data/mask') if not os.path.exists(self.publishPath+'/data/output'): os.makedirs(self.publishPath+'/data/output') finalfiles = dict() for key, band in self.bands.items(): if band['imagetype'] == 'qualityInformation': continue dataFile = self.bandfiles[key] maskFile = self.publishPath+'/data/mask/'+os.path.basename(dataFile) dataFile = self.publishPath+'/data/output/'+os.path.basename(dataFile) dataDS = gdal.Open(self.bandfiles[key]) dataBand = dataDS.GetRasterBand(1) dataArray = dataBand.ReadAsArray(0, 0, dataDS.RasterXSize, dataDS.RasterYSize) dataNoData = dataBand.GetNoDataValue() maskArray = np.copy(dataArray) maskArray[:] = dataNoData if qualityBand['quality_datatype'] == 'int': values = qualityInfo.split(';') #0;1 (e.g., good data and marginal data for MOD13Q1) for quality in qualityValues: if str(quality) not in values: dataArray[qualityArray==quality] = dataNoData maskArray[qualityArray==quality] = 0 else: maskArray[qualityArray==quality] = 1 elif qualityBand['quality_datatype'] == 'bit': values = qualityInfo.split(';') #2-5=0000;6-7<10 valuesAr = self.splitBinaryQualityInfo(values) for quality in qualityValues: val = np.binary_repr(quality).zfill(16)[::-1] #flipped result = self.checkQualityBinary(val, valuesAr) LOGGER.info('Quality value '+val+' set to '+str(result)) if result: maskArray[qualityArray==quality] = 1 else: maskArray[qualityArray==quality] = 0 dataArray[qualityArray==quality] = dataNoData else: LOGGER.error('No quality info') raise Exception('No quality info') dataDSMasked = gdal_array.SaveArray(dataArray, dataFile, 'HDF4Image') gdal_array.CopyDatasetInfo(dataDS, dataDSMasked) maskDSMasked = gdal_array.SaveArray(maskArray, maskFile, 'HDF4Image') gdal_array.CopyDatasetInfo(dataDSMasked, maskDSMasked) finalfiles[key] = dataFile maskDS, maskDSMasked, dataDS, dataDSMasked = [None]*4 del maskDS, maskDSMasked, dataDS, dataDSMasked return finalfiles
def Copy_Geo(array, prototype=None, xoffset=0, yoffset=0): #从原数据集拷贝数组 ds = gdal.Open(gdal_array.GetArrayFilename(array)) prototype = gdal.Open(prototype) gdal_array.CopyDatasetInfo(prototype, ds, xoff=xoffset, yoff=yoffset) return ds
def reclassify_raster(infile, outfile, operator, threshold, pixel_value=100, frmt="GTiff", silence=True): """ Reclassify a raster into a single value according to a threshold :param infile: path to the input :param outfile: path to the output :param operator: string, possible values ">","<",">=","<=" :param threshold: threshold value :param pixel_value: the output pixel value for pixels that pass the threshold :param frmt: the output format, default "GTiff" :param silence: pass any value to enable debug info :return: None """ '''using this instead of gdal_calc because gdal calc didn't work scriptpath = os.path.join(os.path.dirname(__file__), '../', "pysdss/utility/gdal_calc.py") scriptpath = os.path.normpath(scriptpath) params = [scriptpath,"-A",path+input,"--outfile="+path+output,"--calc='(A>0.5)'", "--debug"] print("Reclassify raster") out,err = utils.run_script(params, callpy=["python3"]) print("output" + out) if err: print("ERROR:" + err) raise Exception("gdal grid failed")''' if operator not in [">", "<", ">=", "<="]: raise Exception("unknown operator") band1 = None ds1 = None bandOut = None dsOut = None try: if not silence: print("opening input") # Open the dataset ds1 = gdal.Open(infile, gdct.GA_ReadOnly) band1 = ds1.GetRasterBand(1) # Read the data into numpy arrays data1 = gdar.BandReadAsArray(band1) # get the nodata value nodata = band1.GetNoDataValue() if not silence: print("filtering input") # The actual calculation filter = "data1" + operator + str(threshold) data1[eval(filter)] = pixel_value data1[data1 != pixel_value] = nodata if not silence: print("output result") # Write the out file driver = gdal.GetDriverByName(frmt) dsOut = driver.Create(outfile, ds1.RasterXSize, ds1.RasterYSize, 1, band1.DataType) gdar.CopyDatasetInfo(ds1, dsOut) bandOut = dsOut.GetRasterBand(1) bandOut.SetNoDataValue(nodata) gdar.BandWriteArray(bandOut, data1) except RuntimeError as err: raise err except Exception as e: raise e finally: # Close the datasets if band1: band1 = None if ds1: ds1 = None if bandOut: bandOut = None if dsOut: dsOut = None