如何使用多个shapefile数据集创建for循环函数?

时间:2014-10-16 10:53:14

标签: r for-loop raster shapefile

我创建了这个脚本来计算两个常量rc_Mod2000_LC,y = S01_E031_FC的一个输出。

# Extract LUC in Mod2000
LU_S01_E031_FC_Mod2000 <- extract(x=rc_Mod2000_LC, y=S01_E031_FC)
maj <- function(x){
  y <- as.numeric(names(which.max(table(x))))
  return(y)
}
LU_S01_E031_FC_Mod2000 <- extract(x=rc_Mod2000_LC, y=S01_E031_FC, fun=maj)
colnames (LU_S01_E031_FC_Mod2000) <- c("LU_2000")

现在,我想创建一个for循环函数,其中rc_Mod2000_LC是常量(栅格),但其中S01_E031_FC是变量(shapefile)。 rc_Mod2000_LC是全局土地覆盖图,S01_E031_FC是图块。我有61个shapefile列表。

[[1]]
class       : SpatialPolygonsDataFrame 
features    : 16 
extent      : 30.95493, 31.02964, -1.040257, -0.9624111  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
variables   : 16
names       :   ID, Area_ORG, LU_1990, LU_2000, CHLU_90_00, LU_2005, CHLU_00_05,     Tile,       UNIQ_ID,      AREA, D_90_00, D_00_05, Sour_90_00, Sour_00_05, Conf_90_00, ... 
min values  : 1139,    10.44,      11,      11,       1111,       0,       1112, S01_E031, S01_E031_1139,   90260.9,     100,     200,    GLSE_00,    GLSE_05,          3, ... 
max values  :  935,     8.97,      11,      12,       1112,       0,       1230, S01_E031,  S01_E031_935, 1029736.2,     520,     520,    GLSE_00,    GLSE_05,          3, ... 

[[2]]
class       : SpatialPolygonsDataFrame 
features    : 2 
extent      : 30.95484, 30.99854, -2.022452, -1.971676  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
variables   : 16
names       :  ID, Area_ORG, LU_1990, LU_2000, CHLU_90_00, LU_2005, CHLU_00_05,     Tile,      UNIQ_ID,       AREA, D_90_00, D_00_05, Sour_90_00, Sour_00_05, Conf_90_00, ... 
min values  : 466,     0.06,      11,      11,       1111,       0,       1130, S02_E031, S02_E031_466,   603.7435,     100,     520,    GLSE_00,      GE_06,          3, ... 
max values  : 975,     3.15,      11,      11,       1111,       0,       1130, S02_E031, S02_E031_975, 31698.1260,     100,     520,    GLSE_00,    GLSE_05,          3, ... 

因此我想要x = rc_Mod2000_LC和y = shapefile列表。希望有人可以帮助我,如果你需要一些澄清,请告诉我。

1 个答案:

答案 0 :(得分:0)

我会做这样的事情,首先你提取功能:

extract_shape(y=shape,x=rc_Mod2000_LC){
  S01_E031_FC<- readShapePoly(shape)
  proj4string(S01_E031_FC) <- "+proj=longlat +datum=WGS84"
  extract(y=S01_E031_FC, fun=maj)
}

然后你遍历形状名称:

lapply(list_shapes,extract_shape)

其中:

list_shapes <- c("S01_E031_FC","S01_E032_FC","S01_E033_FC",...)

或者更好的是,如果您的形状名称具有某种模式,您可以使用:

list_shapes <-  ls(pattern='S1_.*_FC')