Skip to content

Commit

Permalink
update new funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
billbillbilly committed Jun 7, 2024
1 parent e9d06fb commit 2a68db0
Show file tree
Hide file tree
Showing 21 changed files with 99 additions and 54 deletions.
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
^viewscape\.md$
^viewscape_hex-01\.png$
^LICENSE$
^\.vscode$
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
.Rhistory
.RData
.Ruserdata
.DS_Store
.vscode
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ Imports:
Rcpp,
rlang,
methods,
rlang,
dplyr,
sf,
sp,
Expand Down
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ reference <- function(viewpoint, dsm, h, max_dis) {
.Call('_viewscape_reference', PACKAGE = 'viewscape', viewpoint, dsm, h, max_dis)
}

LOS <- function(viewpoint, dsm, h, max_dis) {
.Call('_viewscape_LOS', PACKAGE = 'viewscape', viewpoint, dsm, h, max_dis)
}

15 changes: 15 additions & 0 deletions R/compute_viewshed.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ compute_viewshed <- function(dsm,
r <- 1000
}
}
ext_dsm <- terra::ext(dsm)
# if (dsm_units == "ft" && r > 3281) {
# r <- 3281
# } else if (dsm_units == "m" && r > 1000) {
Expand All @@ -117,6 +118,13 @@ compute_viewshed <- function(dsm,
}
}
if (multiviewpoints == FALSE){
# validate the viewpoint
if (viewpoints[,1] < ext_dsm[1] | viewpoints[,1] > ext_dsm[2]) {
stop("The input viewpoint(s) is not on input dsm")
}
if (viewpoints[,2] < ext_dsm[3] | viewpoints[,2] > ext_dsm[4]) {
stop("The input viewpoint(s) is not on input dsm")
}
viewpoints <- c(viewpoints[,1], viewpoints[,2])
# compute viewshed
output <- radius_viewshed(dsm, r, viewpoints,
Expand Down Expand Up @@ -145,6 +153,13 @@ compute_viewshed <- function(dsm,
return(output)
}
}else if (multiviewpoints){
# validate the viewpoints
if (min(viewpoints[,1]) < ext_dsm[1] | max(viewpoints[,1]) > ext_dsm[2]) {
stop("Some of input viewpoint(s) is not on input dsm")
}
if (min(viewpoints[,2]) < ext_dsm[3] | max(viewpoints[,2]) > ext_dsm[4]) {
stop("Some of input viewpoint(s) is not on input dsm")
}
inputs <- split(viewpoints,seq(nrow(viewpoints)))
if (parallel == TRUE){
if (workers == 0) {
Expand Down
18 changes: 10 additions & 8 deletions R/sector_mask.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@
#'
#'
#' @return viewshed object
#'
#' \dontrun{
#' viewshed <- compute_viewshed(dsm,
#' viewpoints = test_viewpoint,
#' offset_viewpoint = 6)
#' @examples
#' # Load a viewpoint
#' test_viewpoint <- sf::read_sf(system.file("test_viewpoint.shp", package = "viewscape"))
#' # load dsm raster
#' dsm <- terra::rast(system.file("test_dsm.tif", package ="viewscape"))
#' # Compute viewshed
#' viewshed <- viewscape::compute_viewshed(dsm,
#' viewpoints = test_viewpoint,
#' offset_viewpoint = 6)
#' # subset viewshed using the field of view
#' out <- viewscape::sector_mask(viewshed, c(40,160))
#' terra::plot(viewscape::visualize_viewshed(out, outputtype = 'raster'),
#' axes=FALSE, box=FALSE, legend = FALSE, add = TRUE, col = "red")
#' }
#'
#' @seealso [compute_viewshed()]
#' @export
Expand Down
16 changes: 10 additions & 6 deletions R/visual_magnitude.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
#' @return SpatRaster
#'
#' @examples
#' \dontrun{
#' viewshed<- compute_viewshed(dsm = dsm,
#' viewpoints = test_viewpoint,
#' offset_viewpoint = 6)
#' vm <- viewscape::visual_magnitude(viewshed, test_dsm)
#' }
#' # Load a viewpoint
#' test_viewpoint <- sf::read_sf(system.file("test_viewpoint.shp", package = "viewscape"))
#' # load dsm raster
#' dsm <- terra::rast(system.file("test_dsm.tif", package ="viewscape"))
#' # Compute viewshed
#' viewshed <- viewscape::compute_viewshed(dsm = dsm,
#' viewpoints = test_viewpoint,
#' offset_viewpoint = 6)
#' # Compute visual magnitude
#' vm <- viewscape::visual_magnitude(viewshed, dsm)
#'
#' @importFrom terra terrain
#'
Expand Down
5 changes: 4 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ output: github_document
---

<!-- badges: start -->
[![Codecov test coverage](https://codecov.io/github/land-info-lab/viewscape/branch/master/graph/badge.svg)](https://codecov.io/github/land-info-lab/viewscape?branch=master)
[![](https://www.r-pkg.org/badges/version/viewscape)](https://www.r-pkg.org/pkg/viewscape)
[![GPLv3 License](https://img.shields.io/badge/License-GPL%20v3-yellow.svg)](https://opensource.org/licenses/)
[![R-CMD-check](https://github.com/land-info-lab/viewscape/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/land-info-lab/viewscape/actions/workflows/R-CMD-check.yaml)
[![](https://cranlogs.r-pkg.org/badges/viewscape)](https://CRAN.R-project.org/package=viewscape)
![total](https://cranlogs.r-pkg.org/badges/grand-total/viewscape)
<!-- badges: end -->

```{r setup, include=FALSE}
Expand Down
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

<!-- badges: start -->
[![](https://www.r-pkg.org/badges/version/viewscape)](https://www.r-pkg.org/pkg/viewscape)
[![GPLv3 License](https://img.shields.io/badge/License-GPL%20v3-yellow.svg)](https://opensource.org/licenses/)
[![R-CMD-check](https://github.com/land-info-lab/viewscape/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/land-info-lab/viewscape/actions/workflows/R-CMD-check.yaml)
[![](https://cranlogs.r-pkg.org/badges/viewscape)](https://CRAN.R-project.org/package=viewscape)
![total](https://cranlogs.r-pkg.org/badges/grand-total/viewscape)
<!-- badges: end -->


# viewscape

<p align="left">
Expand Down Expand Up @@ -54,7 +53,7 @@ The metrics are including:
- Number of patches: Visible fragmentation measured by total visible patches
with the viewscape.
- Mean shape index: Visible patchiness based on average perimeter-to-area ratio
for all viewscape patches.
for all viewscape patches (vegetation and building).
- Edge density: A measure of visible complexity based on the length of
patch edges per unit area.
- Patch size: Total average size of a patches over the entire viewscape area.
Expand All @@ -69,14 +68,6 @@ The metrics are including:
the viewshed.
- Relief: The standard deviation of elevations of the visible ground
surface.
- Number of patches: Visible fragmentation measured by total visible patches
with the viewscape
- Mean shape index: Visible patchiness based on average perimeter-to-area ratio
for all viewscape patches (vegetation and building)
- Edge density: A measure of visible complexity based on the length of
patch edges per unit area
- Patch size: Total average size of a patches over the entire viewscape area
- Patch density: Visible landscape granularity based on measuring patch density
- Skyline: Variation of (Standard deviation) of the vertical viewscape
(visible canopy and buildings).
- Shannon diversity index: Based on the number of land use/cover
Expand Down
2 changes: 2 additions & 0 deletions man/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
inst/.DS_Store
man/.DS_Store
22 changes: 13 additions & 9 deletions man/sector_mask.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions man/visual_magnitude.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,27 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// LOS
Rcpp::IntegerMatrix LOS(const Rcpp::NumericVector& viewpoint, const Rcpp::NumericMatrix& dsm, const double h, const int max_dis);
RcppExport SEXP _viewscape_LOS(SEXP viewpointSEXP, SEXP dsmSEXP, SEXP hSEXP, SEXP max_disSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< const Rcpp::NumericVector& >::type viewpoint(viewpointSEXP);
Rcpp::traits::input_parameter< const Rcpp::NumericMatrix& >::type dsm(dsmSEXP);
Rcpp::traits::input_parameter< const double >::type h(hSEXP);
Rcpp::traits::input_parameter< const int >::type max_dis(max_disSEXP);
rcpp_result_gen = Rcpp::wrap(LOS(viewpoint, dsm, h, max_dis));
return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_viewscape_VM", (DL_FUNC) &_viewscape_VM, 7},
{"_viewscape_get_depths", (DL_FUNC) &_viewscape_get_depths, 5},
{"_viewscape_sectorMask", (DL_FUNC) &_viewscape_sectorMask, 3},
{"_viewscape_reference", (DL_FUNC) &_viewscape_reference, 4},
{"_viewscape_LOS", (DL_FUNC) &_viewscape_LOS, 4},
{NULL, NULL, 0}
};

Expand Down
Binary file modified src/RcppExports.o
Binary file not shown.
Binary file modified src/viewscape.so
Binary file not shown.
1 change: 1 addition & 0 deletions src/visibleLabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ Rcpp::IntegerMatrix reference(
}

// Line of sight
//[[Rcpp::export]]
Rcpp::IntegerMatrix LOS(
const Rcpp::NumericVector &viewpoint,
const Rcpp::NumericMatrix &dsm,
Expand Down
Binary file modified src/visibleLabel.o
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/testthat/test-calculate_diversity.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ testthat::test_that("runs correctly", {
test_landcover,
proportion = TRUE)

testthat::expect_type(test_diversity[[1]], "double")
testthat::expect_type(test_diversity, "list")

})
2 changes: 1 addition & 1 deletion tests/testthat/test_sector_mask.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ testthat::test_that("runs correctly", {
offset_viewpoint = 6,
plot=FALSE)
test_sector <- viewscape::sector_mask(output, c(40,160))
testthat::expect_type(test_vm, "S4")
testthat::expect_type(test_sector, "S4")
})
18 changes: 8 additions & 10 deletions vignettes/viewscape.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ The basic viewshed analysis can be accessed through calling the `compute_viewshe

## 1. Compute viewshed

```{r}
```{r val=FALSE}
library(viewscape)
```

### 1.1 Compute single viewshed

```{r}
```{r val=FALSE}
#Load in DSM
test_dsm <- terra::rast(system.file("test_dsm.tif",
package ="viewscape"))
Expand Down Expand Up @@ -51,8 +51,6 @@ sector <- viewscape::sector_mask(output, c(40,160))
terra::plot(test_dsm, axes=FALSE, box=FALSE, legend = FALSE)
terra::plot(viewscape::visualize_viewshed(sector, outputtype = 'raster'),
axes=FALSE, box=FALSE, legend = FALSE, add = TRUE, col = "red")
terra::plot(sp::SpatialPoints(viewpoint), add = TRUE, col = "blue",
axes=FALSE, box=FALSE, legend = FALSE)
```

### 1.3 Compute visual magnitude
Expand All @@ -65,7 +63,7 @@ terra::plot(vm, axes=FALSE, box=FALSE)

### 1.4 Compute the viewshed for multiple viewpoints

```{r}
```{r val=FALSE}
#Load in DSM
test_dsm <- terra::rast(system.file("test_dsm.tif",
package ="viewscape"))
Expand Down Expand Up @@ -102,7 +100,7 @@ The function of extent analysis can calculate the total area of viewshed and nee

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.

```{r}
```{r val=FALSE}
#Load in DSM
test_dsm <- terra::rast(system.file("test_dsm.tif",
package ="viewscape"))
Expand All @@ -120,7 +118,7 @@ test_building <- terra::rast(system.file("test_building.tif",
```

```{r}
```{r val=FALSE}
# calculate metrics given the viewshed, canopy, and building footprints
test_metrics <- viewscape::calculate_viewmetrics(output[[1]],
test_dsm,
Expand All @@ -133,13 +131,13 @@ test_metrics

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

```{r}
```{r val=FALSE}
# load landuse raster
test_landuse <- terra::rast(system.file("test_landuse.tif",
package ="viewscape"))
```

```{r}
```{r val=FALSE}
# the Shannon Diversity Index (SDI)
test_diversity <- viewscape::calculate_diversity(output[[1]],
test_landuse,
Expand All @@ -152,7 +150,7 @@ test_diversity

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

```{r}
```{r val=FALSE}
# load canopy raster
test_canopy <- terra::rast(system.file("test_canopy.tif",
package ="viewscape"))
Expand Down

0 comments on commit 2a68db0

Please sign in to comment.