[R-sig-Geo] spsample taking excessive memory

Ned Horning horning at amnh.org
Sat Apr 30 02:50:25 CEST 2011

Hi -

I am trying to select 1000 random samples from each set of ESRI 
Shapefile polygons with the same attribute value (attName). This has 
worked well in the past with other shapefiles. The file I'm using now 
has 69 relatively simple polygons with 9 unique values for the attribute 
I'm using to group the polygons. When I run spsample it takes several 
minutes to collect the samples for some of the unique attribute values 
and it is using well over 12GB of memory. By the time it start 
collecting samples from the last set of polygons my swap space is nearly 
exhausted and the spsample process goes on for hours until I kill it. Do 
these symptoms sound familiar to anyone? Any ideas about what the 
problem might be? I pasted my code below.

Thanks in advance for any help.


shapefile <- '/home/nedhorning/AMNH/Galapagos/quickbird_train_4.shp'
numsamps <- 1000
attName <- 'type_id'

vec <- readShapePoly(shapefile)
# Create vector of unique land cover attribute values
allAtt <- slot(vec, "data")
tabAtt <-table(allAtt[[attName]])
uniqueAtt <-as.numeric(names(tabAtt))
# Create input data from a Shapefile with training data and the image
for (x in 1:length(uniqueAtt)) {
   # Create a vector of all date for attName=x
   class_data <- vec[vec[[attName]]==uniqueAtt[x],]
   # Get random points for the class
   classpts <- spsample(class_data, type="random", n=numsamps)
   #Append data for all but the first run
   if (x == 1) {
     xy <- classpts
   } else {
     xy <- rbind(xy, classpts)

More information about the R-sig-Geo mailing list