21

I'm currently using the following script to add some attribute data from a table to lots of individual shapefiles:

library(rgdal)
specieslist <- read.csv("SpeciesList1.txt", header=F)
attdata <- read.table("TestAtt.csv", sep = ",", header=T)
for (n in 1:dim(specieslist)[1]) 
{
speciesname <- specieslist[n,1]
shp <- readOGR("Mesoamerica_modified_polygons", speciesname)
shp$ENGL_NAME<-attdata[n,2]
writeOGR(shp, "PolygonsV2", speciesname, driver="ESRI Shapefile")
}

I get the following warning(s) at the end:

1: In writeOGR(shp, "PolygonsV2", speciesname, driver = "ESRI Shapefile") :
Field names abbreviated for ESRI Shapefile driver

When viewing the attribute table of the shapefiles after this process, the Field name has been shortened to 'ENGL_', but I want it to stay as 'ENGL_NAME'.

Is there a way to turn this abbreviating off?

0

7 Answers 7

9

You can't, it's a shapefile issue. See http://gdal.org/drv_shapefile.html under 'Creation Options'

0
7

Your 'ENGL_NAME' shouldn't be abbreviated at all (less than 10 characters), but writeOGR has its own will, it seems.

Instead of

writeOGR(shp, "PolygonsV2", speciesname, driver="ESRI Shapefile")

you might try

library(maptools)
currdir <- getwd() #store your current working directory
setwd(paste(currdir,"PolygonsV2",sep="/")) #switch to your desired folder

writeSpatialShape(shp, speciesname) # write shapefile

setwd(currdir) #switch back to parent folder

As writeSpatialShape seems not have a parameter for the destination, I found this workaround switching the working directory back and forth.

The other problem there is, that it does't produce a .prj-file, but thats a minor problem, compared to destroyed field names.

0
3

I've been having similar troubles working in RStudio. Per the advice in various comments and answers above, my scorched-earth solution is:

  • at the point where the SpatialWhateverDataFrame is ready to be written to Shape, make a copy
  • names(copy@data) <- c('new', 'short', 'names', 'you', 'pickd', 'yrslf')
  • names(copy@data) <- strtrim(names(copy@data), 10) just to be sure
  • writeOGR(copy, dsn, layer, driver = 'ESRI Shapefile') but don't run it yet
  • save the script, clear the workspace including hidden objects, restart R, rerun the entire script.

writeOGR() uses base::abbreviate - here's a test with a copy of lines 158-164:

fld_names <- c('short', 'longlonglong', 'middle_1')
if (any(nchar(fld_names) > 10)) {
    fld_names <- abbreviate(fld_names, minlength = 7)
    warning("Field names abbreviated for ESRI Shapefile driver")
    if (any(nchar(fld_names) > 10)) 
      fld_names <- abbreviate(fld_names, minlength = 5)
  }

> fld_names
       short longlonglong     middle_1 
     "short"    "lnglngl"    "middl_1" 
> names(fld_names)
[1] "short"        "longlonglong" "middle_1"  

You can see it actually calls abbreviate twice (possibly pointlessly, I can't figure out how you'd trigger that sub-loop), and if even one column name > 10 then it'll shorten any column name with > 7 characters. I cant figure out why one has to clear the workspace and restart if writeOGR has been run on the same object before, but maybe its something to do with fld_names being a named character vector. It might work better if as.character() was wrapped around abbreviate().

0
0

It's possible to abbreviate to 10 characters by overwriting the original code of writeOGR (see https://rdrr.io/cran/rgdal/src/R/ogr_write.R) in your own function also named writeOGR (or use a new function name). Maybe you want to add a note in your script to mention this.

Replace in your function (search "abb"):

fld_names <- abbreviate(fld_names, minlength=7) or fld_names <- abbreviate(fld_names, minlength=5) by fld_names <- abbreviate(fld_names, minlength=10)

0

If someone is still running in this warning like I just did... I stopped receiving the warning after renaming the variable with two underscores (e.g., from "a_b_2" to "a_b").

0

maptools::writeSpatialShape is deprecated.

To overcome the abbreviation of all fieldnames to 7 characters, I used something like:

names(shapefile) <- abbreviate(names(shapefile),10)

Changing the function would be nice too, but my script needs to be run on several machines.

-1

As already mentioned, shapefiles have a field name character limit of 10 characters. writeOGR meets this requirement by changing the field headings using some algorithm that prioritizes what characters to remove when there is a field name that exceeds the limit. I'm not sure how it works, but it seems to shorten field names in strange and unpredictable ways, and can shorten field names this way that already met the 10 requirement.

Here's my work around. Using strtrim() and setting the character length to 10 will truncate the field names to 10 characters more predictably than writeOGR's automation.

One issue that you might have is if you have fieldnames that are identical for the first 10 characters, but I rarely have this problem.

I apply this every time I export a shapefile, just in case.

library(sp)
library(rgdal)

table <- data.frame(X_Coordinates = runif(10)*1000, 
                    Y_Coordinates = runif(10)*1000, 
                    LongFieldNameForData = runif(10))

p <- SpatialPointsDataFrame(SpatialPoints(table), data = table)

names(p) <- strtrim(names(points),10)

writeOGR(p, "OutputDirectory", "Points", "ESRI Shapefile")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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