-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathapi_tile.R
1402 lines (1388 loc) · 38.8 KB
/
api_tile.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
#' @title Tile API
#' @noRd
#'
#' @author Rolf Simoes, \email{rolf.simoes@@inpe.br}
#'
#' @description
#' A cube consists of multiple tiles stacked together as rows of a
#' tibble. A tile is a only-one-row tibble that stores
#' metadata of a spatial partition of a cube.
#'
NULL
#' @title Get first tile of a cube
#' @noRd
#' @description
#' This function should be called by all tile API function to ensure that
#' only one tile will be processed.
#' @param cube A \code{cube} or a \code{tile}.
#' @return The first tile of a cube.
.tile <- function(cube) {
UseMethod(".tile", cube)
}
#' @export
.tile.raster_cube <- function(cube) {
cube <- .cube(cube)
cube[1, ]
}
#' @export
.tile.default <- function(cube) {
cube <- tibble::as_tibble(cube)
cube <- .cube_find_class(cube)
tile <- .tile(cube)
return(tile)
}
#' @title Get source cloud provider for a tile
#' @noRd
#' @param tile A tile.
#' @return Source cloud provider
.tile_source <- function(tile) {
UseMethod(".tile_source", tile)
}
#' @export
.tile_source.raster_cube <- function(tile) {
tile <- .tile(tile)
.as_chr(tile[["source"]])
}
#' @export
.tile_source.default <- function(tile) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
source <- .tile_source(tile)
return(source)
}
#' @title Get image collection for a tile
#' @noRd
#' @param tile A tile.
#' @return Image collection
.tile_collection <- function(tile) {
UseMethod(".tile_collection", tile)
}
#' @export
.tile_collection.raster_cube <- function(tile) {
tile <- .tile(tile)
.as_chr(tile[["collection"]])
}
#' @export
.tile_collection.default <- function(tile) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
collection <- .tile_collection(tile)
return(collection)
}
#' @title Get/Set tile name
#' @noRd
#' @param tile A tile.
#' @return Name of the tile
.tile_name <- function(tile) {
UseMethod(".tile_name", tile)
}
#' @export
.tile_name.raster_cube <- function(tile) {
tile <- .tile(tile)
.as_chr(tile[["tile"]])
}
#' @export
.tile_name.default <- function(tile) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
name <- .tile_name(tile)
return(name)
}
`.tile_name<-` <- function(tile, value) {
UseMethod(".tile_name<-", tile)
}
#' @export
`.tile_name<-.raster_cube` <- function(tile, value) {
tile <- .tile(tile)
tile[["tile"]] <- .as_chr(value)
tile
}
#' @title Get tile number of columns
#' @noRd
#' @param tile A tile.
#' @return Number of columns
.tile_ncols <- function(tile) {
UseMethod(".tile_ncols", tile)
}
#' @export
.tile_ncols.raster_cube <- function(tile) {
tile <- .tile(tile)
.ncols(.fi(tile))
}
#' @export
.tile_ncols.default <- function(tile) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
ncols <- .tile_ncols(tile)
return(ncols)
}
#' @title Get tile number of rows
#' @noRd
#' @param tile A tile.
#' @return Number of rows
.tile_nrows <- function(tile) {
UseMethod(".tile_nrows", tile)
}
#' @export
.tile_nrows.raster_cube <- function(tile) {
tile <- .tile(tile)
.nrows(.fi(tile))
}
#' @export
.tile_nrows.default <- function(tile) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
nrows <- .tile_nrows(tile)
return(nrows)
}
#' @title Get tile size
#' @noRd
#' @param tile A tile.
#' @return Size (list of nrows x ncols)
.tile_size <- function(tile) {
UseMethod(".tile_size", tile)
}
#' @export
.tile_size.raster_cube <- function(tile) {
list(ncols = .tile_ncols(tile), nrows = .tile_nrows(tile))
}
#' @export
.tile_size.default <- function(tile) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
size <- .tile_size(tile)
return(size)
}
#' @title Get X resolution
#' @noRd
#' @param tile A tile.
#' @return x resolution
.tile_xres <- function(tile) {
UseMethod(".tile_xres", tile)
}
#' @export
.tile_xres.raster_cube <- function(tile) {
tile <- .tile(tile)
.xres(.fi(tile))
}
#' @export
.tile_xres.default <- function(tile) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
xres <- .tile_xres(tile)
return(xres)
}
#' @title Get Y resolution
#' @noRd
#' @param tile A tile.
#' @return y resolution
.tile_yres <- function(tile) {
UseMethod(".tile_yres", tile)
}
#' @export
.tile_yres.raster_cube <- function(tile) {
tile <- .tile(tile)
.yres(.fi(tile))
}
#' @export
.tile_yres.default <- function(tile) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
yres <- .tile_yres(tile)
return(yres)
}
#' @title Update tile labels
#' @noRd
#' @param tile A tile.
#' @param labels A character vector with new labels
#' @return vector of labels
.tile_update_label <- function(tile, labels) {
UseMethod(".tile_update_label", tile)
}
#' @export
.tile_update_label.class_cube <- function(tile, labels) {
# Open classified raster
tile_rast <- .raster_open_rast(.tile_path(tile))
# Get frequency values
freq_tbl <- .raster_freq(tile_rast)
# Get tile labels
tile_labels <- .tile_labels(tile)
if (is.null(names(tile_labels))) {
names(tile_labels) <- seq_along(tile_labels)
}
# Get new labels values
tile_labels <- tile_labels[.as_chr(freq_tbl[["value"]])]
# Set new labels
.tile_labels(tile) <- tile_labels
# Return tile with updated labels
return(tile)
}
#' @export
.tile_update_label.default <- function(tile, labels) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
tile <- .tile_update_label(tile, labels)
return(tile)
}
#' @title Get/Set labels
#' @noRd
#' @param tile A tile.
#' @return vector of labels
.tile_labels <- function(tile) {
UseMethod(".tile_labels", tile)
}
#' @export
.tile_labels.raster_cube <- function(tile) {
tile <- .tile(tile)
tile[["labels"]][[1]]
}
#' @export
.tile_labels.default <- function(tile) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
labels <- .tile_labels(tile)
return(labels)
}
#
`.tile_labels<-` <- function(tile, value) {
UseMethod(".tile_labels<-", tile)
}
#' @export
`.tile_labels<-.raster_cube` <- function(tile, value) {
tile <- .tile(tile)
tile[["labels"]] <- list(value)
tile
}
#' @title Get first date from tile
#' @name .tile_start_date
#' @keywords internal
#' @noRd
#' @param tile A tile.
#'
#' @return date
.tile_start_date <- function(tile) {
UseMethod(".tile_start_date", tile)
}
#' @export
.tile_start_date.raster_cube <- function(tile) {
tile <- .tile(tile)
.fi_min_date(.fi(tile))
}
#' @export
.tile_start_date.default <- function(tile) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
start_date <- .tile_start_date(tile)
return(start_date)
}
#'
#' @title Get end date from file_info.
#' @name .tile_end_date
#' @keywords internal
#' @noRd
#' @param tile A tile.
#'
#' @return date
.tile_end_date <- function(tile) {
UseMethod(".tile_end_date", tile)
}
#' @export
.tile_end_date.raster_cube <- function(tile) {
tile <- .tile(tile)
.fi_max_date(.fi(tile))
}
#' @export
.tile_end_date.default <- function(tile) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
end_date <- .tile_end_date(tile)
return(end_date)
}
#' @title Get fid from tile
#' @name .tile_fid
#' @keywords internal
#' @noRd
#' @param tile A tile.
#' @return file ID
.tile_fid <- function(tile) {
UseMethod(".tile_fid", tile)
}
#' @export
.tile_fid.raster_cube <- function(tile) {
tile <- .tile(tile)
.fi_fid(.fi(tile))
}
#' @export
.tile_fid.default <- function(tile) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
fid <- .tile_fid(tile)
return(fid)
}
#' @title Get unique timeline from file_info.
#' @name .tile_timeline
#' @keywords internal
#' @noRd
#' @param tile A tile.
#' @return a timeline
.tile_timeline <- function(tile) {
UseMethod(".tile_timeline", tile)
}
#' @export
.tile_timeline.raster_cube <- function(tile) {
tile <- .tile(tile)
sort(unique(.fi_timeline(.fi(tile))))
}
#' @export
.tile_timeline.default <- function(tile) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
timeline <- .tile_timeline(tile)
return(timeline)
}
#' @title Check if tile is complete
#' @name .tile_is_complete
#' @keywords internal
#' @noRd
#' @param tile A tile.
#' @return TRUE/FALSE
.tile_is_complete <- function(tile) {
UseMethod(".tile_is_complete", tile)
}
#' @export
.tile_is_complete.raster_cube <- function(tile) {
tile <- .tile(tile)
.fi_is_complete(.fi(tile))
}
#' @export
.tile_is_complete.default <- function(tile) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
is_complete <- .tile_is_complete(tile)
return(is_complete)
}
#' @title Get path of first asset from file_info.
#' @name .tile_path
#' @keywords internal
#' @noRd
#' @param tile A tile.
#' @param band A band in the tile
#' @param date A date in the tile
#' @return Path of first asset in `file_info`
.tile_path <- function(tile, band = NULL, date = NULL) {
UseMethod(".tile_path", tile)
}
#' @export
.tile_path.raster_cube <- function(tile, band = NULL, date = NULL) {
tile <- .tile(tile)
if (.has(band)) {
tile <- .tile_filter_bands(tile = tile, bands = band[[1]])
}
if (.has(date)) {
tile <- .tile_filter_dates(tile = tile, dates = date[[1]])
}
# Get path of first asset
path <- .fi_path(.fi(tile))
# Return path
path
}
#' @export
.tile_path.default <- function(tile, band = NULL, date = NULL) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
path <- .tile_path(tile, band, date)
return(path)
}
#' @title Get all file paths from file_info.
#' @name .tile_paths
#' @keywords internal
#' @noRd
#' @param tile A tile.
#' @param bands Required bands
#' @return Paths of assets in `file_info` filtered by bands
.tile_paths <- function(tile, bands = NULL) {
UseMethod(".tile_paths", tile)
}
#' @export
.tile_paths.raster_cube <- function(tile, bands = NULL) {
tile <- .tile(tile)
if (.has(bands)) {
tile <- .tile_filter_bands(tile = tile, bands = bands)
}
# Get assets path
paths <- .fi_paths(.fi(tile))
# Return paths
return(paths)
}
#' @export
.tile_paths.default <- function(tile, bands = NULL) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
paths <- .tile_paths(tile, bands)
return(paths)
}
#' @title Get unique satellite name from tile.
#' @name .tile_satellite
#' @keywords internal
#' @noRd
#' @param tile A tile.
#' @return satellite name in the tile
.tile_satellite <- function(tile) {
UseMethod(".tile_satellite", tile)
}
#' @export
.tile_satellite.raster_cube <- function(tile) {
tile <- .tile(tile)
.as_chr(tile[["satellite"]])
}
#' @export
.tile_satellite.default <- function(tile) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
satellite <- .tile_satellite(tile)
return(satellite)
}
#' @title Get unique sensor name from tile.
#' @name .tile_sensor
#' @keywords internal
#' @noRd
#' @param tile A tile.
#'
#' @return sensor name in the tile
.tile_sensor <- function(tile) {
UseMethod(".tile_sensor", tile)
}
#' @export
.tile_sensor.raster_cube <- function(tile) {
tile <- .tile(tile)
.as_chr(tile[["sensor"]])
}
#' @export
.tile_sensor.default <- function(tile) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
sensor <- .tile_sensor(tile)
return(sensor)
}
#' @title Get sorted unique bands from file_info.
#' @name .tile_bands
#' @keywords internal
#' @noRd
#' @param tile A tile.
#' @return names of bands in the tile
.tile_bands <- function(tile, add_cloud = TRUE) {
UseMethod(".tile_bands", tile)
}
#' @export
.tile_bands.raster_cube <- function(tile, add_cloud = TRUE) {
tile <- .tile(tile)
bands <- unique(.fi_bands(.fi(tile)))
if (add_cloud) {
return(bands)
}
setdiff(bands, .band_cloud())
}
#' @export
.tile_bands.default <- function(tile, add_cloud = TRUE) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
bands <- .tile_bands(tile, add_cloud)
return(bands)
}
#' @title Set bands in tile file_info.
#' @rdname .tile_bands
#' @keywords internal
#' @noRd
#' @param tile A tile.
#'
#' @return tile with renamed bands
`.tile_bands<-` <- function(tile, value) {
UseMethod(".tile_bands<-", tile)
}
#' @export
`.tile_bands<-.raster_cube` <- function(tile, value) {
tile <- .tile(tile)
bands <- .tile_bands(tile)
.check_that(
length(bands) == length(value),
local_msg = paste0("bands must have length ", length(bands)),
msg = "invalid band list"
)
rename <- value
names(rename) <- bands
.fi(tile) <- .fi_rename_bands(.fi(tile), rename = rename)
tile
}
#'
#' @title Get a band definition from config.
#' @name .tile_band_conf
#' @keywords internal
#' @noRd
#' @param tile A tile.
#' @param band Band character vector.
#'
#' @return band_conf or band_cloud_conf
.tile_band_conf <- function(tile, band) {
UseMethod(".tile_band_conf", tile)
}
#' @export
.tile_band_conf.eo_cube <- function(tile, band) {
.conf_eo_band(
source = .tile_source(tile), collection = .tile_collection(tile),
band = band[[1]]
)
}
#' @export
.tile_band_conf.derived_cube <- function(tile, band) {
.conf_derived_band(
derived_class = .tile_derived_class(tile), band = band[[1]]
)
}
#' @export
.tile_band_conf.default <- function(tile, band) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
band_conf <- .tile_band_conf(tile, band)
return(band_conf)
}
#'
#' @title Filter file_info entries of a given \code{band}.
#' @name .tile_filter_bands
#' @keywords internal
#' @noRd
#' @param tile A tile.
#' @param bands Band names to be filtered.
#'
#' @return tile with selected files for the bands
.tile_filter_bands <- function(tile, bands) {
UseMethod(".tile_filter_bands", tile)
}
#' @export
.tile_filter_bands.eo_cube <- function(tile, bands) {
tile <- .tile(tile)
.fi(tile) <- .fi_filter_bands(fi = .fi(tile), bands = .band_eo(bands))
tile
}
#' @export
.tile_filter_bands.derived_cube <- function(tile, bands) {
tile <- .tile(tile)
.fi(tile) <- .fi_filter_bands(fi = .fi(tile), bands = .band_derived(bands))
tile
}
#' @export
.tile_filter_bands.class_cube <- function(tile, bands) {
tile <- .tile(tile)
.fi(tile) <- .fi_filter_bands(fi = .fi(tile), bands = "class")
tile
}
#' @export
.tile_filter_bands.default <- function(tile, bands) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
tile <- .tile_filter_bands(tile, bands)
return(tile)
}
#'
#' @title Get crs from tile
#' @name .tile_crs
#' @keywords internal
#' @noRd
#' @param tile A tile.
#'
#' @return CRS
.tile_crs <- function(tile) {
UseMethod(".tile_crs", tile)
}
#' @export
.tile_crs.raster_cube <- function(tile) {
tile <- .tile(tile)
.crs(tile)
}
#' @export
.tile_crs.default <- function(tile) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
crs <- .tile_crs(tile)
return(crs)
}
#' @title Get bbox from tile
#' @name .tile_bbox
#' @keywords internal
#' @noRd
#' @param tile A tile.
#'
#' @return bbox
.tile_bbox <- function(tile, as_crs = NULL) {
UseMethod(".tile_bbox", tile)
}
#' @export
.tile_bbox.raster_cube <- function(tile, as_crs = NULL) {
tile <- .tile(tile)
.bbox(tile, as_crs = as_crs)
}
#' @export
.tile_bbox.default <- function(tile, as_crs = NULL) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
bbox <- .tile_bbox(tile, as_crs = as_crs)
return(bbox)
}
#' @title Convert tile \code{bbox} to a sf polygon object.
#' @noRd
#' @param tile A tile.
#' @return sf object
.tile_as_sf <- function(tile, as_crs = NULL) {
UseMethod(".tile_as_sf", tile)
}
#' @export
.tile_as_sf.raster_cube <- function(tile, as_crs = NULL) {
.bbox_as_sf(.tile_bbox(tile), as_crs = as_crs)
}
#' @export
.tile_as_sf.default <- function(tile, as_crs = NULL) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
sf_obj <- .tile_as_sf(tile, as_crs = as_crs)
return(sf_obj)
}
#'
#' @title Does tile \code{bbox} intersect \code{roi} parameter?
#' @name .tile_intersects
#' @keywords internal
#' @noRd
#' @param tile A tile.
#' @param roi A region of interest (ROI).
#'
#' @return logical
.tile_intersects <- function(tile, roi) {
UseMethod(".tile_intersects", tile)
}
#' @export
.tile_intersects.raster_cube <- function(tile, roi) {
.intersects(.tile_as_sf(tile), .roi_as_sf(roi))
}
#' @export
.tile_intersects.default <- function(tile, roi) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
intersects <- .tile_intersects(tile, roi)
return(intersects)
}
#' @title Is tile inside roi?
#' @name .tile_within
#' @keywords internal
#' @noRd
#' @param tile A tile.
#' @param roi A region of interest (ROI).
#'
#' @return logical
.tile_within <- function(tile, roi) {
UseMethod(".tile_within", tile)
}
#' @export
.tile_within.raster_cube <- function(tile, roi) {
.within(.tile_as_sf(tile), .roi_as_sf(roi))
}
#' @export
.tile_within.default <- function(tile, roi) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
within <- .tile_within(tile, roi)
return(within)
}
#'
#' @title Is any date of tile's timeline between 'start_date'
#' and 'end_date'?
#' @name .tile_during
#' @keywords internal
#' @noRd
#' @param tile A tile.
#' @param start_date,end_date Date of start and end.
#'
#' @return logical
.tile_during <- function(tile, start_date, end_date) {
UseMethod(".tile_during", tile)
}
#' @export
.tile_during.raster_cube <- function(tile, start_date, end_date) {
tile <- .tile(tile)
any(.fi_during(
fi = .fi(tile), start_date = start_date, end_date = end_date
))
}
#' @export
.tile_during.default <- function(tile, start_date, end_date) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
result <- .tile_during(tile, start_date, end_date)
return(result)
}
#'
#' @title Filter file_info entries by 'start_date' and 'end_date.'
#' @name .tile_filter_interval
#' @keywords internal
#' @noRd
#' @param tile A tile.
#' @param start_date,end_date Date of start and end.
#'
#' @return file_info entries
.tile_filter_interval <- function(tile, start_date, end_date) {
UseMethod(".tile_filter_interval", tile)
}
#' @export
.tile_filter_interval.raster_cube <- function(tile, start_date, end_date) {
tile <- .tile(tile)
.fi(tile) <- .fi_filter_interval(
fi = .fi(tile), start_date = start_date, end_date = end_date
)
tile
}
#' @export
.tile_filter_interval.default <- function(tile, start_date, end_date) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
tile <- .tile_filter_interval(tile, start_date, end_date)
return(tile)
}
#'
#' @title Filter file_info entries by date
#' @name .tile_filter_dates
#' @keywords internal
#' @noRd
#' @param tile A tile.
#' @param dates Desired date
#'
#' @return file_info entries
.tile_filter_dates <- function(tile, dates) {
tile <- .tile(tile)
.fi(tile) <- .fi_filter_dates(fi = .fi(tile), dates = dates)
tile
}
#'
#' @title Get derived class of a tile.
#' @name .tile_derived_class
#' @keywords internal
#' @noRd
#' @param tile A tile.
#'
#' @return derived class
.tile_derived_class <- function(tile) {
UseMethod(".tile_derived_class", tile)
}
#' @export
.tile_derived_class.derived_cube <- function(tile) {
class(tile)[[1]]
}
#'
#' @title Read and preprocess a block of band values from
#' file_info rasters.
#' @name .tile_read_block
#' @keywords internal
#' @noRd
#' @description
#' eo_cube tiles preprocess is slightly different from
#' derived_cube tiles. Values outside the range of minimum and maximum for
#' a band are replaced by NA in eo_cubes. In derived_cubes,
#' values outside allowed range are clamped and replaced by minimum or maximum
#' values.
#'
#' @param tile A tile.
#' @param band Band character vector.
#' @param block A block list with (col, row, ncols, nrows).
#'
#' @return set of values from a band of a tile inside a block
.tile_read_block <- function(tile, band, block) {
UseMethod(".tile_read_block", tile)
}
#' @export
.tile_read_block.eo_cube <- function(tile, band, block) {
tile <- .tile(tile)
fi <- .fi(tile)
# Stops if band is not found
values <- .fi_read_block(fi = fi, band = .band_eo(band), block = block)
#
# Log here
#
.debug_log(
event = "start_block_data_process",
key = "band",
value = band
)
# Correct missing, minimum, and maximum values and
# apply scale and offset.
band_conf <- .tile_band_conf(tile = tile, band = band)
miss_value <- .miss_value(band_conf)
if (.has(miss_value)) {
values[values == miss_value] <- NA
}
min_value <- .min_value(band_conf)
if (.has(min_value)) {
values[values < min_value] <- NA
}
max_value <- .max_value(band_conf)
if (.has(max_value)) {
values[values > max_value] <- NA
}
scale <- .scale(band_conf)
if (.has(scale) && scale != 1) {
values <- values * scale
}
offset <- .offset(band_conf)
if (.has(offset) && offset != 0) {
values <- values + offset
}
#
# Log here
#
.debug_log(
event = "end_block_data_process",
key = "band",
value = band
)
# Return values
return(values)
}
#' @export
.tile_read_block.derived_cube <- function(tile, band, block) {
tile <- .tile(tile)
fi <- .fi(tile)
# Stops if band is not found
values <- .fi_read_block(fi = fi, band = .band_derived(band), block = block)
# Correct missing, minimum, and maximum values and
# apply scale and offset.
band_conf <- .tile_band_conf(tile = tile, band = band)
miss_value <- .miss_value(band_conf)
if (.has(miss_value)) {
values[values == miss_value] <- NA
}
min_value <- .min_value(band_conf)
if (.has(min_value)) {
values[values < min_value] <- min_value
}
max_value <- .max_value(band_conf)
if (.has(max_value)) {
values[values > max_value] <- max_value
}
scale <- .scale(band_conf)
if (.has(scale) && scale != 1) {
values <- values * scale
}
offset <- .offset(band_conf)
if (.has(offset) && offset != 0) {
values <- values + offset
}
# Return values
return(values)
}
#' @export
.tile_read_block.default <- function(tile, band, block) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
tile <- .tile_read_block(tile, band, block)
return(tile)
}
#'
#' @title Read and preprocess a block of cloud values from
#' file_info rasters.
#' @name .tile_cloud_read_block
#' @keywords internal
#' @noRd
#' @param tile A tile.
#' @param block A block list with (col, row, ncols, nrows).
#' @return set of values of a band of a tile in a block
.tile_cloud_read_block <- function(tile, block) {
UseMethod(".tile_cloud_read_block", tile)
}
#' @export
.tile_cloud_read_block.eo_cube <- function(tile, block) {
if (!.band_cloud() %in% .tile_bands(tile)) {
return(NULL)
}
values <- .tile_read_block(
tile = tile, band = .band_cloud(), block = block
)
#
# Log here
#
.debug_log(
event = "start_block_data_process",
key = "cloud_mask",
value = "cloud_mask"
)
# Get cloud parameters
cloud_conf <- .tile_band_conf(tile = tile, band = .band_cloud())
interp_values <- .cloud_interp_values(cloud_conf)
is_bit_mask <- .cloud_bit_mask(cloud_conf)
# Prepare cloud_mask
# Identify values to be removed
if (!is_bit_mask) {
values <- values %in% interp_values
} else {
values <- matrix(bitwAnd(values, sum(2^interp_values)) > 0,
nrow = length(values)
)
}
#
# Log here
#
.debug_log(
event = "end_block_data_process",
key = "cloud_bit_mask",
value = is_bit_mask
)
# Return values
return(values)
}
#' @export
.tile_cloud_read_block.default <- function(tile, block) {
tile <- tibble::as_tibble(tile)
tile <- .cube_find_class(tile)
tile <- .tile_cloud_read_block(tile, block)
return(tile)
}
#' @title Create chunks of a tile to be processed
#' @name .tile_chunks_create
#' @keywords internal
#' @noRd
#' @param tile tile to be processed
#' @param overlap overlap between tiles
#' @param block Current block
#' @return set of chunks to be read from the file
.tile_chunks_create <- function(tile, overlap, block = NULL) {
# Get block size
block <- .default(
x = block,
default = .raster_file_blocksize(.raster_open_rast(.tile_path(tile)))
)
# Compute chunks
.chunks_create(
block = block,
overlap = overlap,
image_size = .tile_size(tile),
image_bbox = .tile_bbox(tile)
)
}
#' @title Get tile from file
#' @keywords internal
#' @noRd
#' @param file Raster file
#' @param base_tile reference tile used in the operation
#' @param band Spectral band
#' @param update_bbox should bbox be updated?
#' @param labels Labels for classified cube
#' @return set of values of a band of a tile in a block
.tile_from_file <- function(file, base_tile, band, update_bbox, labels = NULL) {
UseMethod(".tile_from_file", base_tile)
}
#' @export
.tile_from_file.eo_cube <- function(file, base_tile, band, update_bbox,
labels = NULL) {
.tile_eo_from_files(
files = file,
fid = .tile_fid(base_tile),
bands = band,
date = .tile_start_date(base_tile),
base_tile = base_tile,
update_bbox = update_bbox
)
}
#' @export
.tile_from_file.derived_cube <- function(file, base_tile, band, update_bbox,
labels = NULL) {
.tile_derived_from_file(