如何屏蔽基础上,shapefile中的特定阵列数据 [英] how to mask the specific array data based on the shapefile

查看:291
本文介绍了如何屏蔽基础上,shapefile中的特定阵列数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 重新present的2-D numpy的阵列数据每个网格空间的某些属性

  • shape文件为研究区域(如城市)的行政区划。

在全区有40×40格网,我想提取的紫色区域中的数据。换句话说,我要屏蔽外部的管理数据
边界进入np.nan。

The whole area has 40x40 grids network, and I want to extract the data inside the purple area. In other words , I want to mask the data outside the administrative boundary into np.nan.

我标记电网号,然后选择特定的阵列数据到np.nan。

I label the grid number and select the specific array data into np.nan.

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

有人能告诉我一个更简单的方法来实现目标?

Can Someone show me a easier method to achieve the target?

找到答案这里它可以绘制栅格数据到shape文件,但数据本身不发生变化。

Found an answer here which can plot the raster data into shapefile, but the data itself doesn't change.

我已经解决了这个问题,通过一些答案的启发。结果
有人有兴趣在此目标上,检查我已经问过这个两个职位:结果
1。<一个href=\"http://stackoverflow.com/questions/34825074/testing-point-with-in-out-of-a-vector-shapefile\">Testing点进/出矢量shape文件的结果
2. <一个href=\"http://stackoverflow.com/questions/25701321/how-to-use-set-clipped-path-for-basemap-polygon/34224925#34224925\">How使用设置剪辑路径多边形底图

I have already solved this problem inspired by some answers.
Someone which are interested on this target, check this two posts which I have asked:
1. Testing point with in/out of a vector shapefile
2. How to use set clipped path for Basemap polygon

的关键步骤是在输入/输出,我已经转换成shapely.polygon shape文件的测试点。

The key step was to test the point within/out of the shapefile which I have already transform into shapely.polygon.

推荐答案

步骤1.栅格化shape文件

Step 1. Rasterize shapefile

创建可确定坐标点是否(X,Y)是或不是在该地区的作用。请参见这里如何光栅化你的shape文件到更多的细节相同的尺寸的目标掩模阵列

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) 

步骤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

这篇关于如何屏蔽基础上,shapefile中的特定阵列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆