Skip to content

Commit

Permalink
Merge pull request #161 from r-spatialecology/master
Browse files Browse the repository at this point in the history
v1.4.1
  • Loading branch information
mhesselbarth authored Jan 12, 2020
2 parents fa9f455 + 904d448 commit a218801
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 17 deletions.
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
Version: 1.4.1
Authors@R: c(person("Maximillian H.K.", "Hesselbarth",
role = c("aut", "cre"),
email = "[email protected]",
Expand Down
8 changes: 6 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# landscapemetrics 1.4
* Improvements
* Improved connected-component labelling algorithm. **ATTENTION: THIS LEADS TO DIFFERENT PATCH IDS COMPARED TO PREVIOUS VERSIONS.**
* Bugfixes
* Bug in the connected-component labelling algorithm for non-rectangular landscapes

# landscapemetrics 1.4
* Improvements
* Improved connected-component labelling algorithm. **Note: The algorithm labels the patches in a different order and therefore may use different patch IDs compared to previous versions.**

# landscapemetrics 1.3.2
* Bugfixes
* Make sure all CRAN checks run
Expand Down
3 changes: 2 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ 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.

## Using landscapemetrics

The resolution of a raster cell has to be in **meters**, as the package converts units internally and returns results in either meters, square meters or hectares. Before using **landscapemetrics**, be sure to check your raster (see `check_raster()`).
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ 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.

## Using landscapemetrics

The resolution of a raster cell has to be in **meters**, as the package
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.1
Major bugfixes

# landscapemetrics 1.4
Improvments of existing functions and make sure all CRAN checks run

Expand Down
25 changes: 12 additions & 13 deletions src/rcpp_cclabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,26 @@ void rcpp_ccl(IntegerMatrix mat, int directions)
const unsigned nNeig = neigCoordinates.size();

// it's convinient to have patch cells marked as 0 (i.e. un-labeled) and matrix cells as NA
for (int row = 0; row < nrows; row++) {
for (int col = 0; col < ncols; col++) {
if (mat[row * ncols + col] == NA) {
for (int col = 0; col < ncols; col++) {
for (int row = 0; row < nrows; row++) {
if (mat[col * nrows + row] == NA) {
continue;
}
mat[row * ncols + col] = 0;
mat[col * nrows + row] = 0;
}
}

int label = 0;
for (int row = 0; row < nrows; row++) {
for (int col = 0; col < ncols; col++) {

for (int col = 0; col < ncols; col++) {
for (int row = 0; row < nrows; row++) {
// ignore background cells and cells that are already labeled
if (mat[row * ncols + col] == NA ||
mat[row * ncols + col] > 0) {
if (mat[col * nrows + row] == NA ||
mat[col * nrows + row] > 0) {
continue;
}

// label the first cell of the patch
mat[row * ncols + col] = ++label;
mat[col * nrows + row] = ++label;

std::queue<std::array<const int, 2> > patchcells;
patchcells.push(std::array<const int, 2>{col, row});
Expand All @@ -73,12 +72,12 @@ void rcpp_ccl(IntegerMatrix mat, int directions)
continue;

// skip if background or already labeled
if (mat[row_neig * ncols + col_neig] == NA ||
mat[row_neig * ncols + col_neig] > 0)
if (mat[col_neig * nrows + row_neig] == NA ||
mat[col_neig * nrows + row_neig] > 0)
continue;

// label cell and put in queue
mat[row_neig * ncols + col_neig] = label;
mat[col_neig * nrows + row_neig] = label;
patchcells.push(std::array<const int, 2>{col_neig, row_neig});
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-rcpp_ccl.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
context("rcpp_ccl")

mat8 <- matrix(c(NA, 1L, NA, NA, 1L, 1L, 1L, NA), 4, 2)
mat4 <- matrix(c(NA, 1L, NA, NA, 1L, 1L, 1L, NA), 4, 2)

landscapemetrics:::rcpp_ccl(mat4, 4)
landscapemetrics:::rcpp_ccl(mat8, 8)

test_that("rcpp_ccl patch labelling is correct", {
expect_equal(length(unlist(get_unique_values(mat4))), 1)
expect_equal(length(unlist(get_unique_values(mat8))), 1)
})

0 comments on commit a218801

Please sign in to comment.