[英]Clipping surface polygon by a rather nasty large Spatial polygon data frame
[英]Replace a Polygon in a Spatial Polygon Data Frame
我有一个空间多边形 dataframe 和几个 shapefile。 我想通过高程修剪这些 shapefile 并替换 dataframe 中的原始 shapefile。 但是,当我尝试在修剪后替换多边形时似乎出现错误。 目前,我的计划通过数据集中每个 shapefile 的以下循环运行。 但是,当我尝试dist[i,] <- temp3
时,我收到以下错误:
匹配错误(值,lx):“匹配”需要向量 arguments 另外:警告消息:在检查名称(值)中:尝试设置无效名称:这可能会导致以后出现问题。 看到了吗?make.names
有什么建议么?
# Load spdf
dist <- rgdal::readOGR('critterDistributions.shp');
# Load elevational ranges
rangeElevation <- read.csv(file = 'elevationRanges.csv');
# Load altitude data
elevation <- raster('ETOPO1_Bed_g_geotiff.tif');
# Tidy up CRSes
crs(elevation) <- crs(dist);
# Run loop
for (i in 1:length(dist)){
subjName <- as.character(dist@data$Species[i]);
if (!(subjName %in% rangeElevation$?..Species_name)){
paste0(subjName, 'does not exist in the elevational range database.');
}
else{
erNameMatch <- match(subjName, rangeElevation$?..Species_name);
temp <- raster::reclassify(elevation, rcl = c(-Inf,rangeElevation[erNameMatch,2],NA,
rangeElevation[erNameMatch,2],rangeElevation[erNameMatch,3],1,
rangeElevation[erNameMatch,3],Inf,NA));
temp2 <- dist[i,];
temp <- mask(temp, temp2);
temp <- crop(temp, temp2);
temp3 <- rasterToPolygons(temp, na.rm = T, dissolve = T);
names(temp3) <- make.names(names(temp2), unique = T);
temp3@data <- temp2@data;
dist[i,] <- temp3; # <<<< This is the line of code that doesn't work.
}
}
经过进一步思考,我想出了一个解决方法:启动一个列表,然后在循环之后使用 rbind 将所有内容重新组合到一个 object 中。 我仍然有兴趣找出为什么dist[i,] <- temp3
不起作用,但至少我能够完成这项工作。
oneSPDFtoRuleThemAll <- vector(mode = "list", length = length(dist));
for (i in 1:length(dist)){
subjName <- as.character(dist@data$Species[i]);
if (!(subjName %in% rangeElevation$?..Species_name)){
paste0(subjName, 'does not exist in the elevational range database.');
}
else{
erNameMatch <- match(subjName, rangeElevation$?..Species_name);
temp <- raster::reclassify(elevation, rcl = c(-Inf,rangeElevation[erNameMatch,2],NA,
rangeElevation[erNameMatch,2],rangeElevation[erNameMatch,3],1,
rangeElevation[erNameMatch,3],Inf,NA));
temp2 <- dist[i,];
temp <- mask(temp, temp2);
temp <- crop(temp, temp2);
temp3 <- rasterToPolygons(temp, na.rm = T, dissolve = T);
names(temp3) <- make.names(names(temp2), unique = T);
temp3@data <- temp2@data;
oneSPDFtoRuleThemAll[[i]] <- temp3; # <<<< This is the line of code that doesn't work.
}
}
finalSPDF <- rbind(unlist(oneSPDFtoRuleThemAll));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.