Rasterio 等效于 GDAL 的 GetProjection() 和 GetGeoTransform()?

Rasterio equivalent for GDAL's GetProjection() and GetGeoTransform()?

我有一个 GeoTIFF,我在 OpenCV 中用它做了很多处理。处理后,它丢失了它的空间信息,所以我需要将它重新插入。在 GDAL 中,我只是在处理之前从原始图像中提取它,然后像这样将它推回 post-processed 图像:

#open original geotiff
original_img = gdal.Open('/path-to-file/original_img.tif', 1)
crs = original_img.GetProjection()
gt = original_img.GetGeoTransform()
del original_ds

#open cv processes here....write post-processed file

# open processed raster and set the projection and geotransform
img = gdal.Open('/path-to-file/processed_img.tif', 1)
img.SetProjection(crs)
img.SetGeoTransform(gt)

我可以在 Rasterio 中执行此操作吗?

根据RasterIO documentation, you can simply access crs and transform attributes on an open dataset to get the projection and geotransform respectively. You can then pass them back to output image file on opening

示例:

# Open original Geotiff
original_img = rasterio.open("path/to/input.tif")
# Extract spatial metadata
input_crs = original_img.crs
input_gt  = original.transform

# Do your processing. For this example, just read first band of input dataset
processed_img = original_img.read(1)

# Prepare output geotiff file. We give crs and gt read from input as spatial metadata
with rasterio.open(
  'path/to/output.tif',
  'w',
  driver = 'GTiff',
  count = 1,
  height = processed_img.shape[0],
  width  = processed_img.shape[1],
  dtype  = processed_img.dtype,
  crs    = input_crs,
  transform = input_gt  
) as output:
  output.write(processed_img, 1)

上面例子的注释 :

  • 假设在处理图像时保留图像尺寸。如果不是,则必须相应地导出空间变换。
  • 假设单波段数据集