from osgeo import gdal
# 根据某个图像返回经纬度范围、地理信息和行列数
def image_lon_lat_range(file):
dataset = gdal.Open(file)
geo_information = dataset.GetGeoTransform()
# GeoTransform[0],左上角横坐标(应该是投影坐标)
# GeoTransform[1],像元宽度(影像在水平空间的分辨率)
# GeoTransform[2],行旋转
# GeoTransform[3],左上角纵坐标(应该是投影坐标)
# GeoTransform[4],列旋转
# GeoTransform[5],像元高度(影像在垂直空间的分辨率),
# 如果影像是指北的,GeoTransform[2]和GeoTransform[4]这两个参数的值为0,GeoTransform[5]为负;
# 如果图像不含地理坐标信息,默认返回值是:(0,1,0,0,0,1)
col = dataset.RasterXSize # 列数
row = dataset.RasterYSize # 行数
top_left_corner = [0, 0] # 左上角
bottom_left_corner = [col-1, 0] # 左下角
top_right_corner = [0, row-1] # 右上角
bottom_right_corner = [col-1, row-1] # 右下角
# 左上角经纬度
top_left_corner_lon = \
geo_information[0] + top_left_corner[0] * geo_information[1] + top_left_corner[1] * geo_information[2]
top_left_corner_lat = \
geo_information[3] + top_left_corner[0] * geo_information[4] + top_left_corner[1] * geo_information[5]
# 左下角经纬度
bottom_left_corner_lon = \
geo_information[0] + bottom_left_corner[0] * geo_information[1] + bottom_left_corner[1] * geo_information[2]
bottom_left_corner_lat = \
geo_information[3] + bottom_left_corner[0] * geo_information[4] + bottom_left_corner[1] * geo_information[5]
# 右上角经纬度
top_right_corner_lon = \
geo_information[0] + top_right_corner[0] * geo_information[1] + top_right_corner[1] * geo_information[2]
top_right_corner_lat = \
geo_information[3] + top_right_corner[0] * geo_information[4] + top_right_corner[1] * geo_information[5]
# 右下角经纬度
bottom_right_corner_lon = \
geo_information[0] + bottom_right_corner[0] * geo_information[1] + bottom_right_corner[1] * geo_information[2]
bottom_right_corner_lat = \
geo_information[3] + bottom_right_corner[0] * geo_information[4] + bottom_right_corner[1] * geo_information[5]
max_lon = max(top_left_corner_lon, bottom_left_corner_lon, top_right_corner_lon, bottom_right_corner_lon)
min_lon = min(top_left_corner_lon, bottom_left_corner_lon, top_right_corner_lon, bottom_right_corner_lon)
max_lat = max(top_left_corner_lat, bottom_left_corner_lat, top_right_corner_lat, bottom_right_corner_lat)
min_lat = min(top_left_corner_lat, bottom_left_corner_lat, top_right_corner_lat, bottom_right_corner_lat)
return max_lon, min_lon, max_lat, min_lat, geo_information, col, row
col、row和geo_information在计算经纬度范围中都有用到,其实也可以不返回;如果整个代码后面还会用到这三个也可以返回。