-
Notifications
You must be signed in to change notification settings - Fork 1
/
ALPA_main.R
8271 lines (8271 loc) · 236 KB
/
ALPA_main.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
#
# ###################################################################### #
# ###################################################################### #
# #
# ALPA #
# #
# An R-Script for for automatic analysis of landslide path #
# #
# Version 4.4 #
# #
# February 24th, 2024 #
# #
# Langping LI, Hengxing LAN #
# #
# LREIS, IGSNRR, CAS #
# #
# Email: [email protected], [email protected] #
# #
# ###################################################################### #
# ###################################################################### #
#
#
#
# ########## commonly it is NOT SUGGESTED to change this script ##########
#
#
#
ALPA <- function(FileDEM, FileLandslides, FileParameters = 0, CfgEndAnchorsInt = 1, CfgEndAnchorsDit = 1, CfgEndStripsInt = 1, CfgEndStripsDit = 1, OutputHrcl = F) {
#
# get raster
raster_dem <- raster::raster(FileDEM)
# get CRS
raster_CRS <- sp::proj4string(raster_dem)
#
# import vector layer
shp_lasld <- rgdal::readOGR(FileLandslides)
#
# get polygons
list_polygons <- shp_lasld@polygons
#
# get count
count_polygons <- length(list_polygons)
#
# get Parameters
if (FileParameters != 0) {
#
# read xlsx
paras <- read.xlsx(FileParameters, 1, header = TRUE)
}
#
# get EndAnchorsInt
if (is.character(CfgEndAnchorsInt)) {
#
# read shapefile
shp_EndAnchorsInt <- rgdal::readOGR(CfgEndAnchorsInt)
#
# get coords
coords_EndAnchorsInt <- shp_EndAnchorsInt@coords
}
#
# get EndAnchorsDit
if (is.character(CfgEndAnchorsDit)) {
#
# read shapefile
shp_EndAnchorsDit <- rgdal::readOGR(CfgEndAnchorsDit)
#
# get coords
coords_EndAnchorsDit <- shp_EndAnchorsDit@coords
}
#
# get EndStripsInt
if (is.character(CfgEndStripsInt)) {
#
# read shapefile
shp_EndStripsInt <- rgdal::readOGR(CfgEndStripsInt)
#
# get lines
list_lines <- shp_EndStripsInt@lines
#
# get count
count_lines <- length(list_lines)
#
# initial
directions_EndStripsInt <- c()
#
# get directions
for (i in 1:count_lines) {
#
# get Lines
pLines <- list_lines[[i]]
#
# get coords
coords <- pLines@Lines[[1]]@coords
#
# get direction
derection <- atan2(coords[2, 2]-coords[1, 2], coords[2, 1]-coords[1, 1])
#
# debugging
if (derection < 0) {
#
# positive
derection <- derection + pi * 2
}
#
# append
directions_EndStripsInt <- rbind(directions_EndStripsInt, derection)
}
}
#
# get EndStripsDit
if (is.character(CfgEndStripsDit)) {
#
# read shapefile
shp_EndStripsDit <- rgdal::readOGR(CfgEndStripsDit)
#
# get lines
list_lines <- shp_EndStripsDit@lines
#
# get count
count_lines <- length(list_lines)
#
# initial
directions_EndStripsDit <- c()
#
# get directions
for (i in 1:count_lines) {
#
# get Lines
pLines <- list_lines[[i]]
#
# get coords
coords <- pLines@Lines[[1]]@coords
#
# get direction
derection <- atan2(coords[2, 2]-coords[1, 2], coords[2, 1]-coords[1, 1])
#
# debugging
if (derection < 0) {
#
# positive
derection <- derection + pi * 2
}
#
# append
directions_EndStripsDit <- rbind(directions_EndStripsDit, derection)
}
}
#
# initial list_spldf_profile
list_spldf_profile <- list()
#
# get file
file_out_profile <- paste(gsub(".shp", "", FileLandslides), "_paths2d", sep = "", collapse = NULL)
file_out_xlsx <- paste(gsub(".shp", "", FileLandslides), "_paths2d.xlsx", sep = "", collapse = NULL)
#
# split
for (i in 1:count_polygons) {
#
# get time
time_lasld_start <- Sys.time()
# #
# show progress
# cat( paste("Process the ", as.character(i), "/", as.character(count_polygons), " landslide start.", "\n", sep = "") )
#
# get Polygons
pPolygons <- list_polygons[[i]]
#
# get Parameters
if (FileParameters == 0) {
#
# get Parameters, default
pFilePrefix <- paste("lsd", sprintf("%06d", i), sep = "", collapse = NULL)
pMinGrpPntCnt <- 3
pMinGrpAcrDst <- 0
pMinStpHrzLen <- 30
pMinEndRtoInt <- 0.00
pMinEndRtoDit <- 0.00
#
# get Name
iName <- pFilePrefix
}
else {
#
# get Parameters
pFilePrefix <- paras[i, 1]
pMinGrpPntCnt <- paras[i, 2]
pMinGrpAcrDst <- paras[i, 3]
pMinStpHrzLen <- paras[i, 4]
pMinEndRtoInt <- paras[i, 5]
pMinEndRtoDit <- paras[i, 6]
#
# get Name
iName <- pFilePrefix
}
#
# get EndAnchorsInt
if (is.character(CfgEndAnchorsInt)) {
#
pEndAnchorsInt <- c(coords_EndAnchorsInt[i, 1], coords_EndAnchorsInt[i, 2])
}
else {
#
# debugging
if (CfgEndAnchorsInt != 1 && CfgEndAnchorsInt != 2 && CfgEndAnchorsInt != 3) {
#
# message
message("Error: wrong input of CfgEndAnchorsInt.")
}
#
# negative, to distinguish from direction values.
pEndAnchorsInt <- -CfgEndAnchorsInt
}
#
# get EndAnchorsDit
if (is.character(CfgEndAnchorsDit)) {
#
pEndAnchorsDit <- c(coords_EndAnchorsDit[i, 1], coords_EndAnchorsDit[i, 2])
}
else {
#
# debugging
if (CfgEndAnchorsDit != 1 && CfgEndAnchorsDit != 2 && CfgEndAnchorsDit != 3) {
#
# message
message("Error: wrong input of CfgEndAnchorsDit.")
}
#
# negative, to distinguish from direction values.
pEndAnchorsDit <- -CfgEndAnchorsDit
}
#
# get EndStripsInt
if (is.character(CfgEndStripsInt)) {
#
pEndStripsInt <- directions_EndStripsInt[i, ]
}
else {
#
# debugging
if (CfgEndStripsInt != 1 && CfgEndStripsInt != 2 && CfgEndStripsInt != 3) {
#
# message
message("Error: wrong input of CfgEndStripsInt.")
}
#
# negative, to distinguish from direction values.
pEndStripsInt <- -CfgEndStripsInt
}
#
# get EndStripsDit
if (is.character(CfgEndStripsDit)) {
#
pEndStripsDit <- directions_EndStripsDit[i, ]
}
else {
#
# debugging
if (CfgEndStripsDit != 1 && CfgEndStripsDit != 2 && CfgEndStripsDit != 3) {
#
# message
message("Error: wrong input of CfgEndStripsDit.")
}
#
# negative, to distinguish from direction values.
pEndStripsDit <- -CfgEndStripsDit
}
#
# update FilePrefix
pFilePrefix <- paste(pFilePrefix, "_c", sprintf("%03d", pMinGrpPntCnt), sep = "", collapse = NULL)
pFilePrefix <- paste(pFilePrefix, "_d", sprintf("%03d", pMinGrpAcrDst), sep = "", collapse = NULL)
pFilePrefix <- paste(pFilePrefix, "_s", sprintf("%03d", pMinStpHrzLen), sep = "", collapse = NULL)
# update FilePrefix
pFilePrefix <- paste(pFilePrefix, "_eri", sprintf("%.4f", pMinEndRtoInt), sep = "", collapse = NULL)
pFilePrefix <- paste(pFilePrefix, "_erd", sprintf("%.4f", pMinEndRtoDit), sep = "", collapse = NULL)
# update FilePrefix
if (is.character(CfgEndAnchorsInt)) { pFilePrefix <- paste(pFilePrefix, "_eai", sprintf("%f", pEndAnchorsInt[1]), "&", sprintf("%f", pEndAnchorsInt[2]), sep = "", collapse = NULL) }
else { pFilePrefix <- paste(pFilePrefix, "_eai", sprintf("%01d", abs(pEndAnchorsInt)), sep = "", collapse = NULL) }
if (is.character(CfgEndAnchorsDit)) { pFilePrefix <- paste(pFilePrefix, "_ead", sprintf("%f", pEndAnchorsDit[1]), "&", sprintf("%f", pEndAnchorsDit[2]), sep = "", collapse = NULL) }
else { pFilePrefix <- paste(pFilePrefix, "_ead", sprintf("%01d", abs(pEndAnchorsDit)), sep = "", collapse = NULL) }
# update FilePrefix
if (is.character(CfgEndStripsInt)) { pFilePrefix <- paste(pFilePrefix, "_esi", sprintf("%f", pEndStripsInt * 180 / pi), sep = "", collapse = NULL) }
else { pFilePrefix <- paste(pFilePrefix, "_esi", sprintf("%01d", abs(pEndStripsInt)), sep = "", collapse = NULL) }
if (is.character(CfgEndStripsDit)) { pFilePrefix <- paste(pFilePrefix, "_esd", sprintf("%f", pEndStripsDit * 180 / pi), sep = "", collapse = NULL) }
else { pFilePrefix <- paste(pFilePrefix, "_esd", sprintf("%01d", abs(pEndStripsDit)), sep = "", collapse = NULL) }
#
# split using one polygons
spldf_profile <- f_lasld_split(raster_dem, pPolygons, iName, pFilePrefix, pMinGrpPntCnt, pMinGrpAcrDst, pMinStpHrzLen, pMinEndRtoInt, pMinEndRtoDit, pEndAnchorsInt, pEndAnchorsDit, pEndStripsInt, pEndStripsDit, OutputHrcl)
#
# update list
if (is.null(spldf_profile) == F) {
#
# append list
list_spldf_profile[[length(list_spldf_profile)+1]] <- spldf_profile
#
# list2shp
data_df <- f_save_list2spldf(list_spldf_profile, raster_CRS, file_out_profile)
#
# save xlsx
f_pnts_save_xls(data_df, file_out_xlsx)
#
# show progress
cat( paste("Process the ", as.character(i), "/", as.character(count_polygons), " landslide done.", "\n", sep = "") )
}
else {
#
# show progress
cat( paste("Process the ", as.character(i), "/", as.character(count_polygons), " landslide failed.", "\n", sep = "") )
}
#
# get time
time_lasld_end <- Sys.time()
#
# show time
cat(paste(as.character(as.numeric(time_lasld_end - time_lasld_start, units = "secs")), " seconds used.", "\n", sep = "", collapse = NULL))
}
}
#
#
#
f_lasld_split <- function(pRasterDEM, pPolygons, iName, FilePrefix, MinGrpPntCnt, MinGrpAcrDst, MinStpHrzLen, MinEndRtoInt, MinEndRtoDit, EndAnchorsInt, EndAnchorsDit, EndStripsInt, EndStripsDit, OutputHrcl) {
# #
# # show step
# time_step_start <- f_show_step(paste("Read pnts start.", "\n", sep = ""))
#
# initial
spldf_profile <- NULL
#
# get CRS
raster_CRS <- sp::proj4string(pRasterDEM)
#
# get cell size
raster_cellsize <- (raster::res(pRasterDEM))[1]
#
# get SpatialPolygons
pSpatialPolygons <- sp::SpatialPolygons(list(pPolygons), proj4string = sp::CRS(raster_CRS))
#
# rasterize polygon
list_polygon_rasterized <- f_polygon_rasterize(pRasterDEM, pSpatialPolygons)
#
# get
shp_lasld <- list_polygon_rasterized[[1]]
# plot(shp_lasld, main = "polygon", axes = TRUE, border = "blue")
#
# set possible file_out (for the line of rasterized landslide plolygon)
file_out <- paste(FilePrefix, "_lsd_pln2d", sep = "", collapse = NULL)
#
# get
pRasterDEM_masked <- list_polygon_rasterized[[2]]
#
# get
spxdf_lasld <- as(pRasterDEM_masked, "SpatialPixelsDataFrame")
#
# get pnts
pnts <- f_pnts_read(pRasterDEM_masked, shp_lasld, file_out)
#
# if read pnts fail
if (max(pnts[, "IDB"]) == 0) {
#
# message
str_failed <- paste("Error: Read landslide pnts failed.", sep = "", collapse = NULL)
message(str_failed)
#
# save pnts
file_out <- paste(FilePrefix, "_pnts.xlsx", sep = "", collapse = NULL)
f_pnts_save_xls(pnts, file_out)
#
# save shp
file_out <- paste(FilePrefix, "_pnts", sep = "", collapse = NULL)
f_pnts_save_points(pnts, raster_CRS, file_out)
#
# return
return(NULL)
}
# #
# # show step time
# f_show_time(paste("Read pnts done.", "\n", sep = ""), time_step_start)
#
# save the original pnts
if (T) {
#
# save pnts
file_out <- paste(FilePrefix, "_pnts.xlsx", sep = "", collapse = NULL)
f_pnts_save_xls(pnts, file_out)
#
# save shp
file_out <- paste(FilePrefix, "_pnts", sep = "", collapse = NULL)
f_pnts_save_points(pnts, raster_CRS, file_out)
}
#
# initial pnts_split
pnts_split <- pnts
#
# if normal is vertical (horizontal plane)
if (pnts_split[1, "Dx"] == 0 && pnts_split[1, "Dy"] == 0 && pnts_split[1, "Dz"] == 0) {
#
# message, if when reading pnts
message("Error: The first fitted plane for the landslide is horizontal.")
#
# set SMS, to be "LSH" (horizontal landslide)
pnts_split[, "SMS"] <- "LSH"
#
# message
message("Warning: SMS is LSH")
#
# save pnts
file_out <- paste(FilePrefix, "_pnts_grp.xlsx", sep = "", collapse = NULL)
f_pnts_save_xls(pnts_split, file_out)
#
# save shp
file_out <- paste(FilePrefix, "_pnts_grp", sep = "", collapse = NULL)
f_pnts_save_points(pnts_split, raster_CRS, file_out)
#
# return
return(NULL)
}
#
# initial list_pnts
list_pnts <- list(pnts_split)
#
# initial list_pntu, pntu is a pnt marking the position of the upper stream, for each pnts
# list_pntu <- list(pnts_split[which(pnts_split[, "Cz"] == max(pnts_split[, "Cz"])), ])
list_pntu <- list(pnts_split[which(pnts_split[, "zip"] == max(pnts_split[, "zip"])), ])
#
# initial list_grpm, grpm is a marker indicating subgrouping state, for each pnts (0 means need group)
list_grpm <- list(c(0))
#
# get the maximum ID of the boundary points of the landslide
IDB_max <- max(pnts_split[, "IDB"])
#
# initial list_pnts, list
list_list_pnts <- NULL
#
# subgrouping
while (TRUE) {
#
# save, for every split
if (OutputHrcl) {
#
# get path (centers and bnd sides)
list_pnts_path <- f_pnts_path(list_pnts, IDB_max, pPolygons, EndAnchorsInt, EndAnchorsDit, EndStripsInt, EndStripsDit, F)
#
# check
if (is.null(list_pnts_path)) {
#
# message
message("Error: f_pnts_path return NULL.")
#
# return
return(NULL)
}
#
# get centers
pnts_split_anchors <- list_pnts_path[[1]]
#
# save pnts
file_out <- paste(FilePrefix, "_pnts_grps", sprintf("%03d", pnts_split[nrow(pnts_split), "grp"]), ".xlsx", sep = "", collapse = NULL)
f_pnts_save_xls(pnts_split, file_out)
#
# save shp
file_out <- paste(FilePrefix, "_pnts_grps", sprintf("%03d", pnts_split[nrow(pnts_split), "grp"]), "", sep = "", collapse = NULL)
f_pnts_save_points(pnts_split, raster_CRS, file_out)
#
# save anchors
file_out <- paste(FilePrefix, "_pnts_grps", sprintf("%03d", pnts_split[nrow(pnts_split), "grp"]), "_anchors.xlsx", sep = "", collapse = NULL)
f_pnts_save_xls(pnts_split_anchors, file_out)
#
# save shp
file_out <- paste(FilePrefix, "_pnts_grps", sprintf("%03d", pnts_split[nrow(pnts_split), "grp"]), "_anchors", sep = "", collapse = NULL)
f_pnts_save_points(pnts_split_anchors, raster_CRS, file_out)
}
#
# append
list_list_pnts[[length(list_list_pnts)+1]] <- list_pnts
#
# get finalization
Finalization <- min(data.frame(list_grpm)) == 1
#
# save, and break, if no group need split
if (Finalization) {
#
# DO NOT merge before Finalization,
# or, it will disturb the sequences of list_pntu and list_grpm.
# Then, the original list_pnts will be saved by OutputHrcl (if true),
# and, the possibly merged list_pnts will be saved in the final results.
#
# if EndRto is small, merge end groups, initial
if (length(list_pnts) >= 3) {
#
# save original
list_pnts0 <- list_pnts
#
# get
GrpCnt <- length(list_pnts0)
EndCnt <- nrow(list_pnts0[[1]])
idx_EndMerged <- 1
#
# check end groups, initial
for (i in 2:GrpCnt) {
#
# get pnts
pnts_i <- list_pnts0[[i]]
#
# get ratio for end group
EndCnt <- EndCnt + nrow(pnts_i)
EndRto <- EndCnt / nrow(pnts)
#
# check
if (EndRto > MinEndRtoInt) {
#
# when large than the minimum
# which is a little larger than the to-be-merged ratio
idx_EndMerged <- i-1
#
# break
break
}
}
#
# merge end groups, initial
if (idx_EndMerged > 1) {
#
# initial
list_pnts <- list()
#
# initial
pnts_initial <- list_pnts0[[1]]
#
# merge pnts
for (i in 2:idx_EndMerged) {
#
# get pnts
pnts_i <- list_pnts0[[i]]
#
# merge
pnts_initial <- rbind(pnts_initial, pnts_i)
}
#
# set pnts
pnts_initial[, "SMG"] <- "SGR"
pnts_initial[, "grp"] <- 1
#
# update list_pnts
list_pnts[[1]] <- pnts_initial
#
# update list_pnts
for (i in (idx_EndMerged+1):GrpCnt) {
#
# get pnts
pnts_i <- list_pnts0[[i]]
#
# set pnts
pnts_i[, "grp"] <- i-idx_EndMerged+1
#
# update list_pnts
list_pnts[[i-idx_EndMerged+1]] <- pnts_i
}
}
}
#
# if EndRto is small, merge end groups, distal
if (length(list_pnts) >= 3) {
#
# save original
list_pnts0 <- list_pnts
#
# get
GrpCnt <- length(list_pnts0)
EndCnt <- nrow(list_pnts0[[GrpCnt]])
idx_EndMerged <- GrpCnt
#
# check end groups, distal
for (i in ((GrpCnt-1):-1:1)) {
#
# get pnts
pnts_i <- list_pnts0[[i]]
#
# get ratio for end group
EndCnt <- EndCnt + nrow(pnts_i)
EndRto <- EndCnt / nrow(pnts)
#
# check
if (EndRto > MinEndRtoDit) {
#
# when large than the minimum
# which is a little larger than the to-be-merged ratio
idx_EndMerged <- i+1
#
# break
break
}
}
#
# merge end groups, distal
if (idx_EndMerged < GrpCnt) {
#
# initial
list_pnts <- list()
#
# update list_pnts
for (i in 1:(idx_EndMerged-1)) {
#
# get pnts
pnts_i <- list_pnts0[[i]]
#
# set pnts
pnts_i[, "grp"] <- i
#
# update list_pnts
list_pnts[[i]] <- pnts_i
}
#
# distal
pnts_distal <- list_pnts0[[idx_EndMerged]]
#
# merge pnts
for (i in (idx_EndMerged+1):GrpCnt) {
#
# get pnts
pnts_i <- list_pnts0[[i]]
#
# merge
pnts_distal <- rbind(pnts_distal, pnts_i)
}
#
# set pnts
pnts_distal[, "SMG"] <- "SGR"
pnts_distal[, "grp"] <- idx_EndMerged
#
# update list_pnts
list_pnts[[idx_EndMerged]] <- pnts_distal
}
}
#
# replace
list_list_pnts[[length(list_list_pnts)]] <- list_pnts
#
# initial idx
idx_hierarchical <- length(list_list_pnts)
#
# search valid, from the last hierarchical
while (idx_hierarchical >= 1) {
#
# get list_pnts
list_pnts_Hierarchical <- list_list_pnts[[idx_hierarchical]]
#
# get package
strips_package <- f_strips_package(list_pnts_Hierarchical, IDB_max, pPolygons, EndAnchorsInt, EndAnchorsDit, EndStripsInt, EndStripsDit, MinStpHrzLen, pRasterDEM)
#
# check
if (is.null(strips_package)) {
#
# message
message("Error: f_pnts_path return NULL.")
#
# return
return(NULL)
}
#
# check
if (length(strips_package) == 0) {
#
# next
idx_hierarchical <- idx_hierarchical - 1
}
else {
#
# break
break
}
}
#
# check, if no valid
if (idx_hierarchical == 0) {
#
# message
message("Error: no valid sub- grouping.")
#
# initial pnts_split
pnts_split <- pnts
#
# set SMS, to be "SMO" (all outside),
# no valid sub- grouping for anchors or their collecting line inside landslide polygon
pnts_split[, "SMS"] <- "SMO"
#
# message
message("Warning: SMS is SMO.")
#
# save pnts
file_out <- paste(FilePrefix, "_pnts_grp.xlsx", sep = "", collapse = NULL)
f_pnts_save_xls(pnts_split, file_out)
#
# save shp
file_out <- paste(FilePrefix, "_pnts_grp", sep = "", collapse = NULL)
f_pnts_save_points(pnts_split, raster_CRS, file_out)
#
# return
return(NULL)
}
#
# check, if have valid
else {
#
# if valid in last hierarchical
if (idx_hierarchical == length(list_list_pnts)) {
#
# list2pnts
pnts_split <- f_list2pnts(list_pnts)
#
# set SMS, to be "SMG", stop because all subgroup stop split
pnts_split[, "SMS"] <- "SMG"
}
# if valid in previous hierarchical
else {
#
# save original
list_pnts0 <- list_pnts
#
# get
GrpCnt <- length(list_pnts0)
#
# get
EndCnt_Initial <- 0
EndCnt_Initial_Target <- nrow(list_pnts_Hierarchical[[1]])
#
# initial
bool_replace_Intial <- TRUE
idx_replace_Intial <- 1
#
# replace idx, initial
for (i in 1:GrpCnt) {
#
# get pnts
pnts_i <- list_pnts0[[i]]
#
# get ratio for end group
EndCnt_Initial <- EndCnt_Initial + nrow(pnts_i)
#
# it is POSSIBLE that the end-est group is larger than the target
# then, use the end-est group, do not need to replace
if (i == 1 && EndCnt_Initial > EndCnt_Initial_Target) {
#
bool_replace_Intial <- FALSE
break
}
#
# check
if (EndCnt_Initial == EndCnt_Initial_Target)
{ idx_replace_Intial <- i }
}
#
# get
EndCnt_Distal <- 0
EndCnt_Distal_Target <- nrow(list_pnts_Hierarchical[[length(list_pnts_Hierarchical)]])
#
# initial
bool_replace_Distal <- TRUE
idx_replace_Distal <- GrpCnt
#
# replace idx, distal
for (i in GrpCnt:(-1):1) {
#
# get pnts
pnts_i <- list_pnts0[[i]]
#
# get ratio for end group
EndCnt_Distal <- EndCnt_Distal + nrow(pnts_i)
#
# it is POSSIBLE that the end-est group is larger than the target
# then, use the end-est group, do not need to replace
if (i == GrpCnt && EndCnt_Distal > EndCnt_Distal_Target) {
#
bool_replace_Distal <- FALSE
break
}
#
# check
if (EndCnt_Distal == EndCnt_Distal_Target)
{ idx_replace_Distal <- i }
}
#
# replace, initial
list_pnts_EndReplaceInitial <- list()
#
# replace, initial
pnts_initial <- list_pnts_Hierarchical[[1]]
#
# set pnts
pnts_initial[, "SMG"] <- "SGS"
pnts_initial[, "grp"] <- 1
#
# replace
list_pnts_EndReplaceInitial[[1]] <- pnts_initial
#
# update list_pnts
for (i in (idx_replace_Intial+1):GrpCnt) {
#
# check
if (idx_replace_Intial+1 > GrpCnt || i < 1 || i > length(list_pnts0))
{ break }
#
# get pnts
pnts_i <- list_pnts0[[i]]
#
# set pnts
pnts_i[, "grp"] <- i-idx_replace_Intial+1
#
# update list_pnts
list_pnts_EndReplaceInitial[[i-idx_replace_Intial+1]] <- pnts_i
}
# check list_pnts
pnts_split_EndReplaceInitial <- f_list2pnts(list_pnts_EndReplaceInitial)
# check list_pnts
if (nrow(pnts_split_EndReplaceInitial) != nrow(pnts)) {
#
# debugging
message("Error: count of pnts inconsistent.")
#
# set
list_pnts_EndReplaceInitial <- NULL
}
#
# replace, distal
list_pnts_EndReplaceDistal <- list()
#
# replace, distal
pnts_distal <- list_pnts_Hierarchical[[length(list_pnts_Hierarchical)]]
#
# set pnts
pnts_distal[, "SMG"] <- "SGS"
pnts_distal[, "grp"] <- idx_replace_Distal
#
# replace
list_pnts_EndReplaceDistal[[idx_replace_Distal]] <- pnts_distal
#
# update list_pnts
for (i in 1:(idx_replace_Distal-1)) {
#
# check
if (1 > idx_replace_Distal-1 || i < 1 || i > length(list_pnts0))
{ break }
#
# get pnts
pnts_i <- list_pnts0[[i]]
#
# set pnts
pnts_i[, "grp"] <- i
#
# update list_pnts
list_pnts_EndReplaceDistal[[i]] <- pnts_i
}
# check list_pnts
pnts_split_EndReplaceDistal <- f_list2pnts(list_pnts_EndReplaceDistal)
# check list_pnts
if (nrow(pnts_split_EndReplaceDistal) != nrow(pnts)) {
#
# debugging
message("Error: count of pnts inconsistent.")
#
# set
list_pnts_EndReplaceDistal <- NULL
}
#
# replace, both
list_pnts_EndReplaceBoth <- list()
#
# set pnts
pnts_distal[, "grp"] <- idx_replace_Distal - idx_replace_Intial + 1
#
# replace
list_pnts_EndReplaceBoth[[1]] <- pnts_initial
# replace
if (idx_replace_Distal - idx_replace_Intial + 1 >= 2)
list_pnts_EndReplaceBoth[[idx_replace_Distal - idx_replace_Intial + 1]] <- pnts_distal
#
# update list_pnts
for (i in (idx_replace_Intial+1):(idx_replace_Distal-1)) {
#
# check
if (idx_replace_Intial+1 > idx_replace_Distal-1 || i < 1 || i > length(list_pnts0))
{ break }
#
# get pnts
pnts_i <- list_pnts0[[i]]
#
# set pnts
pnts_i[, "grp"] <- i-idx_replace_Intial+1
#
# update list_pnts
list_pnts_EndReplaceBoth[[i-idx_replace_Intial+1]] <- pnts_i
}
# check list_pnts
pnts_split_EndReplaceBoth <- f_list2pnts(list_pnts_EndReplaceBoth)
# check list_pnts
if (nrow(pnts_split_EndReplaceBoth) != nrow(pnts)) {
#
# debugging
message("Error: count of pnts inconsistent.")
#
# set
list_pnts_EndReplaceBoth <- NULL
}
#
# initial
strips_package_EndReplaceInitial <- NULL
#
# get package
if (bool_replace_Intial) {
#
# get package
strips_package_EndReplaceInitial <- f_strips_package(list_pnts_EndReplaceInitial, IDB_max, pPolygons, EndAnchorsInt, EndAnchorsDit, EndStripsInt, EndStripsDit, MinStpHrzLen, pRasterDEM)
#
# check
if (is.null(strips_package_EndReplaceInitial)) {
#
# message
message("Error: f_pnts_path return NULL.")
#
# return
return(NULL)
}
}
#
# initial
strips_package_EndReplaceDistal <- NULL
#
# get package
if (bool_replace_Distal) {
#
# get package
strips_package_EndReplaceDistal <- f_strips_package(list_pnts_EndReplaceDistal, IDB_max, pPolygons, EndAnchorsInt, EndAnchorsDit, EndStripsInt, EndStripsDit, MinStpHrzLen, pRasterDEM)
#
# check
if (is.null(strips_package_EndReplaceDistal)) {
#
# message
message("Error: f_pnts_path return NULL.")
#
# return
return(NULL)
}
}
#
# initial
strips_package_EndReplaceBoth <- NULL
#
# get package
if (bool_replace_Intial && bool_replace_Distal) {
#
# get package
strips_package_EndReplaceBoth <- f_strips_package(list_pnts_EndReplaceBoth, IDB_max, pPolygons, EndAnchorsInt, EndAnchorsDit, EndStripsInt, EndStripsDit, MinStpHrzLen, pRasterDEM)
#
# check
if (is.null(strips_package_EndReplaceBoth)) {
#
# message
message("Error: f_pnts_path return NULL.")
#
# return
return(NULL)
}
}
#
# check
if ((!is.null(strips_package_EndReplaceInitial) && length(strips_package_EndReplaceInitial) != 0) ||
(!is.null(strips_package_EndReplaceDistal) && length(strips_package_EndReplaceDistal) != 0) ||
(!is.null(strips_package_EndReplaceBoth) && length(strips_package_EndReplaceBoth) != 0)) {
#
# get
if ((!is.null(strips_package_EndReplaceInitial) && length(strips_package_EndReplaceInitial) != 0))
{ list_pnts <- list_pnts_EndReplaceInitial }
#
else if ((!is.null(strips_package_EndReplaceDistal) && length(strips_package_EndReplaceDistal) != 0))
{ list_pnts <- list_pnts_EndReplaceDistal }
#
else if ((!is.null(strips_package_EndReplaceBoth) && length(strips_package_EndReplaceBoth) != 0))
{ list_pnts <- list_pnts_EndReplaceBoth }
#
# list2pnts
pnts_split <- f_list2pnts(list_pnts)
#
# set SMS, to be "SMG", stop because all subgroup stop split
pnts_split[, "SMS"] <- "SMG"
}
else {
#
# get
list_pnts <- list_pnts_Hierarchical
#
# list2pnts
pnts_split <- f_list2pnts(list_pnts)
#
# set SMS, to be "SMV" (have valid),
# stop at valid sub- grouping for anchors or their collecting line inside landslide polygon
pnts_split[, "SMS"] <- "SMV"
#
# message
message("Warning: SMS is SMV.")
}
}
#