首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何为这段代码创建一个for循环?

如何为这段代码创建一个for循环?
EN

Stack Overflow用户
提问于 2022-01-19 22:47:30
回答 1查看 69关注 0票数 0

下面的代码实现了我的目标,计算一个家庭范围重叠我的治疗区域的百分比。不过,我有190个家范围。对于创建一个for循环或其他方法来自动化所有归属范围的这个过程,有什么建议吗?谢谢

我不知道如何在这种情况下创造一个可复制的例子,但如果有人能指导我完成这一过程,我会的。

代码语言:javascript
复制
library(sf)
library(plyr)
library(amt)
library(raster)
library(mapview)

######################################################
#read in treatment area shapefile
treatment <- st_read('C:\\Users\\kujld016\\Desktop\\All\\Projects\\Brush_Management\\imagery\\Treatment_perimeter.shp')

#check projection
projection(treatment)

#transform treatment shapefile to correct projection
treatment<-st_transform(treatment,'+proj=utm +zone=14 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs')
projection(treatment)

#######################################################
#read in home-range shapefile
home-range <- st_read("C:\\Users\\kujld016\\Desktop\\All\\Projects\\Brush_Management\\KingR_BBM\\contour_95_151_880_25_2 2009-08.shp")

#check projection
projection(home-range)

#transform home-range shapefile projection to match treatment area shapefile
home-range <- st_transform(home-range,'+proj=utm +zone=14 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs')
projection(home-range)

###################################################
#calculate area overlap between home-range shapefile and treatment area shapefile
pi <- st_intersection(treatment,home-range)
st_area(pi)

#calculate percentage of home-range shapefile overlapping treatment area shapefile and add value to new column
home-range$percent_area <- st_area(pi) / st_area(home-range)```
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-19 23:17:52

您可以修改脚本以遍历多个主范围,如下所示:

首先,按照您所做的那样定义您的treatment,假设对每个家庭范围都是相同的处理方式:

代码语言:javascript
复制
treatment <- st_read('C:\\Users\\kujld016\\Desktop\\All\\Projects\\Brush_Management\\imagery\\Treatment_perimeter.shp')
treatment <- st_transform(treatment,'+proj=utm +zone=14 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs')

接下来,使用list.files()创建190个shapefile名称的列表。为此,将所有190个shapefiles放在同一个文件夹中,最好在父目录中的某个位置。path参数在list.files()中将指向何处。"*.shp"将包括任何具有.shp扩展名的文件。

代码语言:javascript
复制
home.names <- list.files(path = "foldername", pattern = "*.shp")

现在编写循环,它将遍历每个shapefile。

代码语言:javascript
复制
all.home.ranges <- list()

for (shp in 1:length(home.names)) {
    home.range <- st_read(home.names[shp])
    home.range.t <- st_transform(home.range,'+proj=utm +zone=14 +ellps=GRS80 
     +towgs84=0,0,0,0,0,0,0 +units=m +no_defs')
    pi <- st_intersection(treatment, home.range.t)
    home.range.t$percent_area <- st_area(pi) / st_area(home.range.t)
    all.home.ranges[[shp]] <- home.range.t
}

all.home.ranges将是一个包含190个元素的列表,每个元素都是一个不同的主范围。

注意,这个循环可能需要一段时间才能运行。我建议通过将for (shp in 1:length(home.names))更改为for (shp in 1:length(home.names[1:2]))来测试它,后者只执行两次迭代。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70778716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档