readOGR() cannot open file
You could have shown that you have the right path with:
list.files('~/R/funwithR/data/ne_110m_land', pattern='\\.shp$')
file.exists('~/R/funwithR/data/ne_110m_land/ne_110m_land.shp')
perhaps try:
readOGR(dsn=path.expand("~/R/funwithR/data/ne_110m_land"), layer="ne_110m_land")
or a simpler alternative that is wrapped around that:
library(raster)
s <- shapefile("~/R/funwithR/data/ne_110m_land/ne_110m_land.shp")
Update:
rgdal
has changed a bit and you do not need to separate the path and layer anymore (at least for some formats). So you can do
x <- readOGR("~/R/funwithR/data/ne_110m_land/ne_110m_land.shp")
(perhaps still using path.expand)
Also, if you are still using readOGR
you are a bit behind the times. It is better to use terra::vect
or sf::st_read
.
R: readOGR() cannot open file
To import shape files with readOGR
you can either use
readOGR(dsn = "/home/lucho/data/EnglandGIS/", layer = "england_gor_2011")
where dsn
is the folder containing england_gor_2011.shp
(and other files with the same name but different extensions, e.g. england_gor_2011.dbf
, etc.) or you can specify the full path to the shape file (including the extension):
readOGR("/home/lucho/data/EnglandGIS/england_gor_2011.shp")
The second method won't work for earlier versions of rgdal
as far as I remember.
readOGR(), cannot open file error
Check inside the Shapefile folder if the .shp
, .dbf
, .xml
, .shx
, and .prj
files are actually named as Shapefile. I did that and it helped . I hope it helps you too
readOGR cannot open layer error
Try altering the layer name to the filename without the extension:
countries <- readOGR("https://raw.githubusercontent.com/datasets/geoboundaries-world-110m/master/countries.geojson", layer = "countries")
For some reason in some environments the layer needs to be called "OGRGeoJSON", in other it needs to be called the filename without the extension.
For example with the latest R and rgdal versions on my OSX, it requires "OGRGeoJSON", but on our production machine running CentOS, it requires the filename. I suspect this is something to do with different versions of the underlying gdal C libraries.
R: readOGR() cannot open file
To import shape files with readOGR
you can either use
readOGR(dsn = "/home/lucho/data/EnglandGIS/", layer = "england_gor_2011")
where dsn
is the folder containing england_gor_2011.shp
(and other files with the same name but different extensions, e.g. england_gor_2011.dbf
, etc.) or you can specify the full path to the shape file (including the extension):
readOGR("/home/lucho/data/EnglandGIS/england_gor_2011.shp")
The second method won't work for earlier versions of rgdal
as far as I remember.
Related Topics
Create Unique Identifier from the Interchangeable Combination of Two Variables
Create an Expression from a Function for Data.Table to Eval
Generating a Vector of Difference Between Two Vectors
How to Print R Variables in Middle of String
Aesthetics Must Either Be Length One, or the Same Length as the Dataproblems
Adding Elements to a List in for Loop in R
One-Class Classification with Svm in R
How to Compute Correlations Between All Columns in R and Detect Highly Correlated Variables
R: 'Split' Preserving Natural Order of Factors
Ggplot Year by Year Comparison
How to Extract All the Rows If a Level in One Column Contains All the Levels of Another Column in R
R Reading in a Zip Data File Without Unzipping It
Parallel Execution of Random Forest in R
Figure Captions, References Using Knitr and Markdown to HTML
How to Suppress Automatic Table Name and Number in an .Rmd File Using Xtable or Knitr::Kable