Coordinate Reference System of Raster & Polygons in R - r

I have several polygons & I like to extract mean values from several raster layers within these polygons.
When I added those to ArcMap I realized that the projections of the two data types do not match. I could solve the problem for the display in ArcGIS by using the Project tool (Data Management toolbox > Projections and Transformations toolset > Raster). So I tried to standardize the projection by loading the data into R in the following manner (part of the code):
Rasters:
for (i in 1:length(rasterlist1))
{ndvi_raster_stack1[i]<-raster(rasterlist1[i])
raster::NAvalue(ndvi_raster_stack1[[i]])<--999
projection(ndvi_raster_stack1[[i]])<-"+proj=utm +ellps=WGS84 +datum=WGS84 +units=m"}
> ndvi_raster_stack1[[1]]
class : RasterLayer
dimensions : 226, 150, 33900 (nrow, ncol, ncell)
resolution : 0.57504, 0.5753628 (x, y)
extent : -28.728, 57.528, -55.08, 74.952 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +ellps=WGS84 +datum=WGS84 +units=m +towgs84=0,0,0
values : Z:\master\lusmeg_sw_kernel_data\ndvi0910\Y2008_P47.tif
min value : -91
max value : 550.8125
Polygons:
for (i in 1:length(poplist))
{pop_kernels[i]<-readShapeSpatial(poplist[i],repair=TRUE,proj4string=CRS("+proj=utm +ellps=WGS84 +datum=WGS84 +units=m"))
pop_kernels[[i]]<-unionSpatialPolygons(pop_kernels[[i]],ID=c(rep(1,times=length(pop_kernels[[i]])-1),0),threshold=NULL,avoidGEOS=FALSE)}
> str(pop_kernels[[1]])
Formal class 'SpatialPolygons' [package "sp"] with 4 slots
..# polygons :List of 2
.. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
.. .. .. ..# Polygons :List of 2
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..# labpt : num [1:2] 2404422 893343
.. .. .. .. .. .. ..# area : num 1.15e+12
.. .. .. .. .. .. ..# hole : logi FALSE
.. .. .. .. .. .. ..# ringDir: int 1
.. .. .. .. .. .. ..# coords : num [1:1625, 1:2] 2551236 2533236 2533236 2523236 2523236 ...
.. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. .. .. .. ..$ : NULL
.. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..# labpt : num [1:2] 2468549 865776
.. .. .. .. .. .. ..# area : num 6.31e+11
.. .. .. .. .. .. ..# hole : logi TRUE
.. .. .. .. .. .. ..# ringDir: int -1
.. .. .. .. .. .. ..# coords : num [1:1385, 1:2] 2551236 2551236 2563236 2563236 2569236 ...
.. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. .. .. .. ..$ : NULL
.. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
.. .. .. ..# plotOrder: int [1:2] 1 2
.. .. .. ..# labpt : num [1:2] 2404422 893343
.. .. .. ..# ID : chr "0"
.. .. .. ..# area : num 1.15e+12
.. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
.. .. .. ..# Polygons :List of 1
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..# labpt : num [1:2] 2468549 865776
.. .. .. .. .. .. ..# area : num 6.31e+11
.. .. .. .. .. .. ..# hole : logi FALSE
.. .. .. .. .. .. ..# ringDir: int 1
.. .. .. .. .. .. ..# coords : num [1:1385, 1:2] 2551236 2541236 2541236 2529236 2529236 ...
.. .. .. .. .. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. .. .. .. .. ..$ : NULL
.. .. .. .. .. .. .. .. ..$ : chr [1:2] "x" "y"
.. .. .. ..# plotOrder: int 1
.. .. .. ..# labpt : num [1:2] 2468549 865776
.. .. .. ..# ID : chr "1"
.. .. .. ..# area : num 6.31e+11
..# plotOrder : int [1:2] 1 2
..# bbox : num [1:2, 1:2] 1819236 207017 3013236 1577017
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "x" "y"
.. .. ..$ : chr [1:2] "min" "max"
..# proj4string:Formal class 'CRS' [package "sp"] with 1 slots
.. .. ..# projargs: chr " +proj=utm +ellps=WGS84 +datum=WGS84 +units=m +towgs84=0,0,0"
I can plot the polygons and the rasters seperately, but when I try to plot one of the polygons over a raster, the polygons are not displayed:
plot(ndvi_raster_stack1[[1]],xlab="Longitude",ylab="Latitude")
plot(pop_kernels[[1]],col="black",add=TRUE)
It seems that they still do not "overlap". This is also indicated by the different bounding boxes, I think:
> bbox(ndvi_raster_stack1[[1]])
min max
s1 -28.728 57.528
s2 -55.080 74.952
> bbox(pop_kernels[[1]])
min max
x 1819236 3013236
y 207017 1577017
Because I want to extract the raster values within the polygons, I have to be sure that they are referenced in a correct way.
Does someone have an idea what the problem could be?

Your polygon shapefile has a coordinate system that isnt lat-long - the numbers are very large and probably metres in some system. Assigning a proj4string won't reproject the data to lat-long, it just sets the label of what coordinates it thinks it is. In this case, its wrong!
You need to make sure your polygons get the right proj4string for the numbers they have in them - there may be a [shapefile].prj file along with the .shp and .dbf that tells you. Set the proj4string to that.
Then you can use spTransform from sp or rgdal to project your polygons to lat-long WGS84 coordinates.
Its always best to transform polygons to raster coordinates, since messing with raster coordinates can mean reprojecting the whole grid, which is messy...

Related

Extract shape file coordinates from a leaflet map in shiny app

I have a shape file to plot on the map in shiny app. The file structure is as follow :
user#servr:~/home/shp$ ls -la
total 48
drwxr-xr-x 2 user user Jan 17 13:17 .
drwxr-xr-x 7 user user 4096 Jan 17 13:13 ..
-rw-r--r-- 1 user user 5 Jan 17 13:13 myshape.cpg
-rw-r--r-- 1 user user 423 Jan 17 13:13 myshape.dbf
-rw-r--r-- 1 user user 404 Jan 17 13:13 myshape.prj
-rw-r--r-- 1 user user 132 Jan 17 13:13 myshape.sbn
-rw-r--r-- 1 user user 116 Jan 17 13:13 myshape.sbx
-rw-r--r-- 1 user user 15044 Jan 17 13:13 myshape.shp
-rw-r--r-- 1 user user 108 Jan 17 13:13 myshape.shx
I could read those file in R as well :
myspdf <- readOGR(dsn = "/home/shp/",layer = "myshape")
myspdf <- spTransform(myspdf, CRS("+init=epsg:4326"))
Now the str of myspdf is :
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
..# data :'data.frame': 1 obs. of 3 variables:
.. ..$ Shape_Leng: num 80111
.. ..$ Shape_Area: num 59127699
.. ..$ GISFILE : chr "O22_EVAR_ArchWreckFound_v01_221216dehm25833_Buff400m"
..# polygons :List of 1
.. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
.. .. .. ..# Polygons :List of 3
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..# labpt : num [1:2] 420838 6079834
.. .. .. .. .. .. ..# area : num 50391583
.. .. .. .. .. .. ..# hole : logi FALSE
.. .. .. .. .. .. ..# ringDir: int 1
.. .. .. .. .. .. ..# coords : num [1:586, 1:2] 429230 429217 429183 429148 429114 ...
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..# labpt : num [1:2] 424698 6072349
.. .. .. .. .. .. ..# area : num 6408830
.. .. .. .. .. .. ..# hole : logi FALSE
.. .. .. .. .. .. ..# ringDir: int 1
.. .. .. .. .. .. ..# coords : num [1:312, 1:2] 421726 421745 421765 421785 421806 ...
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. ..# labpt : num [1:2] 422217 6071588
.. .. .. .. .. .. ..# area : num 2329246
.. .. .. .. .. .. ..# hole : logi FALSE
.. .. .. .. .. .. ..# ringDir: int 1
.. .. .. .. .. .. ..# coords : num [1:32, 1:2] 425110 421328 420681 420682 420683 ...
.. .. .. ..# plotOrder: int [1:3] 1 2 3
.. .. .. ..# labpt : num [1:2] 420838 6079834
.. .. .. ..# ID : chr "0"
.. .. .. ..# area : num 59129659
.. .. .. ..$ comment: chr "0 0 0"
..# plotOrder : int 1
..# bbox : num [1:2, 1:2] 413125 6069810 430341 6084226
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:2] "x" "y"
.. .. ..$ : chr [1:2] "min" "max"
..# proj4string:Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..# projargs: chr "+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"
.. .. ..$ comment: chr "PROJCRS[\"ETRS89 / UTM zone 33N\",\n BASEGEOGCRS[\"ETRS89\",\n DATUM[\"European Terrestrial Reference"| __truncated__
..$ comment: chr "TRUE"
This shows that I have 3 separated area with coordinates array of length 586,312 and 32. I'm able to get the coordinate of the polygons as well :
coor01 <- myspdf#polygons[[1]]#Polygons[[1]]#coords
coor02 <- myspdf#polygons[[1]]#Polygons[[2]]#coords
coor03 <- myspdf#polygons[[1]]#Polygons[[3]]#coords
What I am looking for is the option on shiny app, which by click on the shape file I get the coordinate (coor01,coor02 or coor03) !
Right now if I click on shape file from the app, I get always one single lat and lng value not the complete list !
library(shiny)
library(leaflet)
library(rgdal)
ui <- fluidPage(
titlePanel(p("Spatial app", style = "color:#3474A7")),
sidebarLayout(
sidebarPanel(
),
mainPanel(
leafletOutput("map")
)
)
)
# server()
server <- function(input, output, session) {
data <- reactiveValues(clickedShape=NULL)
myspdf <- readOGR(dsn = "/home/shp/",layer = "myshape")
myspdf <- spTransform(myspdf, CRS("+init=epsg:4326"))
output$map<-renderLeaflet({
leaflet() %>% addTiles()%>% addPolygons(data = myspdf,highlight = highlightOptions(
weight = 5,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE))
})
observeEvent(input$map_shape_click,{
print("observed map_marker_click")
data$clickedShape <- input$map_shape_click
print(data$clickedShape)
})

Plotting RasterLayer objects using ggplot

I am incredibly sorry, but I feel like there arenĀ“t good example datasets for this question, and thus I will not be able to prdocue a reproducible example. If this makes this question untenable then I will definitely delete.
I am trying to plot a RasterLayer object in ggplot2 using geom_raster(), but get an error. I have seen that it seems like you have to convert RasterLayer objects to dataframes, but doing a simple conversion doesn't seem to work on this particular RasterLayer.
For reference, this data plots easily using the plot() function, but I'd like to do it in ggplot if possible due to its versatility.
Here is the code that attempts to plot the data. Of note, the object is stored in a list, along with many other model objects that I would like to plot:
library(ggplot2)
ggplot() + geom_raster(data = mylist$model_object)
And the error:
`data` must be a <data.frame>, or an object coercible by `fortify()`, not an S4 object with class
<RasterLayer>.
Run `rlang::last_error()` to see where the error occurred.
Here is what happens when I run mylist$model_object :
class : RasterLayer
dimensions : 114, 180, 20520 (nrow, ncol, ncell)
resolution : 0.1666667, 0.1666667 (x, y)
extent : -116, -86, 14, 33 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : memory
names : layer
values : 0, 0.5845846 (min, max)
And here is the structure of the object:
str(mylist$model_object)
Formal class 'RasterLayer' [package "raster"] with 12 slots
..# file :Formal class '.RasterFile' [package "raster"] with 13 slots
.. .. ..# name : chr ""
.. .. ..# datanotation: chr "FLT4S"
.. .. ..# byteorder : chr "little"
.. .. ..# nodatavalue : num -Inf
.. .. ..# NAchanged : logi FALSE
.. .. ..# nbands : int 1
.. .. ..# bandorder : chr "BIL"
.. .. ..# offset : int 0
.. .. ..# toptobottom : logi TRUE
.. .. ..# blockrows : int 0
.. .. ..# blockcols : int 0
.. .. ..# driver : chr ""
.. .. ..# open : logi FALSE
..# data :Formal class '.SingleLayerData' [package "raster"] with 13 slots
.. .. ..# values : num [1:20520] 0.002 0 0 0 0 ...
.. .. ..# offset : num 0
.. .. ..# gain : num 1
.. .. ..# inmemory : logi TRUE
.. .. ..# fromdisk : logi FALSE
.. .. ..# isfactor : logi FALSE
.. .. ..# attributes: list()
.. .. ..# haveminmax: logi TRUE
.. .. ..# min : num 0
.. .. ..# max : num 0.585
.. .. ..# band : int 1
.. .. ..# unit : chr ""
.. .. ..# names : chr "layer"
..# legend :Formal class '.RasterLegend' [package "raster"] with 5 slots
.. .. ..# type : chr(0)
.. .. ..# values : logi(0)
.. .. ..# color : logi(0)
.. .. ..# names : logi(0)
.. .. ..# colortable: logi(0)
..# title : chr(0)
..# extent :Formal class 'Extent' [package "raster"] with 4 slots
.. .. ..# xmin: num -116
.. .. ..# xmax: num -86
.. .. ..# ymin: num 14
.. .. ..# ymax: num 33
..# rotated : logi FALSE
..# rotation:Formal class '.Rotation' [package "raster"] with 2 slots
.. .. ..# geotrans: num(0)
.. .. ..# transfun:function ()
..# ncols : int 180
..# nrows : int 114
..# crs :Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..# projargs: chr "+proj=longlat +datum=WGS84 +no_defs"
.. .. ..$ comment: chr "GEOGCRS[\"unknown\",\n DATUM[\"World Geodetic System 1984\",\n ELLIPSOID[\"WGS 84\",6378137,298.25722"| __truncated__
..# history : list()
..# z : list()
I totally understand if this is not enough to go off of. If anyone has a suggestion about easy to access raster data online that I can use as a proxy repoducible example, it would be much appreciated. Thank you!
We may use the as.data.frame method for raster
> grep("data.frame", methods(class = "RasterLayer"), value = TRUE)
[1] "as.data.frame,Raster-method" "extract,Raster,data.frame-method" "rasterize,data.frame,Raster-method" "subs,Raster,data.frame-method"
as.data.frame(mylist$model_object, xy = TRUE)
I would use "terra" and "tidyterra"
library(raster)
library(terra)
library(tidyterra)
library(ggplot2)
# RasterLayer
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
# create SpatRaster (can also do `x <- rast(f)`
x <- rast(r)
ggplot() + geom_spatraster(data = x)

Error: number of items to replace is not a multiple of replacement length in R

I am currently trying to test for collinearity amongst my climate raster layers using the 'vifcor' function in R. However I keep getting this error:
Error in m[, i] <- getValues(x#layers[[i]]) :
number of items to replace is not a multiple of replacement length
My full R code is as follows:
predictors.files <- list.files(path="/Users/josh/Desktop/Predictors/",pattern='asc$',full.names=TRUE)
preds.stack<-stack(predictors.files)
vif(preds.stack)
v1<-vifcor(preds.stack,th=0.7)
Then it goes wrong. Can anyone help me with this issue?
The output of str(preds.stack)
Formal class 'RasterStack'
[package "raster"] with 11 slots ..# filename: chr "" ..# layers :List of 19 .. ..$ :Formal class 'RasterLayer'
[package "raster"] with 12 slots .. .. .. ..# file :Formal class '.RasterFile'
[package "raster"] with 13 slots .. .. .. .. .. ..# name : chr "/Users/josh/Desktop/Predictors/isothermality.asc" .. .. .. .. .. ..# datanotation: chr "FLT4S" .. .. .. .. .. ..# byteorder : chr "little" .. .. .. .. .. ..# nodatavalue : num -Inf .. .. .. .. .. ..# NAchanged : logi FALSE
Thanks :)

Significantly different speeds on raster (netCDF) calculations in R

I have some WRF output data that was subsetted and masked using pythons xarray module.
I'm now performing calculations on raster bricks using R's raster package and finding very different speeds for very similar files.
Knowns:
There are 3 netCDF files, all the exact same size - 9.47 GB, that contain 9 variables
They all have the exact same dimensions (nrow 327, ncol 348, nlayer 365)
All calculations are on individual files (layer calculations)
All calculations are on the same variable with the same values (except for the second which is masked)
system.time(sum(d97[[1:365]]))
user system elapsed
5.428 2.771 8.840
The second file is the exact same file but a masked portion, with all the masked values converted to NaN.
system.time(sum(masked_d97[[1:365]]))
user system elapsed
10.784 2.157 13.052
The last file is a slightly modified version (daily values rather than cummulative values) of the first file. It was modified using Xarray in Python.
system.time(sum(mod_d97[[1:365]]))
user system elapsed
22.015 1.773 24.474
What on earth is happening here? I'm happy to provide more details (code, ncdumps, etc) as requested.
EDIT: added str() of files
d97 <- brick(files[8], varname = "TMIN")
masked_97 <- brick(files[3], varname = "TMIN")
d03 <- brick(files[11], varname = "TMIN")
str(d97)
Formal class 'RasterBrick' [package "raster"] with 12 slots
..# file :Formal class '.RasterFile' [package "raster"] with 13 slots
.. .. ..# name : chr "/Users/charlesbecker/Desktop/Data/Project Data/Shiny/WY1997_yearly_stats.nc"
.. .. ..# datanotation: chr "FLT4S"
.. .. ..# byteorder : chr "little"
.. .. ..# nodatavalue : num NaN
.. .. ..# NAchanged : logi FALSE
.. .. ..# nbands : int 365
.. .. ..# bandorder : chr "BIL"
.. .. ..# offset : int 0
.. .. ..# toptobottom : logi TRUE
.. .. ..# blockrows : int 0
.. .. ..# blockcols : int 0
.. .. ..# driver : chr "netcdf"
.. .. ..# open : logi FALSE
..# data :Formal class '.MultipleRasterData' [package "raster"] with 14 slots
.. .. ..# values : logi[0 , 0 ]
.. .. ..# offset : num 0
.. .. ..# gain : num 1
.. .. ..# inmemory : logi FALSE
.. .. ..# fromdisk : logi TRUE
.. .. ..# nlayers : int 365
.. .. ..# dropped : NULL
.. .. ..# isfactor : logi FALSE
.. .. ..# attributes: list()
.. .. ..# haveminmax: logi FALSE
.. .. ..# min : num [1:365] Inf Inf Inf Inf Inf ...
.. .. ..# max : num [1:365] -Inf -Inf -Inf -Inf -Inf ...
.. .. ..# unit : chr "K"
.. .. ..# names : chr [1:365] "X1" "X2" "X3" "X4" ...
..# legend :Formal class '.RasterLegend' [package "raster"] with 5 slots
.. .. ..# type : chr(0)
.. .. ..# values : logi(0)
.. .. ..# color : logi(0)
.. .. ..# names : logi(0)
.. .. ..# colortable: logi(0)
..# title : chr "TMIN"
..# extent :Formal class 'Extent' [package "raster"] with 4 slots
.. .. ..# xmin: num 0.5
.. .. ..# xmax: num 348
.. .. ..# ymin: num 0.5
.. .. ..# ymax: num 328
..# rotated : logi FALSE
..# rotation:Formal class '.Rotation' [package "raster"] with 2 slots
.. .. ..# geotrans: num(0)
.. .. ..# transfun:function ()
..# ncols : int 348
..# nrows : int 327
..# crs :Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..# projargs: chr NA
..# history : list()
..# z :List of 1
.. ..$ : int [1:365] 1 2 3 4 5 6 7 8 9 10 ...
str(masked_d97)
Formal class 'RasterBrick' [package "raster"] with 12 slots
..# file :Formal class '.RasterFile' [package "raster"] with 13 slots
.. .. ..# name : chr "/Users/charlesbecker/Desktop/Data/Project Data/Shiny/AVA_WY1997_yearly_stats.nc"
.. .. ..# datanotation: chr "FLT4S"
.. .. ..# byteorder : chr "little"
.. .. ..# nodatavalue : num NaN
.. .. ..# NAchanged : logi FALSE
.. .. ..# nbands : int 365
.. .. ..# bandorder : chr "BIL"
.. .. ..# offset : int 0
.. .. ..# toptobottom : logi TRUE
.. .. ..# blockrows : int 0
.. .. ..# blockcols : int 0
.. .. ..# driver : chr "netcdf"
.. .. ..# open : logi FALSE
..# data :Formal class '.MultipleRasterData' [package "raster"] with 14 slots
.. .. ..# values : logi[0 , 0 ]
.. .. ..# offset : num 0
.. .. ..# gain : num 1
.. .. ..# inmemory : logi FALSE
.. .. ..# fromdisk : logi TRUE
.. .. ..# nlayers : int 365
.. .. ..# dropped : NULL
.. .. ..# isfactor : logi FALSE
.. .. ..# attributes: list()
.. .. ..# haveminmax: logi FALSE
.. .. ..# min : num [1:365] Inf Inf Inf Inf Inf ...
.. .. ..# max : num [1:365] -Inf -Inf -Inf -Inf -Inf ...
.. .. ..# unit : chr ""
.. .. ..# names : chr [1:365] "X1" "X2" "X3" "X4" ...
..# legend :Formal class '.RasterLegend' [package "raster"] with 5 slots
.. .. ..# type : chr(0)
.. .. ..# values : logi(0)
.. .. ..# color : logi(0)
.. .. ..# names : logi(0)
.. .. ..# colortable: logi(0)
..# title : chr "TMIN"
..# extent :Formal class 'Extent' [package "raster"] with 4 slots
.. .. ..# xmin: num 0.5
.. .. ..# xmax: num 348
.. .. ..# ymin: num 0.5
.. .. ..# ymax: num 328
..# rotated : logi FALSE
..# rotation:Formal class '.Rotation' [package "raster"] with 2 slots
.. .. ..# geotrans: num(0)
.. .. ..# transfun:function ()
..# ncols : int 348
..# nrows : int 327
..# crs :Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..# projargs: chr NA
..# history : list()
..# z :List of 1
.. ..$ : int [1:365] 1 2 3 4 5 6 7 8 9 10 ...
str(d03)
Formal class 'RasterBrick' [package "raster"] with 12 slots
..# file :Formal class '.RasterFile' [package "raster"] with 13 slots
.. .. ..# name : chr "/Users/charlesbecker/Desktop/Data/Project Data/Shiny/WY2003_yearly_stats.nc"
.. .. ..# datanotation: chr "FLT4S"
.. .. ..# byteorder : chr "little"
.. .. ..# nodatavalue : num NaN
.. .. ..# NAchanged : logi FALSE
.. .. ..# nbands : int 365
.. .. ..# bandorder : chr "BIL"
.. .. ..# offset : int 0
.. .. ..# toptobottom : logi TRUE
.. .. ..# blockrows : int 0
.. .. ..# blockcols : int 0
.. .. ..# driver : chr "netcdf"
.. .. ..# open : logi FALSE
..# data :Formal class '.MultipleRasterData' [package "raster"] with 14 slots
.. .. ..# values : logi[0 , 0 ]
.. .. ..# offset : num 0
.. .. ..# gain : num 1
.. .. ..# inmemory : logi FALSE
.. .. ..# fromdisk : logi TRUE
.. .. ..# nlayers : int 365
.. .. ..# dropped : NULL
.. .. ..# isfactor : logi FALSE
.. .. ..# attributes: list()
.. .. ..# haveminmax: logi FALSE
.. .. ..# min : num [1:365] Inf Inf Inf Inf Inf ...
.. .. ..# max : num [1:365] -Inf -Inf -Inf -Inf -Inf ...
.. .. ..# unit : chr "K"
.. .. ..# names : chr [1:365] "X1" "X2" "X3" "X4" ...
..# legend :Formal class '.RasterLegend' [package "raster"] with 5 slots
.. .. ..# type : chr(0)
.. .. ..# values : logi(0)
.. .. ..# color : logi(0)
.. .. ..# names : logi(0)
.. .. ..# colortable: logi(0)
..# title : chr "TMIN"
..# extent :Formal class 'Extent' [package "raster"] with 4 slots
.. .. ..# xmin: num 0.5
.. .. ..# xmax: num 348
.. .. ..# ymin: num 0.5
.. .. ..# ymax: num 328
..# rotated : logi FALSE
..# rotation:Formal class '.Rotation' [package "raster"] with 2 slots
.. .. ..# geotrans: num(0)
.. .. ..# transfun:function ()
..# ncols : int 348
..# nrows : int 327
..# crs :Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..# projargs: chr NA
..# history : list()
..# z :List of 1
.. ..$ : int [1:365] 1 2 3 4 5 6 7 8 9 10 ...
system.time(sum(d97[[1:365]]))
user system elapsed
5.569 2.219 8.048
system.time(sum(masked_97[[1:365]]))
user system elapsed
11.887 2.342 14.569
system.time(sum(d03[[1:365]]))
user system elapsed
22.253 1.772 24.879
The most likely difference is that data in your new netCDF file is now compressed differently. Two forms of compression are common with netCDF files:
scale/offset encoding, e.g., to decode from int16 via a formula like scale_factor * values + add_offset.
zlib compression on individual chunks of the array (only supported with netCDF4 files).
If you don't slice or manipulate your variables, xarray will preserve compression setting via the encoding attribute, but this is generally dropped by xarray operations. See the xarray docs on reading/writing encoded data for more details.

how to differentiate the colours for background and NA values in gplot

I am generating a chlorophyll map for lake. I want to fill the lake with blue colour where there is a very low chlorophyll concentration and light blue for NA values. I am using a code as given below
gplot(Chlorophyll_map_5) + geom_tile(aes(fill=value)) + scale_fill_gradient(low = 'blue', high = 'red', na.value='blue',name="Chl-a (ug/l)",limits=c(0,1000)) + coord_equal()+theme_bw()
Which gives me a plot like this for na.value='blue':
na.value='blue'
When I use na.value='transparent' I got this image:
na.value='transparent'
If I change the colour of the na.value it also changes the background. Is there a way to fill the lake with colour without changing the background?
The output of my data:`The output of my data:
Formal class 'RasterLayer' [package "raster"] with 12 slots
..# file :Formal class '.RasterFile' [package "raster"] with 13 slots
.. .. ..# name : chr "/private/var/folders/68/hm_5ts9x7psb6j3wnb91_bfr0000gn/T/RtmpZ3BLZD/raster/r_tmp_2017-07-18_133827_28365_34843.grd"
.. .. ..# datanotation: chr "FLT8S"
.. .. ..# byteorder : Named chr "little"
.. .. .. ..- attr(*, "names")= chr "value"
.. .. ..# nodatavalue : num -1.7e+308
.. .. ..# NAchanged : logi FALSE
.. .. ..# nbands : int 1
.. .. ..# bandorder : Named chr "BIL"
.. .. .. ..- attr(*, "names")= chr "value"
.. .. ..# offset : int 0
.. .. ..# toptobottom : logi TRUE
.. .. ..# blockrows : int 0
.. .. ..# blockcols : int 0
.. .. ..# driver : chr "raster"
.. .. ..# open : logi FALSE
..# data :Formal class '.SingleLayerData' [package "raster"] with 13 slots
.. .. ..# values : logi(0)
.. .. ..# offset : num 0
.. .. ..# gain : num 1
.. .. ..# inmemory : logi FALSE
.. .. ..# fromdisk : logi TRUE
.. .. ..# isfactor : logi FALSE
.. .. ..# attributes: list()
.. .. ..# haveminmax: logi TRUE
.. .. ..# min : num 0.00335
.. .. ..# max : num 3870657
.. .. ..# band : int 1
.. .. ..# unit : chr ""
.. .. ..# names : chr "layer"
..# legend :Formal class '.RasterLegend' [package "raster"] with 5 slots
.. .. ..# type : chr(0)
.. .. ..# values : logi(0)
.. .. ..# color : logi(0)
.. .. ..# names : logi(0)
.. .. ..# colortable: logi(0)
..# title : chr(0)
..# extent :Formal class 'Extent' [package "raster"] with 4 slots
.. .. ..# xmin: num 35.8
.. .. ..# xmax: num 36.7
.. .. ..# ymin: num 2.4
.. .. ..# ymax: num 4.65
..# rotated : logi FALSE
..# rotation:Formal class '.Rotation' [package "raster"] with 2 slots
.. .. ..# geotrans: num(0)
.. .. ..# transfun:function ()
..# ncols : int 3240
..# nrows : int 8321
..# crs :Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..# projargs: chr "+proj=longlat +ellps=WGS84 +no_defs"
..# history : list()
..# z : list()
x <- trim(Chlorophyll_map_5)
ggplot(Chlorophyll_map_5) +
geom_tile(aes(fill=value)) +
scale_fill_gradient(low = 'blue', high = 'red', na.value='blue',name="Chl-a (ug/l)",limits=c(0,1000)) +
coord_equal()+theme_bw()
Per documentation, the trim function "crops a RasterLayer by removing the outer rows and columns that
only contain NA values"

Resources