Cannot open layer and Cannot open data source in readOGR

39 Views Asked by At

I have the rgdal abd raster libraries running

I'm trying to open a .shp map in R I've already tried

setwd("C:/Users/user/Documents/tarso first paper/R")
study_area <- readOGR("municipios.shp")

did not work so I tried

study_area <- readOGR("C:/Users/user/Documents/tarso first paper/R/municipios.shp")
study_area <- readOGR("C:/Users/user/Documents/tarso first paper/R","municipios.shp")
study_area <- readOGR("C:/Users/user/Documents/tarso first paper/R/municipios")
study_area <- readOGR("C:/Users/user/Documents/tarso first paper/R","municipios")

Also, it didn't work... Whenever there is one of the 2 types of error message, "Cannot open layer" or "Cannot open data"

Data source is governmental and open in Qgiz So the data is probably ok

I have no idea how to fix it....

1

There are 1 best solutions below

0
Robert Hijmans On

You should not be using readOGR anymore because the underlying package (rgdal) will be retired soon. Instead of "raster" you should now use "terra".

For example:

f <- "municipios.shp"
## check if all files needed exist
ff <- c(f, sapply(c(".shx", ".dbf"), \(ext) gsub("\\.shp$", ext, f)))
all(file.exists(ff))
# TRUE

## open with terra
library(terra)
v <- vect(f)