Skip to content
Prev 10326 / 29559 Next

SGDF with NA values - SGDF2PCT()

There seems to be at least one bug in SGDF2PCT, in that min/max on
band should check for NAs, but also when have_NAs is true, the default
ncolors is increased by 1 (to 257) which causes RGB2PCT to error -
I've not explored enough to see why yet.

Roger?  If I modify this in SGDF2PCT to use na.rm = TRUE, I can then
avoid the error with ncolors = 255:

      if (adjust.bands) {
            bmax <- max(band, na.rm = TRUE)
            bmin <- min(band, na.rm = TRUE)

Otherwise, a workaround like this might help Tim for now.

library(rgdal)

logo <- system.file("pictures/Rlogo.jpg", package="rgdal")[1]
SGlogo <- readGDAL(logo)
cols <- SGDF2PCT(SGlogo) ### works fine

### now introduce some NA values
##band3 <- SGlogo$band3
SGlogo$band3[SGlogo$band3==0] <- NA
##SGlogo$band3 <- band3

## see which are NA
image(SGlogo, "band3")

## detect NAs
NAs <- is.na(SGlogo$band3)  ## may need to combine this with NAs in other bands

## set the missing values to something valid
SGlogo$band3[NAs] <- 0

## which indices match those that were NA
widx <- unique(cols$idx[NAs])

## recreate those colours as fully transparent, of any old colour
if(length(widx) > 0) cols$ct[widx] <- rgb(0, 0, 0, 0)

SGlogo$idx <- cols$idx

image(SGlogo, "idx", col = cols$ct)


## recreate those colours as partially transparent, with red
if(length(widx) > 0) cols$ct[widx] <- rgb(1, 0, 0, 0.5)

image(SGlogo, "idx", col = cols$ct)

Cheers, Mike.
On Mon, Dec 13, 2010 at 8:49 PM, <Tim.Haering at lwf.bayern.de> wrote: