-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFUNCTIONS_ITCD.R
1173 lines (967 loc) · 58.4 KB
/
FUNCTIONS_ITCD.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##########################################################################################################################################################################################
###FUNCTIONS IN THIS SCRIPT
##########################################################################################################################################################################################
# numextract
# numextract_all
# VOX_SLICE_DIST_FUN
# WS_CLUSTERING_FUN
# WS_MOVING_FUN
# SLICE_SURROUND_BBOX_FUN
# GAP_DENSITY_FUN
# VORONOI_FOCAL_FUN
# VOXEL_FUN
# CONVHULL_FUN
# GET_TREE_ATTRIBUTES_FUN
# STEM_PCA_FUN
# plot_UAS_function
# plot_function
##########################################################################################################################################
##########################################################################################################################################
numextract <- function(string){
str_extract(string, "\\-*\\d+\\.*\\d*")
}
numextract_all <- function(string){
unlist(regmatches(string,gregexpr("[[:digit:]]+\\.*[[:digit:]]*",string)))
}
##########################################################################################################################################
##########################################################################################################################################
# DISTANCE BETWEEN ALL VOXELS IN TWO FILES. MAX DISTANCE SPECIFIED
VOX_SLICE_DIST_FUN = function(XYZ_oneCl = XYZ_oneCl,
XYZ_Surr =XYZ_surrOneCl,
Max_Dist = Para_EDT_V,
Para_Vx_R_Res= Para_Vx_R_Res,
Slice_Thickness= Para_Sl_Z){
Slice_Seq <- seq(min(c(min(XYZ_oneCl$Z), min(XYZ_Surr$Z))), max(c(max(XYZ_oneCl$Z), max(XYZ_Surr$Z))) , Slice_Thickness)
for(SS in 1:length(Slice_Seq)){
# GET DATA FOR SLICE
Vx_oneCl_oneSl <- XYZ_oneCl [which(XYZ_oneCl$Z >= Slice_Seq[SS]-Slice_Thickness & XYZ_oneCl$Z <= Slice_Seq[SS]+2*Slice_Thickness ),]
Vx_Surr_oneSl <- XYZ_Surr[which(XYZ_Surr$Z >= Slice_Seq[SS]-Slice_Thickness & XYZ_Surr$Z <= Slice_Seq[SS]+2*Slice_Thickness ),]
# IF THERE IS SURROUND VOXELS
if(nrow(Vx_Surr_oneSl) > 0){
if(nrow(Vx_oneCl_oneSl) > 0){
######################
# DISTANCE CALCULATION
######################
Index_Col1 <- match(c("X", "Y", "Z"), colnames(Vx_oneCl_oneSl))
Index_Col2 <- match(c("X", "Y", "Z"), colnames(Vx_Surr_oneSl))
Dist_oneCl_Surr <- rdist(as.data.frame(Vx_oneCl_oneSl)[,Index_Col1],
as.data.frame(Vx_Surr_oneSl)[,Index_Col2]) # Row Col
Dist_oneCl_Surr <- round( Dist_oneCl_Surr, 3)
Index_Dist_oneCl_Surr <- as.data.frame(which(Dist_oneCl_Surr <= Max_Dist, arr.ind = TRUE))
# UPDATE TABLE OF ALL CLOSE MATCHES
if(nrow(Index_Dist_oneCl_Surr) > 0){
Closest_Vox_Surr<- Vx_Surr_oneSl[Index_Dist_oneCl_Surr$col,]
colnames(Closest_Vox_Surr)[which(colnames(Closest_Vox_Surr) %in% c("TreeID", "VoxID"))] <- c("TID_Surr", "VoxID_Surr")
Closest_Vox_St <- Vx_oneCl_oneSl[Index_Dist_oneCl_Surr$row,]
colnames(Closest_Vox_St)[which(colnames(Closest_Vox_St) %in% c("TreeID", "VoxID"))] <- c("TID_oneCl", "VoxID_oneCl")
Dist_St_Surr_Close <- Dist_oneCl_Surr[ cbind(Index_Dist_oneCl_Surr$row, Index_Dist_oneCl_Surr$col) ]
Closest_Vox_Surr$Dist <- Dist_St_Surr_Close
Closest_Vox_Surr_oneCl <- data.frame(Closest_Vox_Surr, Closest_Vox_St)
if(!exists("All_Closest_Vox_Surr_oneCl")){
All_Closest_Vox_Surr_oneCl <- Closest_Vox_Surr_oneCl
}else{
All_Closest_Vox_Surr_oneCl<- rbind(All_Closest_Vox_Surr_oneCl, Closest_Vox_Surr_oneCl)
}
}
}
}
}
if(exists("All_Closest_Vox_Surr_oneCl")){
# GENERATE SUMMARY OF THE CLOSEST SURROUNDING TID
Index1 <- match(c("X", "Y", "Z"), colnames(All_Closest_Vox_Surr_oneCl))
colnames(All_Closest_Vox_Surr_oneCl)[Index1] <- c("X_Surr", "Y_Surr", "Z_Surr")
Index2 <- match(c("X.1", "Y.1", "Z.1"), colnames(All_Closest_Vox_Surr_oneCl))
colnames(All_Closest_Vox_Surr_oneCl)[Index2] <- c("X_oneCl", "Y_oneCl", "Z_oneCl")
All_Closest_Vox_Surr_oneCl <- All_Closest_Vox_Surr_oneCl[order(All_Closest_Vox_Surr_oneCl$VoxID_oneCl, All_Closest_Vox_Surr_oneCl$Dist), ] #sort by id and reverse of abs(value)
All_Closest_Vox_Surr_oneCl_NoDup <- All_Closest_Vox_Surr_oneCl[!duplicated(All_Closest_Vox_Surr_oneCl$VoxID_oneCl),]
Summary_closeSurr <- as.data.frame(All_Closest_Vox_Surr_oneCl_NoDup %>%
dplyr::group_by(TID_Surr) %>%
dplyr::summarise(minDist = min(Dist),
cntVox = length(which(Dist <= sqrt(Para_Vx_R_Res^2 + Para_Vx_R_Res^2) + 0.01)),
MinZ = min(Z_Surr),
MaxZ = max(Z_Surr),
.groups = 'drop'))
}else{
All_Closest_Vox_Surr_oneCl_NoDup <- NULL
Summary_closeSurr <- NULL
}
return(list(All_Closest_Vox_Surr_oneCl_NoDup = All_Closest_Vox_Surr_oneCl_NoDup,
Summary_closeSurr = Summary_closeSurr))
}
##########################################################################################################################################
##########################################################################################################################################
# THIS FUNCTION COMPUTES THE CENTROIDS USING THE WS DERIVED FROM THE POINT DENSITY DISTRIBUTIONS OVER A SLICE.
WS_CLUSTERING_FUN = function(LAS_WS_Cl, Para_Vx_R_Res = 0.2, LAS_or_Vox = "Vox"){
# RASTERISING THE LAS SLICE with R_Den, R_MaxZ, R_TID_MaxZ
if(LAS_or_Vox == "Vox"){
R_Den <- grid_metrics(LAS_WS_Cl, sum(Vox_PntCnt), res = Para_Vx_R_Res)
}else{
R_Den <- grid_metrics(LAS_WS_Cl, length(Z), res = Para_Vx_R_Res)
}
R_MaxZ <- grid_metrics(LAS_WS_Cl, max(Z), res = Para_Vx_R_Res)
R_TID_MaxZ <- grid_metrics(LAS_WS_Cl, TreeID[which.max(Z)], res = Para_Vx_R_Res)
if(R_Den@ncols > 1 & R_Den@nrows > 1){
# WATERSHED USING LAS DENSITY
algo = lidR::watershed(R_Den, tol = 1, ext=5)
LAS_WS_Cl <- segment_trees(LAS_WS_Cl, algorithm = algo, attribute = "CentID", uniqueness = "incremental")
R_WS <- algo()
if(length(na.omit(R_WS@data@values)) > 0){
Unique_CentID <- as.numeric(names(table(LAS_WS_Cl@data$CentID)))
# WORK AROUND FOR WHEN THE ROWS AND COLS ARE BUGGY (FLIPPED FOR UNKNOWN REASON)
if(!((R_WS@ncols == R_Den@ncols)&(R_WS@nrows == R_Den@nrows))){
R_WS_Nrows <- R_WS@nrows
R_WS_Ncols <- R_WS@ncols
R_WS@ncols <- R_WS_Nrows
R_WS@nrows <- R_WS_Ncols
}
# STACK RASTERS
R_Stack <- stack(R_Den,
R_WS,
R_MaxZ,
R_TID_MaxZ) # R_oneSl_ClID,
names(R_Stack@layers[[1]]) <- "ZCount"
names(R_Stack@layers[[2]]) <- "WS"
names(R_Stack@layers[[3]]) <- "maxZ"
names(R_Stack@layers[[4]]) <- "TID_MaxZ"
# GENERATES DF OF RASTER VALUES (...CONVERT FACTOR TO NUMERIC.... REMOVES NA)
R_Stack_DF <- data.frame(Cell_ID = seq(1, ncell(R_Den),1) , getValues(R_Stack))
R_Stack_DF <- data.frame(sapply(R_Stack_DF, function(x) as.numeric(as.character(x))))
R_Stack_DF <- na.omit(R_Stack_DF)
###################################################
# GET LOCATION OF MAX DENSITY GRID CELL FOR EACH WS
###################################################
# LOCATE MAX DENSITY CELL_ID FOR EACH WS
Cell_ID_maxDen <- as.data.frame(R_Stack_DF %>%
dplyr::select(Cell_ID, ZCount, WS) %>%
dplyr::group_by(WS) %>%
dplyr::summarise(Cell_ID_maxDen = Cell_ID[which.max(ZCount)][1], .groups = 'drop'))
# CLEAN Cell_ID_maxDen
Cell_ID_maxDen <- na.omit(Cell_ID_maxDen)
Cell_ID_maxDen <- Cell_ID_maxDen[which(Cell_ID_maxDen$WS %in% Unique_CentID),]
# XY OF EACH CELL (NEW CENTROID) REPRESENTING MAX DEN FOR EACH WS
WS_XY_maxDen <- xyFromCell(R_WS, Cell_ID_maxDen$Cell_ID_maxDen, spatial=FALSE)
colnames(WS_XY_maxDen) <- c("X", "Y")
Max_Den <- raster::extract(R_Den, WS_XY_maxDen)
CentID<-apply(X = data.frame(WS_XY_maxDen), MARGIN = 1, FUN = function(xy) R_WS@data@values[which.min(replace(distanceFromPoints(R_WS, xy), is.na(R_WS), NA))])
WS_XY_maxDen <- data.frame(WS_XY_maxDen, Max_Den, CentID)
# GET Z VALUE FOR NEW CENTROID (USING LIDAR WITH WS ID ASSOCIATED WITH CENTROID)
WS_Median_Z_TID <- as.data.frame(LAS_WS_Cl@data %>%
dplyr::group_by(CentID) %>%
dplyr::summarise(Z = median(Z),
CentID = as.numeric(names(table(CentID)[which.max(table(CentID))]))[1], .groups = 'drop'))
WS_Median_Z_TID <- na.omit(WS_Median_Z_TID)
WS_XY_maxDen$Z <- WS_Median_Z_TID$Z[match(WS_XY_maxDen$CentID, WS_Median_Z_TID$CentID)]
return(list(WS_XY_maxDen = WS_XY_maxDen,
R_Stack = R_Stack,
LAS_WS_Cl = LAS_WS_Cl))
}else{
return(list(WS_XY_maxDen = NULL,
R_Stack = NULL,
LAS_WS_Cl = LAS_WS_Cl))
}
}else{
return(list(WS_XY_maxDen = NULL,
R_Stack = NULL,
LAS_WS_Cl = LAS_WS_Cl))
}
}
##########################################################################################################################################
##########################################################################################################################################
WS_MOVING_FUN = function(LAS_Above_Sl, LAS_Below_Sl, Para_Vx_R_Res = 0.2, Slice_Thickness = 0.4, Z_Inc_Slice_mm = Z_Inc_Slice[mm], LAS_or_Vox = "Vox"){
#############################################
# APPLY WS CLUSTERING FUNCTION ON ABOVE SLICE
#############################################
WS_Above_Cl <- WS_CLUSTERING_FUN(LAS_Above_Sl, Para_Vx_R_Res = Para_Vx_R_Res, LAS_or_Vox = "Vox")
if(!is.null(WS_Above_Cl$WS_XY_maxDen)){
WS_Class_Above <- WS_Above_Cl$R_Stack$WS
WS_Class_Above_TID <- WS_Above_Cl$R_Stack$TID_MaxZ
# DATA.FRAME OF GRID CELLS WITH WS CLUSTER ID
XYCell_Above <- xyFromCell(WS_Class_Above, which(!is.na(WS_Class_Above@data@values)))
XYCell_Above <- data.frame(XYCell_Above,
WS_ID=na.omit(WS_Class_Above@data@values),
TID = WS_Class_Above_TID@data@values[which(!is.na(WS_Class_Above@data@values))])
Index_Col1 <-match(c("X", "Y"), colnames(LAS_Above_Sl@data))
#############################################
# APPLY WS CLUSTERING FUNCTION ON BELOW SLICE
#############################################
WS_Below_Cl <- WS_CLUSTERING_FUN(LAS_Below_Sl, Para_Vx_R_Res = Para_Vx_R_Res, LAS_or_Vox = "Vox")
if(!is.null(WS_Below_Cl$WS_XY_maxDen)){
Below_MaxDen_WS_XY<- WS_Below_Cl$WS_XY_maxDen
WS_Class_Below_WS <- WS_Below_Cl$R_Stack$WS
WS_Class_Below_GID <- WS_Class_Below_WS
XYCell_Below <- xyFromCell(WS_Class_Below_WS, which(!is.na(WS_Class_Below_WS@data@values)))
XYCell_Below <- data.frame(XYCell_Below, WS_ID=na.omit(WS_Class_Below_WS@data@values))
##########################################################################
# GIVE ALL LAS_BELOW_SL A GRI_ID (FOR NA USE rdist TO GET CLOSEST GRID ID)
##########################################################################
# GET GRID ID OF ALL BELOW GRIDS WITH VALUE
WS_Below_Values <- WS_Class_Below_WS$WS@data@values
G_ID <- which(!is.na(WS_Class_Below_WS$WS@data@values))
WS_Below_Values[!is.na(WS_Class_Below_WS$WS@data@values)] <- G_ID
WS_Class_Below_GID <- setValues(WS_Class_Below_GID, WS_Below_Values)
XYCell_Below <- data.frame(XYCell_Below,
G_ID=WS_Class_Below_GID[which(!is.na(WS_Class_Below_WS@data@values))])
#browser()
LAS_Below_Sl <- merge_spatial(LAS_Below_Sl, WS_Class_Below_GID, attribute = "WSID")
index_Col1<-match(c("X", "Y"), colnames(LAS_Below_Sl@data))
index_NA_GID <- which(is.na(LAS_Below_Sl@data$WSID))
if(length(index_NA_GID)>0){
Dist_BelowG_BeloNA <- rdist(XYCell_Below[, 1:2], LAS_Below_Sl@data[index_NA_GID, ..index_Col1])
Nearest_G_Index <- apply(Dist_BelowG_BeloNA, 2, which.min)
LAS_Below_Sl@data$WSID[index_NA_GID] <- G_ID[Nearest_G_Index]
}
##########################################
# GET TID (MOST COMMON) FOR EACH GRID CELL
##########################################
Grid_Cell_TID <- as.data.frame(LAS_Below_Sl@data %>%
dplyr::group_by(WSID) %>%
dplyr::summarise(TID = TreeID[which.max(length(Z))], .groups = 'drop'))
XYCell_Below <- XYCell_Below[which(XYCell_Below$G_ID %in% Grid_Cell_TID$WSID),]
XYCell_Below$TID <- Grid_Cell_TID$TID
####################################
# ID NEAREST BELOW WS FOR EACH VOXEL
###################################
# DISTANCE BETWEEN TOP AND BOTTOM GRID CELLS
Dist_WS_Below_Above <- rdist(XYCell_Below[,1:2],
XYCell_Above[,1:2])
Dist_WS_Below_Above <- round(Dist_WS_Below_Above, 3)
Dist_WS_Below_Above <- ifelse(Dist_WS_Below_Above > Slice_Thickness*2,NA,Dist_WS_Below_Above) # REMOVE PAIRS THAT ARE VERY FAR APART
# NEAREST BELOW WS FOR EACH VOXEL
if(nrow(Dist_WS_Below_Above) ==1) {
Dist_WS_Below_Above<- as.vector(Dist_WS_Below_Above)
NearestG_Below_To_Above <- which.min(Dist_WS_Below_Above)
if(length(NearestG_Below_To_Above) == 0){
NearestG_Below_To_Above <- NA
NearestDist_Below_To_Above <- NA
}else{
NearestDist_Below_To_Above <- min(Dist_WS_Below_Above, na.rm=T)
}
}else{
Dist_WS_Below_Above <-data.frame(Dist_WS_Below_Above)
NearestG_Below_To_Above <-apply(Dist_WS_Below_Above, 2, function(x) {if (all(is.na(x))) {NA} else {which.min(x)} })
NearestDist_Below_To_Above <- apply(Dist_WS_Below_Above, 2, function(x) {if (all(is.na(x))) {NA} else {min(x, na.rm=T)} })
}
Index_Nearest <- which(NearestDist_Below_To_Above < Slice_Thickness*2 ) #Para_EDT_V == Slice_Thickness*2
################################################################
# UPDATE ABOVE TID (USING NEAREST BELOW TID FOR EACH ABOVE GRID)
################################################################
# UPDATE DATAFRAME OF GRID CELLS
XYCell_Above$TID[Index_Nearest] <- XYCell_Below$TID[NearestG_Below_To_Above[Index_Nearest]]
# UPDATE ABOVE RASTER
WS_Above_Values <- WS_Class_Above$WS@data@values
WS_Above_Values[which(!is.na(WS_Above_Values))] <- XYCell_Above$TID
WS_Class_Above@data@names <- "TID"
WS_Class_Above <- setValues(WS_Class_Above, WS_Above_Values)
# UPDATE ABOVE LAS
LAS_Above_Sl <- merge_spatial(LAS_Above_Sl, WS_Class_Above, attribute = "WSID")
LAS_Above_Sl@data$TreeID <- as.integer(LAS_Above_Sl@data$WSID)
# UPDATE LAS THAT HAD NA (FROM WS algorithm) USING rdist
index_Col1<-match(c("X", "Y"), colnames(LAS_Above_Sl@data))
index_NA_NewTID <- which(is.na(LAS_Above_Sl@data$WSID))
if(length(index_NA_NewTID)>0){
Dist_AboveG_AboveNA <- rdist(XYCell_Above[, 1:2], LAS_Above_Sl@data[index_NA_NewTID, ..index_Col1])
Dist_AboveG_AboveNA <- round(Dist_AboveG_AboveNA, 2)
Nearest_G_Index <- apply(Dist_AboveG_AboveNA, 2, which.min)
if(length(Nearest_G_Index)> 0){
LAS_Above_Sl@data$WSID[index_NA_NewTID] <- XYCell_Above$TID[Nearest_G_Index]
LAS_Above_Sl@data$TreeID <- as.integer(LAS_Above_Sl@data$WSID) # ABOVE WSID AND TreeID IS SAME
}
}
# UPDATE LAS TID (ONLY ONE SLICE NOT THE WHOLE 4)
LAS_Above_oneSl_New <- filter_poi(LAS_Above_Sl, Z >= Z_Inc_Slice_mm &
Z < Z_Inc_Slice_mm+ Slice_Thickness &
WSID > 0)
# IF WS BELOW ISN'T NULL
}else{
LAS_Above_oneSl_New <- filter_poi(LAS_Above_Sl, TreeID < -10000) # GENREATES EMPTY LAS
}
# IF WS ABOVE ISN'T NULL
}else{
LAS_Above_oneSl_New <- filter_poi(LAS_Above_Sl, TreeID < -10000) # GENREATES EMPTY LAS
}
return(list(LAS_Above_oneSl_New = LAS_Above_oneSl_New))
}
##########################################################################################################################################
##########################################################################################################################################
# SLICE_SURROUND_BBOX_FUN:
# GETS ALL THE TID THAT FALL WITHIN THE BBoX Z RANGE
# INCREMENTALLY MOVES UP THE SLICE AND PERFORMS WS ON ABOVE SLICE TO CHANGE ITS TID BASED ON PROXIMITY TO BELOW SLICE
# THE WHILE LOOP USES THE NEW ABOVE SLICE VOXEL TID TO REPEATIDLY UNDERTAKING DISTANCE CALCULATION BETWEEN ORIGINAL AND NEW TID VALUES.
# THE LOOP STOPS WHEN NO FURTHER VOXELS HAVE BEEN CHANGED DUE TO CONSTRAINTS IN VOX_SLICE_DIST_FUN
SLICE_SURROUND_BBOX_FUN = function(LAS_surrOneCl_rmOneCl_allTID, LAS_Empty, Unique_TID,
Para_EDT_V, Z_Inc_Slice, Para_Vx_R_Res, Para_Sl_Z,
LAS_or_Vox = "Vox"){
LAS_bboxTID_allSl <- LAS_Empty
Move_Above_Sl_NonGnd_All <- "No"
for(mm in 1:length(Z_Inc_Slice)){
# GET BELOW AND ABOVE SLICE
LAS_Above_oneSl <- filter_poi(LAS_surrOneCl_rmOneCl_allTID, Z >= Z_Inc_Slice[mm] & Z < Z_Inc_Slice[mm]+(Para_EDT_V))
LAS_Below_oneSl <- filter_poi(LAS_surrOneCl_rmOneCl_allTID, Z >= (Z_Inc_Slice[mm]-Para_EDT_V) & Z < Z_Inc_Slice[mm])
if(nrow(LAS_Above_oneSl@data) > 0 & nrow(LAS_Below_oneSl@data) > 0){
#####################################################
# WS CLUSTER: FOR MOVING ABOVE SLICE and BELOW SLICE
#####################################################
WS_Move_Slice<- WS_MOVING_FUN(LAS_Above_oneSl, LAS_Below_oneSl,
Para_Vx_R_Res = Para_Vx_R_Res,
Slice_Thickness = Para_EDT_V,
Z_Inc_Slice_mm = Z_Inc_Slice[mm],
LAS_or_Vox = "Vox")
LAS_Above_oneSl_New <- WS_Move_Slice$LAS_Above_oneSl_New
if(nrow(LAS_Above_oneSl_New@data) > 0){
# UPDATE WS_MOVING_FUN RESULTS (ASSIGN TreeID AS WSID AND REMOVE WSID ATTRIBUTE)
LAS_Above_oneSl_New@data$TreeID <- as.integer(LAS_Above_oneSl_New@data$WSID)
Index_Remove <- which(colnames(LAS_Above_oneSl_New@data) == "WSID")
LAS_Above_oneSl_New@data <- LAS_Above_oneSl_New@data[,-Index_Remove, with=FALSE]
Index_Above_Sl_OrigTID <- which(unique(LAS_Above_oneSl_New@data$TreeID) %in% Unique_TID)
if(length(Index_Above_Sl_OrigTID) > 0){
# WHILE LOOP ONLY UPDATES VOXELS WITHIN Para_EDT_V AND REPEATS UNTIL ALL UPDATED WITHIN SLICE
Check_Orig_Count <- 1
Change_Orig_Count <- 0
while(Check_Orig_Count != 0){
# USE VOX PROXIMITY TO REPLACE ANY NonGnd TID with Neighbours
LAS_Above_oneSl_New_Orig <- filter_poi(LAS_Above_oneSl_New, TreeID %in% Unique_TID)
LAS_Above_oneSl_New_Changed <- filter_poi(LAS_Above_oneSl_New, !(TreeID %in% Unique_TID)) # CHANGED DUE TO "WS_MOVING_FUN"
if(nrow(LAS_Above_oneSl_New_Changed@data) > 0){
# GET CLOSE VOXEL SUMMARY BETWEEN NonGnd_TID and Gnd_TID
Index_Col1 <- match(c("X", "Y", "Z", "TreeID", "VoxID"), colnames(LAS_Above_oneSl_New_Orig@data))
XYZ_New_Orig <- LAS_Above_oneSl_New_Orig@data[,..Index_Col1]
Index_Col2 <- match(c("X", "Y", "Z", "TreeID", "VoxID"), colnames(LAS_Above_oneSl_New_Changed@data))
XYZ_New_Changed <- LAS_Above_oneSl_New_Changed@data[,..Index_Col2]
################################################################################################ 12
# DIST CALC FOR ABOVE SLICE (BETWEEN VOXELS WITH ORIG TID AND THOSE THAT CHANGED WITH "WS_MOVING_FUN")
################################################################################################ 12
Dist_NewOrig_NewChange <- VOX_SLICE_DIST_FUN(XYZ_New_Orig,
XYZ_New_Changed, # THESE ARE LIKE THE SURROUNDING TID
Max_Dist = Para_EDT_V,
Para_Vx_R_Res= Para_Vx_R_Res,
Slice_Thickness= Para_Sl_Z)
Table_CloseVox <- Dist_NewOrig_NewChange$All_Closest_Vox_Surr_oneCl_NoDup
# UPDATING TID IN ABOVE SLICE
Index <- match(Table_CloseVox$VoxID_oneTID, LAS_Above_oneSl_New@data$VoxID )
LAS_Above_oneSl_New@data$TreeID[Index] <- Table_CloseVox$TID_Surr
# CHECKING IF WHILE LOOP SHOULD BE REPEATED
Check_Orig_Count <- length(which(LAS_Above_oneSl_New@data$TreeID %in% Unique_TID))
if(Change_Orig_Count == Check_Orig_Count){
Check_Orig_Count <- 0 # BREAK OUT OF WHILE LOOP
}
}else{
Check_Orig_Count <- 0 # BREAK OUT OF WHILE LOOP
}
Change_Orig_Count <- Check_Orig_Count
} # WHILE LOOP
} # IF ABOVE SLICE STILL HAS NonGnd TID
if(nrow(LAS_Above_oneSl_New@data) > 0){
# UPDATING LAS WITH ABOVE SLICE TID
Index_1 <- which(LAS_surrOneCl_rmOneCl_allTID@data$VoxID %in% LAS_Above_oneSl_New@data$VoxID )
Index_2 <- match(LAS_surrOneCl_rmOneCl_allTID@data$VoxID[Index_1], LAS_Above_oneSl_New@data$VoxID )
LAS_surrOneCl_rmOneCl_allTID@data$TreeID[Index_1] <- as.integer(LAS_Above_oneSl_New@data$TreeID[Index_2])
if(length(which(LAS_Above_oneSl_New@data$TreeID == Unique_TID)) > 0) {
LAS_Above_oneSl_NonGnd <- filter_poi(LAS_Above_oneSl_New, TreeID %in% Unique_TID)
if(nrow(LAS_bboxTID_allSl@data) == 0){
LAS_bboxTID_allSl <- filter_poi(LAS_surrOneCl_rmOneCl_allTID, Z < Z_Inc_Slice[mm]+(Para_EDT_V) & TreeID == Unique_TID)
}else{
LAS_bboxTID_allSl@data <- rbind(LAS_bboxTID_allSl@data, LAS_Above_oneSl_NonGnd@data)
}
Move_Above_Sl_NonGnd_All <- "No"
}else{
Move_Above_Sl_NonGnd_All <- "Yes"
}
if(nrow(LAS_bboxTID_allSl@data) > 0 & Move_Above_Sl_NonGnd_All == "Yes"){
# GET CLOSE VOXEL SUMMARY BETWEEN NonGnd_TID and Gnd_TID
Index_Col1 <- match(c("X", "Y", "Z", "TreeID", "VoxID"), colnames(LAS_bboxTID_allSl@data))
XYZ_Above_Sl_NonGnd_All <- LAS_bboxTID_allSl@data[,..Index_Col1]
Index_Col2 <- match(c("X", "Y", "Z", "TreeID", "VoxID"), colnames(LAS_Above_oneSl_New@data))
XYZ_Above_Sl_New <- LAS_Above_oneSl_New@data[,..Index_Col2]
################################################################################################
# DIST CALC FOR ABOVE SLICE (BETWEEN VOXELS WITH ORIG TID AND THOSE THAT HAVE NOT BEEN CHANGED)
################################################################################################
Dist_Above_Sl_NonGnd_All <- VOX_SLICE_DIST_FUN(XYZ_Above_Sl_NonGnd_All,
XYZ_Above_Sl_New,
Max_Dist = 40, # Para_EDT_H,
Para_Vx_R_Res= Para_Vx_R_Res,
Slice_Thickness= Para_Sl_Z)
Dist_Above_Sl_NonGnd_All_Summary <- Dist_Above_Sl_NonGnd_All$Summary_closeSurr
if(!is.null(Dist_Above_Sl_NonGnd_All_Summary)){
if(nrow(Dist_Above_Sl_NonGnd_All_Summary) == 1){
LAS_bboxTID_allSl@data$TreeID <- as.integer(Dist_Above_Sl_NonGnd_All_Summary$TID_Surr)
}else{
#browser()
Dist_Above_Sl_NonGnd_All_VoxMatch <- Dist_Above_Sl_NonGnd_All$All_Closest_Vox_Surr_oneCl_NoDup
Index_1 <- which(LAS_bboxTID_allSl@data$VoxID %in% Dist_Above_Sl_NonGnd_All_VoxMatch$VoxID_oneCl)
Index_2 <- match(LAS_bboxTID_allSl@data$VoxID[Index_1], Dist_Above_Sl_NonGnd_All_VoxMatch$VoxID_oneCl)
LAS_bboxTID_allSl@data$TreeID[Index_1] <- as.integer(Dist_Above_Sl_NonGnd_All_VoxMatch$TID_Surr[Index_2])
}
Index_1 <- which(LAS_surrOneCl_rmOneCl_allTID@data$VoxID %in% LAS_bboxTID_allSl@data$VoxID )
Index_2 <- match(LAS_surrOneCl_rmOneCl_allTID@data$VoxID[Index_1], LAS_bboxTID_allSl@data$VoxID )
LAS_surrOneCl_rmOneCl_allTID@data$TreeID[Index_1] <- as.integer(LAS_bboxTID_allSl@data$TreeID[Index_2])
}
LAS_bboxTID_allSl <- LAS_Empty
Move_Above_Sl_NonGnd_All <- "No"
}
}
}
} # IF THERE ARE VOXELS ABOVE SLICE AND VOXELS BELOW SLICE
} # END mm Slice LOOP
return(list(LAS_surrOneCl_rmOneCl_allTID = LAS_surrOneCl_rmOneCl_allTID))
}
##########################################################################################################################################
##########################################################################################################################################
GAP_DENSITY_FUN = function(Z_Values,
Para_BW = 0.4,
Para_Threshold_Percent = 0.2,
Plot = "No",
TreeID = 1) {
pdens <- density(Z_Values, bw=Para_BW)
# FINDING PEAKS WHEN THERE ARE NO ZEROS
PEAK_DIP_FUN = function(pdens)
{
TurnPnts <- turnpoints(pdens$y)
Index <- c(1, TurnPnts$tppos, TurnPnts$n)
Z <- c(0, pdens$x [TurnPnts$tppos], max(pdens$x)) # Adding one extra turning point at start and end
Density <- c(0, pdens$y[TurnPnts$tppos], 0) # Adding one extra turning point at start and end
Peak_Dip_Summary <- t(data.frame(rbind(Z, Density, Index)))
colnames(Peak_Dip_Summary) <- c("Z", "Density", "Index")
Peak_Dip_Summary<- data.frame( Peak_Dip_Summary, Peak_Dip = rep("Dip", nrow(Peak_Dip_Summary)), stringsAsFactors = FALSE)
Peak_Dip_Summary$Peak_Dip [c(1, nrow(Peak_Dip_Summary))] <- c("Start_End", "Start_End")
#################
# PEAKS AND DIPS
#################
Y_Peak <- TurnPnts$points[TurnPnts$peaks]
X_Peak <- pdens$x[TurnPnts$pos[TurnPnts$peaks]]
Peak_Dip_Summary$Peak_Dip[match(X_Peak,Peak_Dip_Summary$Z)] <- "Peak"
Dip_DF <- Peak_Dip_Summary[which(Peak_Dip_Summary$Peak_Dip == "Dip"),]
minDip_DF <- Dip_DF[which.min(Dip_DF$Density),]
Peak_DF <- Peak_Dip_Summary[which(Peak_Dip_Summary$Peak_Dip == "Peak"),]
maxPeak_DF <- Peak_DF[which.max(Peak_DF$Density),]
Range_Den_minDip_maxPeak <- maxPeak_DF$Density - minDip_DF$Density
return(list(Peak_Dip_Summary=Peak_Dip_Summary,
minDip_DF = minDip_DF,
maxPeak_DF = maxPeak_DF,
Range_Den_minDip_maxPeak = Range_Den_minDip_maxPeak))
}
# GET Peak_Dip_Summary
Peak_Dip_Summary <- PEAK_DIP_FUN(pdens)
# GET ALL SECTIONS THAT ARE WITHIN (Para_Threshold_Percent*100)% ABOVE THE MINIMUM DIP
Den_Threshold <- Peak_Dip_Summary$minDip_DF$Density + Peak_Dip_Summary$Range_Den_minDip_maxPeak * Para_Threshold_Percent
# IF THERE ARE NO MINIMUM DIPS
if(length(Den_Threshold) == 0){
Start_Largest_Gap <- max(Peak_Dip_Summary$Peak_Dip_Summary$Z)
End_Largest_Gap <- max(Peak_Dip_Summary$Peak_Dip_Summary$Z)
}else{
Near_min_density <- which(pdens$y < Den_Threshold)
# REMOVE STRING OF LOW DENSITIES NEAR BOTTOM HEIGHT AND TOP CANOPY HEIGHT
Index_PeakDip_inMinDen <- Peak_Dip_Summary$Peak_Dip_Summary$Index[which(Peak_Dip_Summary$Peak_Dip_Summary$Index %in% Near_min_density)]
# IDENTIFY THE SECTION THAT HAS THE LARGEST STRETCH AND USE THOSE BOUNDS TO DETERMINE
# UNDER TOP AND CANOPY BASE AS FIRST PASS
# VEG_PROGILE_GAPS _WITH_LOW_LIDAR_DENSITY FUNCTION
NEAR_MIN_DENSITY_FUN = function(Near_min_density)
{
# CALCULATING WHERE THE GAP IS
Zero_Density <- rle(diff(Near_min_density))
myZero_Density <- which(Zero_Density$values == TRUE & Zero_Density$lengths > 0)
Zero_Density.lengths.cumsum <- cumsum(Zero_Density$lengths)
ends <- Zero_Density.lengths.cumsum[myZero_Density]
newindex <- ifelse(myZero_Density>1, myZero_Density-1, 0)
starts <- Zero_Density.lengths.cumsum[newindex] + 1
starts_2 <- starts
if(length(which(newindex == 0))>0){starts_2 <- c(1,starts)}
starts <- starts_2
Start_Height <- pdens$x[Near_min_density[starts]]
End_Height <- pdens$x[Near_min_density[ends]]
# REMOVING THE GAP BELOW THE GROUND
if(End_Height[1] < 0){
Start_Height <- Start_Height[-1]
End_Height <- End_Height[-1]
}
return(list(Start_Height=Start_Height,
End_Height=End_Height))
} # END NEAR_MIN_DENSITY_FUN
# GET OUTPUT
Near_Zero_Density_List <-NEAR_MIN_DENSITY_FUN(Near_min_density)
Start_Height <- Near_Zero_Density_List$Start_Height
End_Height <- Near_Zero_Density_List$End_Height
# GET LARGEST GAP WITH DENSITY CLOSE TO ZERO AND DETERMINE THE UNDER AND CANOPY BASE
Gaps <- End_Height -Start_Height
Largest_Gap <- max(Gaps)
Start_Largest_Gap <- Start_Height[which(Gaps == Largest_Gap)]
End_Largest_Gap <- End_Height[which(Gaps == Largest_Gap)]
# GET ALL PEAKS AND DIPS WITHIN FIRST PASS OF START AND END
PeakDip_Within_minDen <- Peak_Dip_Summary$Peak_Dip_Summary[which(Peak_Dip_Summary$Peak_Dip_Summary$Index %in% Index_PeakDip_inMinDen),]
# CALCULATE START OF GAP AS "FIRST MIN" WITHIN GAP
Density_Dips_Start <- PeakDip_Within_minDen$Density[ which(PeakDip_Within_minDen$Z > Start_Largest_Gap &
PeakDip_Within_minDen$Peak_Dip == "Dip")]
Start_Largest_Gap <- min(PeakDip_Within_minDen$Z[ which(PeakDip_Within_minDen$Z > Start_Largest_Gap &
PeakDip_Within_minDen$Peak_Dip == "Dip" &
PeakDip_Within_minDen$Density <= median(Density_Dips_Start))])
# CALCULATE END OF GAP AS "LAST MIN" WITHIN GAP
Density_Dips_Ends <- PeakDip_Within_minDen$Density[ which(PeakDip_Within_minDen$Z < End_Largest_Gap &
PeakDip_Within_minDen$Peak_Dip == "Dip")]
End_Largest_Gap <- max(PeakDip_Within_minDen$Z[which(PeakDip_Within_minDen$Z < End_Largest_Gap &
PeakDip_Within_minDen$Peak_Dip == "Dip" &
PeakDip_Within_minDen$Density <= median(Density_Dips_Ends))])
}
if(Plot == "Yes"){
plot(pdens$x, pdens$y, type="l", ylim=c(0, 0.6),
main= paste( "F:", TreeID),
ylab = "Density",
xlab = "Height (m)",
cex.lab = 1.5,
cex.axis = 2)
abline(v = Start_Largest_Gap, col="blue", lwd = 3)
abline(v = End_Largest_Gap, col="dark green", lwd = 3)
abline(h = Den_Threshold, col= "red", lwd = 2)
legend("topright", legend = c("Threshold Height", "Bottom Minima", "Top Minima"),
lty= 1, col=c("red", "blue", "dark green"), lwd=3,
cex= 1.7)
}
return(list(Start_Largest_Gap=Start_Largest_Gap,
End_Largest_Gap=End_Largest_Gap,
Peak_Dip_Summary = Peak_Dip_Summary))
}
##########################################################################################################################################
##########################################################################################################################################
# LOCAL MAX Focal function With Voronoi Assessment around maximas
VORONOI_FOCAL_FUN = function(Raster_Layer, Win_row=15, Win_col=15, min_or_max)
{
# MAX VALUE WITHIN SEARCH WINDOW
if(min_or_max == "max"){
f <- function(X) max(X, na.rm=TRUE)
}else{
f <- function(X) min(X, na.rm=TRUE)
}
# MAX HEIGHT (CHM) IN SEARCH WINDOW
ww <- matrix(1, nrow=Win_row, ncol=Win_col)
CHM_localmax <- focal(Raster_Layer, fun=f, w=ww, pad=TRUE, padValue=NA)
# LOCAL MAXIMAS (CELL XY COORDS)
Local_Max <- Raster_Layer==CHM_localmax
maxXY <- xyFromCell(Local_Max, Which(Local_Max==1, cells=TRUE))
maxZvalue <- raster::extract(Raster_Layer, maxXY)
# GETTING VORONOI OF EACH MAXIMA AND GETTING MEDIAN HEIGHT WITHIN VORONOI
maxXY_ID <- data.frame(maxXY, ID = seq(1, nrow(maxXY), 1))
coordinates(maxXY_ID) = ~x+y
# browser()
Raster_Layer_BoundPoly <- as(extent(Raster_Layer), 'SpatialPolygons')
Maxima_Vori_Poly <- voronoi.polygons(maxXY_ID, Raster_Layer_BoundPoly)
Median_Height_MaximLoc_VoriPoly <- raster::extract(Raster_Layer, Maxima_Vori_Poly,
fun = median,
na.rm = TRUE)
# SMALL WORK AROUND FOR CORNER CASE WITH VALUE Inf ...
Gap_VoriPoly <- raster::extract(Raster_Layer, Maxima_Vori_Poly,
fun = function(x,...)GAP_DENSITY_FUN(na.omit(x), Para_BW = 0.4),
na.rm = TRUE)
# OUTPUT DF
max_XYZ_MedianVoriPoly <- data.frame(maxXY, maxZvalue, Median_Height_MaximLoc_VoriPoly, data.frame(Gap_VoriPoly[,1:2]))
return(list(max_XYZ_MedianVoriPoly=max_XYZ_MedianVoriPoly, Maxima_Vori_Poly=Maxima_Vori_Poly))
}
##########################################################################################################################################
##########################################################################################################################################
VOXEL_FUN <- function(LAS_XYZ_ID, Para_Vox_Res = 1)
{
# DETERMINING WHICH VOXEL EACH TID GOES INTO
Vox_XYZ <- voxel_metrics(LAS_XYZ_ID, list(PID), res = Para_Vox_Res) # Assign XYZ of Voxel to each Voxel
colnames(Vox_XYZ) <- c("X", "Y", "Z", "PID")
Vox_XYZ_Collapse <-Vox_XYZ[,list(PID = paste(unique(as.numeric(PID)),collapse = ',')),by = c("X", "Y", "Z")] # Gives each voxel one row and Points clisted in PID
# browser()
Vox_XYZ_Collapse <- cbind(VoxID=seq(1, nrow(Vox_XYZ_Collapse), 1), Vox_XYZ_Collapse) # GIVES EACH VOXEL A UNIQUE IDENTIFYER
# Create vector of points ordered in same order as VoxID
Vox_XYZ_Collapse_PointID <-paste(Vox_XYZ_Collapse$PID, ",")
Vox_XYZ_Collapse_PointID <-unlist(strsplit(Vox_XYZ_Collapse_PointID, ","))
Vox_XYZ_Collapse_PointID <-as.numeric(Vox_XYZ_Collapse_PointID)
# CALCULATING HOW MANY POINTS IN EACH VOXEL
Vox_XYZ_Point_Count <- voxel_metrics(LAS_XYZ_ID, length(Z), res = Para_Vox_Res)
colnames(Vox_XYZ_Point_Count)[ncol(Vox_XYZ_Point_Count)] <- "Point_Count_In_Vox"
# browser()
Vox_XYZ_Point_Count <- cbind(Vox_XYZ_Point_Count, PID= Vox_XYZ_Collapse$PID, VoxID=Vox_XYZ_Collapse$VoxID)
# CREATING VECTOR THE LENGTH OF POINTS WITH VoxID FOR EACH POINT
Vox_XYZ_Expand <- unlist(mapply(rep, Vox_XYZ_Collapse$VoxID, Vox_XYZ_Point_Count$Point_Count_In_Vox))
if(class(Vox_XYZ_Expand)[1] == "matrix"){Vox_XYZ_Expand <-as.vector(Vox_XYZ_Expand)}
#ASSIGNING EACH POINT TO VOXEL and PUT VOX ID INTO STEM LAS FILE
VoxID_TID_df <- data.frame(PID=Vox_XYZ_Collapse_PointID, VoxID=Vox_XYZ_Expand)
LAS_XYZ_ID@data$VoxID[match(VoxID_TID_df$PID,LAS_XYZ_ID@data$PID)] <- VoxID_TID_df$VoxID
Output <- list(LAS_XYZ_ID, Vox_XYZ_Point_Count, VoxID_TID_df)
}
##########################################################################################################################################
##########################################################################################################################################
# CONVEX HULL FUNCTION (GET TREE POLYGON)
CONVHULL_FUN = function(X, Y, TID)
{
CHull = chull(X, Y)
x_hul = X[CHull]
y_hul = Y[CHull]
Poly = Polygons(list(Polygon(cbind(x_hul,y_hul))), TID[1])
return(list(X_Hull = list(x_hul), Y_Hull = list(y_hul), Poly = list(Poly)))
}
##########################################################################################################################################################################################
###################################################################################################################################################################################
GET_TREE_ATTRIBUTES_FUN = function(X, Y, Z, Classify, Vox_PntCnt, Slice_Size_Zaxis, Unique_Poly_ID = 0)
{
# BINNING TREE
if((length(seq(min(Z), max(Z),Slice_Size_Zaxis))-1) >1) { # THIS IF STATEMENT GETS AROUND TREE HAVING ONE BIN
Slice_Height <- cut(Z, seq(min(Z), max(Z),Slice_Size_Zaxis),labels=seq(min(Z), max(Z),Slice_Size_Zaxis)[-length(seq(min(Z), max(Z),Slice_Size_Zaxis))]) # labels=1:(length(seq(min(Z), max(Z),Slice_Size_Zaxis))-1))
}else{
Slice_Height <- rep(1, length(Z))
}
Slices_ID_DF <-na.omit(data.frame(X,Y,Z,Vox_PntCnt,Slice_Height))
Unique_BIN_ID <- sort(as.numeric(unique(Slices_ID_DF$Slice_Height)))
# Splitting data.frame into lists for each bin
splits <- split(Slices_ID_DF, Slices_ID_DF$Slice_Height)
Count_Splits <- length(sapply(splits, NROW))
if(length(splits) != length(Unique_BIN_ID)){ # WORK AROUND WHEN THERE IS AN EMPTY LIST
splits <- splits[- which(as.vector(sapply(splits, NROW)) ==0)]
}
# GET POLYGON OF EACH BIN AND AREA IT REPRESENTS
Raster_Res <- 0.2
Polygon_Slices <- lapply(splits, function(x) {
# LAS SLICE
LAS_oneSt_oneSl <- LAS(x)
# RASTERISING THE SLICE LAS COUNT, SLICE
if(nrow(LAS_oneSt_oneSl@data)>1){
R_oneSl_ZCount <- grid_metrics(LAS_oneSt_oneSl, ~sum(Vox_PntCnt), res = Raster_Res)
if(ncell(R_oneSl_ZCount) > 1){
algo = lidR::watershed(R_oneSl_ZCount, tol = 1, ext=5)
LAS_oneSt_oneSl <- segment_trees(LAS_oneSt_oneSl, algorithm = algo, attribute = "WSID", uniqueness = "incremental")
R_WS <- algo()
Unique_WS <- as.numeric(names(table(LAS_oneSt_oneSl@data$WSID)))
Unique_WS <- Unique_WS[which(Unique_WS > 0)]
if(length(Unique_WS) > 0){
WS_Pnt_Count <- as.vector(table(LAS_oneSt_oneSl@data$WSID))
WS_Grid_Count <-as.vector(table(getValues(R_WS)))
# IF SOME WS GRIDS DON'T HAVE LAS POINTS (SLIGHT OFFSET OR ON EDGE DOESN'T REGISTER)
if(length(WS_Pnt_Count) != length(WS_Grid_Count)){
Remove_Length <- length(WS_Grid_Count) - length(WS_Pnt_Count)
Remove_Value <- sort(WS_Grid_Count)[Remove_Length]
Remove_Index <- which(WS_Grid_Count %in% Remove_Value)[Remove_Length]
WS_Grid_Count <- WS_Grid_Count[-Remove_Index]
}
# STACK THE RASTERS
# WORK AROUND FOR WHEN THE ROWS AND COLS ARE BUGGY (FLIPPED FOR UNKNOWN REASON)
if((R_WS@ncols == R_oneSl_ZCount@ncols)&(R_WS@nrows == R_oneSl_ZCount@nrows)){
R_Stack <- stack(R_oneSl_ZCount, R_WS) # R_oneSl_ClID,
}else{
R_WS_Nrows <- R_WS@nrows
R_WS_Ncols <- R_WS@ncols
R_WS@ncols <- R_WS_Nrows
R_WS@nrows <- R_WS_Ncols
R_Stack <- stack(R_oneSl_ZCount, R_WS) # R_oneSl_ClID,
}
R_Stack_DF <- data.frame(Cell_ID = seq(1, ncell(R_oneSl_ZCount),1) , getValues(R_Stack))
colnames(R_Stack_DF) <- c("Cell_ID",
"ZCount",
"WS")
R_Stack_DF <- data.frame(sapply(R_Stack_DF, function(x) as.numeric(as.character(x))))
# LOCATE MAX DENSITY CELL_ID FOR EACH WS
Cell_ID_maxDen <- as.data.frame(R_Stack_DF %>%
dplyr::select(Cell_ID, ZCount, WS) %>%
dplyr::group_by(WS) %>%
dplyr::summarise(Cell_ID_maxDen = Cell_ID[which.max(ZCount)][1], .groups = 'drop'))
Cell_ID_maxDen <- na.omit(Cell_ID_maxDen)
if(nrow(Cell_ID_maxDen) > 0){
Cell_ID_maxDen <- Cell_ID_maxDen[which(Cell_ID_maxDen$WS %in% Unique_WS),]
XY_WS_MaxDen <- xyFromCell(R_WS, Cell_ID_maxDen$Cell_ID_maxDen, spatial=FALSE)
# GET POLYGON OF EACH WS
Poly_oneSl_allPoly <- SpatialPolygons(list(), proj4string = CRS(Proj_Sys))
PolyDiamMax_oneSt_oneSl <- c()
PolyDiamMin_oneSt_oneSl <- c()
for(UWS in 1:length(Unique_WS)){
Unique_Poly_ID <- Unique_Poly_ID + 1
LAS_oneSt_oneSl_onePoly <- filter_poi(LAS_oneSt_oneSl, WSID == Unique_WS[UWS])
# WHOLE SLICE WS
Slice_CHull = CONVHULL_FUN(LAS_oneSt_oneSl_onePoly@data$X,
LAS_oneSt_oneSl_onePoly@data$Y,
Unique_Poly_ID)
PolyDiamMax_oneSt_oneSl = c(PolyDiamMax_oneSt_oneSl, max(dist(cbind(unlist(Slice_CHull$X_Hull),unlist(Slice_CHull$Y_Hull)))))
Poly_SP_oneSl_onePoly <- SpatialPolygons(Slice_CHull$Poly, proj4string = CRS(Proj_Sys))
CoordPoly_oneSl_onePoly <- Poly_SP_oneSl_onePoly@polygons[[1]]@Polygons[[1]]@coords
Poly_oneSl_allPoly <- SpatialPolygons(c(slot(Poly_oneSl_allPoly,
"polygons"), Slice_CHull$Poly))
}
Poly_oneSl_allPolyArea <- unlist(lapply(Poly_oneSl_allPoly@polygons, function(x) slot(x, "area")))
# MIN DIAMETER IS CALCULATED USING MAX DIAMETERS AND AREA IN AN ELIPSOID CALCULATION
PolyDiamMin_oneSt_oneSl =Poly_oneSl_allPolyArea/as_units((pi * PolyDiamMax_oneSt_oneSl/2)*2, "m^2")
Poly_oneSl_allPolyArea_Total <- sum(Poly_oneSl_allPolyArea)
}else{
Poly_oneSl_allPoly <- SpatialPolygons(list())
Poly_oneSl_allPolyArea <- 0
Poly_oneSl_allPolyArea_Total <- 0
PolyDiamMax_oneSt_oneSl <- 0
PolyDiamMin_oneSt_oneSl <- 0
XY_WS_MaxDen <- 0
WS_Pnt_Count <- 0
WS_Grid_Count <- 0
}
}else{
Poly_oneSl_allPoly <- SpatialPolygons(list())
Poly_oneSl_allPolyArea <- 0
Poly_oneSl_allPolyArea_Total <- 0
PolyDiamMax_oneSt_oneSl <- 0
PolyDiamMin_oneSt_oneSl <- 0
XY_WS_MaxDen <- 0
WS_Pnt_Count <- 0
WS_Grid_Count <- 0
}
}else{
Poly_oneSl_allPoly <- SpatialPolygons(list())
Poly_oneSl_allPolyArea <- 0
Poly_oneSl_allPolyArea_Total <- 0
PolyDiamMax_oneSt_oneSl <- 0
PolyDiamMin_oneSt_oneSl <- 0
XY_WS_MaxDen <- 0
WS_Pnt_Count <- 0
WS_Grid_Count <- 0
}
}else{
Poly_oneSl_allPoly <- SpatialPolygons(list())
Poly_oneSl_allPolyArea <- 0
Poly_oneSl_allPolyArea_Total <- 0
PolyDiamMax_oneSt_oneSl <- 0
PolyDiamMin_oneSt_oneSl <- 0
XY_WS_MaxDen <- 0
WS_Pnt_Count <- 0
WS_Grid_Count <- 0
}
list(Poly_oneSl_allPoly = Poly_oneSl_allPoly,
Poly_oneSl_allPolyArea = Poly_oneSl_allPolyArea,
Poly_oneSl_allPolyArea_Total = Poly_oneSl_allPolyArea_Total,
PolyDiamMax_oneSt_oneSl = PolyDiamMax_oneSt_oneSl,
PolyDiamMin_oneSt_oneSl = PolyDiamMin_oneSt_oneSl,
XY_WS_MaxDen = XY_WS_MaxDen,
WS_Pnt_Count = WS_Pnt_Count,
WS_Grid_Count = WS_Grid_Count)
}) # END POLYGON FUNCTION
########################
# EACH WS POLYGON OUTPUT
########################
Centroids_allSl_allPoly <- unlist(sapply(Polygon_Slices,function(x) x[6]), use.names = FALSE)
# ID SLICES THAT WERE NOT COMPUTED DUE TO RASTER COUNT IN WS CALCULATION
Centroids_allSl_allPoly_X <- Centroids_allSl_allPoly[which(Centroids_allSl_allPoly < 5000000)]
Zero_Centroid<- which(Centroids_allSl_allPoly_X == 0)
#browser()
if(length(Zero_Centroid) >0){
Centroids_allSl_allPoly_X <- Centroids_allSl_allPoly_X[-Zero_Centroid]
Centroids_allSl_allPoly_Y <- Centroids_allSl_allPoly[which(Centroids_allSl_allPoly > 5000000)]
Centroids_allSl_allPoly <-Centroids_allSl_allPoly [which(Centroids_allSl_allPoly == 0)]
AreaPoly_allSl_allPoly <- unlist(sapply(Polygon_Slices,function(x) x[2]) , use.names = TRUE)
AreaPoly_allSl_allPoly_Height <-str_extract(names(AreaPoly_allSl_allPoly), "\\-*\\d+\\.*\\d*")[-Zero_Centroid]
AreaPoly_allSl_allPoly <- as.vector(AreaPoly_allSl_allPoly)[-Zero_Centroid]
DiamMax_allSl_allPoly <- unlist(sapply(Polygon_Slices,function(x) x[4]), use.names = FALSE)[-Zero_Centroid]
DiamMin_allSl_allPoly <- unlist(sapply(Polygon_Slices,function(x) x[5]), use.names = FALSE)[-Zero_Centroid]
WSPntCount_allSl_allPoly <- unlist(sapply(Polygon_Slices,function(x) x[7]), use.names = FALSE) [-Zero_Centroid]
WSGridCount_allSl_allPoly <- unlist(sapply(Polygon_Slices,function(x) x[8]), use.names = FALSE) [-Zero_Centroid]
Poly_eachWS <- unlist(sapply(Polygon_Slices,function(x) x[1]), use.names = TRUE)[-Zero_Centroid]
}else { # ALL WS RASTERS HAVE BEEN COMPUTED SO NO SLICES NEED REMOVAL
Centroids_allSl_allPoly_X <- Centroids_allSl_allPoly_X
Centroids_allSl_allPoly_Y <- Centroids_allSl_allPoly[which(Centroids_allSl_allPoly > 5000000)]
Centroids_allSl_allPoly <-Centroids_allSl_allPoly
AreaPoly_allSl_allPoly <- unlist(sapply(Polygon_Slices,function(x) x[2]) , use.names = TRUE)
AreaPoly_allSl_allPoly_Height <-str_extract(names(AreaPoly_allSl_allPoly), "\\-*\\d+\\.*\\d*")
AreaPoly_allSl_allPoly <- as.vector(AreaPoly_allSl_allPoly)
DiamMax_allSl_allPoly <- unlist(sapply(Polygon_Slices,function(x) x[4]), use.names = FALSE)
DiamMin_allSl_allPoly <- unlist(sapply(Polygon_Slices,function(x) x[5]), use.names = FALSE)
WSPntCount_allSl_allPoly <- unlist(sapply(Polygon_Slices,function(x) x[7]), use.names = FALSE)
WSGridCount_allSl_allPoly <- unlist(sapply(Polygon_Slices,function(x) x[8]), use.names = FALSE)
Poly_eachWS <- unlist(sapply(Polygon_Slices,function(x) x[1]), use.names = TRUE)
AreaSl_allSl_allPoly <- unlist(sapply(Polygon_Slices,function(x) x[3]) , use.names = TRUE)
AreaSl_allSl_allPoly_Height <-str_extract(names(AreaSl_allSl_allPoly), "\\-*\\d+\\.*\\d*")
AreaSl_allSl_allPoly <- as.vector(AreaSl_allSl_allPoly)
}
Output_eachWS <- data.frame( TreeID = rep(Classify, length(AreaPoly_allSl_allPoly_Height)),
PolyHeight = as.numeric(as.character(AreaPoly_allSl_allPoly_Height)),
PolyArea = AreaPoly_allSl_allPoly,
PolyVolume = AreaPoly_allSl_allPoly * Slice_Size_Zaxis,
DiamMax = DiamMax_allSl_allPoly,
DiamMin = DiamMin_allSl_allPoly,
WS_PntCount = WSPntCount_allSl_allPoly,
WSGrid_Count = WSGridCount_allSl_allPoly,
WSGrid_Volume = WSGridCount_allSl_allPoly * (Raster_Res*Raster_Res*Slice_Size_Zaxis),
X_Cent = Centroids_allSl_allPoly_X,
Y_Cent = Centroids_allSl_allPoly_Y)
Points_Centroids_XYZ <- Output_eachWS[, match(c("X_Cent", "Y_Cent", "PolyHeight", "TreeID"), colnames(Output_eachWS))]
coordinates(Points_Centroids_XYZ) = ~X_Cent + Y_Cent
crs(Points_Centroids_XYZ) <- crs(Proj_Sys)
for(a in 1:length(Poly_eachWS)){
if(a == 1){
Poly_eachWS_All <- Poly_eachWS[[a]]
PolyID_Max <- max(as.numeric(sapply(slot(Poly_eachWS_All, "polygons"), function(x) slot(x, "ID"))))
}else{
Poly_oneWS <-Poly_eachWS[[a]]
new_IDs = as.numeric(sapply(slot(Poly_oneWS, "polygons"), function(x) slot(x, "ID"))) + PolyID_Max
if(length(new_IDs) > 0){
for (i in 1:length(slot(Poly_oneWS, "polygons"))){
slot(slot(Poly_oneWS, "polygons")[[i]], "ID") = as.character(new_IDs[i])
}
Poly_eachWS_All <- rbind(Poly_eachWS_All, Poly_oneWS)
PolyID_Max <- max(as.numeric(sapply(slot(Poly_eachWS_All, "polygons"), function(x) slot(x, "ID"))))
}else{
# WORK AROUND FOR WHEN A Poly_eachWS IS EMPTY AND YOU NEED TO REMOVE A ROW FROM THE OUTPUT DF TO MATCH IT
Remove_Output_Row <- Output_eachWS[which(Output_eachWS$PolyHeight == unique(Output_eachWS$PolyHeight)[a]),]
Remove_Output_Row_Area <- Remove_Output_Row$PolyArea[which(Remove_Output_Row$PolyArea == min(Remove_Output_Row$PolyArea))]
Index_Remove_Output_Row <- which(Output_eachWS$PolyHeight == unique(Output_eachWS$PolyHeight)[a] &
Output_eachWS$PolyArea == Remove_Output_Row_Area)
if(length(Index_Remove_Output_Row) > 0){
Output_eachWS <- Output_eachWS[-Index_Remove_Output_Row,]
}
}
}
}
rname_eachWS <- row.names(Output_eachWS)
rname_Poly <- sapply(slot(Poly_eachWS_All, "polygons"), function(x) slot(x, "ID"))
setdiff_RowNames_Output_eachWS_missing <- setdiff(rname_eachWS, rname_Poly)
if(length(setdiff_RowNames_Output_eachWS_missing) > 0 ){
Index_Remove1 <- which( rname_eachWS %in% setdiff_RowNames_Output_eachWS_missing)
Output_eachWS <- Output_eachWS[-Index_Remove1,]
}
setdiff_RowNames_Poly_eachWS_All_missing <- setdiff(rname_Poly, rname_eachWS)
if(length(setdiff_RowNames_Poly_eachWS_All_missing) > 0 ){
Index_Remove2 <- which( rname_Poly %in% setdiff_RowNames_Poly_eachWS_All_missing)
Poly_eachWS_All <- Poly_eachWS_All[-Index_Remove2,]
}
Poly_eachWS_All_SPDF <- SpatialPolygonsDataFrame(Poly_eachWS_All, Output_eachWS)
# OUTPUT
return(list(Points_Centroids_XYZ = list(Points_Centroids_XYZ),
Poly_eachWS_All_SPDF = list(Poly_eachWS_All_SPDF)))