[R-sig-Geo] SGDF with NA values - SGDF2PCT()

Michael Sumner mdsumner at gmail.com
Mon Dec 13 11:45:54 CET 2010


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:
> Hello,
>
> I want to convert my three-band SGDF into a single color vector using the SGDF2PCT() function in rgdal. Unfortunately my SGDF contains NA values. To plot it with image() this is no problem, because NAs were fully transparent, but if I want to convert my three bands with SGDF2PCT() I get an error message.
> I guess there is an easy way to handle NA values in SpatialGridDataFrame. Can someone please help me?
>
> Here an example:
>> 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
>> band3[band3==0] <- NA
>> SGlogo$band3 <- band3
>> summary(SGlogo)
>> cols <- SGDF2PCT(SGlogo)
> Error in if (bmax == bmin) bmax <- bmin + ncolors :
>  missing value where TRUE/FALSE needed
>
> Thank you.
> TIM
>
> -----------------------------------------------------------------------------------
> Tim Häring
> Bavarian State Institute of Forest Research
> Department of Forest Ecology
> Hans-Carl-von-Carlowitz-Platz 1
> D-85354 Freising
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



-- 
Michael Sumner
Institute for Marine and Antarctic Studies, University of Tasmania
Hobart, Australia
e-mail: mdsumner at gmail.com



More information about the R-sig-Geo mailing list