Skip to content

Commit

Permalink
Merge pull request #162 from r-spatialecology/master
Browse files Browse the repository at this point in the history
v1.4.2
  • Loading branch information
mhesselbarth authored Jan 20, 2020
2 parents a218801 + b6d979e commit 86cab11
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ before_install:
- sudo apt-get -qq install gcc
- mkdir ~/.R
- echo -e "CXXFLAGS = -Ofast -march=native -fpic -pipe -std=c++11\nMAKE = make -j8" > ~/.R/Makevars
- R -q -e 'install.packages(c("remotes", "knitr", "rmarkdown", "tidyverse", "SDMTools", "NLMR", "bench")); remotes::install_github("ropenscilabs/tic"); tic::prepare_all_stages(); remotes::install_deps(dependencies = TRUE); tic::before_install()'
- R -q -e 'install.packages(c("remotes", "knitr", "rmarkdown", "tidyverse", "SDMTools", "bench")); remotes::install_github("ropenscilabs/tic"); remotes::install_github("ropensci/NLMR"); tic::prepare_all_stages(); remotes::install_deps(dependencies = TRUE); tic::before_install()'
install: R -q -e 'tic::install()'
after_install: R -q -e 'tic::after_install()'
before_script: R -q -e 'tic::before_script()'
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: landscapemetrics
Title: Landscape Metrics for Categorical Map Patterns
Version: 1.4.1
Version: 1.4.2
Authors@R: c(person("Maximillian H.K.", "Hesselbarth",
role = c("aut", "cre"),
email = "[email protected]",
Expand Down
8 changes: 7 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# landscapemetrics 1.4
# landscapemetrics 1.4.2
* Bugfixes
* Bug in total edge calculation
* Improvements
* Adds a support for sf polygons in sample_lsm

# landscapemetrics 1.4.1
* Bugfixes
* Bug in the connected-component labelling algorithm for non-rectangular landscapes

Expand Down
11 changes: 8 additions & 3 deletions R/lsm_c_te.R
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,20 @@ lsm_c_te_calc <- function(landscape, count_boundary, directions, resolution = NU
directions = directions,
return_raster = FALSE)[[1]]

# set all non-class patches to -999
landscape_labeled[is.na(landscape_labeled)] <- -999
# set all non-class patches, but not NAs, to -999
edge_cells <- which(!is.na(landscape) & landscape != patches_class)

landscape_labeled[edge_cells] <- -999

# add one row/coloumn to count landscape boundary
if(count_boundary){
if (count_boundary) {
landscape_labeled <- pad_raster(landscape = landscape_labeled,
pad_raster_value = -999,
pad_raster_cells = 1,
return_raster = FALSE)[[1]]

# set NA to -999
landscape_labeled[is.na(landscape_labeled)] <- -999
}

# resolution identical in x and y direction
Expand Down
9 changes: 8 additions & 1 deletion R/lsm_l_te.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,17 @@ lsm_l_te_calc <- function(landscape, count_boundary, resolution = NULL){
resolution_y <- resolution[[2]]

if (count_boundary) {

# get background value not present as class
background_value <- max(landscape, na.rm = TRUE) + 1

# add row/col around raster
landscape <- pad_raster(landscape = landscape,
pad_raster_value = max(landscape, na.rm = TRUE) + 1,
pad_raster_value = background_value,
pad_raster_cells = 1,
return_raster = FALSE)[[1]]
# set NA to background value
landscape[is.na(landscape)] <- background_value
}

if (resolution_x == resolution_y) {
Expand Down
2 changes: 1 addition & 1 deletion R/lsm_p_perim.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ lsm_p_perim_calc <- function(landscape, directions, resolution = NULL) {

top_bottom_matrix <- matrix(c(NA, NA, NA,
1, 0, 1,
NA, NA, NA), 3, 3, byrow = TRUE)
NA, NA, NA), 3, 3, byrow = TRUE)

left_right_matrix <- matrix(c(NA, 1, NA,
NA, 0, NA,
Expand Down
18 changes: 10 additions & 8 deletions R/sample_lsm.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @description Sample metrics
#'
#' @param landscape Raster* Layer, Stack, Brick or a list of rasterLayers.
#' @param y 2-column matrix with coordinates, SpatialPoints, SpatialLines, SpatialPolygons of sf points.
#' @param y 2-column matrix with coordinates, SpatialPoints, SpatialLines, SpatialPolygons, sf points or sf polygons.
#' @param plot_id Vector with id of sample points. If not provided, sample
#' points will be labelled 1...n.
#' @param shape String specifying plot shape. Either "circle" or "square"
Expand Down Expand Up @@ -68,7 +68,7 @@
#' poly_2 <- sp::Polygons(list(poly_2), "p2")
#' sample_plots <- sp::SpatialPolygons(list(poly_1, poly_2))
#'
#' sample_lsm(landscape, y = sample_plots, what = "lsm_l_np", size = 5)
#' sample_lsm(landscape, y = sample_plots, what = "lsm_l_np")
#' }
#'
#' @aliases sample_lsm
Expand Down Expand Up @@ -309,6 +309,10 @@ sample_lsm_int <- function(landscape,
...) {

# use polygon
if (inherits(x = y, what = "sf") && all(sf::st_geometry_type(y) %in% c("POLYGON", "MULTIPOLYGON"))){
y <- methods::as(y, "Spatial")
}

if (inherits(x = y, what = c("SpatialPolygons", "SpatialPolygonsDataFrame"))) {

# convert to SpatialPolygons
Expand All @@ -328,7 +332,7 @@ sample_lsm_int <- function(landscape,

if (verbose) {

warning("Package 'rgeos' not installed. Please make sure polygons are disaggregated.",
warning("Package 'rgeos' is not installed. Please make sure polygons are disaggregated.",
call. = FALSE)
}
}
Expand Down Expand Up @@ -378,12 +382,10 @@ sample_lsm_int <- function(landscape,
if (all(sf::st_geometry_type(y) %in% c("POINT", "MULTIPOINT"))) {

y <- matrix(sf::st_coordinates(y)[, 1:2], ncol = 2)
}

else {
} else {

stop(
"landscapemetrics currently only supports sf point features for landscape metrics sampling"
"landscapemetrics currently only supports sf point and polygon features for landscape metrics sampling"
)
}
}
Expand All @@ -392,7 +394,7 @@ sample_lsm_int <- function(landscape,
"MULTILINESTRING", "MULTIPOLYGON"))) {

stop(
"landscapemetrics currently only supports sf point features for landscape metrics sampling"
"landscapemetrics currently only supports sf point and polygon features for landscape metrics sampling"
)
}

Expand Down
4 changes: 1 addition & 3 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ devtools::install_github("r-spatialecology/landscapemetrics")
```

#### Announcement
Due to an improved connected-component labelling algorithm, the patch ID differs between **landscapemetrics** v1.4 and older versions. However, results for all metrics are identical.

The algorithm labels the patches in a different order and therefore may use different patch IDs compared to previous versions.
Due to an improved connected-component labelling algorithm (**landscapemetrics** v1.4 and higher), patches are labeled in a different order than before and therefore different patch IDs might be used compared to previous versions. However, results for all metrics are identical.

## Using landscapemetrics

Expand Down
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,11 @@ devtools::install_github("r-spatialecology/landscapemetrics")

#### Announcement

Due to an improved connected-component labelling algorithm, the patch ID
differs between **landscapemetrics** v1.4 and older versions. However,
results for all metrics are identical.

The algorithm labels the patches in a different order and therefore may
use different patch IDs compared to previous versions.
Due to an improved connected-component labelling algorithm
(**landscapemetrics** v1.4 and higher), patches are labeled in a
different order than before and therefore different patch IDs might be
used compared to previous versions. However, results for all metrics are
identical.

## Using landscapemetrics

Expand Down Expand Up @@ -134,7 +133,7 @@ lsm_p_enn(landscape)
#> 8 1 patch 1 8 enn 4.12
#> 9 1 patch 1 9 enn 4.24
#> 10 1 patch 2 10 enn 4.47
#> # with 17 more rows
#> # ... with 17 more rows

# calculate the total area and total class edge length
lsm_l_ta(landscape)
Expand Down Expand Up @@ -171,7 +170,7 @@ calculate_lsm(landscape, level = "patch")
#> 8 1 patch 1 8 area 0.0005
#> 9 1 patch 1 9 area 0.0001
#> 10 1 patch 2 10 area 0.0035
#> # with 314 more rows
#> # ... with 314 more rows
```

### Utility functions
Expand Down
3 changes: 3 additions & 0 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# landscapemetrics 1.4.2
Minor improvments and bug fixes

# landscapemetrics 1.4.1
Major bugfixes

Expand Down
Binary file modified man/figures/REAsDME-unnamed-chunk-1-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions man/sample_lsm.Rd

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

20 changes: 20 additions & 0 deletions tests/testthat/test-lsm-c-te.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ context("class level lsm_c_te metric")

landscapemetrics_class_landscape_value <- lsm_c_te(landscape)

test_lsm <- matrix(data = NA, nrow = 25, ncol = 30)

test_lsm[c(5:7), c(5:7)] <- 1
test_lsm[4, 6] <- 1
test_lsm[6, 8] <- 1
test_lsm[8, 6] <- 1
test_lsm[6, 4] <- 1
test_lsm[6, 6] <- 2

test_lsm <- raster::raster(test_lsm, xmn = 0, xmx = 30, ymn = 0, ymx = 25)

test_that("lsm_c_te is typestable", {
expect_is(lsm_c_te(landscape), "tbl_df")
expect_is(lsm_c_te(landscape_stack), "tbl_df")
Expand Down Expand Up @@ -30,3 +41,12 @@ test_that("lsm_l_te equals 0 if only one patch is present", {
test_that("lsm_c_te can handle raster with different xy resolution", {
expect_is(lsm_c_te(landscape_diff_res), "tbl_df")
})

test_that("lsm_c_te is the same if count_boundary = FALSE and vice versa", {

result_cbF <- lsm_c_te(test_lsm, count_boundary = FALSE)
result_cbT <- lsm_c_te(test_lsm, count_boundary = TRUE)

expect_true(result_cbF$value[[1]] == result_cbF$value[[2]])
expect_true(result_cbT$value[[1]] != result_cbF$value[[2]])
})
24 changes: 24 additions & 0 deletions tests/testthat/test-lsm-l-te.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ context("landscape level lsm_l_te metric")

landscapemetrics_landscape_landscape_value <- lsm_l_te(landscape)

test_lsm <- matrix(data = NA, nrow = 25, ncol = 30)

test_lsm[c(5:7), c(5:7)] <- 1
test_lsm[4, 6] <- 1
test_lsm[6, 8] <- 1
test_lsm[8, 6] <- 1
test_lsm[6, 4] <- 1
test_lsm[6, 6] <- 2

test_lsm <- raster::raster(test_lsm, xmn = 0, xmx = 30, ymn = 0, ymx = 25)

test_that("lsm_l_te is typestable", {
expect_is(lsm_l_te(landscape), "tbl_df")
expect_is(lsm_l_te(landscape_stack), "tbl_df")
Expand Down Expand Up @@ -31,3 +42,16 @@ test_that("lsm_l_te option count_boundary is working", {
test_that("lsm_l_te can handle raster with different xy resolution", {
expect_is(lsm_l_te(landscape_diff_res), "tbl_df")
})


test_that("lsm_l_te is the same if count_boundary = FALSE", {

result_cbF <- lsm_c_te(test_lsm, count_boundary = FALSE)
result_cbT <- lsm_c_te(test_lsm, count_boundary = TRUE)

result_l_cbF <- lsm_l_te(test_lsm, count_boundary = FALSE)
result_l_cbT <- lsm_l_te(test_lsm, count_boundary = TRUE)

expect_true(all(result_l_cbF$value == result_cbF$value))
expect_true(all(result_l_cbT$value == max(result_cbT$value)))
})
8 changes: 6 additions & 2 deletions vignettes/articles/publication_record.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ To cite `landscapemetrics` or acknowledge its use, please cite the following Sof

- Bosch, M. 2019. PyLandStats: An open-source Pythonic library to compute landscape metrics. bioRxiv, 715052.

- Fletcher, R., Fortin, M.-J. 2018. Spatial Ecology and Conservation Modeling. Applications with R. Basel, Switzerland: Springer International Publishing.
- Copot, O., Tanase, C., 2019. Dead wood, forest fragmentation and elevation influences macrofungal diversity on downed coarse woody debris in beech and oak old forest ecosystems from northeastern Romania. Journal of Plant Development 26:161-172.

- Fletcher, R., Fortin, M.-J., 2018. Spatial Ecology and Conservation Modeling. Applications with R. Basel, Switzerland: Springer International Publishing.

- Gasparini, K.A.C., Silva Junior, C.H.L., Shimabukuro, Y.E., Arai, E., Aragão, L.E.O.C., Silva, C.A., Marshall, P.L., 2019. Determining a Threshold to Delimit the Amazonian Forests from the Tree Canopy Cover 2000 GFC Data. Sensors 19:5020.

- Heisler, L.M., Simpson, G.L., Poulin, R.G., Wellicome, T.I., Hall, B.D., 2019. Habitat specificity is not universal proxy for sensitivity to habitat conversion among rodents on the Canadian Prairies. bioRxiv 678268.

Expand All @@ -35,4 +39,4 @@ To cite `landscapemetrics` or acknowledge its use, please cite the following Sof

- Nowosad J., TF Stepinski. 2019. Information theory as a consistent framework for quantification and classification of landscape patterns. Landscape Ecol 2019.

- Rodrigues, L.L. 2019. Effects of domestic megafauna and landscape on diversity of mammals in Atlantic Forest remnants (Dissertation M.Sc.). São Paulo State University
- Rodrigues, L.L. 2019. Effects of domestic megafauna and landscape on diversity of mammals in Atlantic Forest remnants (Dissertation M.Sc.). São Paulo State University.

0 comments on commit 86cab11

Please sign in to comment.