Python 给栅格数据添加坐标信息
栅格数据通常不会自带坐标信息,需要用户自定义坐标信息
下面提供了一种用户根据指定范围(Extent)添加坐标信息的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import rasterio from affine import Affine from rasterio.crs import CRS
def proj_TIFF(inputpath="", outputpath="", extent=None): with rasterio.open(inputpath,'r') as src: meta = src.meta.copy() width, height = src.width, src.height xmin, ymin, xmax, ymax = extent pixel_width = (xmax - xmin) / width pixel_height = (ymax - ymin) / height transform = Affine(pixel_width, 0, xmin 0, pixel_height, ymax) meta.update(transform=transform, crs=CRS.form_epsg(3857)) with rasterio.open(outputpath, 'w', **meta) as dst: dst.write(src.read())
|