python:返回图像的经纬度范围,行列数和地理信息

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在计算经纬度范围中都有用到,其实也可以不返回;如果整个代码后面还会用到这三个也可以返回。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值