給定一些示例隨機資料,每個角都有 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))
![給定角坐標 [R],如何在有角度的多邊形內創建均勻間隔點的矩陣](https://img.uj5u.com/2022/05/10/ebd333bdb67f4e9782634db28293c3a4.png)
- 我的問題是,如何在此多邊形內均勻分布的自定義維度中生成點?例如,如果我想在這個網格中生成一個均勻分布的 10x11 點的網格。鑒于角點,任何人都可以建議這樣做嗎?我有數百個離散圖,然后我想回圈/映射一個解決方案。我認為這涉及一些簡單的幾何圖形,但是由于傾斜情節的增加造成的混亂,我真的很困惑,并且在 SO 或其他地方找不到類似的解決方案!僅供參考,在這種情況下,我不希望投影成為問題,因為它是 UTM 坐標,但是考慮全球投影的空間解決方案也很酷!
uj5u.com熱心網友回復:
你可以使用這個小功能:
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()
![給定角坐標 [R],如何在有角度的多邊形內創建均勻間隔點的矩陣](https://img.uj5u.com/2022/05/10/2bdca721b1144156a8af08ffa928a39c.png)
我們可以擴展到任意數量的點:
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()
![給定角坐標 [R],如何在有角度的多邊形內創建均勻間隔點的矩陣](https://img.uj5u.com/2022/05/10/a76a6227bef74db99797a7a52f729ec1.png)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/471588.html
