0

我有地理参考图像,其坐标值如 (475224.0, 4186282.0)。我的图像尺寸为 (647, 2180)。即有 647 列和 2180 行。我想将坐标值放入一个大小为 (647, 2180) 的 numpy 数组中,以便将每个像素的坐标作为一个数组获取。我的代码如下。

rr = rasterio.open(fname) #fname is the georefered image
col = rr.width
row = rr.height
coord = np.empty(shape=(col,row),dtype=rr.dtypes[0])

for i in range(0,col):
    for j in range(0,row):
        coord[i,j] = rr.transform*(i,j)

问题是 rr.transform*(i,j) 会给出类似 (475224.0, 4186282.0) 的值。如何将其保存到单元格中。对于上述程序,我收到如下错误

回溯(最后一次调用):文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/core/interactiveshell.py”,第 2881 行,在 run_code exec(code_obj , self.user_global_ns, self.user_ns) File "", line 3, in coord[i,j] = rr.transform*(i,j) ValueError: setting an array element with a sequence.

4

1 回答 1

1

假设您的rr.transform*()输出是一个有效的 Python tuple,我认为您这样做比必须的要复杂一些。默认情况下,numpy将在创建和/或分配给np.array:s 时处理相等的元组和列表。因此,对您来说更简单的解决方案是添加一个额外的维度并直接分配您的值:

rr = rasterio.open(fname) #fname is the georefered image
col = rr.width
row = rr.height
coord = np.empty(shape=(col,row,2)

for i in range(0,col):
    for j in range(0,row):
        coord[i,j] = rr.transform*(i,j)

As you can see, the only difference is that I add extra dimensions to fit the size of rr. Here I have hard coded the third dimension to 2. It is probably possible to find that dynamically, from the rr object instead. In the general case we are not limited to a tupleof two values.

于 2017-06-30T06:49:40.923 回答