在网上查找了各种方法,发现基于python+GDAL的代码在我电脑上总是无法运行,最后代码不出现错误,总算可以运行了,但是却不出结果图,只好另寻他法。

本文实现影像合并的初始代码源自这篇文章:​ ​https://www.pythonf.cn/read/6926。非常感谢这位作者的分享。​


此代码可以实现将多个.tif文件合并成一个,略微不足之处在于,此代码目前未能实现批量处理。望继续研习,实现改进。

先将完整代码展示如下:

from osgeo import gdal
import os
import glob
import math

#获取影像的左上角和右下角坐标def GetExtent(in_fn):
ds=gdal.Open(in_fn)
geotrans=list(ds.GetGeoTransform())
xsize=ds.RasterXSize
ysize=ds.RasterYSize
min_x=geotrans[0]
max_y=geotrans[3]
max_x=geotrans[0]+xsize*geotrans[1]
min_y=geotrans[3]+ysize*geotrans[5]
ds=None
return min_x,max_y,max_x,min_y

inputfile_path= r'D:\cities\input'list_files=os.listdir(inputfile_path)
outpath= r'D:\cities\Inputimages'os.chdir(outpath)
#如果存在同名影像则先删除
if os.path.exists('ndvi.tif'):
os.remove('ndvi.tif')

# 按遥感生态指数类型读取文件
inputfileList=[]
for readPath in list_files:
if readPath.startswith("ndvi"):
inputfilepath = inputfile_path + "\\" + readPath
print(inputfilepath)
inputfileList.append(inputfilepath)
print("inputfilelist:", inputfileList)

# in_files=glob.glob("*.tif")
in_files=inputfileList
in_fn=in_files[0]
#获取待镶嵌栅格的最大最小的坐标值
min_x,max_y,max_x,min_y=GetExtent(in_fn)
for in_fn in in_files[1:]:
minx,maxy,maxx,miny=GetExtent(in_fn)
min_x=min(min_x,minx)
min_y=min(min_y,miny)
max_x=max(max_x,maxx)
max_y=max(max_y,maxy)
#计算镶嵌后影像的行列号
in_ds=gdal.Open(in_files[0])
geotrans=list(in_ds.GetGeoTransform())
width=geotrans[1]
height=geotrans[5]

columns=math.ceil((max_x-min_x)/width)
rows=math.ceil((max_y-min_y)/(-height))
in_band=in_ds.GetRasterBand(1)

driver=gdal.GetDriverByName('GTiff')
out_ds=driver.Create('ndvi.tif',columns,rows,1,in_band.DataType)
out_ds.SetProjection(in_ds.GetProjection())
geotrans[0]=min_x
geotrans[3]=max_y
out_ds.SetGeoTransform(geotrans)
out_band=out_ds.GetRasterBand(1)
#定义仿射逆变换
inv_geotrans=gdal.InvGeoTransform(geotrans)
#开始逐渐写入
for in_fn in in_files:
in_ds=gdal.Open(in_fn)
in_gt=in_ds.GetGeoTransform()
#仿射逆变换
offset=gdal.ApplyGeoTransform(inv_geotrans,in_gt[0],in_gt[3])
x,y=map(float,offset)
print(x,y)
trans=gdal.Transformer(in_ds,out_ds,[])#in_ds是源栅格,out_ds是目标栅格
success,xyz=trans.TransformPoint(False,0,0)#计算in_ds中左上角像元对应out_ds中的行列号
x,y,z=map(float,xyz)
print(x,y,z)
data=in_ds.GetRasterBand(1).ReadAsArray()
out_band.WriteArray(data,x,y)#x,y是开始写入时左上角像元行列号
del in_ds,out_band,out_ds

下面汇总了我搜索到的用于遥感影像镶嵌的代码,来人或可受用。

1:python 影像拼接

​https://blog.csdn.net/m0_56180742/article/details/119696803​

2:Python+GDAL实现批量进行遥感图像的拼接

​https://blog.csdn.net/qq_43177210/article/details/108402353​

3:python+gdal+遥感图像拼接(mosaic)的实例

​https://www.jb51.net/article/182362.htm​

4: Python 图像拼接

​https://blog.csdn.net/weixin_36670529/article/details/108225674​

5: GDAL将多张影像拼成一张图的最优解决方法

​https://blog.csdn.net/SunStrongInChina/article/details/121089251​

6:Merging GeoTIFF Files to Create a Mosaic

​https://www.neonscience.org/resources/learning-hub/tutorials/merge-lidar-geotiff-py​

7:Creating a raster mosaic

​https://autogis-site.readthedocs.io/en/latest/notebooks/Raster/raster-mosaic.html​

8:Python-栅格图像拼接

​https://zhuanlan.zhihu.com/p/379340470​

9:python GDAL 影像处理系列之裁剪与合并影像(三)

​https://blog.csdn.net/qq_38308388/article/details/102978755?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-102978755-blog-123661973.pc_relevant_multi_platform_whitelistv3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-102978755-blog-123661973.pc_relevant_multi_platform_whitelistv3&utm_relevant_index=2​

10:python 调用gdal读取tiff 遥感大图像。

​https://blog.csdn.net/weixin_40755306/article/details/100747869​


汇总罗列的精粹文章,目的是方便大家查阅。若有侵权,请联系后删除。




上一篇: with Python >= 3.8,,, . set the USE_PATH_FOR_GDAL_PYTHON=YES environment variable to feed the PATH.

下一篇: 基于python实现.tif格式遥感影像的批量裁剪