如何使SF网格与r?

问题描述 投票:0回答:1
st_make_grid

在我的学习区域(农业领域)创建20x20m的网格:

library(sf)
library(ggplot2)

# file below can be downloaded from:
# https://mega.nz/file/5TYDyToS#aIOyeeLGbH6WeSMm8YxF5rBFmrKbrb0BVd7lyYf3HOw
d <- st_read("~/Downloads/yld.sqlite")
plot(d)

g <- st_as_sf(st_make_grid(d, cellsize = 20)) ggplot() + geom_sf(data = g) + geom_sf(data = d["gyld"], fill = "grey", color = "grey") + coord_sf() field contour

,但是,我想更改两件事:grid generated from the field

网格不与场轮廓对齐。我如何旋转它,以使两者对齐?

网格是从田地的边界框创建的。如何在字段中创建
    (即仅使用字段的边界,数据点所在的位置)?
  1. 一个选项是制作sf
  2. 对象的副本,将其旋转,以使您的字段运行东西方,然后创建网格。接下来,将该网格旋转与原始字段保持一致。
看起来您的田地大约是逆时针方向的4.7度,所以我们可以做
r gis geospatial r-sf
1个回答
0
投票
center <- st_centroid(st_union(d)) theta <- 4.7 * pi / 180 rotation_matrix <- matrix(c(cos(theta), sin(theta), -sin(theta), cos(theta)), nrow = 2) reverse_matrix <- matrix(c(cos(-theta), sin(-theta), -sin(-theta), cos(-theta)), nrow = 2) e <- d st_geometry(e) <- (st_geometry(e) - center) * rotation_matrix + center g <- st_as_sf(st_make_grid(e, cellsize = 20)) center <- st_centroid(st_union(g)) st_geometry(g) <- (st_geometry(g) - center) * reverse_matrix + center st_crs(g) <- st_crs(d)

现在,如果我们在原始字段上绘制此网格,我们可以看到它正确对齐。
ggplot() + geom_sf(data = d["gyld"], aes(color = gyld)) + geom_sf(data = g, fill = NA, color = "black") + coord_sf() + scale_color_viridis_c(option = "A")


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.