You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R语言中提取标准差椭圆的几何信息

提取标准差椭圆为几何列并添加到所有观测行

你可以通过提取calc_sde返回对象中的椭圆参数,手动生成椭圆的几何对象,再将其复制到原数据框的每一行。以下是具体实现步骤:

1. 加载所需包

除了aspace,我们还需要sf来处理几何对象:

library(aspace)
library(tibble)
library(sf)

2. 提取标准差椭圆参数

calc_sde返回的SDE对象包含生成椭圆所需的全部关键参数:

  • x0/y0:椭圆中心的经纬度
  • a/b:长半轴、短半轴长度(单位与输入点一致,此处为度)
  • theta:椭圆逆时针旋转角度(弧度)

从你的sde对象中提取这些参数:

# 提取椭圆参数
ellipse_params <- list(
  x0 = sde$x0,
  y0 = sde$y0,
  a = sde$a,
  b = sde$b,
  theta = sde$theta
)

3. 生成椭圆几何对象

编写一个函数,利用上述参数生成sf格式的椭圆多边形:

generate_ellipse_geom <- function(x0, y0, a, b, theta, n_points = 100) {
  # 生成椭圆的角度序列
  angles <- seq(0, 2 * pi, length.out = n_points)
  
  # 计算未旋转的椭圆坐标
  x_unrot <- x0 + a * cos(angles)
  y_unrot <- y0 + b * sin(angles)
  
  # 应用旋转矩阵,将椭圆旋转到指定角度
  x_rot <- x0 + (x_unrot - x0) * cos(theta) - (y_unrot - y0) * sin(theta)
  y_rot <- y0 + (x_unrot - x0) * sin(theta) + (y_unrot - y0) * cos(theta)
  
  # 闭合多边形(首尾点重合)
  x_rot <- c(x_rot, x_rot[1])
  y_rot <- c(y_rot, y_rot[1])
  
  # 返回sf多边形对象
  st_polygon(list(cbind(x_rot, y_rot)))
}

# 生成椭圆几何
ellipse_geom <- generate_ellipse_geom(
  x0 = ellipse_params$x0,
  y0 = ellipse_params$y0,
  a = ellipse_params$a,
  b = ellipse_params$b,
  theta = ellipse_params$theta
)

4. 添加几何列到原数据框

将生成的椭圆几何复制到原数据框的每一行,创建新的几何列:

# 为每一行添加椭圆几何
D_with_ellipse <- D %>%
  mutate(ellipse_geom = st_sfc(rep(list(ellipse_geom), nrow(.)))) %>%
  st_sf()  # 转换为sf数据框

验证结果

你可以通过绘图验证椭圆与原始点的匹配度:

# 绘制椭圆
plot(st_geometry(D_with_ellipse$ellipse_geom), col = "lightblue", border = "darkblue")
# 叠加原始点
points(D$trip_long, D$trip_lat, pch = 16, col = "red", cex = 0.8)

如果需要将椭圆转换为投影坐标系(而非地理坐标系),可以使用st_transform()函数进行转换。


内容的提问来源于stack exchange,提问作者alvpp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 13:02:28