[R-sig-Geo] Raster Export as PNG
mdsumner at gmail.com
Mon Aug 17 01:59:16 CEST 2015
I would use rgdal::writeGDAL to create the png, rather than drawing to the
What you need to do depends on how to display your data, since you'll
either have to modify the raster to replace its data with colour values
(either as a palette or greyscale or rgb). I routinely convert raster data
via a colour map, and I think you could get close to your plot with
scl <- function(x) (x - min(na.omit(x)))/diff(range(na.omit(x)))
cols <- rev(terrain.colors(256)[scl(values(yidw))])
img <- setValues(brick(yidw, yidw, yidw), cbind(cols, cols, cols))
rgdal::writeGDAL(as(img, "SpatialGridDataFrame"), "outputRGB.png",
drivername = "PNG", type = "Byte")
That is untested though, it aims to replace your single band raster with a
RGB version with the colours all expanded out. You can see what the image
file will look like with plotRGB(img).
It's possibly wasteful to build a full RGB if the dynamic range does not
require it, but the details of your specific data matter here.
On Mon, 17 Aug 2015 at 04:56 Alper Dinçer <alperd.web at gmail.com> wrote:
> I'm very new to R and I'm trying to achieve something which may be easy but
> I couldn't find any solutions for a week.
> I read a GeoTiff file with Raster library and then crop with mask it with
> shape file. Then I want to save it as a PNG file to show it on Google Maps
> JS API, but it doesn't work due to inner margins when using plot().
> This the code I'm working on :
> xidw <- raster("output_6.tiff")
> crs(xidw) <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
> xidw.sub <- crop(xidw, extent(state))
> xidw.sub <- mask(xidw.sub, state)
> yidw <- projectRaster(xidw.sub, crs='+proj=merc +a=6378137 +b=6378137
> +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null
> png(filename="output.png", width=800, height=500, bg="transparent")
> plot(yidw, axes = F, box = F, legend = F)
> Is there anything that I can get the PNG file with full extent of of my
> cropped and masked raster?
> Best wishes.
> [[alternative HTML version deleted]]
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
[[alternative HTML version deleted]]
More information about the R-sig-Geo