0

I load dbf files in R and then I manipulate it because I only want some features and after that I export it.

But when I will open the shapefile with the same name in a GIS software features are not located in the correct place. Can it be the result of the change of the primary key of the dbf file?

Code:

recfe<-read.dbf("RECFE.dbf", as.is = FALSE)
recin<-recfe[which(recfe$MUNICIPIO==86),]
write.dbf(recin, "RECFE.dbf", factor2char = TRUE, max_nchar = 254)

Any suggestion?

6

A shapefile has several components - the important ones are .shp, which contains the geographic data and .dbf that contains the attributes. Each row of the DBF corresponds to one geographic feature, and the order is important. There's no "primary key" as such.

So if you delete rows from the DBF, you'll make the shapefile invalid, and get the sort of errors you see.

If you read the shapefile in using spatial data packages like sp or sf then you can do your subsetting and save a new shapefile with fewer rows in the DBF and fewer features in the SHP part. Something like:

library(sf)
d = st_read("data.shp")
d = d[d$age>20,]
st_write(d,"data.shp")
  • I have done it and the features are located in their site. – mike89 Jan 11 '18 at 9:43
  • But I have another question. In the new dbf there are fields where the registers appear with decimals. Can we specified that new fields will be the same type of data of the old dbf? Thanks – mike89 Jan 11 '18 at 9:50
  • Best ask this as a new question, with some example data. I'll promise to take a look at it. – Spacedman Jan 11 '18 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.