def get_lats_lons(example, shape): gdal.UseExceptions() # Create temporary tif-file. temp_file = tempfile.NamedTemporaryFile(suffix='.tif').name inDriver = ogr.GetDriverByName("ESRI Shapefile") inDataSource = inDriver.Open(shape, 1) inLayer = inDataSource.GetLayer() options = gdal.WarpOptions( cutlineDSName=shape, cutlineLayer=inLayer.GetName(), cropToCutline=False, dstNodata=-9999, ) sourceds = gdal.Warp(temp_file, example, options=options) geot = sourceds.GetGeoTransform() xsize = sourceds.RasterXSize # columns ysize = sourceds.RasterYSize # rows lons = np.arange(geot[0], (geot[0] + xsize * geot[1]) - geot[1] + 1e-10, geot[1]) + 0.5 * geot[1] lats = np.arange(geot[3], (geot[3] + ysize * geot[5]) - geot[5] - 1e-10, geot[5]) + 0.5 * geot[5] minX = geot[0] minY = geot[3] + ysize * geot[5] maxX = geot[0] + xsize * geot[1] maxY = geot[3] assert lats.size == ysize assert lons.size == xsize print(np.diff(lats)[0] - geot[5]) print(np.diff(lons)[0] - geot[1]) optionsProj = gdal.WarpOptions( outputBounds=(minX, minY, maxX, maxY), width=xsize, height=ysize, dstNodata=-9999, options=["GDALWARP_IGNORE_BAD_CUTLINE YES"], ) optionsClip = gdal.WarpOptions( cutlineDSName=shape, cutlineLayer=inLayer.GetName(), cropToCutline=False, outputBounds=(minX, minY, maxX, maxY), width=xsize, height=ysize, dstNodata=-9999, options=["GDALWARP_IGNORE_BAD_CUTLINE YES"], ) return lats, lons, optionsProj, optionsClip
def RPCOrthorectification(Alpha=True,is_label=False): try: if not is_label: orginalimage=os.path.join(uploadfiles[0],'chaneltransform.tif') transform_rpc = os.path.join(uploadfiles[0], 'chaneltransform_rpc.txt') else: orginalimage=os.path.join(uploadfiles[0],'label.tif') transform_rpc=os.path.join(uploadfiles[0],'label_rpc.txt') origin_rpc=os.path.join(uploadfiles[0],uploadfiles[2]) shutil.copyfile(origin_rpc,transform_rpc) # with open(rpbfile,'r') as f: # for line in f.readlines(): # hoffLine=re.search(r'heightOffset = ([\+|\-|\d]\d+\.?\d+)',line) # if hoffLine: # hoff=hoffLine.group(1) # break # f.close() # RpcHeight="['RPC_HEIGHT="+str(hoff)+"]'" # transformerOptions=RpcHeight if Alpha: warpOP = gdal.WarpOptions(dstSRS='WGS84', rpc=True, multithread=True, errorThreshold=0.0,creationOptions=['Tiled=yes'], resampleAlg=gdal.gdalconst.GRIORA_Bilinear,dstAlpha=True) else: warpOP = gdal.WarpOptions(dstSRS='WGS84', rpc=True, multithread=True, errorThreshold=0.0,creationOptions=['Tiled=yes'], resampleAlg=gdal.gdalconst.GRIORA_Bilinear,dstNodata=0) image = gdal.Open(orginalimage.encode('utf-8'),gdal.GA_ReadOnly) RPCOrthImage = os.path.join(uploadfiles[0],os.path.basename(orginalimage).replace(".tif","RPC.tif")) srcDS = gdal.Warp(RPCOrthImage.encode('utf-8').decode(), image, options=warpOP) image=None srcDS=None return RPCOrthImage except Exception as e: return Exception("上传失败,RPC正射校正出错:"+str(e))
def create_dem(self, dem_files): dem_files = list(dem_files.values()) output_folder = os.path.dirname(dem_files[0]) dem_src = os.path.join( output_folder, f'Copernicus_{self.mgrs_tile}_{self.resolution}_mosaic.tif') LOGGER.info('Creating DEM mosaic...') no_data = -20000 try: # Mosaic if self.cross_dateline: gdal.SetConfigOption('CENTER_LONG', '180') options = gdal.WarpOptions(dstNodata=no_data, outputType=gdal.GDT_Int16, dstSRS='EPSG:4326') ds = gdal.Warp(dem_src, dem_files, options=options) # Replace no-data by 0 dem_band = ds.GetRasterBand(1) dem_arr = dem_band.ReadAsArray() dem_arr[dem_arr == no_data] = 0 dem_band.WriteArray(dem_arr) dem_band.FlushCache() ds = None gdal.SetConfigOption('CENTER_LONG', '0') LOGGER.debug('DEM mosaic: {}'.format(dem_src)) except Exception as e: LOGGER.fatal(e, exc_info=True) LOGGER.fatal('error using gdalwarp') else: # Crop and reproject LOGGER.info('Cropping and projecting DEM...') extent = self.extent(True) os.makedirs(os.path.dirname(self.dem_output), exist_ok=True) options = gdal.WarpOptions(dstSRS=self.hcs_code, xRes=self.resolution, yRes=self.resolution, resampleAlg='cubicspline', outputType=gdal.GDT_Int16, outputBounds=(extent[0], extent[2], extent[1], extent[3])) try: gdal.Warp(self.dem_output, dem_src, options=options) except Exception as e: LOGGER.fatal(e, exc_info=True) LOGGER.fatal('error using gdalwarp') else: return self.dem_output return None
def RPCOrthorectification(orginalimage, rpcfile, rpbfile, Alpha=True): try: rpcname = os.path.join( os.path.dirname(orginalimage), os.path.basename(orginalimage).replace(".tif", "_rpc.txt")) shutil.copyfile(rpcfile, rpcname) with open(rpbfile, 'r') as f: for line in f.readlines(): hoffLine = re.search( r'heightOffset = ([\+|\-|\d]\d+\.?\d+)', line) if hoffLine: hoff = hoffLine.group(1) break f.close() RpcHeight = "['RPC_HEIGHT=" + str(hoff) + "]'" if Alpha: warpOP = gdal.WarpOptions( dstSRS='WGS84', rpc=True, multithread=True, errorThreshold=0.0, creationOptions=['Tiled=yes'], resampleAlg=gdal.gdalconst.GRIORA_Bilinear, transformerOptions=RpcHeight, dstAlpha=True) else: warpOP = gdal.WarpOptions( dstSRS='WGS84', rpc=True, multithread=True, errorThreshold=0.0, creationOptions=['Tiled=yes'], resampleAlg=gdal.gdalconst.GRIORA_Bilinear, transformerOptions=RpcHeight, dstNodata=0) image = gdal.Open(orginalimage.encode('utf-8'), gdal.GA_ReadOnly) RPCOrthImage = os.path.join( tempfolder, os.path.basename(orginalimage).replace(".tif", "RPC.tif")) srcDS = gdal.Warp(RPCOrthImage.encode('utf-8').decode(), image, options=warpOP) image = None srcDS = None return RPCOrthImage except Exception: return "上传失败,RPC正射校正出错"
def clip_image_by_grid(self, file_path, res_path, tile_name, grid_type, sat_type): """ clip image by grid, search target grid geometry from database """ db_conn = "PG:host={host} user='******' password='******' dbname='{dbname}'".format( host=DB_INFO["host"], user=DB_INFO["user"], password=DB_INFO["password"], dbname=DB_INFO["database"], ) if grid_type == "MRGS": sql_query = "SELECT geom FROM postgis.MRGS WHERE name='{}'".format( tile_name) elif grid_type == "WRS2": path = tile_name[0:3] row = tile_name[3:6] sql_query = "SELECT geom FROM postgis.wrs2 WHERE path='{}' and row='{}'".format( path, row) else: raise ValueError("can't support this grid :{0}".format(grid_type)) warp_opts = gdal.WarpOptions( cutlineDSName=db_conn, cutlineSQL=sql_query, cropToCutline=True, dstNodata=SAT_NODATA_DICT[sat_type], ) res_ds = gdal.Warp(res_path, file_path, options=warp_opts) assert res_ds is not None, "clip result is None"
def __get_src_dataset(self): """ Get source dataset """ # Source dataset src_dataset = gdal.Open(self.source_img) # Get driver driver = src_dataset.GetDriver() # If driver is HDF5Image get Projection and GeoTransform # from file metadata, GDAL cannot read automatically the info if driver.GetDescription() == 'HDF5Image': proj, gt = self.__get_srs_hdf5(src_dataset.GetFileList()[0]) src_dataset.SetProjection(proj) src_dataset.SetGeoTransform(gt) if not self.extent is None: # GDAL warp option # outputBounds (minX, minY, maxX, maxY) # self.extent is (w, e, s, n) outputBounds = (self.extent[0], self.extent[2], self.extent[1], self.extent[3]) gdal_warp_options = gdal.WarpOptions(outputBounds=outputBounds, format='MEM') # Warp to a memory dataset src_dataset = gdal.Warp('', src_dataset, options=gdal_warp_options) return src_dataset
def set_no_data_value(tiff_path, no_data_value, res_format='GTiff', out_tiff_path = ''): """设置 tiff 的无效值, 原来的无效值还是无效值,同时设置新的无效值, 可以返回 dataset 或 改变保存至文件""" # FIXME 可以使用更新模式来做,这样就可以将变化更新到源文件上去 warp_option = gdal.WarpOptions(format=res_format, dstNodata=no_data_value) return gdal.Warp(out_tiff_path, tiff_path, options=warp_option)
def crop_data(): setup_env() temp_path = os.getenv("TEMP_PATH") boundaries_path = os.getenv("BOUNDARIES_PATH") bounds = os.getenv("BOUNDS") print("Starting cropping...") filelist = create_filelist() options = gdal.WarpOptions(resampleAlg=gdal.GRA_NearestNeighbour, format="GTiff", cutlineDSName=boundaries_path + "/" + bounds + "/" + bounds + ".shp", dstSRS="+proj=stere +lon_0=10.0 +lat_0=90.0 +lat_ts=60.0 +a=6370040 +b=6370040 +units=m", srcSRS="+proj=stere +lon_0=10.0 +lat_0=90.0 +lat_ts=60.0 +a=6370040 +b=6370040 +units=m", cropToCutline=True) for file in tqdm(filelist, unit=" files"): file_split = file.split("/") date_time_obj = datetime.strptime( file_split[len(file_split)-1], 'RW_%Y%m%d-%H%M.asc') _ = gdal.Warp( temp_path + "/cropped/{}.tif".format(date_time_obj.strftime("%Y%m%d-%H%M")), file, options=options) _ = None print("Cropping complete.")
def hdf2tif(hdf, reproject=True): """ Converts hdf files to tiff files :param hdf: HDF file to be processed :param reproject: Will be reprojected by default :return: None """ dataset = gdal.Open(hdf, gdal.GA_ReadOnly) subdatasets = dataset.GetSubDatasets() data_dir = create_output_directory(hdf) convert_to_vrt(subdatasets, data_dir) vrt_options = gdal.BuildVRTOptions(separate=True) vrt_list = list_files(data_dir, 'vrt') vrt_output = hdf.replace('.hdf', '.vrt') gdal.BuildVRT(vrt_output, sorted(vrt_list), options=vrt_options) if reproject: proj = "+proj=sinu +R=6371007.181 +nadgrids=@null +wktext" warp_options = gdal.WarpOptions(srcSRS=proj, dstSRS="EPSG:3857") else: warp_options = "" output_tiff = vrt_output.replace(".vrt", "_reprojected.tif") if not os.path.exists(output_tiff): gdal.Warp(output_tiff, vrt_output, options=warp_options) clear_temp_files(data_dir, vrt_output) return os.path.join(DIRECTORY, output_tiff)
def project(input_tif, output_tif): """ gdalwarp栅格投影转换 :param input_tif: :param output_tif: :return: """ opt = gdal.WarpOptions(srcSRS="EPSG:4326", dstSRS="EPSG:32651") dst = gdal.Warp(output_tif, input_tif, options=opt)
def cropDTM2geodata(path_dest, dtm, geo_data): new_bounds = (geo_data.extent[0], geo_data.extent[2], geo_data.extent[1], geo_data.extent[3]) #destName = "C:\\Users\\elisa\\Documents\\git\\MSc\\GempyTopography\\cropped_DTM.tif" gdal.Warp(path_dest, dtm, options=gdal.WarpOptions(options=['outputBounds'], outputBounds=new_bounds)) return gdal.Open(path_dest)
def one_subset(supplierId, filename, polygon, tilepath, tifpath, size,sentinel=2): """ Creates one subsetted image from the large sentinel tile :param supplierId: supplierId (also the name) of the Sentinel 2 tile (str) :param filename: Filename of output image (str) :param polygon: Shapely polygon of desired bla :param tilepath: Path to Sentinel tiles :param tifpath: Path to output images :param size: Length of one side of the output image in pixels :return: none """ # gdalwarp inputs polygons as a csv file. This code creates that csv file so that we can run gdalwarp csvData = [["", "WKT"], [str(1), polygon.wkt]] csvname = './%s.csv' % filename[:-4] with open(csvname, 'w') as csvFile: writer = csv.writer(csvFile) writer.writerows(csvData) csvFile.close() # TODO: Change this in verbose mode #gdal.PushErrorHandler('CPLQuietErrorHandler') os.environ["PROJ_LIB"]="C:/Users/danie/Anaconda3/envs/oilrig/Library/share/proj" # Finds all the image bands as jp2 files and sorts them alphabetically to retain correct order dir = os.path.join(tilepath, supplierId) dir = os.path.abspath(dir) dir = dir.replace('\\', '/') if sentinel == 1: file: ZipFile = ZipFile(dir + '.zip', 'r') fulllist = sorted([name for name in file.namelist() if name.endswith('.tiff') or name.endswith('.dat')]) for tif in fulllist: if not os.path.exists(dir+".SAFE"): file.extract(tif,path=tilepath) fulllist = [os.path.join(tilepath,tif).replace('\\', '/') for tif in fulllist] else: fulllist = sorted(glob.glob(dir + '/*.jp2')) # Builds VRT dataset to speed up conversion vrtname = './%s.vrt' % filename[:-4] buildvrt_options = gdal.BuildVRTOptions(separate=True, xRes=10, yRes=10) vrt_dataset = gdal.BuildVRT(destName=vrtname, srcDSOrSrcDSTab=fulllist, options=buildvrt_options) try: # Subsets image using gdalwarp warp_output = os.path.join(tifpath, filename + ".tif") warp_options = gdal.WarpOptions(cropToCutline=True, cutlineDSName=csvname, srcSRS="EPSG:4326", dstSRS="EPSG:4326", width=size, height=size,multithread=True) gdal.Warp(warp_output, vrt_dataset, options=warp_options) except SystemError as e: os.remove(csvname) return # removes the temporary csv file os.remove(csvname) return
def subdividePixel(self, subdivisions, outputType, outFile=""): """ Permet de subdiviser une image MODIS en un certain nombre de subdivisions pour chaque pixel. On conserve la même valeur de pixel pour chacune des subdivisions (utilisation de 'near' comme resampleAlg). Args: subdivisions (int ou float): nombre de subdivisions voulues (ex: pour une valeur de 10, on subdivise chaque pixel de 1km en 100 (10²) pixels de 100m. outputType (string): type de output qu'on veut que la fonction produise. Si "array", produit un array numpy. Si "file" produit un fichier vrt qu'on peut réutiliser à l'externe de la classe. outFile (string): nom du fichier à sauvegarder si on choisit l'option outputType = "file" Returns: warped (Numpy.array): Array Numpy de l'image avec subdivisions de pixels (si l'option "array" est choisie pour le outputType) """ # ******* à ajuster pour inclure le qa aussi ******* lst_image = Image(self.lst) if outputType == "array": subdividedImage = gdal.Warp( "warped.vrt", lst_image.filename, format="vrt", options=gdal.WarpOptions(xRes=lst_image.gt[1] / subdivisions, yRes=-lst_image.gt[5] / subdivisions, resampleAlg='near')) subdividedImage = None warpedImage = Image("warped.vrt") warped = warpedImage.getArray() os.remove("warped.vrt") return warped elif outputType == "file": subdividedImage = gdal.Warp( outFile, lst_image.filename, format="vrt", options=gdal.WarpOptions(xRes=lst_image.gt[1] / subdivisions, yRes=-lst_image.gt[5] / subdivisions, resampleAlg='near')) subdividedImage = None
def mosaic(fn1, fn2, dst_fn, vector): scripts = 'C:/Miniconda3/envs/python37/Lib/site-packages/osgeo/scripts/gdal_merge.py' # mosaic1 dst_fn_tmp = dst_fn.replace('.tif', '_tmp.tif') os.system('python %s -o %s %s %s' % (scripts, dst_fn_tmp, fn1, fn2)) options = gdal.WarpOptions(cutlineDSName=vector, cropToCutline=True, dstNodata=0) dst_fn1 = dst_fn.replace('.tif', '_1.tif') gdal.Warp(dst_fn1, dst_fn_tmp, options=options) os.remove(dst_fn_tmp) # mosaic2 dst_fn2 = dst_fn.replace('.tif', '_2.tif') os.system('python %s -o %s %s %s' % (scripts, dst_fn_tmp, fn2, fn1)) options = gdal.WarpOptions(cutlineDSName=vector, cropToCutline=True, dstNodata=0) gdal.Warp(dst_fn2, dst_fn_tmp, options=options) os.remove(dst_fn_tmp)
def extend_tiff_to_range(tiff_path, out_tiff_path, output_bounds, res_format='GTiff'): """ 将 tiff 扩展到需要的范围 :param tiff_path: 输入 tiff :param out_tiff_path: 输出 tiff :param output_bounds: 输出的 tiff 范围 :param res_format: 返回的形式, 'GTiff':'tiff' , 'MEM': 内存 :return: """ warp_option = gdal.WarpOptions(outputBounds=output_bounds, format=res_format) return gdal.Warp(out_tiff_path, tiff_path, options=warp_option)
def convert_to_ellipsoid_heights(self, dst_fname, # type: str geoidgrid_fname # type: str ): # type: (...) -> None dst_srs = self.gtiff.get_point_calculator().get_projection().srs src_srs = dst_srs + ' +geoidgrids=' + geoidgrid_fname ops = gdal.WarpOptions(srcSRS=src_srs, dstSRS=dst_srs) new_gtiff = GeotiffImage.gdalwarp_geotiff_image(self.gtiff, ops, dst_fname) self.gtiff.close_image() self.set_geotiff_image(new_gtiff)
def reproject(self, dataset: Union[str, gdal.Dataset]) -> gdal.Dataset: if type(dataset) is str: dataset = gdal.Open(dataset) if self._resampling_mode is None: resampling_mode = _get_resampling(dataset, self._bounds, self._x_res, self._y_res, self._bounds_srs, self._destination_srs) else: resampling_mode = self._resampling_mode warp_options = gdal.WarpOptions(format='Mem', outputBounds=self._bounds, outputBoundsSRS=self._bounds_srs, xRes=self._x_res, yRes=self._y_res, dstSRS=self._destination_srs, resampleAlg=resampling_mode) reprojected_data_set = gdal.Warp('', dataset, options=warp_options) return reprojected_data_set
def delete2(): nbands = 4 mask_value = 65533 # input_path: 输入svi01文件路径 # output_path: 输出tif文件路径 input_path = r'E:\NPP\ori\20200704\1344\RNSCA-RVIRS_npp_d20200704_t1344_svi01.h5' merge_info = { "svi01": (input_path, 'All_Data/VIIRS-I1-SDR_All/Radiance'), "svi02": (input_path.replace("svi01.h5", "svi02.h5"), 'All_Data/VIIRS-I2-SDR_All/Radiance'), "svi03": (input_path.replace("svi01.h5", "svi03.h5"), 'All_Data/VIIRS-I3-SDR_All/Radiance'), "svi04": (input_path.replace("svi01.h5", "svi04.h5"), 'All_Data/VIIRS-I4-SDR_All/Radiance'), "Latitude": (input_path.replace("svi01.h5", "gimgo.h5"), 'All_Data/VIIRS-IMG-GEO_All/Latitude'), "Longitude": (input_path.replace("svi01.h5", "gimgo.h5"), 'All_Data/VIIRS-IMG-GEO_All/Longitude') } out_merge_path = input_path.replace('_svi01.h5', '_merge.hdf') out_merge_file = h5py.File(out_merge_path, 'w') radiance_factor = {} for key in merge_info.keys(): cur_file_path = merge_info[key][0] cur_h5_file = h5py.File(cur_file_path, 'r') # 获取数组 cur_ds = cur_h5_file[merge_info[key][1]] cur_data = np.array(cur_ds) if key not in ['Latitude', 'Longitude']: cur_factor = np.array(cur_h5_file[merge_info[key][1] + 'Factors']) gain = cur_factor[0] bias = cur_factor[1] radiance_factor[key] = (gain, bias) print(gain) print(bias) out_merge_file[key] = cur_data out_merge_file.close() out_geo_tif = 'E:/NPP/temp/svi01.tif' ds = gdal.Open(out_merge_path) abc = get_dataset_path(ds, 'svi01') warp_option = gdal.WarpOptions(xRes=0.0037, yRes=0.0037, dstSRS="EPSG:4326", format='GTiff', geoloc=True) gdal.Warp(out_geo_tif, abc, options=warp_option)
def smac_correction_grid(obs_datetime, extent, hcs_code, resolution=120): output_filename = 'output_file.tif' ecmwf_data = EMWF_Product( config.get('cams_dir'), cams_hourly_directory=config.get('cams_hourly_dir'), cams_climatology_directory=config.get('cams_climatology_dir'), observation_datetime=obs_datetime) new_SRS = gdal.osr.SpatialReference() new_SRS.ImportFromEPSG(int(4326)) if ecmwf_data.is_valid: # Write cams file cams_file = 'cams_file.tif' etype = gdal.GDT_Float32 driver = gdal.GetDriverByName('GTiff') dst_ds = driver.Create(cams_file, xsize=ecmwf_data.longitude.size, ysize=ecmwf_data.latitude.size, bands=4, eType=etype, options=[]) dst_ds.SetProjection(new_SRS.ExportToWkt()) x_res = (ecmwf_data.longitude.max() - ecmwf_data.longitude.min()) / ecmwf_data.longitude.size y_res = (ecmwf_data.latitude.max() - ecmwf_data.latitude.min()) / ecmwf_data.latitude.size geotranform = (ecmwf_data.longitude.min(), x_res, 0, ecmwf_data.latitude.max(), 0, -y_res) dst_ds.SetGeoTransform(geotranform) dst_ds.GetRasterBand(1).WriteArray(ecmwf_data.aod550.astype(np.float)) dst_ds.GetRasterBand(2).WriteArray(ecmwf_data.tcwv.astype(np.float)) dst_ds.GetRasterBand(3).WriteArray(ecmwf_data.gtco3.astype(np.float)) dst_ds.GetRasterBand(4).WriteArray(ecmwf_data.msl.astype(np.float)) dst_ds.FlushCache() # Warp cams data on input spatial extent options = gdal.WarpOptions(srcSRS=dst_ds.GetProjection(), dstSRS=hcs_code, xRes=resolution, yRes=resolution, resampleAlg='cubicspline', outputBounds=extent) gdal.Warp(output_filename, cams_file, options=options) dst_ds = None return output_filename
def get_reprojected_tile(self, tile): source_srs = self.get_source_projection() target_srs = get_mercator_projection() warp_options = gdal.WarpOptions(srcSRS=source_srs, dstSRS=target_srs, format="MEM", warpMemoryLimit=self.memory, multithread=True, outputBounds=tile.corners, width=256, height=256, resampleAlg=self.resampling_method) tile = gdal.Warp("", self.file_path, options=warp_options) return tile
def create_consolidated_water_mask(self): sentinel2_mask_path=os.path.join(self.output_directory, 'sentinel2_water_mask.tif') sentinel2_mask_ds=gdal.Open(sentinel2_mask_path) sentinel2_mask_nodata_value=sentinel2_mask_ds.GetRasterBand(1).GetNoDataValue() sentinel2_mask_array=sentinel2_mask_ds.GetRasterBand(1).ReadAsArray() sentinel2_mask_ds=None landsat_mask_path=os.path.join(self.output_directory, 'landsat_water_mask.tif') landsat_mask_ds=gdal.Warp('MEM', landsat_mask_path, options=gdal.WarpOptions(format = 'MEM', xRes = 10, yRes = -10)) landsat_mask_nodata_value=landsat_mask_ds.GetRasterBand(1).GetNoDataValue() landsat_mask_array=landsat_mask_ds.GetRasterBand(1).ReadAsArray() landsat_mask_ds=None sentinel2_mask_array[sentinel2_mask_array==sentinel2_mask_nodata_value]=landsat_mask_array save_array_as_gtiff(sentinel2_mask_array, os.path.join(self.output_directory, 'sentinel2_landsat_water_mask.tif'), gtiff_path=os.path.join(self.output_directory, 'sentinel2_water_mask.tif'), dtype='uint', nodata_value=landsat_mask_nodata_value)
def extract_by_mask(in_tiff_path, shp_path, out_tiff_path= '', no_data_value= None, crop_to_cutline= True, res_format = 'GTiff'): """ 按掩膜提取 :param in_tiff_path: 被掩膜的 tiff :param shp_path: 用于掩膜的 shp :param out_tiff_path: 输出路径,如果输出路径设置为 '' 则输出 MEM 模式(临时文件) :param no_data_value: 输出 dataset 的无效值,当 输入的 tiff 无无效值时,需要进行设置 :param crop_to_cutline: 使用 shp 的范围作为输出 tiff 的范围,否则使用原 tiff 的范围作为输出范围(可以使用两者的交集范围,待完善) :param res_format: 返回类型, GTiff 输出为文件, MEM 输出为临时文件 :return: """ # FIXME 增加输出结果为 tiff 和 shp 范围的交集的功能 warp_options = gdal.WarpOptions(cutlineDSName=shp_path, dstNodata=no_data_value, format=res_format, cropToCutline=crop_to_cutline) return gdal.Warp(out_tiff_path, in_tiff_path, options=warp_options)
def resample(self, new_xres, new_yres, save_path): """ Decrease the pixel size of the raster. Args: new_resx: desired resolution in x-direction new_resy: desired resolution in y-direction save_path: filepath to where the resampled file should be stored Returns: Nothing, it writes a raster file with decreased resolution. """ props = self.dem.GetGeoTransform() print('current pixel xsize:', props[1], 'current pixel ysize:', -props[-1]) options = gdal.WarpOptions(options=['tr'], xRes=new_xres, yRes=new_yres) newfile = gdal.Warp(save_path, self.dem, options=options) newprops = newfile.GetGeoTransform() print('new pixel xsize:', newprops[1], 'new pixel ysize:', -newprops[-1]) print('file saved in ' + save_path)
def generate_vrt(camera, raster_size, fpath, outpath=None, no_data_value=0): gcps = generate_gcps(camera) xsize, ysize = raster_size if outpath is None: outpath = os.path.dirname(fpath) outname = os.path.splitext(os.path.basename(fpath))[0] + '.vrt' outname = os.path.join(outpath, outname) xsize, ysize = raster_size vrt = r'''<VRTDataset rasterXSize="{{ xsize }}" rasterYSize="{{ ysize }}"> <Metadata/> <GCPList Projection="{{ proj }}"> {% for gcp in gcps -%} {{gcp}} {% endfor -%} </GCPList> <VRTRasterBand dataType="Float32" band="1"> <NoDataValue>{{ no_data_value }}</NoDataValue> <Metadata/> <ColorInterp>Gray</ColorInterp> <SimpleSource> <SourceFilename relativeToVRT="0">{{ fpath }}</SourceFilename> <SourceBand>1</SourceBand> <SourceProperties rasterXSize="{{ xsize }}" rasterYSize="{{ ysize }}" DataType="Float32" BlockXSize="512" BlockYSize="512"/> <SrcRect xOff="0" yOff="0" xSize="{{ xsize }}" ySize="{{ ysize }}"/> <DstRect xOff="0" yOff="0" xSize="{{ xsize }}" ySize="{{ ysize }}"/> </SimpleSource> </VRTRasterBand> </VRTDataset>''' context = { 'xsize': xsize, 'ysize': ysize, 'gcps': gcps, 'proj': '+proj=longlat +a=3396190 +b=3376200 +no_defs', 'fpath': fpath, 'no_data_value': no_data_value } template = jinja2.Template(vrt) tmp = template.render(context) warp_options = gdal.WarpOptions(format='VRT', dstNodata=0) gdal.Warp(outname, tmp, options=warp_options)
def reproject_dataset(dataset: Union[str, gdal.Dataset], bounds: Sequence[float], x_res: int, y_res: int, destination_srs: osr.SpatialReference, bounds_srs: Optional[osr.SpatialReference], resampling_mode: Optional[str]) -> gdal.Dataset: """ Reprojects a gdal dataset to a reference system with the given bounds and the given spatial resolution. :param dataset: A dataset :param bounds: A 1-d float array specifying the bounds of the resulting dataset. Must consist of the following four float values: xmin, ymin, xmax, ymax. :param x_res: The resolution the resulting dataset shall have in x-direction. Must be set in accordance to destination_srs. :param y_res: The resolution the resulting dataset shall have in y-direction. Must be set in accordance to destination_srs. :param destination_srs: The spatial reference system that the resulting data set shall show. :param bounds_srs: The spatial reference system in which the bounds are specified. If not given, it is assumed that the bounds are given in the destination_srs. :param resampling_mode: The mode by which the values from the source dataset shall be combined to values in the target dataset. Available modes are: * near (Nearest Neighbour) * bilinear * cubic * cubicspline * lanczos * average * mode * max * min * med * q1 * q3 If none is selected, 'bilinear' will be selected in case the source values need to be sampled up to a finer destination resolution and 'average' in case the values need to be sampled down to a coarser destination resolution. :return: A spatial dataset with the chosen destination spatial reference system, in the bounds and the x- and y- resolutions that have been set. """ if type(dataset) is str: dataset = gdal.Open(dataset) if bounds_srs is None: bounds_srs = destination_srs if resampling_mode is None: resampling_mode = _get_resampling(dataset, bounds, x_res, y_res, bounds_srs, destination_srs) warp_options = gdal.WarpOptions(format='Mem', outputBounds=bounds, outputBoundsSRS=bounds_srs, xRes=x_res, yRes=y_res, dstSRS=destination_srs, resampleAlg=resampling_mode) reprojected_data_set = gdal.Warp('', dataset, options=warp_options) return reprojected_data_set
def crop2grid(self): ''' Crops raster to extent of the geomodel grid. ''' cornerpoints_geo = self._get_cornerpoints(self.grid.extent) cornerpoints_dtm = self._get_cornerpoints(self.extent) if np.any(cornerpoints_geo[:2] - cornerpoints_dtm[:2]) != 0: path_dest = '_cropped_DEM.tif' new_bounds = (self.grid.extent[[0, 2, 1, 3]]) gdal.Warp(path_dest, self.dem, options=gdal.WarpOptions(options=['outputBounds'], outputBounds=new_bounds)) self.dem = gdal.Open(path_dest) self.dem_zval = self.dem.ReadAsArray() self._get_raster_dimensions() print('Cropped raster to geo_model.grid.extent.')
def crop2grid(self): ''' evtl in anderer Klasse weil xyz kann gecroppt werden output_path: Returns: ''' cornerpoints_geo = self._get_cornerpoints(self.grid.extent) cornerpoints_dtm = self._get_cornerpoints(self.extent) if np.any(cornerpoints_geo[:2] - cornerpoints_dtm[:2]) != 0: path_dest = '_cropped_DEM.tif' new_bounds = (self.grid.extent[[0, 2, 1, 3]]) gdal.Warp(path_dest, self.dem, options=gdal.WarpOptions( options=['outputBounds'], outputBounds=new_bounds)) self.dem = gdal.Open(path_dest) self.dem_zval = self.dem.ReadAsArray() self._get_raster_dimensions() print('Cropped raster to geo_model.grid.extent.')
def resample(src_dir, dst_dir, resolution): class_band_list = ['B02', 'B03', 'B04', 'B08', 'B10', 'B11', 'B12'] mid_list1 = glob(os.path.join(src_dir, 'GRANULE', '*'))[0] mid_list2 = os.path.join(mid_list1, 'IMG_DATA') fn_list = glob(os.path.join(mid_list2, '*.jp2')) if resolution == 20 or resolution == 60: resample_alg = gdal.gdalconst.GRIORA_Average else: resample_alg = gdal.gdalconst.GRIORA_Bilinear for item in fn_list: warp_options = gdal.WarpOptions(xRes=resolution, yRes=-1 * resolution, resampleAlg=resample_alg, format='GTiff') name_short = os.path.split(item)[1] dst_fn = os.path.join(dst_dir, name_short.replace('.jp2', '.tiff')) if (name_short[-7:-4]) in class_band_list: print(os.path.split(item)[1]) gdal.Warp(dst_fn, item, options=warp_options)
def Clip_shapefile(raster_fh, shape_fh, output_fh): driver, NDV, xsize, ysize, GeoT, Projection = GetGeoInfo(raster_fh) inDriver = ogr.GetDriverByName("ESRI Shapefile") inDataSource = inDriver.Open(shape_fh, 1) inLayer = inDataSource.GetLayer() minX = GeoT[0] minY = GeoT[3] + ysize * GeoT[5] maxX = GeoT[0] + xsize * GeoT[1] maxY = GeoT[3] optionsClip = gdal.WarpOptions( cutlineDSName=shape_fh, cutlineLayer=inLayer.GetName(), cropToCutline=False, outputBounds=(minX, minY, maxX, maxY), width=xsize, height=ysize, dstNodata=-9999, options=["GDALWARP_IGNORE_BAD_CUTLINE YES"], ) gdal.Warp(output_fh, raster_fh, options=optionsClip)
def cropDEM2geodata(self, output_path): ''' Args: output_path: Returns: ''' path_dest = output_path + '_cropped_DEM.tif' print( 'Extents of geo_data and DEM do not match. DEM is cropped and stored as', path_dest) new_bounds = (self.geo_data.extent[0], self.geo_data.extent[2], self.geo_data.extent[1], self.geo_data.extent[3]) # destName = "C:\\Users\\elisa\\Documents\\git\\MSc\\GempyTopography\\cropped_DTM.tif" gdal.Warp(path_dest, self.dem, options=gdal.WarpOptions(options=['outputBounds'], outputBounds=new_bounds)) # cropped_dem = gdal.Open(path_dest) # return topography(path_dest, self.geo_data, output_path) return gdal.Open(path_dest)