Opening a SHP File in RStudio

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

I am using this code:

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

who gets me:

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

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

Thanks for any help.

+4
source share
4 answers

The error is because the dbf file extension is .DBF , not .DBF , so one way is to simply rename it.

And it is better to use the rgdal::readOGR to read the form file in R.

I have a form file in my /tmp folder to change it to your actual path so that it works.

 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 
+4
source

I also start with R (in French ...) and I had a similar problem with the same message you received. And I found a solution: I just insert exactly the path to the .shp file. For example, with my Mac: /Applications/R/DEPARTEMENT/DEPARTEMENT.SHP. The GEOFLAT 2014 Repertoire (containing the file "DEPARTEMENT.SHP") was downloaded from the IGN (National Geographic) website. The first time I changed "SHP" to "shp" (and so I asked in R consol: "DEPARTEMENT.shp"): no result. The second time I thought it was a complete file path: and success.

I used the extensions "maps" and "maptools" (with "sp", etc.).

Please try and answer me.

+1
source

readOGR(dsn = "/path/to/data", layer = "LIMITE_DEPARTEMENT", ...) look at readOGR(dsn = "/path/to/data", layer = "LIMITE_DEPARTEMENT", ...) , which comes with rgdal . Usually it has no problems with .dbf files.

0
source

You may need to include an external package to read .dbf files.

-1
source

Source: https://habr.com/ru/post/1488320/


All Articles