r-spatialecology/landscapemetrics

how to spilt one classification Map into different tiles,and then calcaulate all tiles's landscape values

Closed this issue · 4 comments

how to spilt one classification Map into different tiles,and then calcaulate all tiles's landscape values

Hey,

One approach would be to split the raster before calculating any metrics. There are several packages that allow to do so (e.g. SpaDES).

Also, it would be possible to use the sample_lsm() function if sample plots are basically a grid on top of the RasterLayer. Here is one way to do that:

library(landscapemetrics)
library(raster)
#> Loading required package: sp
library(sp)

# create grid with 9 cells which are 10x10
poly_grid <- sp::GridTopology(cellcentre.offset = c(5, 5), 
                              cellsize = c(10, 10), 
                              cells.dim = c(3, 3))

# convert to SpatialPolygons
poly_grid <- as(poly_grid, "SpatialPolygons")

# plot result
plot(landscape)
plot(poly_grid, add = TRUE)

# "sample" metrics in each grid cell
result <- sample_lsm(landscape = landscape, y = poly_grid, 
                     level = "landscape", verbose = FALSE)

# results
result
#> # A tibble: 585 x 8
#>    layer level     class    id metric        value plot_id percentage_inside
#>    <int> <chr>     <int> <int> <chr>         <dbl>   <int>             <dbl>
#>  1     1 landscape    NA    NA ai         87.4           1               100
#>  2     1 landscape    NA    NA area_cv   110.            1               100
#>  3     1 landscape    NA    NA area_mn     0.002         1               100
#>  4     1 landscape    NA    NA area_sd     0.00219       1               100
#>  5     1 landscape    NA    NA cai_cv    140.            1               100
#>  6     1 landscape    NA    NA cai_mn     14.9           1               100
#>  7     1 landscape    NA    NA cai_sd     20.8           1               100
#>  8     1 landscape    NA    NA circle_cv  56.2           1               100
#>  9     1 landscape    NA    NA circle_mn   0.467         1               100
#> 10     1 landscape    NA    NA circle_sd   0.262         1               100
#> # … with 575 more rows

Created on 2019-12-12 by the reprex package (v0.3.0)

Hey,

One approach would be to split the raster before calculating any metrics. There are several packages that allow to do so (e.g. SpaDES).

Also, it would be possible to use the sample_lsm() function if sample plots are basically a grid on top of the RasterLayer. Here is one way to do that:

library(landscapemetrics)
library(raster)
#> Loading required package: sp
library(sp)

# create grid with 9 cells which are 10x10
poly_grid <- sp::GridTopology(cellcentre.offset = c(5, 5), 
                              cellsize = c(10, 10), 
                              cells.dim = c(3, 3))

# convert to SpatialPolygons
poly_grid <- as(poly_grid, "SpatialPolygons")

# plot result
plot(landscape)
plot(poly_grid, add = TRUE)

# "sample" metrics in each grid cell
result <- sample_lsm(landscape = landscape, y = poly_grid, 
                     level = "landscape", verbose = FALSE)

# results
result
#> # A tibble: 585 x 8
#>    layer level     class    id metric        value plot_id percentage_inside
#>    <int> <chr>     <int> <int> <chr>         <dbl>   <int>             <dbl>
#>  1     1 landscape    NA    NA ai         87.4           1               100
#>  2     1 landscape    NA    NA area_cv   110.            1               100
#>  3     1 landscape    NA    NA area_mn     0.002         1               100
#>  4     1 landscape    NA    NA area_sd     0.00219       1               100
#>  5     1 landscape    NA    NA cai_cv    140.            1               100
#>  6     1 landscape    NA    NA cai_mn     14.9           1               100
#>  7     1 landscape    NA    NA cai_sd     20.8           1               100
#>  8     1 landscape    NA    NA circle_cv  56.2           1               100
#>  9     1 landscape    NA    NA circle_mn   0.467         1               100
#> 10     1 landscape    NA    NA circle_sd   0.262         1               100
#> # … with 575 more rows

Created on 2019-12-12 by the reprex package (v0.3.0)

Thanks,but I still met some problem about how to change the values of GridTopology().

Here are one example,

create grid with 9 cells which are 10x10

poly_grid <- sp::GridTopology(cellsize = c(5,5),
cellcentre.offset = c(5,5),
cells.dim = c(6,6))

image

I hope to split the raster to 6*6 grids,and can every tiles export with the coordinates?

You need to adjust the cellcentre.offset argument to be 1/2 of the cellsize argument.

If you set return_raster = TRUE in the sample_lsm() function, all tiles will be returned as additional column.

Should be solved. Please feel free to re-open if nessesaire.