你有中国不同省份的影像,想要拼接镶嵌成完整的中国影像
镶嵌一
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)
批量裁剪
设置文件路径
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)
}