ViSa
ViSa

Reputation: 2247

How to add polygon to 3D Map in r rayshader?

I am new to geo spatial data and just manage to plot in small bits & pieces by looking at few articles on web.

I am trying to plot polygon boundaries on the 3D plot which I have built using rayshader package but facing issues with displaying polygon boundaries on top.

Shape file used is 2011_Dist.shp which can be downloaded from Shapefile Github link

Code I have tried:

library(tidyverse)
library(sf)
library(elevatr)
library(raster)
library(rayshader)
library(osmdata)
# read districts shape file
ind_distirct_shp <- sf::st_read("local path/2011_Dist.shp")
ind_distirct_shp
# filter State
delhi_district_shp <- ind_distirct_shp %>% 
                       sf::st_as_sf() %>% 
                       filter(ST_NM %in% c("NCT of Delhi"))
# this shows the polygon boundaries that I need on top of map
plot(delhi_district_shp)

enter image description here

# download elevation data for State Delhi
delhi_raster <- elevatr::get_elev_raster(delhi_district_shp, z = 10, clip = "location")
# convert to matrix
delhi_mat <- raster_to_matrix(delhi_raster)
# 3D plot using Rayshader
delhi_mat %>%
  
  height_shade(texture = grDevices::colorRampPalette(c("#9a133d","orange","red","purple"
                                                       ))(256)) %>%
  plot_3d(heightmap = delhi_mat, 
          windowsize = c(800,800), # c(800*wr,800*hr) 
          solid = FALSE, 
          zscale = 1,
          phi = 90, 
          zoom = .6, 
          theta = 0,
          shadowcolor = "grey50",  
          linewidth = 6,
          background = "white",
          solidlinecolor = "#013b39") 

enter image description here

Issue: Now when I try to Modify this to add polygon lines it doesn't work.

polygon_layer = generate_polygon_overlay(delhi_district_shp, extent = extent(delhi_raster),
                                    heightmap = delhi_mat) # , palette="grey30"

polygon_layer
delhi_mat %>%
  
  height_shade(texture = grDevices::colorRampPalette(c("#9a133d","orange","red","purple"
                                                       ))(256)) %>% 
  add_overlay(polygon_layer) %>% 
  
  plot_3d(heightmap = delhi_mat, 
          windowsize = c(800,800), # c(800*wr,800*hr) 
          solid = FALSE, 
          zscale = 1,
          phi = 90, 
          zoom = .6, 
          theta = 0,
          shadowcolor = "grey50",  
          linewidth = 6,
          background = "white",
          solidlinecolor = "#013b39")

enter image description here

It should have been polygon lines on top of the Orange 3D map but it didn't work as I expected.

  1. I also tried with Rayshder's tyler website example https://www.tylermw.com/adding-open-street-map-data-to-rayshader-maps-in-r/
library(osmdata)

osm_bbox = c(extent(delhi_raster)[3],extent(delhi_raster)[1],extent(delhi_raster)[4],extent(delhi_raster)[2])
osm_bbox
raster_polygon_boundary <- osmdata::opq(osm_bbox) %>% 
  add_osm_feature("highway") %>%  # "admin_level" 
  osmdata_sf()

raster_polygon_boundary

Output:

Object of class 'osmdata' with:
                 $bbox : 76.8425681832661,28.4030759258059,77.347719586084,28.8793200072187
        $overpass_call : The call submitted to the overpass API
                 $meta : metadata including timestamp and version numbers
           $osm_points : 'sf' Simple Features Collection with 0 points
            $osm_lines : NULL
         $osm_polygons : 'sf' Simple Features Collection with 0 polygons
       $osm_multilines : NULL
    $osm_multipolygons : NULL

I am getting 0 polygons & lines above so I wont't be able to add any polygon on top of 3D plot.

How can I fix this. Appreciate any help.

Upvotes: 2

Views: 544

Answers (2)

TryingToHelp
TryingToHelp

Reputation: 11

You might be able to achieve what you're looking for by creating a custom color (white, 100% transparent) to fill the polygon with:

polygon_layer = generate_polygon_overlay(delhi_district_shp, 
                                         extent = extent(delhi_raster),
                                         heightmap = delhi_mat,
                                         palette = rgb(0,0,0,0)) # R,G,B,alpha

Upvotes: 1

ViSa
ViSa

Reputation: 2247

Adding alphalayer to add_overlay(polygon_layer,alphalayer = .6) helped even though it worked but still transparency is not working perfectly. It is causing some whiteness in the plot. If anyone else has better answer or way to improve it then please feel free to share.

delhi_mat %>%
  
  height_shade(texture = grDevices::colorRampPalette(c("#9a133d","orange","red","purple"
                                                       ))(256)) %>% 
  add_overlay(polygon_layer,alphalayer = .6) %>% 
  
  add_overlay(generate_label_overlay(delhi_district_shp, extent = extent(delhi_raster),
                                     text_size = 5, point_size = 1, 
                                     halo_color = "white",halo_expand = 5, 
                                     seed=1,
                                     heightmap = delhi_mat, data_label_column = "DISTRICT")) %>% 
  
  plot_3d(heightmap = delhi_mat, 
          windowsize = c(800,800), # c(800*wr,800*hr) 
          solid = FALSE, 
          zscale = .7,
          phi = 90, 
          zoom = .6, 
          theta = 0,
          shadowcolor = "grey50",  
          linewidth = 6,
          background = "white",
          solidlinecolor = "#013b39")

enter image description here

Upvotes: 1

Related Questions