get_patches: get_patches

Description Usage Arguments Value Examples

View source: R/get_patches.R

Description

Find hot and cold patches in a numeric matrix or raster, and calculate patch statistics.

Usage

1
2
3
4
5
6
7
8
get_patches(
  img,
  id = NULL,
  style = "C",
  img_proj = NULL,
  img_extent = NULL,
  return_vals = c("df", "patches", "pstats")
)

Arguments

img

A numeric temperature matrix (such as that returned from Thermimage::raw2temp) or raster.

id

The image ID (optional). Useful when iterating over numerous images.

style

Style to use when calculating neighbourhood weights using spdep::nb2listw. Defaults to 'C' (globally standardised).

img_proj

Spatial projection. Optional, but necessary for geographic data to plot correctly.

img_extent

Spatial extent. Optional, but necessary for geographic data to plot correctly.

return_vals

Which values to return? Any combination of the dataframe (df), SpatialPolygonsDataFrame of hot and cold patches (patches) and patch statistics dataframe (pstats).

Value

A list containing:

df

A dataframe with one row for each pixel, and variables denoting: the pixel value (val); the spatial location of the pixel (x and y); its patch classification (G_bin) into a hot (1), cold (-1) or no patch (0) according to the Z value (see spdep::localG); the unique ID of the patch in which the pixel fell; the image ID (if applicable); and the original spatial location of the pixel (x_orig and y_orig).

patches

A SpatialPolygonsDataFrame of hot and cold patches, named according to id (if applicable). Hot patches have a value of 1, and cold patches a value of -1.

pstats

A dataframe with patch statistics for hot patches and cold patches, respectively. See patch_stats for details of all the statistics returned.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
## Not run: 
# FLIR temperature matrix ----------------------------------------
# Find hot and cold patches
flir_results <-
    get_patches(img = flir11835$flir_matrix,
                id = flir11835$photo_no)

# Look at the results for individual pixels
head(flir_results$df)

# Look at the patch statistics for hot and cold patches
flir_results$pstats

# Plot the patches
sp::plot(flir_results$patches)

# Plot using ThermStats::plot_patches
plot_patches(df = flir_results$df,
             patches = flir_results$patches,
             print_plot = TRUE,
             save_plot = FALSE)
             
# Plot using landscapemetrics package
flir_patch_rast <- raster::rasterFromXYZ(flir_results$df[,c("x", "y", "G_bin")]) 
if (requireNamespace("landscapemetrics", quietly = TRUE)){
    show_landscape(flir_patch_rast)
    show_patches(flir_patch_rast, class = "all", labels = FALSE)
    show_cores(flir_patch_rast, class = c(1, -1), labels = FALSE)
}

# Worldclim2 temperature raster ----------------------------------
# Dataset 'sulawesi_temp' represents mean January temperature for 
# the island of Sulawesi

# Define projection and extent
img_proj <- raster::projection(sulawesi_temp)
img_extent <- raster::extent(sulawesi_temp)

# Find hot and cold patches
worldclim_results <-
 get_patches(img = sulawesi_temp,
             id = "sulawesi",
             img_proj = img_proj,
             img_extent = img_extent)

# Look at the results for individual pixels
head(worldclim_results$df)

# Look at the patch statistics for hot and cold patches
worldclim_results$pstats

# Plot the patches
sp::plot(worldclim_results$patches)

# Plot using ThermStats::plot_patches
plot_patches(df = worldclim_results$df,
             patches = worldclim_results$patches,
             print_plot = TRUE,
             save_plot = FALSE)

## End(Not run)

rasenior/ThermStats documentation built on Oct. 31, 2020, 3:48 p.m.