输入/输出矢量shapefile的测试点 [英] Testing point with in/out of a vector shapefile

查看:314
本文介绍了输入/输出矢量shapefile的测试点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题.

  • 多边形类型的shapefile代表研究区域

http://i8.tietuku.com/08fdccbb7e11c0a9.png

  • 位于整个矩形地图中的某个点

http://i8.tietuku.com/877f87022bf817b8.png

我想测试每个点是否位于多边形内/外,并做一些进一步的操作(例如,求和研究区域内的网格点数量)

I want to test whether each point was located within/out the polygon and do some further operation(for example, sum the grid point amount within the study area)

由于有堆栈溢出的信息,我有两种方法.

I have two methods all thanks to the information on stack overflow.

将shapefile栅格化为栅格文件,然后进行测试.

Rasterize the shapefile into raster file and then test.

我还没有这样做,但是我问了一个问题

I haven't done that yet, but I have asked one question here and get an answer.

我尝试使用poly.contain()测试散点的位置,但结果与实际情况不符.

I have tried to using poly.contain() to test the scatter point's location, but the result wasn't match with the reality.

例如:

  • 原始数据由 pt (一个熊猫数据框)表示,其中包含1000个网格X,Y.
  • 我已经显示的
  • shapefile是研究区域,我想过滤原始数据,只保留该区域内的点.
  • Original data represent by pt(a pandas Dataframe) which contain 1000 grids X,Y.
  • shapefile I already shown was the study area, I want to filter the original data leaving only point within this area.
# map four boundaries
xc1,xc2,yc1,yc2 = 113.49805889531724,115.5030664238035,37.39995194888143,38.789235929357105
# grid definition
lon_grid  = np.linspace(x_map1,x_map2,38)
lat_grid  = np.linspace(y_map1,y_map2,32)

3.1准备

# generate (lon,lat)   
xx = lon_grid[pt.X.iloc[:].as_matrix()]
yy = lat_grid[pt.Y.iloc[:].as_matrix()]

sh = (len(xx),2)
data = np.zeros(len(xx)*2).reshape(*sh)
for i in range(0,len(xx),1):
    data[i] = np.array([xx[i],yy[i]])

# reading the shapefile

map = Basemap(llcrnrlon=x_map1,llcrnrlat=y_map1,urcrnrlon=x_map2,\
              urcrnrlat=y_map2)
map.readshapefile('/xx,'xx')

3.2测试

patches=[]
for info, shape in zip(map.xxx_info, map.xxx):
    x,y=zip(*shape)
    patches.append(Polygon(np.array(shape), True) )
for poly in patches:
     mask = np.array([poly.contains_point(xy) for xy in data])

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