在 R 中,保存一个 shapefile [英] in R, save a shapefile
问题描述
我想在操作后保存一个 shapefile.
首先,我阅读了我的对象
map<-readOGR("C:/MAPS","33SEE250GC_SIR")
在此之后,我对 shapefile 进行子集化:
test <- fortify(map, region=CD_GEOCODI")测试<-子集(测试,-43.41<long&long<-43.1&-23.05<lat&lat<-22.79)
我得到了这个子集对应的id
ids<-唯一的(test$id)地图2<-地图[地图$CD_GEOCODI %in% ids,]
当我绘制 map2 时,一切正常.但是,当我尝试保存此 shapefile 时,出现了错误
writeOGR(map2, dsn = "C:/MAPS" , layer = "nameofmynewmap")
<块引用>
匹配错误(驱动程序,drvs$name):参数驱动程序"缺少,没有默认
我不知道如何获得驱动器.一些解决方案?
问题是您的 map2
对象不再是 shapefile,因此您无法将其另存为 shapefile.fortify
命令将形状文件的数据槽 (map@data
) 转换为用于映射目的的 data.frame 对象.ggplot2
无法处理 sp 类对象(空间多边形,即形状文件).我假设你想保存这个减少"或子集"的数据.您需要做的是:
库(rgdal)图书馆(dplyr)map <- readOGR("C:/MAPS","33SEE250GC_SIR")地图 <- 子集(世界,LON>-43.41 | LON < -43.1 & LAT>- 23.05 | LAT< -22.79)writeOGR(map, ".", "filename",driver = "ESRI Shapefile") #你也错过了驱动程序参数
I would like to save a shapefile after a manipulation.
First, I read my object
map<-readOGR("C:/MAPS","33SEE250GC_SIR")
After this, I subset my shapefile:
test <- fortify(map, region="CD_GEOCODI")
test<- subset(test, -43.41<long & long < -43.1 & - 23.05<lat & lat< -22.79)
I get the corresponding id's of this subset
ids<- unique(test$id)
map2<- map[map$CD_GEOCODI %in% ids ,]
When I plot the map2, it is all right. But, when I try to save this shapefile, somethinh is wrong
writeOGR(map2, dsn = "C:/MAPS" , layer = "nameofmynewmap")
Error in match(driver, drvs$name) : argument "driver" is missing, with no default
I don't know how to get the drive. Some solution?
The problem is that your map2
object is no longer a shapefile and therefore you cannot save it as a shapefile. The fortify
command converts the data slot of the shape file (map@data
) to data.frame object to be used for mapping purposes. ggplot2
cannot handle objects of class sp (spatial polygon i.e. shape files). I'm assuming you want to save this 'reduced' or 'subsetted' data. What you need to do is the following:
library(rgdal)
library(dplyr)
map <- readOGR("C:/MAPS","33SEE250GC_SIR")
map <- subset(world, LON>-43.41 | LON < -43.1 & LAT>- 23.05 | LAT< -22.79)
writeOGR(map, ".", "filename",
driver = "ESRI Shapefile") #also you were missing the driver argument
这篇关于在 R 中,保存一个 shapefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!