def copyproj(src_fn, dst_fn, gt=True):
"""Copy projection and geotransform from one raster file to another
"""
src_ds = gdal.Open(src_fn, gdal.GA_ReadOnly)
dst_ds = gdal.Open(dst_fn, gdal.GA_Update)
dst_ds.SetProjection(src_ds.GetProjection())
if gt:
src_gt = np.array(src_ds.GetGeoTransform())
src_dim = np.array([src_ds.RasterXSize, src_ds.RasterYSize])
dst_dim = np.array([dst_ds.RasterXSize, dst_ds.RasterYSize])
#This preserves dst_fn resolution
if np.any(src_dim != dst_dim):
res_factor = src_dim/dst_dim.astype(float)
src_gt[[1, 5]] *= max(res_factor)
#src_gt[[1, 5]] *= min(res_factor)
#src_gt[[1, 5]] *= res_factor
dst_ds.SetGeoTransform(src_gt)
src_ds = None
dst_ds = None
python类GA_Update()的实例源码
crop_mask_resample_reproject.py 文件源码
项目:uncover-ml
作者: GeoscienceAustralia
项目源码
文件源码
阅读 16
收藏 0
点赞 0
评论 0
def apply_mask(mask_file, tmp_output_file, output_file, jpeg):
"""
Parameters
----------
mask_file: mask file path
tmp_output_file: intermediate cropped geotiff before mask application
output_file: output geotiff path
jpeg: boolean, whether to produce jpeg or not
-------
"""
mask = get_mask(mask_file)
out_ds = gdal.Open(tmp_output_file, gdal.GA_Update)
out_band = out_ds.GetRasterBand(1)
out_data = out_band.ReadAsArray()
no_data_value = out_band.GetNoDataValue()
log.info('Found NoDataValue {} for file {}'.format(
no_data_value, os.path.basename(tmp_output_file)))
if no_data_value is not None:
out_data[mask] = no_data_value
else:
log.warning('NoDataValue was not set for {}'.format(tmp_output_file))
log.info('Manually setting NoDataValue for {}'.format(tmp_output_file))
out_band.WriteArray(out_data)
out_ds = None # close dataset and flush cache
# move file to output file
shutil.move(tmp_output_file, output_file)
log.info('Output file {} created'.format(tmp_output_file))
if jpeg:
dir_name = os.path.dirname(output_file)
jpeg_file = os.path.basename(output_file).split('.')[0] + '.jpg'
jpeg_file = os.path.join(dir_name, jpeg_file)
cmd_jpg = ['gdal_translate', '-ot', 'Byte', '-of', 'JPEG', '-scale',
output_file,
jpeg_file] + COMMON
subprocess.check_call(cmd_jpg)
log.info('Created {}'.format(jpeg_file))
setNoData.py 文件源码
项目:python_scripting_for_spatial_data_processing
作者: upsdeepak
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def setNoData(inputFile, noDataVal):
# Open the image file, in update mode
# so that the image can be edited.
dataset = gdal.Open(inputFile, gdal.GA_Update)
# Check that the image has been opened.
if not dataset is None:
# Iterate throughout the image bands
# Note. i starts at 0 while the
# band count in GDAL at 1.
for i in range(dataset.RasterCount):
# Print information to the user on what is
# being set.
print("Setting No Data (" + str(noDataVal) +") for band " + str(i+1))
# Get the image band
# the i+1 is because GDAL bands
# start with 1
band = dataset.GetRasterBand(i+1)
# Set the no data value
band.SetNoDataValue(noDataVal)
else:
# Print an error message if the file
# could not be oppened
print("Could not open the input image file: ", inputFile)
# This is the first part of the script to
# be executed
ruleBasedClassifier.py 文件源码
项目:python_scripting_for_spatial_data_processing
作者: upsdeepak
项目源码
文件源码
阅读 18
收藏 0
点赞 0
评论 0
def setThematic(imageFile):
# Use GDAL to open the dataset
ds = gdal.Open(imageFile, gdal.GA_Update)
# Itearte through the image bands
for bandnum in range(ds.RasterCount):
# Get the image band
band = ds.GetRasterBand(bandnum+1)
# Define the meta-data for the LAYER_TYPE
band.SetMetadataItem('LAYER_TYPE', 'thematic')
# This is the first part of the script to
# be executed.
def set_ndv(dst_fn, ndv):
dst_ds = gdal.Open(dst_fn, gdal.GA_Update)
for n in range(1, dst_ds.RasterCount+1):
b = dst_ds.GetRasterBand(1)
b.SetNoDataValue(ndv)
dst_ds = None
#Should overload these functions to handle fn, ds, or b
#Perhaps abstract, as many functions will need this functionality
def read_tif(fname):
src = gdal.Open(fname, gdal.GA_Update)
pdem = src.GetRasterBand(1)
gt = src.GetGeoTransform()
image = pdem.ReadAsArray()
return [gt,image]