Search code examples
rshapefiler-maptools

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.


Solution

  • 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