python - 如何根据shapefile屏蔽特定数组数据

标签 python arrays numpy matplotlib matplotlib-basemap

这是我的问题:

  • 二维 numpy 数组数据表示每个网格空间的一些属性
  • 作为研究区域(如城市)的行政区划的 shapefile。

例如:

http://i4.tietuku.com/84ea2afa5841517a.png

整个区域有40x40的网格网络,我想提取紫色区域内的数据。换句话说,我想屏蔽管理之外的数据 边界进入 np.nan。

我早期的尝试

我标注格子号,选择具体的数组数据放入np.nan中。

http://i4.tietuku.com/523df4783bea00e2.png

 value[0,:] = np.nan
 value[1,:] = np.nan
       .
       . 
       .
       .

有人可以告诉我实现目标的更简单方法吗?

添加

找到答案 here它可以将栅格数据绘制到 shapefile 中,但数据本身不会改变。

更新-2016-01-16

我已经在一些答案的启发下解决了这个问题。
对这个目标感兴趣的人,查看我问过的这两篇文章:
1. Testing point with in/out of a vector shapefile
2. How to use set clipped path for Basemap polygon

关键步骤是测试我已经转换为 shapely.polygon 的 shapefile 内/外的点。

最佳答案

步骤 1. 栅格化 shapefile

创建一个函数,可以确定坐标 (x, y) 处的点是否在该区域中。参见 here有关如何将 shapefile 栅格化为与目标蒙版尺寸相同的数组的更多详细信息

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

第 2 步。创建你的面具

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

关于python - 如何根据shapefile屏蔽特定数组数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34585582/

相关文章:

python - 监听 dbus 信号以在 debian linux 上挂载 mtp 设备

python - windows7 安装psycopg2 报错: Unable to find vcvarsall. bat

java - Java int[] 压缩工具

python - 不是一维的一维 numpy/scipy 插值

python - 在 Numpy 中预处理 Tensorflow 张量

python pandas/numpy 根据映射方案替换所有值的快速方法

python - 如何在 GTK 中更新绘图区域

python - Python 中的 Selenium 如果元素不存在,如何使用 If - else 代码

javascript - 按元素对数据数组进行分组和求和

java - 字符串到整数数组