R语言影像批量镶嵌与裁剪——mosaic

你有中国不同省份的影像,想要拼接镶嵌成完整的中国影像

镶嵌一

ruby 复制代码
library("raster")                                                                
library("sp")
library("rgdal")
library("rgeos")
library("foreach")
rm (list=ls())
setwd("your path/code")
IOA= c("beijing","changchun","changsha","chengdu","your city")
#图像存储在CC里
CC=foreach(ioa=1:36) %do% {
# cat=print功能
  cat (IOA[ioa],"\n")
# 读取raster
 lulc=raster(paste0("../input/lulc_city/reclass_",IOA[ioa],".tif"))
  lulc
  #print(origin(lulc))
}
a<-raster(paste0("../input/lulc_city/lulc_reclass_",IOA[36],".tif"))
origin(a)
#以最后一个原点为标准
names(CC)[1:2] <- c('x', 'y')
#重叠部分取均值
CC$fun <- mean
#计算空值
CC$na.rm <- TRUE
#容忍偏移
CC$tolerance=0.3
r <- do.call(mosaic,CC)
writeRaster(r,paste0("output/lulc_raw_cn.tif"),overwrite=TRUE)

镶嵌二

ruby 复制代码
library(raster)
tif_file_name <- list.files(path = r"(E:\02_Project\01_Chlorophyll\Select\Result)", pattern = ".tif$", full.names = TRUE, ignore.case = TRUE)
tif_file_list <- list()
for (i in 1:length(tif_file_name)){
  tif_file_list[i] <- raster(tif_file_name[i])
}
tif_file_list$fun <- max
tif_file_list$na.rm <- TRUE
tif_mosaic <- do.call(mosaic, tif_file_list)
plot(tif_mosaic)
# tif_merge <- do.call(merge, tif_file_list)
rf <- writeRaster(tif_mosaic, filename = r"(E:\02_Project\01_Chlorophyll\Select\NewClip\LCC_SC_3.tif)", overwrite = TRUE)

R语言raster包批量拼接、融合大量栅格图像

批量裁剪

设置文件路径

ruby 复制代码
raster_path <- "F:/tif2022/"
output_path <- "F:/month"

list <- list.files(raster_path, pattern = ".tif$")
dir <- paste0(raster_path, list)

clip_raster <- vect("F:/China/sheng2022.shp")
crs(clip_raster)

for (i in 1:length(dir)){
  raster_data <- rast(dir[i])
  
  # 投影图层
  clip_raster <- project(clip_raster, crs(raster_data))
  
  # 裁剪数据
  data <- trim(mask(raster_data, clip_raster))
  
  output_file <- paste0(output_path, "/", basename(list[i]))
  writeRaster(data, output_file, overwrite = TRUE)
}

R语言 | 批量裁剪

相关推荐
没有梦想的咸鱼185-1037-16634 小时前
AI大模型支持下的顶刊绘图|散点图、气泡图、柱状图、热力图、柱状图、热力图、箱线图、热力图、云雨图、韦恩图、瀑布图、神经网络图、时间序列或分布展示
人工智能·神经网络·arcgis·信息可视化·数据分析·r语言·ai写作
青春不败 177-3266-052021 小时前
基于R语言BIOMOD2及机器学习方法的物种分布模拟与案例分析
随机森林·机器学习·r语言·生态学·植被遥感·生物多样性·物种分布
青春不败 177-3266-05202 天前
R语言与DSSAT作物模型
r语言·生态学·作物模型·数字农业·dssat模型·农业科学
asyxchenchong8882 天前
农业系统模拟APSIM全流程详解(气象/土壤/碳氮平衡/NG版本)附R批量处理代码
开发语言·r语言
idolao6 天前
R语言4.4.3统计分析软件安装教程:详细步骤+自定义安装路径(64位)
开发语言·r语言
做cv的小昊6 天前
【TJU】应用统计学——第四周作业(2.3 C-R不等式、2.4区间估计)
c语言·人工智能·算法·机器学习·数学建模·r语言·概率论
爱技术的阿呆8 天前
R code debug 和 study
开发语言·r语言
Q一件事9 天前
R语言制图-相关性及关系网络图
开发语言·r语言
weixin_446934039 天前
多分类暴露变量的亚组分析森林图功能上线了,R语言搞不了风暴统计平台一键搞定
人工智能·机器学习·分类·数据挖掘·r语言