Learn R Programming

shadow (version 0.2.0)

shadeHeight: Shade height calculation considering sun position and buildings outlines.

Description

This function calculates shade height at a given point (location), taking into account:
  • Buildings outline, given by a polygonal layer including a height attribute
  • Sun position, given by azimuth and elevation angles

Usage

shadeHeight(location, build, height_field, solar_pos, b = 0.1, messages = TRUE)

Arguments

location
A SpatialPoints* object specifying the location for which to calculate shade height
build
A SpatialPolygonsDataFrame object specifying the buildings outline
height_field
The name of the column with building height in build
solar_pos
A matrix with two columns: solar azimuth (in degrees from North), and elevation
b
Buffer size when joining intersection points with building outlines, to determine intersection height
messages
Whether a message regarding distance units of the CRS should be displayed

Value

Shade height, in meters; NA if there is no shade, Inf if there is complete shade (i.e. sun below horizon)

Examples

Run this code
# Single location
location = rgeos::gCentroid(build)
time = as.POSIXct("2004-12-24 13:30:00", tz = "Asia/Jerusalem")
solar_pos = maptools::solarpos(
  matrix(c(34.7767978098526, 31.9665936050395), ncol = 2),
  time
  )
plot(build, main = time)
plot(location, add = TRUE)
sun = shadow:::.sunLocation(location = location, sun_az = solar_pos[1,1], sun_elev = solar_pos[1,2])
sun_ray = ray(from = location, to = sun)
build_outline = as(build, "SpatialLinesDataFrame")
inter = rgeos::gIntersection(build_outline, sun_ray)
plot(sun_ray, add = TRUE, col = "yellow")
plot(inter, add = TRUE, col = "red")
shadeHeight(location, build, "BLDG_HT", solar_pos)

## Not run: 
# 
# # Grid
# ext = as(raster::extent(build), "SpatialPolygons")
# r = raster::raster(ext, res = 3)
# proj4string(r) = proj4string(build)
# grid = raster::rasterToPoints(r, spatial = TRUE)
# grid = sp::SpatialPointsDataFrame(grid, data.frame(grid_id = 1:length(grid)))
# height_field = "BLDG_HT"
# for(i in 1:length(grid)) {
#   grid$shade_height[i] =
#     shadeHeight(grid[i, ], build, height_field, solar_pos, messages = FALSE)
# }
# shade = as(grid, "SpatialPixelsDataFrame")
# shade = raster::raster(shade, layer = "shade_height")
# plot(shade, col = grey(seq(0.9, 0.2, -0.01)), main = time)
# raster::contour(shade, add = TRUE)
# plot(build, add = TRUE, border = "red")
# 
# ## End(Not run)

Run the code above in your browser using DataLab