使用 NumPy 和 Rasterio 翻转/反转栅格

问题描述 投票:0回答:1

我目前正在开发一个项目,使用包含 netCDF 数据的 numpy 数组创建光栅文件。栅格创建得很顺利,但是当我绘制它们时,我意识到它们实际上是颠倒的(垂直镜像,而不是旋转)。

# find data values and append to correct list
  if tas_val >= min and tas_val <= max:
     GDDs[row, column] += 1
  
  # create the transform
  x, y = lat, lon
  xres, yres = 0.25, -0.25
  transform = Affine.translation(x[0] - xres / 2, y[0] - yres / 2) * Affine.scale(xres, yres)

  # write out GDDs array to a new raster
  with rasterio.open(
          f"/content/drive/Shareddrives/Brazil/GDDTiffs/GDD_Count_{ssp}_{year}_{crop}.tif",
          mode="w",
          driver="GTiff",
          height=GDDs.shape[0],
          width=GDDs.shape[1],
          count=1,
          dtype=GDDs.dtype,
          crs="+proj=latlong",
          transform = transform) as new_dataset:
          new_dataset.write(GDDs, 1)

我基本上循环遍历 NetCDF 中的值,查找温度信息是否在范围内,并增加 GDDs 数组中的索引,该索引的位置与 NetCDF 中的像素匹配。我的转换只是仿射平移和缩放,并且似乎工作正常 - 它返回与原始 NetCDF 相同维度和大小的栅格,这就是目标。

我尝试使用不同的数组、不同的变换以及

np.invert(array)
行中的
new_dataset.write()
函数,但没有任何东西能够真正翻转我的栅格。任何想法将不胜感激!

python numpy matplotlib netcdf rasterio
1个回答
0
投票

无论我尝试什么转换,我也有类似的问题(下面的每个都做同样的事情)。目前,我正在做一个数组翻转,但我很好奇你是否找到了除轴反转之外的解决方案(我仅显示了我的代码的一部分)。

import numpy as np
import pandas as pd
import rasterio as rio
import rasterio
from rasterio import transform
from rasterio.transform import from_origin

data = np.flipud(data
transform = rio.Affine(res_x, 0, xmin, 0, res_y, ymax)
# or
# transform = rasterio.transform.from_bounds(xmin, ymin, xmax, ymax, width = width, height = height)
# or
#transform = rasterio.transform.from_origin(xmin, ymax, 30, 30)

dst_filename = 'output7.tif'

try:
    with rio.open(
            dst_filename, 'w',
            driver='GTIFF',
            height = height,
            width = width,
            count=count,
            dtype=dtype,
            crs=crs,
            transform=transform,
            nodata = -9999
        ) as ds:
        ds.write(data, 1)
except Exception as e:
    print('something:', e)
    pass
© www.soinside.com 2019 - 2024. All rights reserved.