Render from lat/lng cordinates with Rayshader

Hi.

I am very new to R and having issues with getting Rayshader height shade graphs from csv data to work. If I do it from kontur.io population data the code results in a nice render, but if I use a custom CSV with lat/lng converted into points it does not seem to render anything.

It will plot the points just fine, just no rendering of the height shade graph.

We also tried converting data into polygons in a gpkg file giving us the same result.

I am not getting any error messages.

Current example:

library(xlsx)
library(ggplot2)
library(ggplot2)
library(grid)
library(png)
library(rgl)
library(sf)
library(raster)
library(tidyverse)
library(tigris)
library(stars)
library(MetBrewer)
library(colorspace)
library(rayshader)
library(glue)
library(data.table)

########################################
# Import data from csv
########################################

# https://simplemaps.com/data/world-cities
csvdata = read.csv('worldcities.csv')
csvdata = csvdata %>%
  st_as_sf(coords = c("lng","lat"), crs=4326)


########################################
# Create color palette 
########################################
color <- MetBrewer::met.brewer(name="Troy")
tx <- grDevices::colorRampPalette(color, bias = 2)(256)
swatchplot(tx)
swatchplot(color)


########################################
# Create plot
########################################

csvdata |>
  ggplot() +
  geom_sf(size = 0.1)


########################################
# Setup aspect ratio
########################################

bb <- st_bbox(csvdata)
extent(bb)

yind <- st_distance(st_point(c(bb[["xmin"]], bb[["ymin"]])), 
                    st_point(c(bb[["xmin"]], bb[["ymax"]])))
xind <- st_distance(st_point(c(bb[["xmin"]], bb[["ymin"]])), 
                    st_point(c(bb[["xmax"]], bb[["ymin"]])))

# Handle conditions of width or height being the longer side
if (yind > xind) {
  y_rat <- 1
  x_rat <- xind / yind
} else {
  x_rat <- 1
  y_rat <- yind / xind
}


########################################
# Create raster
########################################

# Convert to raster so we can then convert to matrix
size <- 6000

blk_rast <- st_rasterize(csvdata, nx = floor(size * x_rat), ny = floor(size * y_rat))


########################################
# Create matrix
########################################

mat <- matrix(blk_rast$population, nrow = floor(size * x_rat), ncol = floor(size * y_rat))



########################################
# Height shade 3d plot
########################################

rgl::close3d()

mat |>
  height_shade(texture = tx) |>
  plot_3d(heightmap = mat,
          size = 4,
          zscale = 15,
          solid = FALSE,
          theta = -8,
          phi = 50,
          zoom = .82,          
          windowsize = c(500,500),
          verbose = TRUE,
          shadowdepth = 0)

Data sample:

"city","city_ascii","lat","lng","country","iso2","iso3","admin_name","capital","population","id"
"Tokyo","Tokyo","35.6897","139.6922","Japan","JP","JPN","Tōkyō","primary","37732000","1392685764"
"Jakarta","Jakarta","-6.1750","106.8275","Indonesia","ID","IDN","Jakarta","primary","33756000","1360771077"
"Delhi","Delhi","28.6100","77.2300","India","IN","IND","Delhi","admin","32226000","1356872604"
"Guangzhou","Guangzhou","23.1300","113.2600","China","CN","CHN","Guangdong","admin","26940000","1156237133"
"Mumbai","Mumbai","19.0761","72.8775","India","IN","IND","Mahārāshtra","admin","24973000","1356226629"
"Manila","Manila","14.5958","120.9772","Philippines","PH","PHL","Manila","primary","24922000","1608618140"
"Shanghai","Shanghai","31.1667","121.4667","China","CN","CHN","Shanghai","admin","24073000","1156073548"
"São Paulo","Sao Paulo","-23.5500","-46.6333","Brazil","BR","BRA","São Paulo","admin","23086000","1076532519"

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.