读取栅格影像坐标系,如果为投影坐标系统,转为地理坐标系统

from osgeo import gdal, osr

def reproject_to_geographic(raster_file, output_file):
    """如果为投影坐标系统,则将栅格影像转换为地理坐标系统"""
    # 打开原始影像
    src_ds = gdal.Open(raster_file)
    src_proj = src_ds.GetProjection()

    # 创建空间参考对象
    src_srs = osr.SpatialReference()
    src_srs.ImportFromWkt(src_proj)

    # 检查是否为投影坐标系统
    if src_srs.IsProjected():
        # 定义目标坐标系统为WGS84
        dst_srs = osr.SpatialReference()
        dst_srs.ImportFromEPSG(4326)

        # 创建转换对象
        transform = osr.CoordinateTransformation(src_srs, dst_srs)

        # 使用gdal.Warp重新投影影像
        gdal.Warp(output_file, src_ds, dstSRS=dst_srs.ExportToWkt())

        print(f"影像已从投影坐标系统转换为地理坐标系统,输出文件:{output_file}")
    else:
        print("影像不是投影坐标系统,无需转换。")

# 输入和输出文件路径
input_raster = 'dataset/Land cover/4.tif'
output_raster = 'dataset/Land cover/5.tif'

# 调用函数
reproject_to_geographic(input_raster, output_raster)


你可能感兴趣的:(python,简单地理应用,python)