plot3d for readGDAL? - r

I would like to prepare 3D graph in R! using raster data.
I heard about command plot3d in raster package.
Unfortunately I have data in GeoTiff format, so it is impossible to read it directly through raster package (instead of it, to load this file, I am using readGDAL command).
Is there any opportunity to prepare 3D graph using such data?
How I can retrieve coordinates? I know how to get values of raster using as.matrix command, but because it is in GeoTiff format, I am unable to use xmin or xmax.

I do not see why you cannot read a geotiff with raster, but at any rate the SpatialGridDataFrame (package sp) provided by readGDAL (package rgdal) can be passed directly to raster().
Here's an example with a GeoTIFF from the rgdal package. Note that the rasterVis function is in a separate package and is called plot3D (not plot3d which is in the rgl package):
library(rgdal)
library(rasterVis)
r <- raster(system.file("pictures/cea.tif", package = "rgdal")[1])
plot3D(r)
The rasterVis package handles all the scaling and colours and provides a nice default.
If you want to delve further into the supporting packages, here's a simple example.
library(rgdal)
library(raster)
library(rgl)
## read the file with raster
r <- raster(system.file("external/test.ag", package="sp")[1])
## just use simple persp plot
persp(r)
## convert to sp's SpatialGridDataFrame (or use readGDAL directly)
## (for very large rasters this could be prohibitive in terms of memory)
sgdf <- as(r, "SpatialGridDataFrame")
## convert to R's image() format, a list with x,y vector and z matrix
x <- as.image.SpatialGridDataFrame(sgdf)
## plot with rgl, ugly but see ?surface3d for colour options etc.
surface3d(x$x, x$y, x$z)

Related

R: Isolating Maps for Viewing and Export

I'm attempting to view one map separately from a raster stack of maps each with unique titles. When I run the map function, all 5 maps included in the raster stack are displayed side by side (see below). I am only interested in viewing and exporting the last. How would you go about isolating this map? Below is an image of the outputs and a link to the plotting function I'm using.
https://github.com/azizka/sampbias/blob/master/R/map_bias.R
Raster Stack maps
With these data
library(terra)
f <- system.file("ex/elev.tif", package="terra")
s <- rast(c(f, f, f))
You can plot all three layers
plot(s)
Or a single layer (here the second)
plot(s, 2)
Or a subset
plot(s, 2:3)
Alternatively, you can first subset s like this to get the second layer
r <- s[[2]]
And then use
plot(r)
Or functions from other packages. The above is for SpatRaster objects from the terra package. But it works the same for the RasterStack objects from raster.

R: Handling of sf objects in raster package

Previously I was using raster::crop and raster::mask with shapefiles of class Spatial*, read in using rgal::readOGR.
I am just "upgrading" my scripts to use sf for reading and manipulating polygons.
raster::crop
raster::crop expects an 'extent' object as second argument. Up to now, this was automatically extracted from a Spatial* object. So I could just do raster::crop(raster, polygon).
To get this working with an sf object, I can call raster::crop(raster, as.vector(st_bbox(polygon))) as an ugly workaround.
raster::mask
Since raster::mask clearly expects a Raster* object or a Spatial* object the only solution was to coerce the sf object back to a Spatial* object using as("Spatial").
I assume this problem generalized to all raster functions? Did I overlook something or is it just the case that the raster package does not (yet) work with sf objects?
For future reference, it works now! Here's some slightly modified example code from ?crop, tested with raster version 2.6-7 which has been released on 2017-11-13.
library(raster)
library(sf)
r <- raster(nrow=45, ncol=90)
r[] <- 1:ncell(r)
# crop Raster* with sf object
b <- as(extent(0, 8, 42, 50), 'SpatialPolygons')
crs(b) <- crs(r)
b <- st_as_sf(b) # convert polygons to 'sf' object
rb <- crop(r, b)
# mask Raster* with sf object
mb <- mask(r, b)

Plotting 3D Raster (DEM) in R?

I am trying to plot a 3D surface of a DEM imported as a raster in R using the raster package.
So far, my code is:
DEM <- raster("DSM_TLS_2010_25cm_v4.tif")
DEM <- setMinMax(DEM)
col <- rainbow(20)
plot(DEM, col=col, zlim=c(0,790.22), main="Digital Elevation Model (DEM)")
Which works perfect for a 2D plot of the DEM, but, when I try to make it 3D, with:
plot3d(DEM)
or
surface3d(DEM)
It says cannot coerce type 'S4' to vector of type 'double'.
I am sure that answer is very simple but I have not managed to get it working with the similar questions that I have found.
The raster() function returns RasterLayer objects, and I suspect that the plot3d() and surface3d() functions in the rgl package do not (or do not fully) support RasterLayer objects.
The plot3D() function in the RasterVis package, however, does. Give this a try:
install.packages("rasterVis")
library(rasterVis)
plot3D(DEM) # note: 3D not 3d

Create Georeference in R

I work with R to analyse satellite data from MODIS (file attached). I want to georeference my .image/.tif file using R. This is my script that I used:
library(raster)
x <- raster('bali_test.tif')
extent(x) <- c(114,115,-8,-7)
projection(x) <- CRS("+proj=longlat +datum=WGS![enter image description here][1]84")
Unfortunately, when I plot it using levelplot and world map, it appears in the wrong position. The white area is land/island, and the black line is the Indonesian coastline
You must use the levelplot() function from the rasterVis package. The levelplot function you are using is not for raster matrix.

Extracting scale factor and offsets from raster made with netcdf in R

I have a raster file generated with the raster package from a netcdf file, and I need to extract the fillvalue, the scale factor and the offset. With the package ncdf I can extract those attributes with the next code:
fillvalue <- att.get.ncdf(ncdf,"tp","_FillValue")
scale <- att.get.ncdf(ncdf,"tp","scale_factor")
offset <- att.get.ncdf(ncdf,"tp","add_offset")
But with the raster package I don't know how to extract them, is it possible? Or do I have to extract them with ncdf?
I don't have a ncdf file to test, but if you read the file using raster and saved it using raster, I believe the below should work. If not try using readGDAL from the rgdal package to read the ncdf image.
library(raster)
img <- raster(file_path)
scale <- gain(img)
offset <- offs(img)
fillvalue <- NAvalue(img)

Resources