-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathrasterfunctions.py
More file actions
1434 lines (1003 loc) · 48.2 KB
/
rasterfunctions.py
File metadata and controls
1434 lines (1003 loc) · 48.2 KB
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
#
# This software is licensed under the Apache 2 license, quoted below.
#
# Copyright 2019 Astraea, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# [http://www.apache.org/licenses/LICENSE-2.0]
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#
# SPDX-License-Identifier: Apache-2.0
#
"""
This module creates explicit Python functions that map back to the existing Scala
implementations. Most functions are standard Column functions, but those with unique
signatures are handled here as well.
"""
from typing import Iterable, List, Optional, Union
from deprecation import deprecated
from py4j.java_gateway import JavaObject
from pyspark.sql.column import Column, _to_java_column
from pyspark.sql.functions import lit
from .rf_context import RFContext
from .rf_types import CRS, CellType, Extent
from .version import __version__
THIS_MODULE = "pyrasterframes"
Column_type = Union[str, Column]
def _context_call(name: str, *args):
f = RFContext.active().lookup(name)
return f(*args)
def _apply_column_function(name: str, *args: Column_type) -> Column:
jfcn = RFContext.active().lookup(name)
jcols = [_to_java_column(arg) for arg in args]
return Column(jfcn(*jcols))
def _apply_scalar_to_tile(name: str, tile_col: Column_type, scalar: Union[int, float]) -> Column:
jfcn = RFContext.active().lookup(name)
return Column(jfcn(_to_java_column(tile_col), scalar))
def _parse_cell_type(cell_type_arg: Union[str, CellType]) -> JavaObject:
"""Convert the cell type representation to the expected JVM CellType object."""
def to_jvm(ct):
return _context_call("_parse_cell_type", ct)
if isinstance(cell_type_arg, str):
return to_jvm(cell_type_arg)
elif isinstance(cell_type_arg, CellType):
return to_jvm(cell_type_arg.cell_type_name)
def rf_cell_types() -> List[CellType]:
"""Return a list of standard cell types"""
return [CellType(str(ct)) for ct in _context_call("rf_cell_types")]
def rf_assemble_tile(
col_index: Column_type,
row_index: Column_type,
cell_data_col: Column_type,
num_cols: Union[int, Column_type],
num_rows: Union[int, Column_type],
cell_type: Optional[Union[str, CellType]] = None,
) -> Column:
"""Create a Tile from a column of cell data with location indices"""
jfcn = RFContext.active().lookup("rf_assemble_tile")
if isinstance(num_cols, Column):
num_cols = _to_java_column(num_cols)
if isinstance(num_rows, Column):
num_rows = _to_java_column(num_rows)
if cell_type is None:
return Column(
jfcn(
_to_java_column(col_index),
_to_java_column(row_index),
_to_java_column(cell_data_col),
num_cols,
num_rows,
)
)
else:
return Column(
jfcn(
_to_java_column(col_index),
_to_java_column(row_index),
_to_java_column(cell_data_col),
num_cols,
num_rows,
_parse_cell_type(cell_type),
)
)
def rf_array_to_tile(array_col: Column_type, num_cols: int, num_rows: int) -> Column:
"""Convert array in `array_col` into a Tile of dimensions `num_cols` and `num_rows'"""
jfcn = RFContext.active().lookup("rf_array_to_tile")
return Column(jfcn(_to_java_column(array_col), num_cols, num_rows))
def rf_convert_cell_type(tile_col: Column_type, cell_type: Union[str, CellType]) -> Column:
"""Convert the numeric type of the Tiles in `tileCol`"""
jfcn = RFContext.active().lookup("rf_convert_cell_type")
return Column(jfcn(_to_java_column(tile_col), _parse_cell_type(cell_type)))
def rf_interpret_cell_type_as(tile_col: Column_type, cell_type: Union[str, CellType]) -> Column:
"""Change the interpretation of the tile_col's cell values according to specified cell_type"""
jfcn = RFContext.active().lookup("rf_interpret_cell_type_as")
return Column(jfcn(_to_java_column(tile_col), _parse_cell_type(cell_type)))
def rf_make_constant_tile(
scalar_value: Union[int, float],
num_cols: int,
num_rows: int,
cell_type: Union[str, CellType] = CellType.float64(),
) -> Column:
"""Constructor for constant tile column"""
jfcn = RFContext.active().lookup("rf_make_constant_tile")
return Column(jfcn(scalar_value, num_cols, num_rows, _parse_cell_type(cell_type)))
def rf_make_zeros_tile(
num_cols: int, num_rows: int, cell_type: Union[str, CellType] = CellType.float64()
) -> Column:
"""Create column of constant tiles of zero"""
jfcn = RFContext.active().lookup("rf_make_zeros_tile")
return Column(jfcn(num_cols, num_rows, _parse_cell_type(cell_type)))
def rf_make_ones_tile(
num_cols: int, num_rows: int, cell_type: Union[str, CellType] = CellType.float64()
) -> Column:
"""Create column of constant tiles of one"""
jfcn = RFContext.active().lookup("rf_make_ones_tile")
return Column(jfcn(num_cols, num_rows, _parse_cell_type(cell_type)))
def rf_rasterize(
geometry_col: Column_type,
bounds_col: Column_type,
value_col: Column_type,
num_cols_col: Column_type,
num_rows_col: Column_type,
) -> Column:
"""Create a tile where cells in the grid defined by cols, rows, and bounds are filled with the given value."""
return _apply_column_function(
"rf_rasterize", geometry_col, bounds_col, value_col, num_cols_col, num_rows_col
)
def st_reproject(geometry_col: Column_type, src_crs: Column_type, dst_crs: Column_type) -> Column:
"""Reproject a column of geometry given the CRSs of the source and destination."""
return _apply_column_function("st_reproject", geometry_col, src_crs, dst_crs)
def rf_explode_tiles(*tile_cols: Column_type) -> Column:
"""Create a row for each cell in Tile."""
jfcn = RFContext.active().lookup("rf_explode_tiles")
jcols = [_to_java_column(arg) for arg in tile_cols]
return Column(jfcn(RFContext.active().list_to_seq(jcols)))
def rf_explode_tiles_sample(sample_frac: float, seed: int, *tile_cols: Column_type) -> Column:
"""Create a row for a sample of cells in Tile columns."""
jfcn = RFContext.active().lookup("rf_explode_tiles_sample")
jcols = [_to_java_column(arg) for arg in tile_cols]
return Column(jfcn(sample_frac, seed, RFContext.active().list_to_seq(jcols)))
def rf_with_no_data(tile_col: Column_type, scalar: Union[int, float]) -> Column:
"""Assign a `NoData` value to the Tiles in the given Column."""
return _apply_scalar_to_tile("rf_with_no_data", tile_col, scalar)
def rf_local_add(left_tile_col: Column_type, rhs: Union[float, int, Column_type]) -> Column:
"""Add two Tiles, or add a scalar to a Tile"""
if isinstance(rhs, (float, int)):
rhs = lit(rhs)
return _apply_column_function("rf_local_add", left_tile_col, rhs)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_add_double(tile_col: Column_type, scalar: float) -> Column:
"""Add a floating point scalar to a Tile"""
return _apply_scalar_to_tile("rf_local_add_double", tile_col, scalar)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_add_int(tile_col, scalar) -> Column:
"""Add an integral scalar to a Tile"""
return _apply_scalar_to_tile("rf_local_add_int", tile_col, scalar)
def rf_local_subtract(left_tile_col: Column_type, rhs: Union[float, int, Column_type]) -> Column:
"""Subtract two Tiles, or subtract a scalar from a Tile"""
if isinstance(rhs, (float, int)):
rhs = lit(rhs)
return _apply_column_function("rf_local_subtract", left_tile_col, rhs)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_subtract_double(tile_col, scalar):
"""Subtract a floating point scalar from a Tile"""
return _apply_scalar_to_tile("rf_local_subtract_double", tile_col, scalar)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_subtract_int(tile_col, scalar):
"""Subtract an integral scalar from a Tile"""
return _apply_scalar_to_tile("rf_local_subtract_int", tile_col, scalar)
def rf_local_multiply(left_tile_col: Column_type, rhs: Union[float, int, Column_type]) -> Column:
"""Multiply two Tiles cell-wise, or multiply Tile cells by a scalar"""
if isinstance(rhs, (float, int)):
rhs = lit(rhs)
return _apply_column_function("rf_local_multiply", left_tile_col, rhs)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_multiply_double(tile_col, scalar):
"""Multiply a Tile by a float point scalar"""
return _apply_scalar_to_tile("rf_local_multiply_double", tile_col, scalar)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_multiply_int(tile_col, scalar):
"""Multiply a Tile by an integral scalar"""
return _apply_scalar_to_tile("rf_local_multiply_int", tile_col, scalar)
def rf_local_divide(left_tile_col: Column_type, rhs: Union[float, int, Column_type]) -> Column:
"""Divide two Tiles cell-wise, or divide a Tile's cell values by a scalar"""
if isinstance(rhs, (float, int)):
rhs = lit(rhs)
return _apply_column_function("rf_local_divide", left_tile_col, rhs)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_divide_double(tile_col, scalar):
"""Divide a Tile by a floating point scalar"""
return _apply_scalar_to_tile("rf_local_divide_double", tile_col, scalar)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_divide_int(tile_col, scalar):
"""Divide a Tile by an integral scalar"""
return _apply_scalar_to_tile("rf_local_divide_int", tile_col, scalar)
def rf_local_less(left_tile_col: Column_type, rhs: Union[float, int, Column_type]) -> Column:
"""Cellwise less than comparison between two tiles, or with a scalar value"""
if isinstance(rhs, (float, int)):
rhs = lit(rhs)
return _apply_column_function("rf_local_less", left_tile_col, rhs)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_less_double(tile_col, scalar):
"""Return a Tile with values equal 1 if the cell is less than a scalar, otherwise 0"""
return _apply_scalar_to_tile("foo", tile_col, scalar)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_less_int(tile_col, scalar):
"""Return a Tile with values equal 1 if the cell is less than a scalar, otherwise 0"""
return _apply_scalar_to_tile("rf_local_less_double", tile_col, scalar)
def rf_local_less_equal(left_tile_col: Column_type, rhs: Union[float, int, Column_type]) -> Column:
"""Cellwise less than or equal to comparison between two tiles, or with a scalar value"""
if isinstance(rhs, (float, int)):
rhs = lit(rhs)
return _apply_column_function("rf_local_less_equal", left_tile_col, rhs)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_less_equal_double(tile_col, scalar):
"""Return a Tile with values equal 1 if the cell is less than or equal to a scalar, otherwise 0"""
return _apply_scalar_to_tile("rf_local_less_equal_double", tile_col, scalar)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_less_equal_int(tile_col, scalar):
"""Return a Tile with values equal 1 if the cell is less than or equal to a scalar, otherwise 0"""
return _apply_scalar_to_tile("rf_local_less_equal_int", tile_col, scalar)
def rf_local_greater(left_tile_col: Column, rhs: Union[float, int, Column_type]) -> Column:
"""Cellwise greater than comparison between two tiles, or with a scalar value"""
if isinstance(rhs, (float, int)):
rhs = lit(rhs)
return _apply_column_function("rf_local_greater", left_tile_col, rhs)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_greater_double(tile_col, scalar):
"""Return a Tile with values equal 1 if the cell is greater than a scalar, otherwise 0"""
return _apply_scalar_to_tile("rf_local_greater_double", tile_col, scalar)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_greater_int(tile_col, scalar):
"""Return a Tile with values equal 1 if the cell is greater than a scalar, otherwise 0"""
return _apply_scalar_to_tile("rf_local_greater_int", tile_col, scalar)
def rf_local_greater_equal(left_tile_col: Column, rhs: Union[float, int, Column_type]) -> Column:
"""Cellwise greater than or equal to comparison between two tiles, or with a scalar value"""
if isinstance(rhs, (float, int)):
rhs = lit(rhs)
return _apply_column_function("rf_local_greater_equal", left_tile_col, rhs)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_greater_equal_double(tile_col, scalar):
"""Return a Tile with values equal 1 if the cell is greater than or equal to a scalar, otherwise 0"""
return _apply_scalar_to_tile("rf_local_greater_equal_double", tile_col, scalar)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_greater_equal_int(tile_col, scalar):
"""Return a Tile with values equal 1 if the cell is greater than or equal to a scalar, otherwise 0"""
return _apply_scalar_to_tile("rf_local_greater_equal_int", tile_col, scalar)
def rf_local_equal(left_tile_col, rhs: Union[float, int, Column_type]) -> Column:
"""Cellwise equality comparison between two tiles, or with a scalar value"""
if isinstance(rhs, (float, int)):
rhs = lit(rhs)
return _apply_column_function("rf_local_equal", left_tile_col, rhs)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_equal_double(tile_col, scalar):
"""Return a Tile with values equal 1 if the cell is equal to a scalar, otherwise 0"""
return _apply_scalar_to_tile("rf_local_equal_double", tile_col, scalar)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_equal_int(tile_col, scalar):
"""Return a Tile with values equal 1 if the cell is equal to a scalar, otherwise 0"""
return _apply_scalar_to_tile("rf_local_equal_int", tile_col, scalar)
def rf_local_unequal(left_tile_col, rhs: Union[float, int, Column_type]) -> Column:
"""Cellwise inequality comparison between two tiles, or with a scalar value"""
if isinstance(rhs, (float, int)):
rhs = lit(rhs)
return _apply_column_function("rf_local_unequal", left_tile_col, rhs)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_unequal_double(tile_col, scalar):
"""Return a Tile with values equal 1 if the cell is not equal to a scalar, otherwise 0"""
return _apply_scalar_to_tile("rf_local_unequal_double", tile_col, scalar)
@deprecated(deprecated_in="0.9.0", removed_in="1.0.0", current_version=__version__)
def rf_local_unequal_int(tile_col, scalar):
"""Return a Tile with values equal 1 if the cell is not equal to a scalar, otherwise 0"""
return _apply_scalar_to_tile("rf_local_unequal_int", tile_col, scalar)
def rf_local_no_data(tile_col: Column_type) -> Column:
"""Return a tile with ones where the input is NoData, otherwise zero."""
return _apply_column_function("rf_local_no_data", tile_col)
def rf_local_data(tile_col: Column_type) -> Column:
"""Return a tile with zeros where the input is NoData, otherwise one."""
return _apply_column_function("rf_local_data", tile_col)
def rf_local_is_in(tile_col: Column_type, array: Union[Column_type, List]) -> Column:
"""Return a tile with cell values of 1 where the `tile_col` cell is in the provided array."""
from pyspark.sql.functions import array as sql_array
if isinstance(array, list):
array = sql_array([lit(v) for v in array])
return _apply_column_function("rf_local_is_in", tile_col, array)
def rf_dimensions(tile_col: Column_type) -> Column:
"""Query the number of (cols, rows) in a Tile."""
return _apply_column_function("rf_dimensions", tile_col)
def rf_tile_to_array_int(tile_col: Column_type) -> Column:
"""Flattens Tile into an array of integers."""
return _apply_column_function("rf_tile_to_array_int", tile_col)
def rf_tile_to_array_double(tile_col: Column_type) -> Column:
"""Flattens Tile into an array of doubles."""
return _apply_column_function("rf_tile_to_array_double", tile_col)
def rf_cell_type(tile_col: Column_type) -> Column:
"""Extract the Tile's cell type"""
return _apply_column_function("rf_cell_type", tile_col)
def rf_is_no_data_tile(tile_col: Column_type) -> Column:
"""Report if the Tile is entirely NODDATA cells"""
return _apply_column_function("rf_is_no_data_tile", tile_col)
def rf_exists(tile_col: Column_type) -> Column:
"""Returns true if any cells in the tile are true (non-zero and not NoData)"""
return _apply_column_function("rf_exists", tile_col)
def rf_for_all(tile_col: Column_type) -> Column:
"""Returns true if all cells in the tile are true (non-zero and not NoData)."""
return _apply_column_function("rf_for_all", tile_col)
def rf_agg_approx_histogram(tile_col: Column_type) -> Column:
"""Compute the full column aggregate floating point histogram"""
return _apply_column_function("rf_agg_approx_histogram", tile_col)
def rf_agg_approx_quantiles(tile_col, probabilities, relative_error=0.00001):
"""
Calculates the approximate quantiles of a tile column of a DataFrame.
:param tile_col: column to extract cells from.
:param probabilities: a list of quantile probabilities. Each number must belong to [0, 1].
For example 0 is the minimum, 0.5 is the median, 1 is the maximum.
:param relative_error: The relative target precision to achieve (greater than or equal to 0). Default is 0.00001
:return: An array of values approximately at the specified `probabilities`
"""
_jfn = RFContext.active().lookup("rf_agg_approx_quantiles")
_tile_col = _to_java_column(tile_col)
return Column(_jfn(_tile_col, probabilities, relative_error))
def rf_agg_stats(tile_col: Column_type) -> Column:
"""Compute the full column aggregate floating point statistics"""
return _apply_column_function("rf_agg_stats", tile_col)
def rf_agg_mean(tile_col: Column_type) -> Column:
"""Computes the column aggregate mean"""
return _apply_column_function("rf_agg_mean", tile_col)
def rf_agg_data_cells(tile_col: Column_type) -> Column:
"""Computes the number of non-NoData cells in a column"""
return _apply_column_function("rf_agg_data_cells", tile_col)
def rf_agg_no_data_cells(tile_col: Column_type) -> Column:
"""Computes the number of NoData cells in a column"""
return _apply_column_function("rf_agg_no_data_cells", tile_col)
def rf_agg_extent(extent_col):
"""Compute the aggregate extent over a column"""
return _apply_column_function("rf_agg_extent", extent_col)
def rf_agg_reprojected_extent(extent_col, src_crs_col, dest_crs):
"""Compute the aggregate extent over a column, first projecting from the row CRS to the destination CRS."""
return Column(
RFContext.call(
"rf_agg_reprojected_extent",
_to_java_column(extent_col),
_to_java_column(src_crs_col),
CRS(dest_crs).__jvm__,
)
)
def rf_agg_overview_raster(
tile_col: Column,
cols: int,
rows: int,
aoi: Extent,
tile_extent_col: Column = None,
tile_crs_col: Column = None,
):
"""Construct an overview raster of size `cols`x`rows` where data in `proj_raster` intersects the
`aoi` bound box in web-mercator. Uses bi-linear sampling method."""
ctx = RFContext.active()
jfcn = ctx.lookup("rf_agg_overview_raster")
if tile_extent_col is None or tile_crs_col is None:
return Column(jfcn(_to_java_column(tile_col), cols, rows, aoi.__jvm__))
else:
return Column(
jfcn(
_to_java_column(tile_col),
_to_java_column(tile_extent_col),
_to_java_column(tile_crs_col),
cols,
rows,
aoi.__jvm__,
)
)
def rf_tile_histogram(tile_col: Column_type) -> Column:
"""Compute the Tile-wise histogram"""
return _apply_column_function("rf_tile_histogram", tile_col)
def rf_tile_mean(tile_col: Column_type) -> Column:
"""Compute the Tile-wise mean"""
return _apply_column_function("rf_tile_mean", tile_col)
def rf_tile_sum(tile_col: Column_type) -> Column:
"""Compute the Tile-wise sum"""
return _apply_column_function("rf_tile_sum", tile_col)
def rf_tile_min(tile_col: Column_type) -> Column:
"""Compute the Tile-wise minimum"""
return _apply_column_function("rf_tile_min", tile_col)
def rf_tile_max(tile_col: Column_type) -> Column:
"""Compute the Tile-wise maximum"""
return _apply_column_function("rf_tile_max", tile_col)
def rf_tile_stats(tile_col: Column_type) -> Column:
"""Compute the Tile-wise floating point statistics"""
return _apply_column_function("rf_tile_stats", tile_col)
def rf_render_ascii(tile_col: Column_type) -> Column:
"""Render ASCII art of tile"""
return _apply_column_function("rf_render_ascii", tile_col)
def rf_render_matrix(tile_col: Column_type) -> Column:
"""Render Tile cell values as numeric values, for debugging purposes"""
return _apply_column_function("rf_render_matrix", tile_col)
def rf_render_png(
red_tile_col: Column_type, green_tile_col: Column_type, blue_tile_col: Column_type
) -> Column:
"""Converts columns of tiles representing RGB channels into a PNG encoded byte array."""
return _apply_column_function("rf_render_png", red_tile_col, green_tile_col, blue_tile_col)
def rf_render_color_ramp_png(tile_col, color_ramp_name):
"""Converts columns of tiles representing RGB channels into a PNG encoded byte array."""
return Column(RFContext.call("rf_render_png", _to_java_column(tile_col), color_ramp_name))
def rf_rgb_composite(
red_tile_col: Column_type, green_tile_col: Column_type, blue_tile_col: Column_type
) -> Column:
"""Converts columns of tiles representing RGB channels into a single RGB packaged tile."""
return _apply_column_function("rf_rgb_composite", red_tile_col, green_tile_col, blue_tile_col)
def rf_no_data_cells(tile_col: Column_type) -> Column:
"""Count of NODATA cells"""
return _apply_column_function("rf_no_data_cells", tile_col)
def rf_data_cells(tile_col: Column_type) -> Column:
"""Count of cells with valid data"""
return _apply_column_function("rf_data_cells", tile_col)
def rf_normalized_difference(left_tile_col: Column_type, right_tile_col: Column_type) -> Column:
"""Compute the normalized difference of two tiles"""
return _apply_column_function("rf_normalized_difference", left_tile_col, right_tile_col)
def rf_agg_local_max(tile_col: Column_type) -> Column:
"""Compute the cell-wise/local max operation between Tiles in a column."""
return _apply_column_function("rf_agg_local_max", tile_col)
def rf_agg_local_min(tile_col: Column_type) -> Column:
"""Compute the cellwise/local min operation between Tiles in a column."""
return _apply_column_function("rf_agg_local_min", tile_col)
def rf_agg_local_mean(tile_col: Column_type) -> Column:
"""Compute the cellwise/local mean operation between Tiles in a column."""
return _apply_column_function("rf_agg_local_mean", tile_col)
def rf_agg_local_data_cells(tile_col: Column_type) -> Column:
"""Compute the cellwise/local count of non-NoData cells for all Tiles in a column."""
return _apply_column_function("rf_agg_local_data_cells", tile_col)
def rf_agg_local_no_data_cells(tile_col: Column_type) -> Column:
"""Compute the cellwise/local count of NoData cells for all Tiles in a column."""
return _apply_column_function("rf_agg_local_no_data_cells", tile_col)
def rf_agg_local_stats(tile_col: Column_type) -> Column:
"""Compute cell-local aggregate descriptive statistics for a column of Tiles."""
return _apply_column_function("rf_agg_local_stats", tile_col)
def rf_mask(src_tile_col: Column_type, mask_tile_col: Column_type, inverse: bool = False) -> Column:
"""Where the rf_mask (second) tile contains NODATA, replace values in the source (first) tile with NODATA.
If `inverse` is true, replaces values in the source tile with NODATA where the mask tile contains valid data.
"""
if not inverse:
return _apply_column_function("rf_mask", src_tile_col, mask_tile_col)
else:
rf_inverse_mask(src_tile_col, mask_tile_col)
def rf_inverse_mask(src_tile_col: Column_type, mask_tile_col: Column_type) -> Column:
"""Where the rf_mask (second) tile DOES NOT contain NODATA, replace values in the source
(first) tile with NODATA."""
return _apply_column_function("rf_inverse_mask", src_tile_col, mask_tile_col)
def rf_mask_by_value(
data_tile: Column_type,
mask_tile: Column_type,
mask_value: Union[int, float, Column_type],
inverse: bool = False,
) -> Column:
"""Generate a tile with the values from the data tile, but where cells in the masking tile contain the masking
value, replace the data value with NODATA."""
if isinstance(mask_value, (int, float)):
mask_value = lit(mask_value)
jfcn = RFContext.active().lookup("rf_mask_by_value")
return Column(
jfcn(
_to_java_column(data_tile),
_to_java_column(mask_tile),
_to_java_column(mask_value),
inverse,
)
)
def rf_mask_by_values(
data_tile: Column_type,
mask_tile: Column_type,
mask_values: Union[List[Union[int, float]], Column_type],
) -> Column:
"""Generate a tile with the values from `data_tile`, but where cells in the `mask_tile` are in the `mask_values`
list, replace the value with NODATA.
"""
from pyspark.sql.functions import array as sql_array
if isinstance(mask_values, list):
mask_values = sql_array([lit(v) for v in mask_values])
jfcn = RFContext.active().lookup("rf_mask_by_values")
col_args = [_to_java_column(c) for c in [data_tile, mask_tile, mask_values]]
return Column(jfcn(*col_args))
def rf_inverse_mask_by_value(
data_tile: Column_type, mask_tile: Column_type, mask_value: Union[int, float, Column_type]
) -> Column:
"""Generate a tile with the values from the data tile, but where cells in the masking tile do not contain the
masking value, replace the data value with NODATA."""
if isinstance(mask_value, (int, float)):
mask_value = lit(mask_value)
return _apply_column_function("rf_inverse_mask_by_value", data_tile, mask_tile, mask_value)
def rf_mask_by_bit(
data_tile: Column_type,
mask_tile: Column_type,
bit_position: Union[int, Column_type],
value_to_mask: Union[int, float, bool, Column_type],
) -> Column:
"""Applies a mask using bit values in the `mask_tile`. Working from the right, extract the bit at `bitPosition` from the `maskTile`. In all locations where these are equal to the `valueToMask`, the returned tile is set to NoData, else the original `dataTile` cell value."""
if isinstance(bit_position, int):
bit_position = lit(bit_position)
if isinstance(value_to_mask, (int, float, bool)):
value_to_mask = lit(bool(value_to_mask))
return _apply_column_function(
"rf_mask_by_bit", data_tile, mask_tile, bit_position, value_to_mask
)
def rf_mask_by_bits(
data_tile: Column_type,
mask_tile: Column_type,
start_bit: Union[int, Column_type],
num_bits: Union[int, Column_type],
values_to_mask: Union[Iterable[Union[int, float]], Column_type],
) -> Column:
"""Applies a mask from blacklisted bit values in the `mask_tile`. Working from the right, the bits from `start_bit` to `start_bit + num_bits` are @ref:[extracted](reference.md#rf_local_extract_bits) from cell values of the `mask_tile`. In all locations where these are in the `mask_values`, the returned tile is set to NoData; otherwise the original `tile` cell value is returned."""
if isinstance(start_bit, int):
start_bit = lit(start_bit)
if isinstance(num_bits, int):
num_bits = lit(num_bits)
if isinstance(values_to_mask, (tuple, list)):
from pyspark.sql.functions import array
values_to_mask = array([lit(v) for v in values_to_mask])
return _apply_column_function(
"rf_mask_by_bits", data_tile, mask_tile, start_bit, num_bits, values_to_mask
)
def rf_local_extract_bits(
tile: Column_type, start_bit: Union[int, Column_type], num_bits: Union[int, Column_type] = 1
) -> Column:
"""Extract value from specified bits of the cells' underlying binary data.
* `startBit` is the first bit to consider, working from the right. It is zero indexed.
* `numBits` is the number of bits to take moving further to the left."""
if isinstance(start_bit, int):
start_bit = lit(start_bit)
if isinstance(num_bits, int):
num_bits = lit(num_bits)
return _apply_column_function("rf_local_extract_bits", tile, start_bit, num_bits)
def rf_round(tile_col: Column_type) -> Column:
"""Round cell values to the nearest integer without changing the cell type"""
return _apply_column_function("rf_round", tile_col)
def rf_local_min(tile_col, min):
"""Performs cell-wise minimum two tiles or a tile and a scalar."""
if isinstance(min, (int, float)):
min = lit(min)
return _apply_column_function("rf_local_min", tile_col, min)
def rf_local_max(tile_col, max):
"""Performs cell-wise maximum two tiles or a tile and a scalar."""
if isinstance(max, (int, float)):
max = lit(max)
return _apply_column_function("rf_local_max", tile_col, max)
def rf_local_clamp(tile_col, min, max):
"""Return the tile with its values limited to a range defined by min and max, inclusive."""
if isinstance(min, (int, float)):
min = lit(min)
if isinstance(max, (int, float)):
max = lit(max)
return _apply_column_function("rf_local_clamp", tile_col, min, max)
def rf_where(condition, x, y):
"""Return a tile with cell values chosen from `x` or `y` depending on `condition`.
Operates cell-wise in a similar fashion to Spark SQL `when` and `otherwise`."""
return _apply_column_function("rf_where", condition, x, y)
def rf_standardize(tile, mean=None, stddev=None):
"""
Standardize cell values such that the mean is zero and the standard deviation is one.
If specified, the `mean` and `stddev` are applied to all tiles in the column.
If not specified, each tile will be standardized according to the statistics of its cell values;
this can result in inconsistent values across rows in a tile column.
"""
if isinstance(mean, (int, float)):
mean = lit(mean)
if isinstance(stddev, (int, float)):
stddev = lit(stddev)
if mean is None and stddev is None:
return _apply_column_function("rf_standardize", tile)
if mean is not None and stddev is not None:
return _apply_column_function("rf_standardize", tile, mean, stddev)
raise ValueError(
"Either `mean` or `stddev` should both be specified or omitted in call to rf_standardize."
)
def rf_rescale(tile, min=None, max=None):
"""
Rescale cell values such that the minimum is zero and the maximum is one. Other values will be linearly interpolated into the range.
If specified, the `min` parameter will become the zero value and the `max` parameter will become 1. See @ref:[`rf_agg_stats`](reference.md#rf_agg_stats).
Values outside the range will be set to 0 or 1.
If `min` and `max` are not specified, the __tile-wise__ minimum and maximum are used; this can result in inconsistent values across rows in a tile column.
"""
if isinstance(min, (int, float)):
min = lit(float(min))
if isinstance(max, (int, float)):
max = lit(float(max))
if min is None and max is None:
return _apply_column_function("rf_rescale", tile)
if min is not None and max is not None:
return _apply_column_function("rf_rescale", tile, min, max)
raise ValueError(
"Either `min` or `max` should both be specified or omitted in call to rf_rescale."
)
def rf_abs(tile_col: Column_type) -> Column:
"""Compute the absolute value of each cell"""
return _apply_column_function("rf_abs", tile_col)
def rf_log(tile_col: Column_type) -> Column:
"""Performs cell-wise natural logarithm"""
return _apply_column_function("rf_log", tile_col)
def rf_log10(tile_col: Column_type) -> Column:
"""Performs cell-wise logartithm with base 10"""
return _apply_column_function("rf_log10", tile_col)
def rf_log2(tile_col: Column_type) -> Column:
"""Performs cell-wise logartithm with base 2"""
return _apply_column_function("rf_log2", tile_col)
def rf_log1p(tile_col: Column_type) -> Column:
"""Performs natural logarithm of cell values plus one"""
return _apply_column_function("rf_log1p", tile_col)
def rf_exp(tile_col: Column_type) -> Column:
"""Performs cell-wise exponential"""
return _apply_column_function("rf_exp", tile_col)
def rf_exp2(tile_col: Column_type) -> Column:
"""Compute 2 to the power of cell values"""
return _apply_column_function("rf_exp2", tile_col)
def rf_exp10(tile_col: Column_type) -> Column:
"""Compute 10 to the power of cell values"""
return _apply_column_function("rf_exp10", tile_col)
def rf_expm1(tile_col: Column_type) -> Column:
"""Performs cell-wise exponential, then subtract one"""
return _apply_column_function("rf_expm1", tile_col)
def rf_sqrt(tile_col: Column_type) -> Column:
"""Performs cell-wise square root"""
return _apply_column_function("rf_sqrt", tile_col)
def rf_identity(tile_col: Column_type) -> Column:
"""Pass tile through unchanged"""
return _apply_column_function("rf_identity", tile_col)
def rf_focal_max(
tile_col: Column_type,
neighborhood: Union[str, Column_type],
target: Union[str, Column_type] = "all",
) -> Column:
"""Compute the max value in its neighborhood of each cell"""
if isinstance(neighborhood, str):
neighborhood = lit(neighborhood)
if isinstance(target, str):
target = lit(target)
return _apply_column_function("rf_focal_max", tile_col, neighborhood, target)
def rf_focal_mean(
tile_col: Column_type,
neighborhood: Union[str, Column_type],
target: Union[str, Column_type] = "all",
) -> Column:
"""Compute the mean value in its neighborhood of each cell"""
if isinstance(neighborhood, str):
neighborhood = lit(neighborhood)
if isinstance(target, str):
target = lit(target)
return _apply_column_function("rf_focal_mean", tile_col, neighborhood, target)
def rf_focal_median(
tile_col: Column_type,
neighborhood: Union[str, Column_type],
target: Union[str, Column_type] = "all",
) -> Column:
"""Compute the max in its neighborhood value of each cell"""
if isinstance(neighborhood, str):
neighborhood = lit(neighborhood)
if isinstance(target, str):
target = lit(target)
return _apply_column_function("rf_focal_median", tile_col, neighborhood, target)
def rf_focal_min(
tile_col: Column_type,
neighborhood: Union[str, Column_type],
target: Union[str, Column_type] = "all",
) -> Column:
"""Compute the min value in its neighborhood of each cell"""
if isinstance(neighborhood, str):
neighborhood = lit(neighborhood)
if isinstance(target, str):
target = lit(target)
return _apply_column_function("rf_focal_min", tile_col, neighborhood, target)
def rf_focal_mode(
tile_col: Column_type,
neighborhood: Union[str, Column_type],
target: Union[str, Column_type] = "all",
) -> Column:
"""Compute the mode value in its neighborhood of each cell"""
if isinstance(neighborhood, str):
neighborhood = lit(neighborhood)
if isinstance(target, str):
target = lit(target)
return _apply_column_function("rf_focal_mode", tile_col, neighborhood, target)
def rf_focal_std_dev(
tile_col: Column_type,
neighborhood: Union[str, Column_type],
target: Union[str, Column_type] = "all",
) -> Column:
"""Compute the standard deviation value in its neighborhood of each cell"""
if isinstance(neighborhood, str):
neighborhood = lit(neighborhood)
if isinstance(target, str):
target = lit(target)
return _apply_column_function("rf_focal_std_dev", tile_col, neighborhood, target)
def rf_moransI(
tile_col: Column_type,
neighborhood: Union[str, Column_type],
target: Union[str, Column_type] = "all",
) -> Column:
"""Compute moransI in its neighborhood value of each cell"""
if isinstance(neighborhood, str):
neighborhood = lit(neighborhood)
if isinstance(target, str):
target = lit(target)
return _apply_column_function("rf_focal_moransi", tile_col, neighborhood, target)
def rf_aspect(tile_col: Column_type, target: Union[str, Column_type] = "all") -> Column:
"""Calculates the aspect of each cell in an elevation raster"""
if isinstance(target, str):
target = lit(target)
return _apply_column_function("rf_aspect", tile_col, target)
def rf_slope(
tile_col: Column_type,
z_factor: Union[int, float, Column_type],
target: Union[str, Column_type] = "all",
) -> Column:
"""Calculates slope of each cell in an elevation raster"""
if isinstance(z_factor, (int, float)):
z_factor = lit(z_factor)
if isinstance(target, str):
target = lit(target)
return _apply_column_function("rf_slope", tile_col, z_factor, target)
def rf_hillshade(
tile_col: Column_type,
azimuth: Union[int, float, Column_type],
altitude: Union[int, float, Column_type],
z_factor: Union[int, float, Column_type],
target: Union[str, Column_type] = "all",
) -> Column:
"""Calculates the hillshade of each cell in an elevation raster"""
if isinstance(azimuth, (int, float)):
azimuth = lit(azimuth)
if isinstance(altitude, (int, float)):