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语言 | 批量裁剪

相关推荐
asyxchenchong8883 天前
R语言混合效应(多水平/层次/嵌套)模型及贝叶斯实现技术应用
开发语言·r语言
临床数据科学和人工智能兴趣组5 天前
R语言中,列表是一种非常灵活的数据结构,它可以存储不同类型的对象,如向量、矩阵、数据框、甚至其他列表
数据结构·数据库·r语言·r语言-4.2.1·临床试验
临床数据科学和人工智能兴趣组6 天前
矩阵是一种二维数组,每个矩阵仅能包含一类数据(数值型、字符型或者逻辑型)
数据库·线性代数·矩阵·r语言·r语言-4.2.1·临床试验
临床数据科学和人工智能兴趣组8 天前
网络爬虫(Web Scraping)是一种用于自动提取网页内容的技术
r语言·r语言-4.2.1
生态学者8 天前
Ecological Indicators | 机场鸟调的新方法:用 AWM-PC1 读懂干扰梯度下的鸟类群落
人工智能·算法·r语言·微信公众平台
临床数据科学和人工智能兴趣组8 天前
运用rvest包进行数据爬虫
r语言·r语言-4.2.1
霸道流氓气质9 天前
基于本地 PaddleOCR 的 PDF 文字识别完整技术文档
开发语言·r语言·pdf
临床数据科学和人工智能兴趣组9 天前
SQL专为数据操作而设计,能够高效执行复杂的查询、筛选、排序、分组等操作
r语言·r语言-4.2.1
青春不败 177-3266-052010 天前
基于R、Python的Copula变量相关性分析及AI大模型应用
人工智能·python·r语言·贝叶斯·统计学·copula
统计学小王子11 天前
分类模型评价指标——R语言(数学建模常用)
数学建模·分类·r语言