0

I have datasets in .nc format. I imported them in R as rasters but they appear as lateral inversions (see image below).

NC data as raster

I tried changing the orientation and inversion with the codes below but none worked as the results are all laterally inverted. In ArcMap, one can use the mirror feature tool to correct the lateral inversion.

Is there a way to do something similar to mirroring features in R?

f <- list.files(path=".", 
                   pattern='nc$', full.names=TRUE) 
s <- stack(raster(f)) 


P <- t(flip(s, direction='x'))


P2 <- t(flip(s, direction='y'))

The resulting images for P and P2 are shown below.

Image for P

Image for P2

1
  • As per the [hrlp/behavior] please do not include chit chat like thanks in your posts.
    – PolyGeo
    Commented Aug 2, 2021 at 20:17

1 Answer 1

1

Finally figured it out. My approach may not be very tidy so better solutions are welcome. I did several variants of transposing and flipping the raster to see which on works using the code below:

f_PM <- list.files(path=".", 
                   pattern='nc$', full.names=TRUE)
s_PM <- stack(raster(f_PM))

plot(s_PM[[1]])

PM <- t(flip(s_PM))
plot(PM[[1]])

PM2 <- flip(t(s_PM))
plot(PM2[[1]])


PM_1 <- flip(PM,direction='y')

PM_2 <- flip(PM,direction='x')

PM2_1 <- flip(PM2,direction='y')

PM2_2 <- flip(PM2,direction='x')

par(mfrow = c(2,2))
plot(PM_1[[1]])
plot(PM_2[[1]])
plot(PM2_1[[1]])
plot(PM2_2[[1]])

The result from the plots is shown below

Results from the plots

From the images, it seems two approaches work:

  1. flip then transpose the original data. After flip the resulting raster in direction y. Given by solution 1 below

  2. transpose then flip the original data. After flip the resulting raster in direction x. Given by solution 2 below

#solution 1

PM <- t(flip(s_PM))

PM_1 <- flip(PM,direction='y')
    

#solution 2

PM2 <- flip(t(s_PM))

PM2_2 <- flip(PM2,direction='x')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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