我想用R和包SF来构建一个矩形, 此矩形必须在两个给定点开始和结束:

library(sf)
library(units)

>     point1$geometry
Geometry set for 1 feature 
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -73.51295 ymin: 45.529 xmax: -73.51295 ymax: 45.529
Geodetic CRS:  WGS 84
POINT (-73.51295 45.529)
>     point2$geometry
Geometry set for 1 feature 
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -73.51347 ymin: 45.52816 xmax: -73.51347 ymax: 45.52816
Geodetic CRS:  WGS 84
POINT (-73.51347 45.52816)

然后我定义了中心和矩形的大小

    center <- st_point(c((st_coordinates(point1)[1] + st_coordinates(point2)[1]) / 2,
                         (st_coordinates(point1)[2] + st_coordinates(point2)[2]) / 2))
    half_width <- as.numeric(st_distance(point1, point2) / 2)
    constant_half_height <- 100 / 111000  # 1 degree is approximately 111,000 meters
    half_width <- half_width / 111000  # 1 degree is approximately 111,000 meters
constant_half_height <- set_units(100, "meters")

我建造了一个长方形

 corners <- rbind(
      st_coordinates(center) + c(-half_width, -constant_half_height),
      st_coordinates(center) + c(half_width, -constant_half_height),
      st_coordinates(center) + c(half_width, constant_half_height),
      st_coordinates(center) + c(-half_width, constant_half_height),
      st_coordinates(center) + c(-half_width, -constant_half_height)
    )
    
    rectangle <- st_polygon(list(st_linestring(corners)))

但我不能得到正确的矩形,因为我想让它沿着点的方位角

enter image description here

推荐答案

如果您创建了一条从point1point2的直线,并添加了一个线端平坦的缓冲区,则应该会得到所述的矩形(/../must start and end at two given points /../ follow the points azymuthal angle /../).不过,对于此类缓冲区选项,您需要禁用S2(sf_use_s2(use_s2 = FALSE))或使用投影坐标.

library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
library(ggplot2)
library(ggspatial)

point1 <- 
  data.frame(geometry = "POINT (-73.51295 45.529)") |> 
  st_as_sf(wkt = "geometry", crs = "WGS84") 
point2 <-
  data.frame(geometry = "POINT (-73.51347 45.52816)") |> 
  st_as_sf(wkt = "geometry", crs = "WGS84") 

# transform to projected CRS first; and back to WGS84, if needed
poly_sf <- 
  rbind(point1, point2) |> 
  # UTM 18N
  st_transform(32618) |>
  st_combine() |>
  st_cast("LINESTRING") |>
  st_buffer(dist = 10, endCapStyle="FLAT") |>
  st_transform("WGS84")

st_as_text(poly_sf)
#> [1] "POLYGON ((-73.51335 45.52812, -73.51359 45.5282, -73.51307 45.52904, -73.51283 45.52896, -73.51335 45.52812))"

ggplot() + 
  annotation_map_tile(zoomin = 0 ) +
  geom_sf(data = poly_sf, alpha = 0.6, fill = "gold") +
  geom_sf(data = rbind(point1, point2), size = 3) +
  theme_void()

创建于2023-12-14年第reprex v2.0.2

R相关问答推荐

使用列表列作为case_when LHS的输入

在Julia中调用R函数

如何创建构成多个独立列条目列表的收件箱框列?

如何将在HW上运行的R中的消息(错误、警告等)作为批处理任务输出

使用gggrassure减少地块之间的空间

使用gcuminc,如何使用逗号格式化风险表?

r替换lme S4对象的字符串的一部分

将非重复序列高效转换为长格式

我不能在docker中加载sf

在R中为马赛克图中的每个字段着色

在组中添加值增加和减少的行

标识R中多个列中缺少的唯一值

R中的时间序列(Ts)函数计数不正确

WRS2包中带有bwtrim的简单ANOVA抛出错误

优化从每个面的栅格中提取值

将具有坐标列表列的三角形转换为多个多边形

为什么我对圆周率图的蒙特卡罗估计是空的?

如何将这个小列表转换为数据帧?

我如何使用循环来编写冗余的Rmarkdown脚本?

以不同于绘图中元素的方式对GG图图例进行排序