I am trying to plot SpatialPoints with render_label, and I got a bit confused about the y-value in render_label, since I have to take the "opposite" of what I think it should be.
I hope this example illustrates my point:
library(sp)
library(raster)
library(rayshader)
## Get DEM Data and some random Points
poly <- Polygon(rbind(c(4488182, 2663172), c(4488182, 2669343),
c(4499991, 2669343), c(4499991, 2663172)))
poly <- Polygons(list(poly), 1);
poly <- SpatialPolygons(list(poly))
proj <- "+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000
+ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"
proj4string(poly) <- CRS(proj)
DEM_meter <- raster::getData("alt", country="AUT")
DEM <- projectRaster(DEM_meter, crs = CRS(proj))
DEM <- crop(x = DEM, extent(poly))
textlabls = spsample(poly, 10, type = "random");
## Convert DEM to matrix
DEM_matrix = t(as.matrix(DEM))
## Get x/y matrix coordinates from SpatialPoints
textlabls_df <- as.data.frame(sp::coordinates(textlabls))
raster_indic <- raster::cellFromXY(DEM, textlabls_df)
raster_x <- raster::colFromX(DEM, x = textlabls_df$x)
raster_y <- raster::rowFromY(DEM, y = textlabls_df$y)
I struggle with this next line, as without it the rayshader plot seems flipped although the z-values are all equal. If I remove this line, the 3d-plot looks ok, but I get different z-values, which feels weird.
raster_y = ncol(DEM_matrix) - raster_y + 1
## Get z-values of points
z_vals <- raster::extract(DEM, textlabls)
textlabls_df$z <- z_vals
## Compare z-values
all.equal(textlabls_df$z[1],
DEM_matrix[raster_indic[1]])
all.equal(DEM_matrix[raster_x[1],raster_y[1]],
DEM_matrix[raster_indic[1]])
## Plot DEM and points
plot(DEM); points(textlabls$x,textlabls$y, pch=20)
## Plot with rayshader
shadow = ray_shade(DEM_matrix, zscale=200, lambert=FALSE)
amb = ambient_shade(DEM_matrix,zscale=200)
DEM_matrix %>%
sphere_shade(zscale=200, texture = "imhof1") %>%
add_shadow(shadow, 0.5) %>%
add_shadow(amb) %>%
plot_3d(DEM_matrix, zscale=200, zoom=0.8)
a <- lapply(1:length(raster_x), function(i) {
render_label(DEM_matrix, x=raster_x[i], y=raster_y[i], z=textlabls_df$z[i],
freetype = FALSE, zscale=200, text = round(textlabls_df$z[i]))
})
Am I missing something, or are you still working on that function?
I noticed that the labels are placed in the cell centers. Is it or will it be possible to include exact coordinates instead of matrix indices?
And a little sidenote:
This raster to matrix transformation t(as.matrix(DEM)) is much faster, than the example in your Readme, although you loose the option to include a buffer or special function, etc. But in my cases, the results were always identical.
matrix(raster::extract(DEM,raster::extent(DEM),buffer=1000), nrow=ncol(DEM),ncol=nrow(DEM))
Labels can be specified via lat/longs or other coordinates as of v0.17 by passing in the extent of your raster. I believe that mostly solves this issue.
I am trying to plot SpatialPoints with
render_label
, and I got a bit confused about the y-value inrender_label
, since I have to take the "opposite" of what I think it should be.I hope this example illustrates my point:
I struggle with this next line, as without it the rayshader plot seems flipped although the z-values are all equal. If I remove this line, the 3d-plot looks ok, but I get different z-values, which feels weird.
raster_y = ncol(DEM_matrix) - raster_y + 1
Am I missing something, or are you still working on that function?
I noticed that the labels are placed in the cell centers. Is it or will it be possible to include exact coordinates instead of matrix indices?
And a little sidenote: This raster to matrix transformation
t(as.matrix(DEM))
is much faster, than the example in your Readme, although you loose the option to include a buffer or special function, etc. But in my cases, the results were always identical.matrix(raster::extract(DEM,raster::extent(DEM),buffer=1000), nrow=ncol(DEM),ncol=nrow(DEM))