Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

generate_polygon_overlay

Generate Polygon Overlay


Description

Transforms an input 'sf' object into an image overlay for the current height map.

Usage

generate_polygon_overlay(
  geometry,
  extent,
  heightmap = NULL,
  width = NA,
  height = NA,
  offset = c(0, 0),
  data_column_fill = NULL,
  linecolor = "black",
  palette = "white",
  linewidth = 1
)

Arguments

geometry

An 'sf' object with POLYGON geometry.

extent

A 'raster::Extent' object with the bounding box for the height map used to generate the original map.

heightmap

Default 'NULL'. The original height map. Pass this in to extract the dimensions of the resulting overlay automatically.

width

Default 'NA'. Width of the resulting overlay. Default the same dimensions as height map.

height

Default 'NA'. Width of the resulting overlay. Default the same dimensions as height map.

offset

Default 'c(0,0)'. Horizontal and vertical offset to apply to the polygon, in units of 'geometry'.

data_column_fill

Default 'NULL'. The column to map the polygon fill color to.

linecolor

Default 'black'. Color of the lines.

palette

Default 'black'. Single color, named vector color palette, or palette function. If this is a named vector and 'data_column_fill' is not 'NULL', it will map the colors in the vector to the names. If 'data_column_fill' is a numeric column, this will give a continuous mapping.

linewidth

Default '1'. Line width.

Value

Image overlay representing the input polygon data.

Examples

#Plot the counties around Monterey Bay, CA
#Only run these examples if the `magick` package is installed.
if ("magick" %in% rownames(utils::installed.packages())) {

generate_polygon_overlay(monterey_counties_sf, palette = rainbow, 
                        extent = attr(montereybay,"extent"), heightmap = montereybay) %>%
 plot_map() 

#These counties include the water, so we'll plot bathymetry data over the polygon
#data to only include parts of the polygon that fall on land.
water_palette = colorRampPalette(c("darkblue", "dodgerblue", "lightblue"))(200)
bathy_hs = height_shade(montereybay, texture = water_palette)

generate_polygon_overlay(monterey_counties_sf, palette = rainbow, 
                        extent = attr(montereybay,"extent"), heightmap = montereybay) %>%
 add_overlay(generate_altitude_overlay(bathy_hs, montereybay, start_transition = 0)) %>%
 plot_map()

#Add a semi-transparent hillshade and change the palette, and remove the polygon lines
montereybay %>%
 sphere_shade(texture = "bw") %>%
 add_overlay(generate_polygon_overlay(monterey_counties_sf, 
                        palette = terrain.colors, linewidth=NA,
                        extent = attr(montereybay,"extent"), heightmap = montereybay),
                        alphalayer=0.7) %>%
 add_overlay(generate_altitude_overlay(bathy_hs, montereybay, start_transition = 0)) %>%
 add_shadow(ray_shade(montereybay,zscale=50),0) %>%
 plot_map()

#Map one of the variables in the sf object and use an explicitly defined color palette
county_palette = c("087" = "red",    "053" = "blue",   "081" = "green", 
                  "069" = "yellow", "085" = "orange", "099" = "purple") 
montereybay %>%
 sphere_shade(texture = "bw") %>%
 add_shadow(ray_shade(montereybay,zscale=50),0) %>%
 add_overlay(generate_polygon_overlay(monterey_counties_sf, linecolor="white", linewidth=3,
                        palette = county_palette, data_column_fill = "COUNTYFP",
                        extent = attr(montereybay,"extent"), heightmap = montereybay),
                        alphalayer=0.7) %>%
 add_overlay(generate_altitude_overlay(bathy_hs, montereybay, start_transition = 0)) %>%
 add_shadow(ray_shade(montereybay,zscale=50),0.5) %>%
 plot_map()

}

rayshader

Create Maps and Visualize Data in 2D and 3D

v0.24.10
GPL-3
Authors
Tyler Morgan-Wall
Initial release
2021-04-25

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.