给定角坐标 [R],如何在成角度的多边形内创建均匀间隔点的矩阵

How to create a matrix of evenly-spaced points within an angled polygon, given the corner coordinates [R]

给定一些示例随机数据,每个角都有 UTM 坐标:

       test<-structure(list(name = c("P11C1", "P11C2", "P11C3", "P11C4"), 
    east = c(6404807.016, 6404808.797, 6404786.695, 6404784.761
    ), north = c(497179.4834, 497159.1862, 497156.6599, 497176.4444
    ), plot_num = c(11, 11, 11, 11)), row.names = c(NA, -4L), class = c("tbl_df", 
"tbl", "data.frame"))

如果我们将其绘制为多边形。我们可以看到一个倾斜的矩形(这是因为这个形状是使用实际差分 GPS 捕获的地面坐标生成的):

library(ggplot2)
ggplot(test) + geom_polygon(aes(east, north))

你可以使用这个小功能:

gridify <- function(x, y, grid_x = 10, grid_y = 10) {
  x <- sort(x)
  y <- sort(y)
  xvals <- do.call(rbind, Map(function(a, b) seq(b, a, length = grid_x),
      a = seq(x[1], x[3], length = grid_y), 
      b = seq(x[2], x[4], length = grid_y)))
  yvals <- do.call(rbind, Map(function(a, b) seq(a, b, length = grid_y),
                              a = seq(y[1], y[3], length = grid_x), 
                              b = seq(y[2], y[4], length = grid_x)))
  as.data.frame(cbind(x = c(xvals), y = c(t(yvals))))
}

例如,要绘制一个 10 x 11 的网格,我们会这样做:

ggplot(test) + 
  geom_polygon(aes(east, north)) +
  geom_point(data = gridify(x = test$east, y = test$north, grid_x = 11),
             aes(x, y), color = 'red') +
  coord_equal()

我们可以扩展到任意数量的点:

library(ggplot2)

ggplot(test) + 
  geom_polygon(aes(east, north)) +
  geom_point(data = gridify(x = test$east, y = test$north, 50, 50),
             aes(x, y), color = 'red') +
  coord_equal()