【问题标题】:Create square buffer zone around point - gridded data - python在点周围创建方形缓冲区 - 网格数据 - python
【发布时间】:2021-06-01 15:17:02
【问题描述】:

我有一个数据集,我想为其创建额外的训练标签,方法是在二维数据集(经度、纬度)中的真实标签周围创建一个缓冲区。为了我的问题,假设我的数据集看起来像:

array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]])

代码:df = np.array([0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0]).reshape(5,5)

创建缓冲区后。我的输出数据应该类似于:

array([[0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 0, 0, 0, 0]])

从技术上讲,我的数据集是具有 5000 个时间变量的 3D 数据集。我知道在 ArcGIS 中有一个工具可以做到这一点。但是,它一次只执行一次。正如你所理解的,我不想导出 5000 个单独的文件。有谁知道如何解决这个问题?

也许很高兴知道我所有的一个“像素”都是 0.5 x 0.5。

【问题讨论】:

    标签: python grid buffer


    【解决方案1】:

    虽然它可能不是最漂亮的答案。我确实找到了解决方法。下面的代码(如果可能)在原始数据集中的每个真实标签周围创建一个 3x3 真实标签网格。它也可以毫无问题地处理边界和边缘。如果有人知道更好/更快的解决方案,请分享!

    df = np.array([0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0]).reshape(5,5)

    values = np.where(df == 1) # find indexes of true labels
        for ind in range(len(values[0])):
            x = values[0][ind]
            y = values[1][ind]
            #upper left
            if x == 0 and y == 0:
                df[x,y:y+2] = 1
                df[x+1,y:y+2] = 1 
            #upper right
            elif x == 0 and y == df.shape[1]-1:
                df[x,y-1:y+1] = 1
                df[x+1,y-1:y+1] = 1
            #bottom left
            elif x == df.shape[0]-1 and y == 0:
                df[x-1,y:y+2] = 1
                df[x,y:y+2] = 1
            #bottom right
            elif x == df.shape[0]-1 and y == df.shape[1]-1:
                df[x-1,y-1:y+1] = 1
                df[x,y-1:y+1] = 1
            ### along borders
            #along top border
            elif x == 0 and y < df.shape[1]-1:
                df[x,y-1:y+2] = 1
                df[x+1,y-1:y+2] = 1
            #along bottom border
            elif x == df.shape[0]-1 and y < df.shape[1]-1:
                df[x-1,y-1:y+2] = 1
                df[x,y-1:y+2] = 1
            #along left border
            elif x < df.shape[0]-1 and y == 0:
                df[x-1,y:y+2] = 1  
                df[x,y:y+2] = 1
                df[x+1,y:y+2] = 1
            #along right border
            elif x < df.shape[0]-1 and y == df.shape[0]-1:
                df[x-1,y-1:y+1] = 1  
                df[x,y-1:y+1] = 1
                df[x+1,y-1:y+1] = 1
            ### everywhere aside along borders
            else:
                df[x-1,y-1:y+2] = 1
                df[x,y-1:y+2] = 1
                df[x+1,y-1:y+2] = 1
    

    【讨论】:

      猜你喜欢
      • 2022-10-15
      • 1970-01-01
      • 1970-01-01
      • 2022-09-22
      • 2018-05-27
      • 2017-07-18
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多