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
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
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
.