Skip to content

R-stats - GRASS6

3 messages · Markus Neteler, Roger Bivand

#
Dear list members,

I am currently trying out the new GRASS 6/R-stats interface.
I installed all software today (R-2.1.0, all interfaces, using
GRASS 6.1-CVS).

There is a little problem which I don't understand:
List of 28
 $ GISDBASE         : chr "/ssi0/ssi/neteler/grassdata"
 $ LOCATION_NAME    : chr "spearfish60"
 $ MAPSET           : chr "neteler"
 $ GRASS_DB_ENCODING: chr "utf-8"
 $ DEBUG            : chr "0"
 $ MONITOR          : chr "x0"
 $ GRASS_GUI        : chr "text"
 $ projection       : chr "1 (UTM)"
[...]
datatype=CELL
Error in "names<-.default"(`*tmp*`, value = "landcover.orig") :
        'names' attribute [1] must be the same length as the vector [0]

# this works:
Error in "names<-.default"(`*tmp*`, value = "landcover.30m") :
        'names' attribute [1] must be the same length as the vector [0]


I was inspired by
 http://r-spatial.sourceforge.net/xtra/xtra.RHnw.html
and the upcoming article from Roger Bivand in the GRASS Newsletter Vol3.

Was there a change recently which makes category transfer fail?

Best regards

 Markus
#
On Tue, 31 May 2005, Markus Neteler wrote:

            
I can't replicate this, I'm afraid (R 2.1.0, Linux, sp 0.7-7, spgrass6
0.1-4 (from CVS on sourceforge, GRASS 6.0.0 release). I don't think the sp 
or spgrass6 versions make a difference. 

Could you say debug(readCELL6sp) and re-run this please - that should show 
which line is failing. Once we know that, please check the arguments to 
that function for sanity. I don't think it is important, but I'm running 
under LANG=en_GB ; export LANG, not UTF8. My current guess is that the 
problem is in:

res <- read.asciigrid(tmpfl, colname = vname, proj4string = p4)

where vname is the layer name. If you additionally do 
debug(read.asciigrid), it may fail at: names(df) = colname - please see 
what is in colname, and if length(colname) is the same as length(df). One 
possibility is that there is something amiss in read.asciigrid().

The landcover.orig layer is strange because there are no category labels 
at all, so:
11 
21 
22 
23 
31 
32 
41 
42 
43 
51 
71 
81 
82 
83 
85 
91 
92 
* no data

so the factor gets built without labels:
Formal class 'SpatialGridDataFrame' [package "sp"] with 6 slots
  ..@ data       :Formal class 'AttributeList' [package "sp"] with 1 slots
  .. .. ..@ att:List of 1
  .. .. .. ..$ landcover.orig: Factor w/ 17 levels "","","","","",..: NA 15 2 2 2 2 2 2 2 2 ...
  ..@ grid       :Formal class 'GridTopology' [package "sp"] with 3 slots
  .. .. ..@ cellcentre.offset: num [1:2]  589995 4913715
  .. .. ..@ cellsize         : num [1:2] 30 30
  .. .. ..@ cells.dim        : int [1:2] 634 477
  ..@ grid.index : int(0) 
  ..@ coords     : num [1:2, 1:2]  589995  608985 4913715 4927995
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : NULL
  .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
  ..@ bbox       : num [1:2, 1:2]  589995 4913715  608985 4927995
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
  .. .. ..@ projargs: chr "+proj=utm +zone=13 +a=6378206.4 +rf=294.9786982 +no_defs +nadgrids=conus"

suggesting that the function should check for unique labels of the 
matching number.

Best wishes,

Roger

  
    
#
Dear Roger,
On Tue, May 31, 2005 at 06:53:31PM +0200, Roger Bivand wrote:
[... took the wrong landuse map, but it doesn't change ...]
Loading required package: rgdal
Loading required package: abind
Loading required package: pixmap
Geospatial Data Abstraction Library extensions to R successfully loaded
Loading required package: sp
Error in "names<-.default"(`*tmp*`, value = "landcover.30m") :
        'names' attribute [1] must be the same length as the vector [0]
11 Open Water
21 Low Intensity Residential
22 High Intensity Residential
23 Commercial/Industrial/Transportation
31 Bare Rock/Sand/Clay
32 Quarries/Strip Mines/Gravel Pits
41 Deciduous Forest
42 Evergreen Forest
43 Mixed Forest
51 Shrubland
71 Grasslands/Herbaceous
81 Pasture/Hay
82 Row Crops
83 Small Grains
85 Urban/Recreational Grasses
91 Woody Wetlands
92 Emergent Herbaceous Wetlands
* no data
debugging in: readCELL6sp("landcover.30m", cat = TRUE)
debug: {
[...]
}
Browse[1]>
debug: tmpfl <- tempfile()
Browse[1]>
debug: system(paste("r.out.arc input=", vname, " output=", tmpfl, sep = ""))
Browse[1]>
debug: library(sp)
Browse[1]>
debug: p4 <- CRS(system("g.proj -j -f", intern = TRUE))
Browse[1]>
debug: res <- read.asciigrid(tmpfl, colname = vname, proj4string = p4)
Browse[1]>
debug: if (cat) {
    cats <- strsplit(system(paste("r.stats -l -q", vname), intern = TRUE),
        " ")
    catnos <- sapply(cats, function(x) x[1])
    catlabs <- sapply(cats, function(x) paste(x[-1], collapse = " "))
    if (any(!is.na(match(catnos, "*")))) {
        isNA <- which(catnos == "*")
        catnos <- catnos[-isNA]
        catlabs <- catlabs[-isNA]
    }
    res at data[, 1] <- factor(res at data[, 1], levels = catnos, labels = catlabs)
} else {
    res at data[, 1] <- as.integer(res at data[, 1])
}
Browse[1]>
debug: cats <- strsplit(system(paste("r.stats -l -q", vname), intern = TRUE),
    " ")
Browse[1]>
debug: catnos <- sapply(cats, function(x) x[1])
Browse[1]>
debug: catlabs <- sapply(cats, function(x) paste(x[-1], collapse = " "))
Browse[1]>
debug: if (any(!is.na(match(catnos, "*")))) {
    isNA <- which(catnos == "*")
    catnos <- catnos[-isNA]
    catlabs <- catlabs[-isNA]
}
Browse[1]>
debug: isNA <- which(catnos == "*")
Browse[1]>
debug: catnos <- catnos[-isNA]
Browse[1]>
debug: catlabs <- catlabs[-isNA]
Browse[1]>
debug: res at data[, 1] <- factor(res at data[, 1], levels = catnos, labels = catlabs)
Browse[1]>
Error in "names<-.default"(`*tmp*`, value = "landcover.30m") :
        'names' attribute [1] must be the same length as the vector [0]
[...]
Browse[1]>
debug: df = data.frame(map)
Browse[1]>
debug: names(df) = colname
Browse[1]> length(colname)
[1] 1
Browse[1]> length(df)
[1] 1
Browse[1]> colname
[1] "landcover.30m"
Browse[1]> df
[...all values...]
`data.frame':   302418 obs. of  1 variable:
 $ map: num  NA NA NA NA NA NA NA NA NA NA ...
Browse[1]>
debug: grid = GridTopology(c(xllcenter, yllcenter), rep(cellsize, 2),
    c(ncols, nrows))
Browse[1]>
debug: SpatialGridDataFrame(grid, data = df, proj4string = proj4string)
Browse[1]>
exiting from: read.asciigrid(tmpfl, colname = vname, proj4string = p4)
Error in "names<-.default"(`*tmp*`, value = "landcover.30m") :
        'names' attribute [1] must be the same length as the vector [0]
I don't know if this is already helpful.

Best regards

 Markus