我想用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)))
但我不能得到正确的矩形,因为我想让它沿着点的方位角