我有附加的代码,可以将单多边形转换为ASCII格式,但是我有27000个多边形,因此我需要能够遍历所有多边形。当前,此循环采用相同的多边形并将其写入27084次,每次都覆盖输出。
我需要进行哪些更改,以使其进入目录中的每个多边形并为每个多边形写一个不同的* .ascii文件?
P.S生成的ascii文件以“ 0.0000000”作为第一个值,有关如何避免这种情况的任何想法?
代码
library("rgdal")
library("sp")
library("raster")
library("stringr")
wd<- "path//to//directory"
setwd(wd)
PatchShapes <- list.files(wd, pattern = "\\.shp$")
asciiID<- paste0("", str_pad(1:27084, width = 2, side = "left", pad = 0),
".ascii") #List to save each ASCII as
## Raster to be used as extent templates
template<-raster("path//to//raster//file.txt")
for (i in 1:27084){
p <- readOGR(dsn= wd , layer = "1") #Need to turn this "1" into i
pr<-raster()
extent(pr)<-extent(template)
template raster
res(pr)<-1000
patch<-rasterize(x=p, y=pr ,field= p$Patch)
writeRaster(patch, filename = "place//to//save//ASCII//file",
format="ascii",overwrite=T, NAflag=0) }
答案 0 :(得分:0)
似乎您想要这样的东西:
library("raster")
library("rgdal")
PatchShapes <- list.files(pattern = "\\.shp$")
template <- raster("path//to//raster//file.asc")
res(template)<-1000
for (i in 1:length(PatchShapes)) {
s <- shapefile(PatchShapes[i])
f <- paste0(i, ".asc")
patch <- rasterize(s, template ,field= s$Patch, filename=f, overwrite=TRUE, NAflag=0)
}