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

如何在Terra包绘制的栅格图上叠加满足阈值的点?

使用Terra包在栅格图上叠加阈值点的解决方法

问题背景

你已有如下绘制栅格的代码:

library(terra)
set.seed(1)
rr <- rast(matrix(rnorm(400, 1.5, 1), nrow=20, ncol=20))
plot(rr)

想要在该栅格图上叠加值大于阈值3的点,但尝试以下两种方法均无效:
1.

plot(rr)
points(clamp(rr, lower=3))
plot(rr)
points(rr[rr>3])

可行实现方法

之前的方法失效是因为points()函数需要接收坐标点(x,y),而非栅格对象或单纯的栅格值。以下两种方法可以解决问题:

方法一:提取符合条件单元的坐标

library(terra)
set.seed(1)
rr <- rast(matrix(rnorm(400, 1.5, 1), nrow=20, ncol=20))
plot(rr)
# 筛选值>3的栅格单元索引,再提取对应坐标
target_cells <- which(values(rr) > 3)
coords <- xyFromCell(rr, target_cells)
# 绘制红色实心点
points(coords, col="red", pch=19)

方法二:将栅格转为点矢量对象

plot(rr)
# 用clamp保留值>=3的单元,其余转为NA,再转为点对象
threshold_pts <- as.points(clamp(rr, lower=3))
# 绘制蓝色三角形点
points(threshold_pts, col="blue", pch=17)

关键说明

  • xyFromCell():根据栅格单元的索引,返回对应的地理坐标。
  • as.points():将栅格对象转换为点矢量图层,自动忽略NA值,直接适配points()的输入要求。

内容的提问来源于stack exchange,提问作者we need a Mat. Stat.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 11:05:27