in R, save a shapefile

倖福魔咒の 提交于 2020-12-05 20:20:33

问题


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?


回答1:


The problem is that your map2object 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


来源:https://stackoverflow.com/questions/36510531/in-r-save-a-shapefile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!