0

I am trying to use the below code using maptool:

stateShape <- readShapeSpatial("tl_2013_us_state")

but i get an error:

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

my directory is correct

chrki
  • 6,143
  • 6
  • 35
  • 55
  • 1
    Have you seen [this post](http://stackoverflow.com/questions/16607532/error-opening-shp-file-in-r-using-maptools-readshapepoly)? – jazzurro Oct 31 '14 at 02:39

1 Answers1

3

To read in spatial data you need the .shp, .shx, and .dbf files. You get the following behaviour if bits are missing:

NO error if all there:

> m=readShapeSpatial("metro")

Let's remove the .dbf

> file.remove("metro.dbf")
[1] TRUE
> m=readShapeSpatial("metro")
Error in read.dbf(filen1) : unable to open DBF file

Now without the .shx:

> file.remove("metro.shx")
[1] TRUE
> m=readShapeSpatial("metro")
Error in getinfo.shape(fn) : Error opening SHP file

which looks like your error. Do you also have the corresponding .shx file?

Shapefiles may also have a .prj file, but readShapeSpatial ignores this, which is why you shouldn't use it. ALWAYS use the rgdal package:

> m = readOGR(".","metro")
OGR data source with driver: ESRI Shapefile 
Source: ".", layer: "metro"
with 5 features and 12 fields
Feature type: wkbPolygon with 2 dimensions
> summary(m)
Object of class SpatialPolygonsDataFrame
Coordinates:
        min       max
x -96.90567 -95.84013
y  40.99636  41.74546
Is projected: FALSE 
proj4string :
[+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0]

Note the correctly-set proj4string value. You don't get that with readShapeSpatial.

Spacedman
  • 92,590
  • 12
  • 140
  • 224