给定角坐标 [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))
- 我的问题是,如何在自定义维度之间生成点
在这个多边形内均匀分布?例如,如果我想
在该网格内生成一个均匀分布的 10x11 点网格。鉴于角点,任何人都可以建议这样做吗?我有数百个离散的地块,然后我想 loop/map 一个解决方案。我假设这个
涉及一些简单的几何图形,但增加了
倾斜的情节,我真的很困惑,找不到类似的
SO 或其他地方的解决方案!仅供参考,在这种情况下我不是
期望投影成为一个问题,因为它是 UTM 坐标,但是
考虑到全球预测的空间解决方案会很酷
也来看看!
你可以使用这个小功能:
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()
给定一些示例随机数据,每个角都有 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))
- 我的问题是,如何在自定义维度之间生成点 在这个多边形内均匀分布?例如,如果我想 在该网格内生成一个均匀分布的 10x11 点网格。鉴于角点,任何人都可以建议这样做吗?我有数百个离散的地块,然后我想 loop/map 一个解决方案。我假设这个 涉及一些简单的几何图形,但增加了 倾斜的情节,我真的很困惑,找不到类似的 SO 或其他地方的解决方案!仅供参考,在这种情况下我不是 期望投影成为一个问题,因为它是 UTM 坐标,但是 考虑到全球预测的空间解决方案会很酷 也来看看!
你可以使用这个小功能:
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()