1

I'm trying to read a shp file but to no success. I already tried all the answers in this readOGR() cannot open file and none of them worked for me.

Using

file.exists('../Downloads/NUTS_RG_03M_2013_4326_LEVL_2.shp')

I get back TRUE

But when I run

shapefile('../Downloads/NUTS_RG_03M_2013_4326_LEVL_2.shp')

I get

Error: file.exists(extension(x, ".shp")) is not TRUE

I tried running

readOGR(dsn = "..Downloads/NUTS_RG_03M_2013_4326_LEVL_2.shp", layer ="NUTS_RG_03M_2013_4326_LEVL_2")

and get the error

Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv,  : 
  Cannot open data source

Also tried

readOGR(dsn=path.expand("../Downloads"), layer="NUTS_RG_03M_2013_4326_LEVL_2")

and got

Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv,  : 
  Cannot open layer

I don´t know what else to try

bulba
  • 57
  • 3
  • Most likely explanation: `"..Downloads/NUTS_RG_03M_2013_4326_LEVL_2.shp"` is a folder rather than a file. Let me know if this is the correct explanation, if so I'll add this as an answer – dww Jun 26 '19 at 04:01
  • Does this help? [Things I Forget: Reading a Shapefile in R with readOGR](https://landeco2point0.wordpress.com/2013/09/27/things-i-forget-reading-a-shapefile-in-r-with-readogr/). The first argument should be a directory and the layer argument is the file minus the `.shp` extension. – neilfws Jun 26 '19 at 04:28
  • It is a file, and I tried using "." in the dsn but still got the same error. Also tried to load other shp files to see if the problem was with the one I had, but no results – bulba Jun 26 '19 at 04:35
  • What we have here is an issue specific to your local system. Is there a folder in `Downloads` which contains the `.shp` file and other files? Then the path to that folder, minus the trailing slash is the `dsn`. The shapefile name, without its extension, is the `layer`. Specify the arguments correctly and it will work. What you seem to be doing is trying multiple combinations of things in the hope that one will work. – neilfws Jun 26 '19 at 06:58

2 Answers2

1

I was facing the same issue, but got around it when I put other files that were in the same .zip file as my .shp file together with it in the working directory. Files with extensions such as .shx, .qpg, .prj and .cpj. I didn't change anything in the code. Just added the files.

0

All options tried so far were slightly wrong :)

Assuming you downloaded the ZIP file from here and extracted it in Downloads, then the path to the file should be:

Downloads/NUTS_RG_03M_2013_4326_LEVL_2.shp/NUTS_RG_03M_2013_4326_LEVL_2.shp

Note that both the unzipped directory and the file inside it have the .shp extension.

The dsn should be the path to the directory without a trailing slash and the layer is the shapefile without the .shp extension.

So I think you want:

readOGR("../../Downloads/NUTS_RG_03M_2013_4326_LEVL_2.shp", "NUTS_RG_03M_2013_4326_LEVL_2")

EDIT:

I ran this code on my local (Windows 10) machine:

nuts <- readOGR("../../Downloads/NUTS_RG_03M_2013_4326_LEVL_2.shp",
                "NUTS_RG_03M_2013_4326_LEVL_2")

and the file reads, with message:

OGR data source with driver: ESRI Shapefile 
Source: "C:\Users\nsaunders8\Downloads\NUTS_RG_03M_2013_4326_LEVL_2.shp", 
  layer: "NUTS_RG_03M_2013_4326_LEVL_2"
with 320 features
It has 5 fields
neilfws
  • 32,751
  • 5
  • 50
  • 63
  • It didnt work :( I trid putting the files in other directorys, but still geting the "cannot open data source" error. I have no ideia what the problem is, because I tried using with other shp files, and nothing works – bulba Jun 26 '19 at 06:30
  • If the files and directories on your system are as specified in the answer, it will work. If you are essentially just trying random combinations of things, it will not work. Read the link in my comment, compare with your situation and try again. – neilfws Jun 26 '19 at 06:54
  • I tried already. I put the file in my working directory, and used readOGR(".","nameofile") and gave me the "Cannot open layer" error. What i find it weird is that when I run list.file, the file is there (and the only one with that name) and file.exists gives me TRUE – bulba Jun 26 '19 at 16:44
  • Don't put the file in your working directory. Download the `.zip` to `Downloads`. Unzip it. Run the code with the correct specification for `dsn` and `layer`, as described in the answer. – neilfws Jun 26 '19 at 22:39