how to mask the specific array data based on the shapefile

前端 未结 2 858
北荒
北荒 2021-01-01 04:12

Here is my question:

  • the 2-d numpy array data represent some property of each grid space
  • the shapefile as the administrative division of the study a
2条回答
  •  独厮守ぢ
    2021-01-01 04:44

    Step 1. Rasterize shapefile

    Create a function that can determine whether a point at coordinates (x, y) is or is not in the area. See here for more details on how to rasterize your shapefile into an array of the same dimensions as your target mask

    def point_is_in_mask(mask, point):
        # this is just pseudocode
        return mask.contains(point) 
    

    Step 2. Create your mask

    mask = np.zeros((height, width))
    value = np.zeros((height, width))
    for y in range(height):
        for x in range(width):
            if not point_is_in_mask(mask, (x, y)):
                value[y][x] = np.nan
    

提交回复
热议问题