[R] Incorrect positioning of raster images on Windows

Sharpie chuck at sharpsteen.net
Tue Oct 19 17:45:36 CEST 2010



Paul Murrell-2 wrote:
> 
> Hi
> 
> This is a rounding (truncation) problem.
> Working on a fix.
> 
> Paul
> 
> Sharpie wrote:
>> 
>> Michael Sumner-2 wrote:
>>> I think there's something about the "discrete cell" versus "centre
>>> value"
>>> interpretation here, and you are pushing the "pixels" through R's
>>> graphics
>>> engine as well as whatever the png device has to do.
>>>
>>> I can't enlighten you about the details of that, but by creating an
>>> image
>>> file more directly with "pixels as data" you can get the result exactly:
>>>
>>> test <- matrix(c(0, 255), 3, 5)
>>> library(rgdal)
>>> ## transpose to get orientation right
>>> x <- image2Grid(list(x = 1:ncol(test), y = 1:nrow(test), z = t(test)))
>>> writeGDAL(x, "raster.png", driver = "PNG", type = "Byte")
>>>
>>>
>>>
>>> On Mon, Oct 18, 2010 at 3:17 PM, Sharpie <chuck at sharpsteen.net> wrote:
>>>
>>>> I am working on dumping raster data from R into PNG files using
>>>> rasterImage().  I am working with a test matrix from the rasterImage()
>>>> example and using it to produce a PNG image with the following code:
>>>>
>>>>
>>>> # From the example for rasterImage(). A 3 pixel by 5 pixel b/w
>>>> checkerboard.
>>>> testImage <- as.raster(0:1, nrow=3, ncol=5)
>>>>
>>>> testImage
>>>>     [,1]      [,2]      [,3]      [,4]      [,5]
>>>> [1,] "#000000" "#FFFFFF" "#000000" "#FFFFFF" "#000000"
>>>> [2,] "#FFFFFF" "#000000" "#FFFFFF" "#000000" "#FFFFFF"
>>>> [3,] "#000000" "#FFFFFF" "#000000" "#FFFFFF" "#000000"
>>>>
>>>> png('test.png', width=5, height=3, units='px')
>>>>
>>>> # Just want the image, no margins, boarders or other fancy stuff.
>>>> par(mar = c(0,0,0,0) )
>>>> plot.new()
>>>> plotArea = par('fig')
>>>>
>>>> rasterImage(testImage, plotArea[1], plotArea[3],
>>>>  plotArea[2], plotArea[4], interpolate = FALSE )
>>>>
>>>> dev.off()
>>>>
>>>>
>>>> However, using R 2.12.0, 64 bit on Windows 7 I have a strange issue
>>>> where
>>>> the image is shifted up by one row and to the left by one row.  In
>>>> other
>>>> words, the bottom row of pixels is missing along with the right column.
>>>>  The
>>>> code works as I expect it to on OS X and Debian.
>>>>
>>>>
>>>> Am I misusing the plotting commands in some way or should I submit an
>>>> off-by-one bugreport to Bugzilla?
>>>>
>>>> Any suggestions or comments are most welcome.
>>>>
>>>> -Charlie
>>>>
>>>> -----
>>>> Charlie Sharpsteen
>>>> Undergraduate-- Environmental Resources Engineering
>>>> Humboldt State University
>>>> --
>>>> View this message in context:
>>>> http://r.789695.n4.nabble.com/Incorrect-positioning-of-raster-images-on-Windows-tp2999649p2999649.html
>>>> Sent from the R help mailing list archive at Nabble.com.
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>
>>>
>>> -- 
>>> Michael Sumner
>>> Institute for Marine and Antarctic Studies, University of Tasmania
>>> Hobart, Australia
>>> e-mail: mdsumner at gmail.com
>>>
>> 
>> 
>> 
>> Hi Micheal,
>> 
>> I appreciate the suggestion.  However, rgdal is very heavyweight and
>> installing the GDAL library is not a trivial operation automagically
>> handled
>> `install.packages()` on every platform R supports.  As I am not doing
>> spatial analysis, I am very reluctant to add rgdal to the dependency list
>> of
>> my package.
>> 
>> I would very much prefer to find the root cause of the difference in
>> `png()`
>> behavior on Windows when compared to OS X and Linux.  If anyone on this
>> list
>> has some insight to share, I would be very grateful to hear it.
>> 
>> I waffled a bit on whether to send this to R-help or R-devel, in the
>> light
>> of day (as opposed to the foggy darkness that surrounds 2am) think it may
>> be
>> more of an R-devel question.  Forwarding it there now.
>> 
>> -Charlie
>> 
>> -----
>> Charlie Sharpsteen
>> Undergraduate-- Environmental Resources Engineering
>> Humboldt State University
> 
> -- 
> Dr Paul Murrell
> Department of Statistics
> The University of Auckland
> Private Bag 92019
> Auckland
> New Zealand
> 64 9 3737599 x85392
> paul at stat.auckland.ac.nz
> http://www.stat.auckland.ac.nz/~paul/le code.
> 
> 

Thank you so much Paul!

I poked around a bit in the Windows C code, but I know nothing about how
devga works, so it wasn't a very fruitful investigation.  Now that I know
what is happening I can put an appropriate warning message in my package.

Thanks again for all the excellent work you have put into the R graphics
system!

-Charlie


-----
Charlie Sharpsteen
Undergraduate-- Environmental Resources Engineering
Humboldt State University
-- 
View this message in context: http://r.789695.n4.nabble.com/Incorrect-positioning-of-raster-images-on-Windows-tp2999649p3002321.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list