如何在 R 中重新投影形状文件列表 (SpatialLinesDataFrame)

问题描述 投票:0回答:1

我在 R 上读取了形状文件列表(SpatialLinesDataFrame)。现在我尝试将此列表重新投影到不同的 EPSG,但我似乎无法让它工作。我尝试过使用 lapply 并循环浏览列表,但似乎都不起作用,这不断给出下面显示的错误。

我知道我的例子是不可重现的,但我可能只是错过了一些非常明显的东西,因为我找不到一个直接的答案。

EPSG_102033 <- "+proj=aea +lat_1=-5 +lat_2=-42 +lat_0=-32 +lon_0=-60 +x_0=0 +y_0=0 +ellps=aust_SA +units=m +no_defs" # CRS EPSG:102033 string

files <- list.files(files_path, pattern = ".shp$", full.names = TRUE)

roads <- setNames(lapply(files, shapefile), tools::file_path_sans_ext(basename(files))) # this gives a list of three SpatialLinesDataFrame S4 objects

### I have tried:

roads <- lapply(files, function(x) {x <- spTransform(x, CRSobj = EPSG_102033);x}) 

# which gives: Error in (function (classes, fdef, mtable) : 
unable to find an inherited method for function ‘spTransform’ for signature ‘"character", "character"’

### Also tried:

for (i in length(roads)){
  roads[[i]] <- spTransform(roads[i],  CRSobj = EPSG_102033) 
}

# which gives: Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘spTransform’ for signature ‘"list", "character"’

我错过了什么?

谢谢!

r lapply shapefile r-raster
1个回答
0
投票

您是否尝试过使用

st_transform
代替
spTransform
?检查对象的类,前者是来自包
sf
的重新投影的“较新版本”,后者来自较老的一代,因此出现“无法找到继承的方法”错误。这个问题也有类似的问题。以下应该有效:

library(sf)
EPSG_102033 <- "+proj=aea +lat_1=-5 +lat_2=-42 +lat_0=-32 +lon_0=-60 +x_0=0 +y_0=0 +ellps=aust_SA +units=m +no_defs" # CRS EPSG:102033 string

files <- list.files(files_path, pattern = ".shp$", full.names = TRUE)

roads <- setNames(lapply(files, shapefile), tools::file_path_sans_ext(basename(files))) # this gives a list of three SpatialLinesDataFrame S4 objects

### Method 1

roads <- lapply(files, function(x) {x <- st_transform(x, CRSobj = EPSG_102033);x}) 


### Method 2

for (i in length(roads)){
  roads[[i]] <- st_transform(roads[i],  CRSobj = EPSG_102033) 
}

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.