在R中循环以下载地图数据(栅格数据包) [英] For loop in R to download map data (raster package)

查看:59
本文介绍了在R中循环以下载地图数据(栅格数据包)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用不同的国家绘制非洲地图并将其绘制在一起.我使用以下代码下载并绘制一个国家/地区,效果很好:

I'm currently trying to plot a map of Africa using the different countries and plotting them together. I use the following code to download and plot a single country, which works perfect:

CMR <- getData('GADM', country='CMR', level=0)
plot(CMR)

我现在想对非洲所有不同的国家这样做.因此,我制作了一个包含所有这些GADM代码的角色向量(charafr),现在我想尝试使用for循环将它们全部保存到单独的向量中.我对如何将它们分配给单独的向量感到困惑.这就是我所拥有的:

I wanted to do this now for all the different countries in Africa. So I've made a charactervector (charafr) containing all these GADM codes and now I want to try to save them all to a seperate vector using a for loop. I'm a bit puzzled on how to assign these to seperate vectors. This is what I have:

for (i in 1:53){
     africancountries[i] <- getData('GADM', country=charafr[i], level=0)
}

这是行不通的,我不确定如何调整格式,因此所有spacepolygondataframe都将位于不同的向量中.

This doesn't work and I'm not 100% sure on how to adjust the formatting so all the spatialpolygondataframes will be in a different vector.

有人有主意吗?

谢谢.

推荐答案

您可以这样做

library(raster)
charafr <- c('RWA', 'BDI', 'UGA') 

带有for循环:

ac <- list()
for (i in 1:length(charafr)){
     ac[[i]] <- getData('GADM', country=charafr[i], level=0)
}
allac <- do.call("bind", ac)

plot(allac)

或者,通过 lapply ,更简洁,但可读性更差:

Or, with lapply, more concise, but less readable:

charafr <- c('RWA', 'BDI', 'UGA') 
allac2 <- do.call("bind", lapply(charafr, function(x)  getData('GADM', country=x, level=0)))

要将其作为shapefile保存到磁盘,可以执行

To save this to disk as a shapefile, you can do

shapefile(allac2, 'allac.shp')

这篇关于在R中循环以下载地图数据(栅格数据包)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆