Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error opening SHP file in R using maptools readShapePoly

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.

like image 739
Steve Senior Avatar asked May 17 '13 11:05

Steve Senior


2 Answers

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.

like image 163
Aaron Lelevier Avatar answered Sep 17 '22 16:09

Aaron Lelevier


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.

like image 26
mdsumner Avatar answered Sep 20 '22 16:09

mdsumner