jonathan
jonathan

Reputation: 149

Opening SHP file in RStudio

I've a package of five files with all French administrative limits (available here). All these five files LIMITE_DEPARTEMENT.SHP/DBF/AVL/PRJ/SHX are in a the folder /home/jonathan/R.

I use this code :

library(maptools)
setwd('/home/jonathan/R')
france<-readShapeSpatial("LIMITE_DEPARTEMENT", proj4string=CRS("+proj=longlat"))

which gets me:

Error in read.dbf(filen1) : unable to open DBF file

I've tried in R (3.0.1) and in Rstudio (0.97.551). I've also read this post and this one. But, now I've no idea about what I can do...

Thanks for any help.

Upvotes: 4

Views: 6228

Answers (4)

fred02840
fred02840

Reputation: 11

I also begin with R (in french…), and I had a similar problem, with the same message you get. And I've discover a solution : I've just insert the precisely way to the .shp file. For example, with my Mac : /Applications/R/DEPARTEMENT/DEPARTEMENT.SHP. The repertory GEOFLAT 2014 (containing the file 'DEPARTEMENT.SHP') had been download from the IGN website (Institut géographique national). In first time, I had changed 'SHP' for 'shp' (and writed like that in R consol : 'DEPARTEMENT.shp') : no result. In a second time, I've thinked to mean the completed path to the file : and success.

I used 'maps' and 'maptools' extensions (with 'sp' and so one).

Try it, please, and answer me.

Upvotes: 1

dickoa
dickoa

Reputation: 18437

The error is related to the fact that the extension of the dbf file is .DBF and not .dbf, so one workaround is just to rename it.

And it's better to use the rgdal::readOGR function to read shapefile in R.

I have the shape file on my /tmp folder so change it to your actual path make it work

require(rgdal)
file.copy(from = "/tmp/LIMITE_DEPARTEMENT.DBF", 
          to = "/tmp/LIMITE_DEPARTEMENT.dbf")
file.remove("/tmp/LIMITE_DEPARTEMENT.DBF")
depart <- readOGR(dsn = "/tmp", layer = "LIMITE_DEPARTEMENT")
str(depart, max.level = 2)
## Formal class 'SpatialLinesDataFrame' [package "sp"] with 4 slots
##   ..@ data       :'data.frame':  330 obs. of  2 variables:
##   ..@ lines      :List of 330
##   .. .. [list output truncated]
##   ..@ bbox       : num [1:2, 1:2] 99226 6049647 1242375 7110524
##   .. ..- attr(*, "dimnames")=List of 2
##   ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots

Upvotes: 4

fdetsch
fdetsch

Reputation: 5308

Have a look at readOGR(dsn = "/path/to/data", layer = "LIMITE_DEPARTEMENT", ...) that comes along with rgdal. It usually has no problems with .dbf files.

Upvotes: 0

matteo
matteo

Reputation: 4873

Maybe you have to turn on the foreign package to read the .dbf files.

Upvotes: -1

Related Questions