如何构建矩阵表征多栅格取值 完成场地适宜性选址分析
你之前代码的核心问题是最后重分类时错误调用了原始DEM数据,没有先对三个重分类后的因子栅格做得分汇总,修正后的实现逻辑与代码如下:
1 前置环境准备
# 加载所需库,补充缺失的RColorBrewer用于配色 library(terra) library(sf) library(RColorBrewer) # 读取基础栅格数据 dem <- rast("dem_300m.tif") roads_dist <- rast("roads_dist.tif") # 计算坡度 dem_slope <- terrain(dem, v="slope", n=4, unit="degrees") # 坡度可视化 plot(dem_slope, main = "坡度(单位:度)", col = brewer.pal(9, "Oranges"))
2 单因子重分类
保持你原有的赋值规则不变,重分类后转为数值型栅格用于后续得分汇总:
# 高程重分类:1200-1700得1分,1700-2500得2分,其余为NA dem_reclassified <- classify(dem, c(0,1200,1700,2500,Inf), include.lowest = T) levels(dem_reclassified) <- c("NA","1","2","NA") # 转为数值型 dem_reclassified <- as.numeric(dem_reclassified) # 坡度重分类:15-30度得2分,30-45度得1分,其余为NA slope_reclassified <- classify(dem_slope, c(0,15,30,45,Inf), include.lowest = T) levels(slope_reclassified) <- c("NA","2","1","NA") # 转为数值型 slope_reclassified <- as.numeric(slope_reclassified) # 道路距离重分类:5000米以内得3分,5000-10000米得2分,10000-15000米得1分,其余为NA distance_reclassified <- classify(roads_dist, c(0,5000,10000,15000,Inf), include.lowest = T) levels(distance_reclassified) <- c("3","2","1","NA") # 转为数值型 distance_reclassified <- as.numeric(distance_reclassified)
3 多栅格得分汇总
对三个因子的得分栅格直接求和,得到总得分栅格:
# 像元级得分求和,任意一个因子为NA时总得分自动为NA total_score <- dem_reclassified + slope_reclassified + distance_reclassified
4 总得分重分类得到适宜性分区
修正重分类矩阵的区间边界避免错判,对总得分栅格做重分类:
# 重分类规则:0-5分设为NA,6-9分对应有限适宜(赋值1),9分以上对应适宜(赋值2) rec <- c(0,6,NA, 6,9,1, 9,Inf,2) rec_mat <- matrix(rec, ncol = 3, byrow = T) # 对总得分栅格做重分类,不是原始DEM! suitability <- classify(total_score, rcl = rec_mat, include.lowest = T) # 设置分类名称 levels(suitability) <- data.frame(id = c(1,2), category = c("有限适宜","适宜")) # 结果可视化 plot(suitability, col = terrain.colors(2), main = "野外场地适宜性分区")
内容的提问来源于stack exchange,提问作者scumbagsurfer
相关产品推荐
相关产品推荐

