Pecners / rayshader_portraits

GNU General Public License v3.0
522 stars 95 forks source link

mat %>% plot_3d() renders an erroneous square #4

Open DanielBGAtlas opened 1 year ago

DanielBGAtlas commented 1 year ago

Hi!

I'm following one of your projects with the Kontur population data, in my case for Spain. I started following your steps, downloading the data, intersecting with the region I want to plot and ploting in 3D with with:

mat %>% height_shade() %>% plot_3d(heightmap = mat,
                                   zscale = 100)

My problem is that after doing that, what is rendered is "blank" square with no relation with the geography I want to plot. I tried with several geographies and even using data not coming from the Kontur dataset and the result is always the same:

img

My code so far:


# Loads libraries

library(sf)
library(tidyverse)
library(units)
library(stars)
library(rayshader)

# Reads population data

data = st_read("kontur_population_ES_20220630.gpkg")  

# Reads Spain zip code contour data

Madrid <- readRDS("CartoCP.rds")

# Filters zip codes to leave Madrid area only

Madrid = Madrid[grepl("^280",Madrid@data$COD_POSTAL),]

# Changes CRS and intersects

Madrid = st_as_sf(Madrid) %>% st_transform(crs = st_crs(data))

st_madrid = st_intersection(data, Madrid)

bb = st_bbox(st_madrid)

# bb to coordinates

bottom_left = st_point(c(bb[["xmin"]], bb[["ymin"]])) %>% st_sfc(crs = st_crs(st_madrid))

bottom_rigth = st_point(c(bb[["xmax"]], bb[["ymin"]])) %>% st_sfc(crs = st_crs(st_madrid))

st_madrid %>% ggplot() + geom_sf() + geom_sf(data = bottom_left, color = "blue") + geom_sf(data = bottom_rigth, color = "red")

width = st_distance(bottom_left, bottom_rigth)

top_left = st_point(c(bb[["xmin"]], bb[["ymax"]])) %>% st_sfc(crs = st_crs(st_madrid))

height = st_distance(bottom_left, top_left)

if(width > height) {

  w_ratio = 1
  h_ratio = drop_units(height / width)

} else {

  h_ratio = 1
  w_ratio = drop_units(width / height)

}

size = 1000

rast = st_rasterize(st_madrid,
                    nx = floor(size * w_ratio),
                    ny = floor(size * h_ratio))

mat = matrix(rast$population, 
             nrow = floor(size * w_ratio),
             ncol = floor(size * h_ratio))

### Plot 3d

mat %>% height_shade() %>% plot_3d(heightmap = mat,
                                   zscale = 100)

I don't know if I'm doing something wrong with the crs, but I tried changing the data I'm using and the result is always the same.

Any tips would be greatly appreciated.

DanielBGAtlas commented 1 year ago

I updated the packages directly from Github and everything is working fine now.

emepuert commented 1 year ago

Hey, could you help me with rayshader, I have my gpkg file already done. I use the following script but all time i have this error : Error in FUN(X[[i]], ...) : only defined on a data frame with all numeric-alike variables

Need serious help please 👍

library(tigris)
library(tidyverse)
library(stars)
library(rayshader)
library(MetBrewer)
library(colorspace)
library(units)
library(elevatr)

data <- st_read("reunion.gpkg")

st_data <- st_intersection(data)

bb <- st_bbox(data)

bottom_left <- st_point(c(bb[["xmin"]], bb[["ymin"]])) |> 
  st_sfc(crs = st_crs(data))

bottom_right <- st_point(c(bb[["xmax"]], bb[["ymin"]])) |> 
  st_sfc(crs = st_crs(data))

data |> 
  ggplot() +
  geom_sf() +
  geom_sf(data = bottom_left) +
  geom_sf(data = bottom_right, color = "red")

width <- st_distance(bottom_left, bottom_right)

top_left <- st_point(c(bb[["xmin"]], bb[["ymax"]])) |> 
  st_sfc(crs = st_crs(data))

height <- st_distance(bottom_left, top_left)

if (width > height) {
  w_ratio <- 1
  h_ratio <- height / width
} else {
  h_ration <- 1
  w_ratio <- width / height
}

size <- 5000

rast <- st_rasterize(st_data, 
                             nx = floor(size * w_ratio),
                             ny = floor(size * h_ratio))

mat <- matrix(data$population, 
              nrow = floor(size * w_ratio),
              ncol = floor(size * h_ratio))
mat|>
  height_shade()|>
  plot_3d(heightmap = population)

https://we.tl/t-Uh3ussgWxL

Thx for help