3
3
# ' @description Sample metrics
4
4
# '
5
5
# ' @param landscape A categorical raster object: SpatRaster; Raster* Layer, Stack, Brick; stars or a list of SpatRasters.
6
- # ' @param y 2-column matrix with coordinates or sf point geometries .
6
+ # ' @param y 2-column matrix with coordinates or spatial object .
7
7
# ' @param plot_id Vector with id of sample points. If not provided, sample
8
8
# ' points will be labelled 1...n.
9
9
# ' @param shape String specifying plot shape. Either "circle" or "square"
25
25
# ' landscape boundary. Therefore, we report the actual clipped sample plot area relative
26
26
# ' in relation to the theoretical, maximum sample plot area e.g. a sample plot only half
27
27
# ' within the landscape will have a `percentage_inside = 50`. Additionally, if the polygon
28
- # ' representing the sample plot is smaller than the cell size of the raster,
28
+ # ' representing the sample plot is smaller than the cell size of the raster,
29
29
# ' the `percentage_inside` may exceed 100%.Please be aware that the
30
30
# ' output is slightly different to all other `lsm`-function of `landscapemetrics`.
31
31
# '
@@ -106,16 +106,16 @@ sample_lsm_int <- function(landscape, y, plot_id, shape, size,
106
106
107
107
}
108
108
109
- # check if y is sf object
110
- if (inherits(x = y , what = " sf" ) | inherits(x = y , what = " sfc" ) | inherits(x = y , what = " sfg" ) |
111
- inherits(x = y , what = " SpatialPolygons" ) | inherits(x = y , what = " SpatVector" )) {
109
+ # check if y is spatial object
110
+ if (inherits(x = y , what = c(" sf" , " sfc" , " sfg" , " SpatialPoints" , " SpatialPolygons" , " SpatVector" ))) {
112
111
113
112
# convert to terra
114
113
y <- methods :: as(y , " SpatVector" )
115
114
116
115
# get crs
117
116
crs <- terra :: crs(y )
118
117
118
+ # points provided
119
119
if (terra :: geomtype(y ) == " points" ) {
120
120
121
121
if (is.null(size ) | size == 0 ) stop(" Please provide size argument size > 0." , call. = FALSE )
@@ -125,7 +125,7 @@ sample_lsm_int <- function(landscape, y, plot_id, shape, size,
125
125
126
126
}
127
127
128
- # y should be matrix or points
128
+ # y should be matrix
129
129
} else if (inherits(x = y , what = " matrix" )) {
130
130
131
131
if (is.null(size )) stop(" Please provide size argument." , call. = FALSE )
@@ -135,12 +135,12 @@ sample_lsm_int <- function(landscape, y, plot_id, shape, size,
135
135
136
136
} else {
137
137
138
- stop(" Please provide a matrix with coords, points or polygons object." , call. = FALSE )
138
+ stop(" Please provide a matrix with coords or spatial object." , call. = FALSE )
139
139
140
140
}
141
141
142
142
# check if y is a polygon
143
- if (terra :: geomtype(y ) != " polygons" ) stop(" Please provide polygon object." , call. = FALSE )
143
+ if (terra :: geomtype(y ) != " polygons" ) stop(" Please provide a matrix with coords or spatial object." , call. = FALSE )
144
144
145
145
# check if length is identical if ids are provided
146
146
if (! is.null(plot_id )) {
@@ -165,57 +165,59 @@ sample_lsm_int <- function(landscape, y, plot_id, shape, size,
165
165
warning_messages <- character (0 )
166
166
167
167
# loop through each sample point and calculate metrics
168
- result <- withCallingHandlers(expr = {do.call( rbind , lapply( X = 1 : number_plots ,
169
- FUN = function (current_plot ) {
168
+ result <- withCallingHandlers(expr = {
169
+ do.call( rbind , lapply( X = 1 : number_plots , FUN = function (current_plot ) {
170
170
171
- # print progess using the non-internal name
172
- if (progress ) {
171
+ # print progess using the non-internal name
172
+ if (progress ) {
173
173
174
- cat(" \r > Progress sample plots: " , current_plot , " /" , number_plots )
175
- }
174
+ cat(" \r > Progress sample plots: " , current_plot , " /" , number_plots )
176
175
177
- # crop sample plot
178
- landscape_mask <- terra :: crop(x = landscape , y = y [current_plot , ], mask = TRUE )
176
+ }
179
177
180
- # calculate actual area of sample plot
181
- area <- lsm_l_ta_calc( landscape_mask , directions = 8 )
178
+ # crop sample plot
179
+ landscape_mask <- terra :: crop( x = landscape , y = y [ current_plot , ], mask = TRUE )
182
180
183
- # calculate lsm
184
- result_current_plot <- calculate_lsm(landscape = landscape_mask ,
185
- verbose = verbose ,
186
- progress = FALSE ,
187
- ... )
181
+ # calculate actual area of sample plot
182
+ area <- lsm_l_ta_calc(landscape_mask , directions = 8 )
188
183
189
- # add plot id 1...n
190
- if (is.null(plot_id )) {
184
+ # calculate lsm
185
+ result_current_plot <- calculate_lsm(landscape = landscape_mask ,
186
+ verbose = verbose ,
187
+ progress = FALSE ,
188
+ ... )
191
189
192
- result_current_plot $ plot_id <- current_plot
190
+ # add plot id 1...n
191
+ if (is.null(plot_id )) {
193
192
194
- # add plot_id
195
- } else {
196
- result_current_plot $ plot_id <- plot_id [current_plot ]
197
- }
193
+ result_current_plot $ plot_id <- current_plot
198
194
199
- # all cells are NA
200
- if (all(is.na(terra :: values(landscape_mask , mat = FALSE )))) {
195
+ # add plot_id
196
+ } else {
197
+ result_current_plot $ plot_id <- plot_id [current_plot ]
198
+ }
201
199
202
- # calculate ratio between actual area and theoretical area
203
- result_current_plot $ percentage_inside <- 0
204
- } else {
200
+ # all cells are NA
201
+ if (all(is.na(terra :: values(landscape_mask , mat = FALSE )))) {
205
202
206
- # calculate ratio between actual area and theoretical area
207
- result_current_plot $ percentage_inside <- area $ value /
208
- maximum_area [[current_plot ]] * 100
209
- }
203
+ # calculate ratio between actual area and theoretical area
204
+ result_current_plot $ percentage_inside <- 0
205
+ } else {
210
206
211
- # add sample plot raster
212
- result_current_plot $ raster_sample_plots <- terra :: as.list(landscape_mask )
207
+ # calculate ratio between actual area and theoretical area
208
+ result_current_plot $ percentage_inside <- area $ value /
209
+ maximum_area [[current_plot ]] * 100
210
+ }
213
211
214
- return (result_current_plot )}))}, warning = function (cond ) {
212
+ # add sample plot raster
213
+ result_current_plot $ raster_sample_plots <- terra :: as.list(landscape_mask )
215
214
216
- warning_messages <<- c(warning_messages , conditionMessage(cond ))
215
+ return (result_current_plot )
216
+ })
217
+ )}, warning = function (cond ) {
218
+ warning_messages <<- c(warning_messages , conditionMessage(cond ))
217
219
218
- invokeRestart(" muffleWarning" )}
220
+ invokeRestart(" muffleWarning" )}
219
221
)
220
222
221
223
if (progress ) {
0 commit comments