/
pipelines.R
100 lines (94 loc) · 2.86 KB
/
pipelines.R
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#' Normalize the point cloud
#'
#' Normalize the point cloud using \link{triangulate} and \link{transform_with}. It triangulates the
#' ground points and then applies `transform_with` to linearly interpolate the elevation of each point
#' within each triangle.
#'
#' @param extrabytes bool. If FALSE the coordinate Z of the point cloud is modified and becomes the
#' height above ground (HAG). If TRUE the coordinate Z is not modified and a new extrabytes attribute
#' named 'HAG' is added to the point cloud.
#'
#' @examples
#' f <- system.file("extdata", "Topography.las", package="lasR")
#' pipeline <- reader_las() + normalize() + write_las()
#' exec(pipeline, on = f)
#' @seealso
#' \link{triangulate}
#' \link{transform_with}
#' @export
#' @md
normalize = function(extrabytes = FALSE)
{
tri <- triangulate(filter = keep_ground())
pipeline <- tri
if (extrabytes)
{
extra <- add_extrabytes("int", "HAG", "Height Above Ground")
trans <- transform_with(tri, store_in_attribute = "HAG")
pipeline <- pipeline + extra + trans
}
else
{
trans <- transform_with(tri)
pipeline <- pipeline + trans
}
return(pipeline)
}
#' Digital Terrain Model
#'
#' Create a Digital Terrain Model using \link{triangulate} and \link{rasterize}.
#'
#' @param res numeric. The resolution of the raster.
#' @param add_class integer. By default it triangulates using ground points (class 2). It is possible
#' to provide additional classes such as 9 for water.
#' @template param-ofile
#'
#' @examples
#' f <- system.file("extdata", "Topography.las", package="lasR")
#' pipeline <- reader_las() + dtm()
#' exec(pipeline, on = f)
#' @seealso
#' \link{triangulate}
#' \link{rasterize}
#' @export
dtm = function(res = 1, add_class = NULL, ofile = temptif())
{
filter = keep_ground()
if (!is.null(add_class)) filter <- filter + keep_class(add_class)
tin <- triangulate(filter = filter)
chm <- rasterize(res, tin, ofile = ofile)
return(tin+chm)
}
#' Canopy Height Model
#'
#' Create a Canopy Height Model using \link{triangulate} and \link{rasterize}.
#'
#' @param res numeric. The resolution of the raster.
#' @param tin bool. By default the CHM is a point-to-raster based methods i.e. each pixel is
#' assigned the elevation of the highest point. If `tin = TRUE` the CHM is a triangulation-based
#' model. The first returns are triangulated and interpolated.
#' @template param-ofile
#'
#' @examples
#' f <- system.file("extdata", "Topography.las", package="lasR")
#' pipeline <- reader_las() + chm()
#' exec(pipeline, on = f)
#' @seealso
#' \link{triangulate}
#' \link{rasterize}
#' @export
#' @md
chm = function(res = 1, tin = FALSE, ofile = tempfile(fileext = ".tif"))
{
if (tin)
{
tin <- triangulate(filter = keep_first())
chm_tin <- rasterize(res, tin, ofile = ofile)
pipeline <- tin + chm_tin
}
else
{
pipeline <- rasterize(res, "max", ofile = ofile)
}
return(pipeline)
}