Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in R, save a shapefile

Tags:

r

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?

like image 486
Vasco Avatar asked Apr 08 '16 22:04

Vasco


1 Answers

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

  
like image 54
Cyrus Mohammadian Avatar answered Oct 03 '22 06:10

Cyrus Mohammadian