forked from r-lidar/lidR
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcatalog_select.r
96 lines (83 loc) · 2.63 KB
/
catalog_select.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
# ===============================================================================
#
# PROGRAMMERS:
#
# [email protected] - https://github.com/Jean-Romain/lidR
#
# COPYRIGHT:
#
# Copyright 2016-2018 Jean-Romain Roussel
#
# This file is part of lidR R package.
#
# lidR is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
#
# ===============================================================================
#' Select LAS files manually from a LAScatalog
#'
#' Select a set of LAS tiles from a LAScatalog interactively using the mouse. This function
#' allows users to subset a LAScatalog by clicking on a map of the file.
#'
#' @param ctg A \link[lidR:LAScatalog-class]{LAScatalog} object
#'
#' @param mapview logical. If \code{FALSE}, use R base plot instead of mapview (no pan, no zoom, see
#' also \link[lidR:plot]{plot})
#'
#' @return A LAScatalog object
#'
#' @export
#'
#' @examples
#' \dontrun{
#' ctg = readLAScatalog("<Path to a folder containing a set of .las files>")
#' new_ctg = catalog_select(ctg)
#' }
catalog_select = function(ctg, mapview = TRUE)
{
assert_is_all_of(ctg, "LAScatalog")
assert_is_a_bool(mapview)
Min.X <- Min.Y <- Max.X <- Max.Y <- NULL
if (mapview & (!requireNamespace("mapview", quietly = TRUE) | !requireNamespace("mapedit", quietly = TRUE)))
{
message("Package 'mapview' and 'mapedit' are needed. Function switched to R base plot mode.")
mapview = FALSE
}
if (mapview)
{
index <- mapedit::selectFeatures(as.spatial(ctg),index = TRUE)
}
else
{
plot(ctg, mapview = FALSE)
index <- with(ctg@data, identify_tile(Min.X, Max.X, Min.Y, Max.Y))
}
return(ctg[index,])
}
identify_tile <- function(minx, maxx, miny, maxy, plot = FALSE, ...)
{
n <- length(minx)
x <- (minx + maxx)/2
y <- (miny + maxy)/2
sel <- rep(FALSE, n)
while (sum(sel) < n)
{
ans <- graphics::identify(x[!sel], y[!sel], n = 1, plot = FALSE, ...)
if (!length(ans))
break
ans <- which(!sel)[ans]
graphics::rect(minx[ans], miny[ans], maxx[ans], maxy[ans], col = "forestgreen")
sel[ans] <- TRUE
}
return(which(sel))
}