19

I am new to R and was following the following tutorial on the ggplot2 package found here. However the readShapePoly() function throws an error whenever I try to load the basic shapefile. I have used the following code:

library("ggplot2")
library("gpclib")
library("maptools")
setwd("~/Documents/R Projects/Intro to ggplot2") 
#Intro to ggplot 2 contains the .shp file and associated data
sport <- readShapePoly("london_sport.shp")

which gets me:

Error in getinfo.shape(filen) : Error opening SHP file

I have tried omitting the file extension. I have also tried downloading other .shp files which throw the same error too. I have also tried calling readShapePoly using the full file path, which doesn't work either. I am using R studio (mac OSX), but I get the same error using the standard R window. I have tried the suggestions on the previous closed threat "Error opening SHP file in Rstudio", but to no avail.

Edit: the error was with a missing .dbf file. Thanks to @Spacedman for the fix.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Steve Senior
  • 191
  • 1
  • 1
  • 6
  • Are you sure the shape file works? Have you tried `readOGR`? – Ari B. Friedman May 17 '13 at 11:27
  • Try `list.files()` and see what is returned. Is your shapefile there, so named? Definitely don't add the extension when trying to read it in with `readShapePoly` – Simon O'Hanlon May 17 '13 at 11:40
  • 9
    Have you ALSO got the `london_sport.shx` and `london_sport.dbf` files in the same folder? You need all three to make a "Shapefile". – Spacedman May 17 '13 at 12:09
  • @Spacedman - Good tip, the .dbf file was missing. Have tracked it down and the problem is fixed. Sorry for the basic error. Kinda embarrassed now. Thx all. – Steve Senior May 17 '13 at 14:09
  • for me the simple solution worked from link : https://gis.stackexchange.com/questions/21533/api-to-reverse-geocode-latitude-longitude-to-census-tract – krishna kumar Mar 14 '18 at 18:47

7 Answers7

16

I had a similar issue, and it was because there were several other files along with the '*.shp' shape file in the zip package that I downloaded. Then I only moved the shape file to another folder and it didn't work. When including all files together, it was fine and I could readShapeSpatial() function okay.

Hack-R
  • 22,422
  • 14
  • 75
  • 131
Aaron Lelevier
  • 19,850
  • 11
  • 76
  • 111
  • humph...I working on a cool problem. I try starting from a clean slate – miles2know Oct 04 '14 at 03:50
  • 2
    This solved similar problem I had. You need more than just the .shp file as the documentation says: `shapefile layer name, when writing omitting the extensions *.shp, *.shx and *.dbf, which are added in the function` – Mikko Sep 22 '15 at 11:50
6

Forget ggplot and gpcclib. Stick to maptools and rgdal that actually provide tools for reading a shapefile.

Don't just say you've tried "this and that", outline the details. For example, does `file.exists("london_sport.shp") return TRUE?

Also, what makes you think readShapePoly() is the right function? It only knows how to read polygon shapefiles, try readShapePoints() and readShapeLines() as well.

If you can, try readOGR which can read a shapefile despite many caveats (including the geometry type).

library(rgdal)
readOGR("~/Documents/R Projects/Intro to ggplot2", "london_sport")

If you can report on all of those things it's likely someone could help.

mdsumner
  • 29,099
  • 6
  • 83
  • 91
2

There is one more, but not much automatic solution which helped me:

file<- readShapePoly(file.choose())

Then just find your *.shp file and run it.

lujjas
  • 68
  • 9
2

Include three of those files (extensions: dbf, shp, shx) in the same folder.

Masood Sadat
  • 1,247
  • 11
  • 18
1

In case this helps anyone:

I had the same problem but none of the solutions worked. Worst, I the same was going on with an script that I'm 100% sure was working previously.

Turns out that it could also be that the shapefile gets damaged. Apparently this can happen while the file is being manipulated (or so that say my geographer friends), so next time you try to open it won't work for no apparent reason. Downloading it again worked fine, but makes me think to have a copy of the more precious ones just in case.

A.Mstt
  • 301
  • 1
  • 3
  • 15
0

I just managed to fix this problem with a shapefile I was trying to read by typing:

file<- readShapeSpatial("filename.shp")

instead of typing in the full file path.

Before then, I tried all the suggestions, including making sure that the .dbf and .shx files were also present. Don't know why this should be the case.

Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69
Sree
  • 1
  • 1
0

I had the same problem. I found out that basically you need three of those files: .shp, .shx and .dbf

Masood Sadat
  • 1,247
  • 11
  • 18