简体   繁体   English

如何在R中的指定多边形内生成坐标网格?

[英]How do I generate a grid of coordinates within specified polygons in R?

I have a list of two-dimensional polygons defined as two-column matrices of x and y coordinates in R. They completely fill a square area and are mutually exclusive. 我有一个二维多边形列表,定义为R中x和y坐标的两列矩阵。它们完全填充方形区域并且是互斥的。 I want to use these polygon definitions to generate a fine grid of x,y coordinate values in which each value is identified by which polygon it falls into. 我想使用这些多边形定义来生成x,y坐标值的精细网格,其中每个值由它落入哪个多边形来标识。

I have explored the sp package and can get my polygons into an object of class SpatialPolygons , but I don't know if that gets me closer to my goal. 我已经探索了sp包并且可以将我的多边形变成SpatialPolygons类的对象,但我不知道这是否让我更接近我的目标。 With my polygons in a dataframe, I can use ggplot with geom_polygon(aes(fill=ID)) to generate a plot of the polygons with coloring based on polygon ID. 使用数据ggplot多边形,我可以使用带有geom_polygon(aes(fill=ID))来生成基于多边形ID着色的多边形图。

I can see several paths forward, but don't know how to accomplish any of them: 我可以看到前进的几​​条路径,但不知道如何完成任何一条路径:

  1. A function that takes a polygon and generates a uniform grid of coordinates within the polygon boundaries. 采用多边形并在多边形边界内生成均匀坐标网格的函数。 (My polygons are quite irregular, with many sides, so creating a custom function for them would be painful and error prone.) (我的多边形非常不规则,有很多边,因此为它们创建自定义函数会很痛苦且容易出错。)

  2. A function that takes a pair of x, y coordinates and my list of polygons and outputs which polygon the coordinates fall into. 一个函数,它接受一对x,y坐标和我的多边形列表以及输出坐标所在的多边形。

  3. A function that takes my ggplot-generated plot and converts the colors into a grid of numeric coordinate values that I could read back into R. 一个函数,它接受我的ggplot生成的绘图并将颜色转换为数字坐标值的网格,我可以读回R.

There may be also be other approaches that I'm not imagining. 可能还有其他方法我无法想象。 I have to believe that other people have had this same need before, but extensive searching has not led me to any existing functions that do what I need. 我不得不相信其他人之前有同样的需求,但是广泛的搜索并没有让我找到任何能够满足我需要的功能。

[mumble about spsample deleted] [关于spsample删除的嘟??]

Hmm in the cold light of day it seems you want something else: 嗯,在寒冷的一天,似乎你想要别的东西:

IF all your polygons make a rectangle AND you want a regular grid of points over that rectangle THEN create a SpatialPoints object of grid coordinates (see 'expand.grid' for part of the solution to that sub-problem) AND THEN use 'overlay' from package:sp to test what polygon your grid points are in. 如果您的所有多边形都构成一个矩形并且您希望在该矩形上有一个规则的点网格,那么创建一个网格坐标的SpatialPoints对象(请参阅'expand.grid'以解决该子问题的部分解决方案)然后使用'overlay' from package:sp来测试网格点所在的多边形。

You might also want to use bbox to get the extent of your polygons. 您可能还想使用bbox来获取多边形的范围。

It sounded like you were doing this positioning on a square grid so it may be simpler than the more general polygon approach would require. 这听起来就像你在正方形网格上进行这种定位,因此它可能比更通用的多边形方法需要的更简单。 Let's say your coordinates for this grid-on-the-square are two vectors, 'xx' and 'yy', and you have list of points in a data.frame or matrix named 'mypoints'. 假设你在这个方格上的坐标是两个向量,'xx'和'yy',你有一个名为'mypoints'的data.frame或矩阵中的点列表。 This will create a matrix of row-col-indices to look up the proper sub-square: 这将创建一个row-col-indices矩阵来查找正确的子方块:

 xx <- seq(0,1,by=.1)
 yy <- seq(0,1,by=.1)
 mypoints <- matrix(runif(10), ncol=2)
 head(mypoints)
#---------------
          [,1]      [,2]
[1,] 0.7731868 0.2707768
[2,] 0.7005779 0.7881789
[3,] 0.9520941 0.6661852
[4,] 0.4625906 0.9176813
[5,] 0.4550811 0.5017386
#---------------
 findInterval(mypoints[1:5,1], xx)
#[1]  8  8 10  5  5
 findInterval(mypoints[1:5,2], yy)
#[1]  3  8  7 10  6
 pointidxs <- matrix( c( findInterval(mypoints[,1], xx), 
                         findInterval(mypoints[,2], yy) ), ncol=2)
 head(pointidxs)
#--------------
     [,1] [,2]
[1,]    8    3
[2,]    8    8
[3,]   10    7
[4,]    5   10
[5,]    5    6

I haven't thought very much, but here is a before-coffee idea: I understand that your polygons form a Voronoi tesselation. 我没有想太多,但这里有一个咖啡前的想法:我知道你的多边形形成了Voronoi的细分。 Now, it is supposed to be easy to obtain the corresponding Delaunay triangulation, which should give you a straight-forward way to decide whether a particular point belongs to the corresponding polygon. 现在,应该很容易获得相应的Delaunay三角剖分,这应该为您提供一种直接的方式来确定特定点是否属于相应的多边形。

Hope that makes sense? 希望有道理吗?

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

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