# 18. 删除旧的异常图斑结果# 防止本次没有异常但保留上次旧文件# ==========================================old_abnormal_files <- list.files( out_dir, pattern = paste0( "^", abnormal_layer, "\\.(shp|shx|dbf|prj|cpg|sbn|sbx|qix|fix|xml)$" ), full.names = TRUE, ignore.case = TRUE)if (length(old_abnormal_files) > 0) { file.remove( old_abnormal_files )}# ==========================================# 19. 写出正式评价结果# ==========================================st_write( result_data, dsn = out_dir, layer = result_layer, driver = "ESRI Shapefile", append = FALSE, delete_layer = TRUE, layer_options = "ENCODING=UTF-8")# ==========================================# 20. 写出规则异常图斑# 只有存在异常时才生成# ==========================================if (nrow(abnormal_data) > 0) { st_write( abnormal_data, dsn = out_dir, layer = abnormal_layer, driver = "ESRI Shapefile", append = FALSE, delete_layer = TRUE, layer_options = "ENCODING=UTF-8" )}# ==========================================# 21. 完成提示# ==========================================cat("\n")cat("==========================================\n")cat("评价计算完成\n")cat("==========================================\n")cat( "正式评价结果:", file.path( out_dir, paste0(result_layer, ".shp") ), "\n")if (nrow(abnormal_data) > 0) { cat( "异常图斑结果:", file.path( out_dir, paste0(abnormal_layer, ".shp") ), "\n" )} else { cat("没有发现规则异常图斑。\n")}