[R] Read PNG file and display with more than 256 colors RGDAL

Paul.Rustomji at csiro.au Paul.Rustomji at csiro.au
Wed Jul 15 03:49:16 CEST 2009


Hello list

I am trying to use a Googlemaps tile (png file, 640 X 640 px tile) as a background to a plot and have been using the rgdal library to read in the PNG file (modified from code in the RGoogleMaps package).  This works OK.  My problem is is that the SGDF2PCT function in rgdal seems to be limited to 256 colors and this is introducing (I think) some degradation to the image which appears on the output graph as speckling.

Is there a way around this?  I have been reading about the EBImage package but it (a) seemed to have too many dependencies on other software for my purposes and (b) wouldn't load properly in any case on my machine (couldn't find dll's that it was looking for....)


Ideally I would like to read in the png image and display in its original form as a plot background, hopefully with all its colors represented rather than being converted to a 256 color scale.


A code example is below:

  png(file="file.png", 4000,4000)
  par(mar=c(0,0,0,0))
  myTile <- readGDAL("basemap.png",silent=TRUE); #basemap.png is a PNG file returned from googlemaps
  myTile at data <- myTile at data[,1:3]
  col <- SGDF2PCT(myTile,ncolors=256) ## myTile is a spatialGridDataFrame with 3 bands
  myTile$ind <- col$idx ## add the colour index to the data frame
  myTile <- as.image.SpatialGridDataFrame(myTile["ind"],1,2)$z;
  attr(myTile, "COL") <- col$ct;
  attr(myTile, "type") <- "rgb";
  myTile <- list(lat.center, lon.center,NA, myTile)

  image(z=myTile[[4]], col = attr(myTile[[4]], "COL"))

  dev.off()

> sessionInfo()
R version 2.9.1 (2009-06-26) 
i386-pc-mingw32 

locale:
LC_COLLATE=English_Australia.1252;LC_CTYPE=English_Australia.1252;LC_MONETARY=English_Australia.1252;LC_NUMERIC=C;LC_TIME=English_Australia.1252

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] abind_1.1-0    rgdal_0.6-9    sp_0.9-37      gridBase_0.4-3

loaded via a namespace (and not attached):
[1] lattice_0.17-25 tools_2.9.1    


Paul Rustomji




More information about the R-help mailing list