# viewscape

#### Xiaohao Yang

#### 2023-12-18

# Viewscape

This vignette provides a basic overview of the functions in R package `viewscape`.

The basic viewshed analysis can be accessed through calling the `compute_viewshed`. The two needed objects to compute the viewshed are a digital surface model (DSM) and a viewpoint.

## 1. Compute viewshed

    library(viewscape)

### 1.1 Compute single viewshed

    #Load in DSM
    test_dsm <- terra::rast(system.file("test_dsm.tif",
                                           package ="viewscape"))

    #Load in the viewpoint
    test_viewpoint <- sf::read_sf(system.file("test_viewpoint.shp",
                                              package = "viewscape"))

    #Compute viewshed
    output <- viewscape::compute_viewshed(dsm = test_dsm,
                                          viewpoints = test_viewpoint,
                                          offset_viewpoint = 6)

    # overlap viewshed on DSM
    output_r <- viewscape::visualize_viewshed(output, outputtype = 'raster')
    terra::plot(test_dsm, axes=FALSE, box=FALSE, legend = FALSE)
    terra::plot(output_r, add=TRUE, col = "red", axes=FALSE, box=FALSE, legend = FALSE)
    terra::plot(test_viewpoint, add = TRUE, col = "blue", axes=FALSE, box=FALSE, legend = FALSE)

### 1.2 Compute the viewshed for multiple viewpoints

    #Load in DSM
    test_dsm <- terra::rast(system.file("test_dsm.tif",
                                           package ="viewscape"))

    # Load points (.shp file)
    test_viewpoints <- sf::read_sf(system.file("test_viewpoints.shp",
                                               package = "viewscape"))

    # Compute viewsheds
    output <- viewscape::compute_viewshed(dsm = test_dsm,
                                          viewpoints = test_viewpoints,
                                          offset_viewpoint = 6,
                                          parallel = TRUE,
                                          workers = 1)

    # Use plot all viewsheds on DSM
    par(mfrow=c(3,3))
    for(i in 1:length(output)) {
      each <- output[[i]]
      raster_data <- viewscape::visualize_viewshed(each, outputtype="raster")
      terra::plot(test_dsm, axes=FALSE, box=FALSE, legend = FALSE)
      terra::plot(raster_data, add=TRUE, col = "red", axes=FALSE, box=FALSE, legend = FALSE)
    }

## 2. Calculate viewscape metrics

### 2.1 Calculate the metrics of viewshed

The function of view depth analysis can calculate two different metrics: the furthest distance and standard deviation of distances. To calculate view depth, there are two needed objects: the DSM that was used to get viewshed and result from viewshed analysis.

The function of extent analysis can calculate the total area of viewshed and needs the DSM that was used to get viewshed and result from viewshed analysis.

The following function can calculate the area of ground surface and standard deviation of elevations within a viewshed. The function needs a DSM and a DEM/DTM to calculate the metrics.

    #Load in DSM
    test_dsm <- terra::rast(system.file("test_dsm.tif",
                                           package ="viewscape"))
    # Load DTM
    test_dtm <- terra::rast(system.file("test_dtm.tif",
                                           package ="viewscape"))

    # Load canopy raster
    test_canopy <- terra::rast(system.file("test_canopy.tif",
                                           package ="viewscape"))

    # Load building footprints raster
    test_building <- terra::rast(system.file("test_building.tif",
                                           package ="viewscape"))

    # calculate metrics given the viewshed, canopy, and building footprints
    test_metrics <- viewscape::calculate_viewmetrics(output[[1]],
                                                     test_dsm,
                                                     test_dtm,
                                                     list(test_canopy, test_building))
    test_metrics

    ## $Nump
    ## [1] 47
    ##
    ## $MSI
    ## [1] 2.650869
    ##
    ## $ED
    ## [1] 1.104127
    ##
    ## $PS
    ## [1] 640.0387
    ##
    ## $PD
    ## [1] 0.007910045
    ##
    ## $extent
    ## [1] 63952
    ##
    ## $depth
    ## [1] 1019.851
    ##
    ## $vdepth
    ## [1] 268.5452
    ##
    ## $horizontal
    ## [1] 48372
    ##
    ## $relief
    ## [1] 0.6656973
    ##
    ## $skyline
    ## [1] 6.002534

### 2.2 Calculate land use/cover diversity

calculate_diversity() calculates the proportion of each type of land use/ cover within a viewshed to get the Shannon Diversity Index.

    # load landuse raster
    test_landuse <- terra::rast(system.file("test_landuse.tif",
                                            package ="viewscape"))

    # the Shannon Diversity Index (SDI)
    test_diversity <- viewscape::calculate_diversity(output[[1]],
                                                     test_landuse,
                                                     proportion = TRUE)
    # SDI and The proportion of each type of land use
    test_diversity

    ## $SDI
    ## [1] 0.79
    ##
    ## $Proportion
    ##                 [,1]        [,2]        [,3]       [,4]        [,5]       [,6]
    ## type       0.0000000 1.000000000 2.000000000 3.00000000 4.000000000 5.00000000
    ## proportion 0.7970978 0.007005254 0.004378284 0.03952965 0.007067801 0.04503378
    ##                   [,7]       [,8]         [,9]
    ## type       6.000000000 7.00000000 8.0000000000
    ## proportion 0.004128096 0.09563423 0.0001250938

### 2.3 calculate a single feature

calculate_feature is to calculate the proportion of a feature (including trees, buildings, parking, and roads) within the viewshed. This function can be applied to

    # load canopy raster
    test_canopy <- terra::rast(system.file("test_canopy.tif",
                                              package ="viewscape"))
    # calculate the percentage of canopy coverage
    test_canopy_proportion <- viewscape::calculate_feature(viewshed = output[[1]],
                                                           feature = test_canopy,
                                                           type = 2,
                                                           exclude_value=0)
    test_canopy_proportion

    ## [1] 0.2945334