Dear List Members

Several months ago I wrote a code to buffer a SpatialPolygonsDataFrame. Each attribute of the SPDF needed to be buffered by a different amount (defined by one of the columns within the SPDF), some of which were negative. Some of the negative buffers were large enough to warrant the vanishing of that particular polygon. Since gBuffer on its own could not deal with these aspects, the code included a function which would buffer each polygon individually, add the outputs to a list, remove any features which no longer existed, then convert it back to a SPDF. However I have come back to my code and it no longer works with negative buffers, even on the original datasets. It now produces the following error:

Error in names(buffs)[i] <- ID[i] :
  'names' attribute [1598] must be the same length as the vector [1597]

even though the length of the SPDF is 1747. It does however still work for positive only buffers. The only thing I can think of is that gBuffer has been updated and now deals with vanishing features differently but I have not been able to adapt the code to make it work. Since all elements of the function come from the same object I don't understand how attributes are ending up at different lengths. Also, if I manually do the function step by step, the attributes remain at 1747 and I am not able to reproduce the same error so I am very confused.

Here is a reproduction of the code:

library(Guerry)
data(gfrance)

bufferby <- function(spdf,widths,ID){
  library(rgeos)
  #Create empty buffer list based on spdf
  buffs<-vector("list",length(spdf))
  #For every item in spdf,
  for(i in seq_len(length(spdf))){
    #Loop buffer each polygon individually based on widths, and add to 'list'
    buffs[[i]] <- gBuffer(spdf[i,], width=widths[i], capStyle="ROUND")
    #Name each object in list by ID
    names(buffs)[i] <- ID[i]
  }
  #Filter results by those that worked (i.e. exclude any 'vanished' buffers)
  buffs=Filter(function(x){!is.null(bbox(x))},buffs)
  #Convert the filtered results to SP
  buffsSP<-SpatialPolygons(lapply(1:length(buffs), function(i) {
    Pol <- slot(buffs[[i]], "polygons")[[1]]
    #Add names to be able to link to other datasets
    slot(Pol, "ID") <- names(buffs)[i]
    Pol
  }))
  #Project output (SP) based on input (spdf)
  proj4string(buffsSP)<-CRS(projection(spdf))
  buffsSP
}

#Create widths defining column, including negatives
gfrance$buffwid<- runif(length(gfrance), min=-200000, max=20000)

newbuffs<-bufferby(gfrance,gfrance$buffwid,gfrance$Department)
plot(newbuffs)

If I define "widths" as a column with only positive values it works fine, but it no longer works if any of the negative features vanish.

N.B. This function should (and did) also work for SpatialPoints.

Many thanks for any help, and your time.

Emma

	[[alternative HTML version deleted]]

