简体   繁体   English

围绕点创建方形缓冲区 - 网格数据 - python

[英]Create square buffer zone around point - gridded data - python

I have a dataset for which I would like to create additional training labels, by creating a buffer zone around the true labels in a two-dimensional dataset (lon, lat).我有一个数据集,我想通过在二维数据集(经度、纬度)中围绕真实标签创建缓冲区来为其创建额外的训练标签。 For the sake of my question, say that my dataset looks like:为了我的问题,假设我的数据集如下所示:

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

code: 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)代码: 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)

After creating the buffer zone.创建缓冲区后。 My output data should look something like:我的 output 数据应该类似于:

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

Technically my dataset is 3D with 5000 time variables.从技术上讲,我的数据集是具有 5000 个时间变量的 3D。 I know in ArcGIS there is a tool that does this.我知道在 ArcGIS 中有一个工具可以做到这一点。 However, it only does this for one time at a time.但是,它一次只执行一次。 I don't want to export 5000 separate files, as you could understand.正如你所理解的,我不想导出 5000 个单独的文件。 Does anyone know how to tackle this issue?有谁知道如何解决这个问题?

Maybe good to know that all my one 'pixel' is 0.5 by 0.5.也许很高兴知道我所有的“像素”都是 0.5 x 0.5。

Although it might not be the prettiest of answers.虽然它可能不是最漂亮的答案。 I did find a way on how to tackle it.我确实找到了解决方法。 The code below creates (if possible) a 3x3 grid of true labels around each true label in the original dataset.下面的代码(如果可能)在原始数据集中的每个真实 label 周围创建一个 3x3 真实标签网格。 It handles borders and edges as well without a problem.它也可以毫无问题地处理边界和边缘。 If anyone knows better / faster solution, please share!如果有人知道更好/更快的解决方案,请分享!

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

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

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