0

In R raster package i write an image in tiff format after certain raster processing but the spatial information is missing of new image. That's why i have to geo-reference the image again. How this can be avoided? For example, i want to get relative normalization image of landsat data. The code is

library(landsat)
library(raster)
library(lmodel2)
library(rgdal)
a<-raster("D:/Paper/test/1997-137-b3.tif")
x<-as.matrix(a)
b<-raster("D:/Paper/test/2007-137-b3.tif")
y<-as.matrix(b)
z<-relnorm(master = x, tofix = y, nperm=100, method="MA")
RMA was not requested: it will not be computed.

image(z$newimage)
m<-raster(z$newimage)
writeRaster(m, filename="D:/Paper/test/2007-b3-rd.tif", format="GTiff", options="INTERLEAVE=BAND", overwrite=TRUE)

But after processing the file i write in raster format missing spatial information.

0

You can try:

# Set up a new "empty" raster inheriting the geo-information from `a`
m<-raster(a) 

# Set the values of the new raster
setValues(m, z$newimage)

writeRaster(m, filename="D:/Paper/test/2007-b3-rd.tif", format="GTiff", options="INTERLEAVE=BAND", overwrite=TRUE)

HTH

0

Instead of converting your raster objects into a matrix, you have to convert them into a "SpatialGridDataFrame" (sp package) to keep their spatial information.

I encountered a similar issue with the relnorm-function and could solve it this way. (See here: link)

In this case, the following code should work:

    library(landsat)
    library(raster)
    library(lmodel2)
    library(rgdal)
    library(sp)
    a<-raster("D:/Paper/test/1997-137-b3.tif")
    x<-as(a, "SpatialGridDataFrame")
    b<-raster("D:/Paper/test/2007-137-b3.tif")
    y<-as(b, "SpatialGridDataFrame")
    z<-relnorm(master = x, tofix = y, nperm=100, method="MA")
    RMA was not requested: it will not be computed.

    m<-as(z$newimage, "RasterBrick")
    writeRaster(m, filename="D:/Paper/test/2007-b3-rd.tif", format="GTiff", options="INTERLEAVE=BAND", overwrite=TRUE)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.