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
Related
I always get error to this
# polygon that to be window
neighborhoods <- st_read("neighborhoods/neighborhoods.shp")
# convert CRS to planar projection as recommended by (https://stackoverflow.com/questions/59597078/use-sf-polygon-object-as-window-in-spatstat)
neighborhoods_3857 <- st_transform(neighborhoods, crs = 3857)
# point that to be PPP spatstat
trees <- st_read("trees/trees.shp")
# convert to planar projection
trees_3857 <- st_transform(trees, crs = 3857)
The problems, the "trees_3857" doesn't have dataframe columns that represent in EPSG3857 coordinates, so Feature column of "trees_3857" doesn't have x and y columns that respect to EPSG 3857
q <- ppp(x=?, y=?, win=neighborhoods_3857)
what I have done but error
z <- as.ppp(trees_3857, win=neighborhoods_3857)
Error in as.ppp.sf(trees_3857, win = neighborhoods_3857): unused argument (win = neighborhoods_3857)
Traceback:
You can get the data freely from datacamp.
https://assets.datacamp.com/production/repositories/738/datasets/96a72364e69d872645038b3a6dc7c0dbcb1114d6/neighborhoods.zip
https://assets.datacamp.com/production/repositories/738/datasets/08a3684dc4d538d59ba051a64a834166883ab5d1/trees.zip
Although you're wanting to transform your data into an object of class "ppp" from the spatstat package, the error message indicates that the problem originated in the function as.ppp.sf which is part of the sf package.
The error message says unused argument: win which means that the function did not recognise or accept the argument win.
Just to make it more challenging, the function as.ppp.sf is not documented and is not visible... By typing sf:::as.ppp.sf we can see the function body and figure out that the function has only one argument, so it does not accept any window information.
This is not the way the generic function as.ppp is designed to work in the spatstat package, so if you looked for help(as.ppp) in spatstat, it's not surprising that you got confused.
The best solution is (as Ege Rubak suggests) to convert the point coordinates and then add the window information:
z <- as.ppp(trees_3857)
Window(z) <- as.owin(neighborhoods_3857)
The conversions as.ppp and as.owin will be executed using code in sf so I can't guarantee they will work. The assignment Window(z) <- will be executed in spatstat code, see help("Window<-.ppp").
would it be possible to add isoclines or something like "filled.contour" to a raster map?
E.g. to a Bioclim variable? (RasterLayer)
# Download Bioclim data
library(dismo)
tmin_06<-getData("worldclim", var="tmin", res=0.5, lon=10, lat=70)
# plot tmin
plot(tmin$tmin1_06)
And then add e.g. filled.contour {graphics}
I can’t seem to extract the correct values from the RasterLayer and get an error massage:
"cannot coerce type 'S4' to vector of type 'double'"
Or what would you use to illustrate the direction of change in a map like this for temperature, or for equidistance in an elevation map?
Thank you!
Daniel
Here are two approaches:
# example data
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
plot(r)
contour(r, add=TRUE)
# or
filledContour(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.
I found some useful links in this SO question An algorithm for inflating/deflating (offsetting, buffering) polygons .
Sorry - no 'net access so sos doesn't work, so does anyone have an implementation of the skeleton algorithm in R?
Edit: I would like to generate deflated polygons as in the StackOverflow link (top image); or as seen on http://en.wikipedia.org/wiki/Straight_skeleton .
gBuffer(), from the elegant and powerful rgeos package accepts negative values in its width argument, returning SpatialPolygons that are 'shrunk' by the given amount.
library(sp)
library(rgeos)
## Create a SpatialPolygons object from set of x-y coordinates (the hard part!)
xy2SP <- function(xy, ID=NULL) {
if(is.null(ID)) ID <- sample(1e12, size=1)
SpatialPolygons(list(Polygons(list(Polygon(xy)), ID=ID)),
proj4string=CRS("+proj=merc"))
}
xy <- data.frame(x=c(0,2,3,1,0), y=c(0,0,2,2,0))
SP <- xy2SP(xy)
## Shrink the SpatialPolygons object by supplying a negative width to gBuffer()
plot(SP)
plot(gBuffer(SP, width=-0.2), add=TRUE, border="red")
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)