[R-sig-Geo] Raster Export as PNG
Michael Sumner
mdsumner at gmail.com
Mon Aug 17 02:00:12 CEST 2015
I made a mistake of course, it should be
cols <- rev(terrain.colors(256)[scl(values(yidw)) * 255 + 1])
On Mon, 17 Aug 2015 at 10:03 Michael Sumner <mdsumner at gmail.com> wrote:
> I would use rgdal::writeGDAL to create the png, rather than drawing to the
> png device.
>
> 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.
>
> Cheers, Mike.
>
>
>
> On Mon, 17 Aug 2015 at 04:56 Alper Dinçer <alperd.web at gmail.com> wrote:
>
>> Hi,
>>
>> 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 :
>> ---------Code----------------------------
>>
>> 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
>> +no_defs')
>>
>> png(filename="output.png", width=800, height=500, bg="transparent")
>> plot(yidw, axes = F, box = F, legend = F)
>> dev.off();
>>
>> ---------Code----------------------------
>>
>> Is there anything that I can get the PNG file with full extent of of my
>> cropped and masked raster?
>>
>> Thanks.
>>
>> Best wishes.
>>
>> A
>>
>> [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list