简体   繁体   English

GDAL 的 GetProjection() 和 GetGeoTransform() 的 Rasterio 等价物?

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

I have a GeoTIFF that I've done a lot of processing with in OpenCV.我有一个 GeoTIFF,我在 OpenCV 中做了很多处理。 After processing, its lost its spatial information so I need to insert it back in. In GDAL, I would just extract that from the original image before processing and then just shove it back in with the post-processed image like so:处理后,它丢失了它的空间信息,所以我需要将它重新插入。在 GDAL 中,我会在处理之前从原始图像中提取它,然后将其与后处理的图像一起推回,如下所示:

#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)

Is there a way I can do this in Rasterio?有没有办法在 Rasterio 中做到这一点?

According to RasterIO documentation , you can simply access crs and transform attributes on an open dataset to get the projection and geotransform respectively.根据RasterIO 文档,您可以简单地访问开放数据集上的crstransform属性,以分别获取投影和地理变换。 You can then pass them back to output image file on opening .然后,您可以在打开时将它们传递回 output 图像文件。

Example:例子:

# 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)

Notes on above example :上述示例的注释

  • assume that image dimensions are preserved when processing it.假设在处理图像时保留图像尺寸。 If not, you'll have to derive the spatial transform accordingly.如果没有,您将不得不相应地推导出空间变换。
  • Assume single-band dataset假设单波段数据集

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM