-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathaccessor.py
More file actions
906 lines (670 loc) · 29.2 KB
/
accessor.py
File metadata and controls
906 lines (670 loc) · 29.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
"""xarray accessors for xarray-spatial.
Registers ``.xrs`` accessors on :class:`xarray.DataArray` and
:class:`xarray.Dataset` so that spatial operations can be called
directly via tab-completion::
import xrspatial # registers accessors
raster.xrs.slope()
raster.xrs.hillshade(azimuth=315)
nir.xrs.ndvi(red)
"""
import xarray as xr
@xr.register_dataarray_accessor("xrs")
class XrsSpatialDataArrayAccessor:
"""DataArray accessor exposing xarray-spatial operations."""
def __init__(self, obj):
self._obj = obj
# ---- Plot ----
def plot(self, **kwargs):
"""Plot the DataArray, using an embedded TIFF colormap if present.
For palette/indexed-color GeoTIFFs (read via ``open_geotiff``),
the TIFF's color table is applied automatically with correct
normalization. For all other DataArrays, falls through to the
standard ``da.plot()``.
Usage::
da = open_geotiff('landcover.tif')
da.xrs.plot() # palette colors used automatically
"""
import numpy as np
cmap = self._obj.attrs.get('cmap')
if cmap is not None and 'cmap' not in kwargs:
from matplotlib.colors import BoundaryNorm
n_colors = len(cmap.colors)
boundaries = np.arange(n_colors + 1) - 0.5
norm = BoundaryNorm(boundaries, n_colors)
kwargs.setdefault('cmap', cmap)
kwargs.setdefault('norm', norm)
kwargs.setdefault('add_colorbar', True)
return self._obj.plot(**kwargs)
# ---- Surface ----
def slope(self, **kwargs):
from .slope import slope
return slope(self._obj, **kwargs)
def aspect(self, **kwargs):
from .aspect import aspect
return aspect(self._obj, **kwargs)
def hillshade(self, **kwargs):
from .hillshade import hillshade
return hillshade(self._obj, **kwargs)
def curvature(self, **kwargs):
from .curvature import curvature
return curvature(self._obj, **kwargs)
# ---- Terrain Metrics ----
def tri(self, **kwargs):
from .terrain_metrics import tri
return tri(self._obj, **kwargs)
def tpi(self, **kwargs):
from .terrain_metrics import tpi
return tpi(self._obj, **kwargs)
def roughness(self, **kwargs):
from .terrain_metrics import roughness
return roughness(self._obj, **kwargs)
# ---- Hydrology ----
def flow_direction(self, **kwargs):
from .flow_direction import flow_direction
return flow_direction(self._obj, **kwargs)
def flow_direction_dinf(self, **kwargs):
from .flow_direction_dinf import flow_direction_dinf
return flow_direction_dinf(self._obj, **kwargs)
def flow_direction_mfd(self, **kwargs):
from .flow_direction_mfd import flow_direction_mfd
return flow_direction_mfd(self._obj, **kwargs)
def flow_accumulation(self, **kwargs):
from .flow_accumulation import flow_accumulation
return flow_accumulation(self._obj, **kwargs)
def flow_accumulation_mfd(self, **kwargs):
from .flow_accumulation_mfd import flow_accumulation_mfd
return flow_accumulation_mfd(self._obj, **kwargs)
def watershed(self, pour_points, **kwargs):
from .watershed import watershed
return watershed(self._obj, pour_points, **kwargs)
def basin(self, **kwargs):
from .basin import basin
return basin(self._obj, **kwargs)
def basins(self, **kwargs):
from .watershed import basins
return basins(self._obj, **kwargs)
def sink(self, **kwargs):
from .sink import sink
return sink(self._obj, **kwargs)
def fill(self, **kwargs):
from .fill import fill
return fill(self._obj, **kwargs)
def stream_order(self, flow_accum, **kwargs):
from .stream_order import stream_order
return stream_order(self._obj, flow_accum, **kwargs)
def stream_link(self, flow_accum, **kwargs):
from .stream_link import stream_link
return stream_link(self._obj, flow_accum, **kwargs)
def snap_pour_point(self, pour_points, **kwargs):
from .snap_pour_point import snap_pour_point
return snap_pour_point(self._obj, pour_points, **kwargs)
def flow_path(self, start_points, **kwargs):
from .flow_path import flow_path
return flow_path(self._obj, start_points, **kwargs)
def flow_length(self, **kwargs):
from .flow_length import flow_length
return flow_length(self._obj, **kwargs)
def twi(self, slope_agg, **kwargs):
from .twi import twi
return twi(self._obj, slope_agg, **kwargs)
def hand(self, flow_accum, elevation, **kwargs):
from .hand import hand
return hand(self._obj, flow_accum, elevation, **kwargs)
# ---- Flood ----
def flood_depth(self, water_level, **kwargs):
from .flood import flood_depth
return flood_depth(self._obj, water_level, **kwargs)
def inundation(self, water_level, **kwargs):
from .flood import inundation
return inundation(self._obj, water_level, **kwargs)
def curve_number_runoff(self, curve_number, **kwargs):
from .flood import curve_number_runoff
return curve_number_runoff(self._obj, curve_number, **kwargs)
def travel_time(self, slope_agg, mannings_n, **kwargs):
from .flood import travel_time
return travel_time(self._obj, slope_agg, mannings_n, **kwargs)
def vegetation_roughness(self, **kwargs):
from .flood import vegetation_roughness
return vegetation_roughness(self._obj, **kwargs)
def vegetation_curve_number(self, soil_group_agg, **kwargs):
from .flood import vegetation_curve_number
return vegetation_curve_number(self._obj, soil_group_agg, **kwargs)
def flood_depth_vegetation(self, slope_agg, mannings_n,
unit_discharge, **kwargs):
from .flood import flood_depth_vegetation
return flood_depth_vegetation(self._obj, slope_agg, mannings_n,
unit_discharge, **kwargs)
def viewshed(self, x, y, **kwargs):
from .viewshed import viewshed
return viewshed(self._obj, x, y, **kwargs)
def min_observable_height(self, x, y, **kwargs):
from .experimental.min_observable_height import min_observable_height
return min_observable_height(self._obj, x, y, **kwargs)
# ---- Classification ----
def natural_breaks(self, **kwargs):
from .classify import natural_breaks
return natural_breaks(self._obj, **kwargs)
def equal_interval(self, **kwargs):
from .classify import equal_interval
return equal_interval(self._obj, **kwargs)
def quantile(self, **kwargs):
from .classify import quantile
return quantile(self._obj, **kwargs)
def reclassify(self, bins, new_values, **kwargs):
from .classify import reclassify
return reclassify(self._obj, bins, new_values, **kwargs)
def binary(self, values, **kwargs):
from .classify import binary
return binary(self._obj, values, **kwargs)
def percentiles(self, **kwargs):
from .classify import percentiles
return percentiles(self._obj, **kwargs)
# ---- Focal ----
def focal_mean(self, **kwargs):
from .focal import mean
return mean(self._obj, **kwargs)
def bilateral(self, **kwargs):
from .bilateral import bilateral
return bilateral(self._obj, **kwargs)
def glcm_texture(self, **kwargs):
from .glcm import glcm_texture
return glcm_texture(self._obj, **kwargs)
# ---- Edge Detection ----
def sobel_x(self, **kwargs):
from .edge_detection import sobel_x
return sobel_x(self._obj, **kwargs)
def sobel_y(self, **kwargs):
from .edge_detection import sobel_y
return sobel_y(self._obj, **kwargs)
def laplacian(self, **kwargs):
from .edge_detection import laplacian
return laplacian(self._obj, **kwargs)
def prewitt_x(self, **kwargs):
from .edge_detection import prewitt_x
return prewitt_x(self._obj, **kwargs)
def prewitt_y(self, **kwargs):
from .edge_detection import prewitt_y
return prewitt_y(self._obj, **kwargs)
# ---- Morphological ----
def morph_erode(self, **kwargs):
from .morphology import morph_erode
return morph_erode(self._obj, **kwargs)
def morph_dilate(self, **kwargs):
from .morphology import morph_dilate
return morph_dilate(self._obj, **kwargs)
def morph_opening(self, **kwargs):
from .morphology import morph_opening
return morph_opening(self._obj, **kwargs)
def morph_closing(self, **kwargs):
from .morphology import morph_closing
return morph_closing(self._obj, **kwargs)
# ---- Proximity / Distance ----
def proximity(self, **kwargs):
from .proximity import proximity
return proximity(self._obj, **kwargs)
def allocation(self, **kwargs):
from .proximity import allocation
return allocation(self._obj, **kwargs)
def direction(self, **kwargs):
from .proximity import direction
return direction(self._obj, **kwargs)
def cost_distance(self, friction, **kwargs):
from .cost_distance import cost_distance
return cost_distance(self._obj, friction, **kwargs)
def surface_distance(self, elevation, **kwargs):
from .surface_distance import surface_distance
return surface_distance(self._obj, elevation, **kwargs)
def surface_allocation(self, elevation, **kwargs):
from .surface_distance import surface_allocation
return surface_allocation(self._obj, elevation, **kwargs)
def surface_direction(self, elevation, **kwargs):
from .surface_distance import surface_direction
return surface_direction(self._obj, elevation, **kwargs)
# ---- Pathfinding ----
def a_star_search(self, start, goal, **kwargs):
from .pathfinding import a_star_search
return a_star_search(self._obj, start, goal, **kwargs)
# ---- Zonal ----
def zonal_stats(self, zones, **kwargs):
from .zonal import stats
return stats(zones, self._obj, **kwargs)
def zonal_apply(self, zones, func, **kwargs):
from .zonal import apply
return apply(zones, self._obj, func, **kwargs)
def zonal_crosstab(self, zones, **kwargs):
from .zonal import crosstab
return crosstab(zones, self._obj, **kwargs)
def crop(self, zones, zones_ids, **kwargs):
from .zonal import crop
return crop(zones, self._obj, zones_ids, **kwargs)
def trim(self, **kwargs):
from .zonal import trim
return trim(self._obj, **kwargs)
def regions(self, **kwargs):
from .zonal import regions
return regions(self._obj, **kwargs)
# ---- Diffusion ----
def diffuse(self, **kwargs):
from .diffusion import diffuse
return diffuse(self._obj, **kwargs)
# ---- Dasymetric ----
def disaggregate(self, values, weight, **kwargs):
from .dasymetric import disaggregate
return disaggregate(self._obj, values, weight, **kwargs)
def pycnophylactic(self, values, **kwargs):
from .dasymetric import pycnophylactic
return pycnophylactic(self._obj, values, **kwargs)
# ---- Terrain generation ----
def generate_terrain(self, **kwargs):
from .terrain import generate_terrain
return generate_terrain(self._obj, **kwargs)
def perlin(self, **kwargs):
from .perlin import perlin
return perlin(self._obj, **kwargs)
# ---- Mahalanobis ----
def mahalanobis(self, other_bands, **kwargs):
from .mahalanobis import mahalanobis
return mahalanobis([self._obj] + list(other_bands), **kwargs)
# ---- Interpolation ----
def idw(self, x, y, z, **kwargs):
from .interpolate import idw
return idw(x, y, z, self._obj, **kwargs)
def kriging(self, x, y, z, **kwargs):
from .interpolate import kriging
return kriging(x, y, z, self._obj, **kwargs)
def spline(self, x, y, z, **kwargs):
from .interpolate import spline
return spline(x, y, z, self._obj, **kwargs)
# ---- Preview ----
def preview(self, **kwargs):
from .preview import preview
return preview(self._obj, **kwargs)
# ---- Normalization ----
def rescale(self, **kwargs):
from .normalize import rescale
return rescale(self._obj, **kwargs)
def standardize(self, **kwargs):
from .normalize import standardize
return standardize(self._obj, **kwargs)
# ---- Reproject ----
def reproject(self, target_crs, **kwargs):
from .reproject import reproject
return reproject(self._obj, target_crs, **kwargs)
# ---- Raster to vector ----
def polygonize(self, **kwargs):
from .polygonize import polygonize
return polygonize(self._obj, **kwargs)
def contours(self, **kwargs):
from .contour import contours
return contours(self._obj, **kwargs)
# ---- Fire ----
def dnbr(self, post_nbr_agg, **kwargs):
from .fire import dnbr
return dnbr(self._obj, post_nbr_agg, **kwargs)
def rdnbr(self, pre_nbr_agg, **kwargs):
from .fire import rdnbr
return rdnbr(self._obj, pre_nbr_agg, **kwargs)
def burn_severity_class(self, **kwargs):
from .fire import burn_severity_class
return burn_severity_class(self._obj, **kwargs)
def fireline_intensity(self, spread_rate_agg, **kwargs):
from .fire import fireline_intensity
return fireline_intensity(self._obj, spread_rate_agg, **kwargs)
def flame_length(self, **kwargs):
from .fire import flame_length
return flame_length(self._obj, **kwargs)
def rate_of_spread(self, wind_speed_agg, fuel_moisture_agg, **kwargs):
from .fire import rate_of_spread
return rate_of_spread(self._obj, wind_speed_agg, fuel_moisture_agg, **kwargs)
def kbdi(self, max_temp_agg, precip_agg, annual_precip, **kwargs):
from .fire import kbdi
return kbdi(self._obj, max_temp_agg, precip_agg, annual_precip, **kwargs)
# ---- Multispectral (self = green band for water indices) ----
def ndwi(self, nir_agg, **kwargs):
from .multispectral import ndwi
return ndwi(self._obj, nir_agg, **kwargs)
def mndwi(self, swir_agg, **kwargs):
from .multispectral import mndwi
return mndwi(self._obj, swir_agg, **kwargs)
# ---- Multispectral (self = NIR band) ----
def ndvi(self, red_agg, **kwargs):
from .multispectral import ndvi
return ndvi(self._obj, red_agg, **kwargs)
def evi(self, red_agg, blue_agg, **kwargs):
from .multispectral import evi
return evi(self._obj, red_agg, blue_agg, **kwargs)
def arvi(self, red_agg, blue_agg, **kwargs):
from .multispectral import arvi
return arvi(self._obj, red_agg, blue_agg, **kwargs)
def savi(self, red_agg, **kwargs):
from .multispectral import savi
return savi(self._obj, red_agg, **kwargs)
def nbr(self, swir2_agg, **kwargs):
from .multispectral import nbr
return nbr(self._obj, swir2_agg, **kwargs)
def sipi(self, red_agg, blue_agg, **kwargs):
from .multispectral import sipi
return sipi(self._obj, red_agg, blue_agg, **kwargs)
# ---- Rasterize ----
def rasterize(self, geometries, **kwargs):
from .rasterize import rasterize
return rasterize(geometries, like=self._obj, **kwargs)
# ---- GeoTIFF I/O ----
def to_geotiff(self, path, **kwargs):
"""Write this DataArray as a GeoTIFF.
Equivalent to ``to_geotiff(da, path, **kwargs)``.
See :func:`xrspatial.geotiff.to_geotiff` for full parameter docs.
"""
from .geotiff import to_geotiff
return to_geotiff(self._obj, path, **kwargs)
@xr.register_dataset_accessor("xrs")
class XrsSpatialDatasetAccessor:
"""Dataset accessor exposing xarray-spatial operations.
Single-input functions apply the operation to each data variable
(via the existing ``@supports_dataset`` decorator). Multi-input
(multispectral) functions accept string kwargs that map band
aliases to Dataset variable names.
"""
def __init__(self, obj):
self._obj = obj
# ---- Surface ----
def slope(self, **kwargs):
from .slope import slope
return slope(self._obj, **kwargs)
def aspect(self, **kwargs):
from .aspect import aspect
return aspect(self._obj, **kwargs)
def hillshade(self, **kwargs):
from .hillshade import hillshade
return hillshade(self._obj, **kwargs)
def curvature(self, **kwargs):
from .curvature import curvature
return curvature(self._obj, **kwargs)
# ---- Terrain Metrics ----
def tri(self, **kwargs):
from .terrain_metrics import tri
return tri(self._obj, **kwargs)
def tpi(self, **kwargs):
from .terrain_metrics import tpi
return tpi(self._obj, **kwargs)
def roughness(self, **kwargs):
from .terrain_metrics import roughness
return roughness(self._obj, **kwargs)
# ---- Hydrology ----
def flow_direction(self, **kwargs):
from .flow_direction import flow_direction
return flow_direction(self._obj, **kwargs)
def flow_direction_dinf(self, **kwargs):
from .flow_direction_dinf import flow_direction_dinf
return flow_direction_dinf(self._obj, **kwargs)
def flow_direction_mfd(self, **kwargs):
from .flow_direction_mfd import flow_direction_mfd
return flow_direction_mfd(self._obj, **kwargs)
def flow_accumulation(self, **kwargs):
from .flow_accumulation import flow_accumulation
return flow_accumulation(self._obj, **kwargs)
def flow_accumulation_mfd(self, **kwargs):
from .flow_accumulation_mfd import flow_accumulation_mfd
return flow_accumulation_mfd(self._obj, **kwargs)
def watershed(self, pour_points, **kwargs):
from .watershed import watershed
return watershed(self._obj, pour_points, **kwargs)
def basin(self, **kwargs):
from .basin import basin
return basin(self._obj, **kwargs)
def basins(self, **kwargs):
from .watershed import basins
return basins(self._obj, **kwargs)
def sink(self, **kwargs):
from .sink import sink
return sink(self._obj, **kwargs)
def fill(self, **kwargs):
from .fill import fill
return fill(self._obj, **kwargs)
def stream_order(self, flow_accum, **kwargs):
from .stream_order import stream_order
return stream_order(self._obj, flow_accum, **kwargs)
def stream_link(self, flow_accum, **kwargs):
from .stream_link import stream_link
return stream_link(self._obj, flow_accum, **kwargs)
def snap_pour_point(self, pour_points, **kwargs):
from .snap_pour_point import snap_pour_point
return snap_pour_point(self._obj, pour_points, **kwargs)
def flow_path(self, start_points, **kwargs):
from .flow_path import flow_path
return flow_path(self._obj, start_points, **kwargs)
def flow_length(self, **kwargs):
from .flow_length import flow_length
return flow_length(self._obj, **kwargs)
def twi(self, slope_agg, **kwargs):
from .twi import twi
return twi(self._obj, slope_agg, **kwargs)
def hand(self, flow_accum, elevation, **kwargs):
from .hand import hand
return hand(self._obj, flow_accum, elevation, **kwargs)
# ---- Flood ----
def flood_depth(self, water_level, **kwargs):
from .flood import flood_depth
return flood_depth(self._obj, water_level, **kwargs)
def inundation(self, water_level, **kwargs):
from .flood import inundation
return inundation(self._obj, water_level, **kwargs)
def curve_number_runoff(self, curve_number, **kwargs):
from .flood import curve_number_runoff
return curve_number_runoff(self._obj, curve_number, **kwargs)
def travel_time(self, slope_agg, mannings_n, **kwargs):
from .flood import travel_time
return travel_time(self._obj, slope_agg, mannings_n, **kwargs)
def vegetation_roughness(self, **kwargs):
from .flood import vegetation_roughness
return vegetation_roughness(self._obj, **kwargs)
def vegetation_curve_number(self, soil_group_agg, **kwargs):
from .flood import vegetation_curve_number
return vegetation_curve_number(self._obj, soil_group_agg, **kwargs)
def flood_depth_vegetation(self, slope_agg, mannings_n,
unit_discharge, **kwargs):
from .flood import flood_depth_vegetation
return flood_depth_vegetation(self._obj, slope_agg, mannings_n,
unit_discharge, **kwargs)
# ---- Classification ----
def natural_breaks(self, **kwargs):
from .classify import natural_breaks
return natural_breaks(self._obj, **kwargs)
def equal_interval(self, **kwargs):
from .classify import equal_interval
return equal_interval(self._obj, **kwargs)
def quantile(self, **kwargs):
from .classify import quantile
return quantile(self._obj, **kwargs)
def reclassify(self, bins, new_values, **kwargs):
from .classify import reclassify
return reclassify(self._obj, bins, new_values, **kwargs)
def binary(self, values, **kwargs):
from .classify import binary
return binary(self._obj, values, **kwargs)
def percentiles(self, **kwargs):
from .classify import percentiles
return percentiles(self._obj, **kwargs)
# ---- Focal ----
def focal_mean(self, **kwargs):
from .focal import mean
return mean(self._obj, **kwargs)
def bilateral(self, **kwargs):
from .bilateral import bilateral
return bilateral(self._obj, **kwargs)
def glcm_texture(self, **kwargs):
from .glcm import glcm_texture
return glcm_texture(self._obj, **kwargs)
# ---- Edge Detection ----
def sobel_x(self, **kwargs):
from .edge_detection import sobel_x
return sobel_x(self._obj, **kwargs)
def sobel_y(self, **kwargs):
from .edge_detection import sobel_y
return sobel_y(self._obj, **kwargs)
def laplacian(self, **kwargs):
from .edge_detection import laplacian
return laplacian(self._obj, **kwargs)
def prewitt_x(self, **kwargs):
from .edge_detection import prewitt_x
return prewitt_x(self._obj, **kwargs)
def prewitt_y(self, **kwargs):
from .edge_detection import prewitt_y
return prewitt_y(self._obj, **kwargs)
# ---- Morphological ----
def morph_erode(self, **kwargs):
from .morphology import morph_erode
return morph_erode(self._obj, **kwargs)
def morph_dilate(self, **kwargs):
from .morphology import morph_dilate
return morph_dilate(self._obj, **kwargs)
def morph_opening(self, **kwargs):
from .morphology import morph_opening
return morph_opening(self._obj, **kwargs)
def morph_closing(self, **kwargs):
from .morphology import morph_closing
return morph_closing(self._obj, **kwargs)
# ---- Diffusion ----
def diffuse(self, **kwargs):
from .diffusion import diffuse
return diffuse(self._obj, **kwargs)
# ---- Proximity ----
def proximity(self, **kwargs):
from .proximity import proximity
return proximity(self._obj, **kwargs)
def allocation(self, **kwargs):
from .proximity import allocation
return allocation(self._obj, **kwargs)
def direction(self, **kwargs):
from .proximity import direction
return direction(self._obj, **kwargs)
def cost_distance(self, friction, **kwargs):
from .cost_distance import cost_distance
return cost_distance(self._obj, friction, **kwargs)
def surface_distance(self, elevation, **kwargs):
from .surface_distance import surface_distance
return surface_distance(self._obj, elevation, **kwargs)
def surface_allocation(self, elevation, **kwargs):
from .surface_distance import surface_allocation
return surface_allocation(self._obj, elevation, **kwargs)
def surface_direction(self, elevation, **kwargs):
from .surface_distance import surface_direction
return surface_direction(self._obj, elevation, **kwargs)
# ---- Preview ----
def preview(self, **kwargs):
from .preview import preview
return preview(self._obj, **kwargs)
# ---- Normalization ----
def rescale(self, **kwargs):
from .normalize import rescale
return rescale(self._obj, **kwargs)
def standardize(self, **kwargs):
from .normalize import standardize
return standardize(self._obj, **kwargs)
# ---- Fire ----
def burn_severity_class(self, **kwargs):
from .fire import burn_severity_class
return burn_severity_class(self._obj, **kwargs)
def flame_length(self, **kwargs):
from .fire import flame_length
return flame_length(self._obj, **kwargs)
# ---- Multispectral (band mapping via kwargs) ----
def ndwi(self, green, nir, **kwargs):
from .multispectral import ndwi
return ndwi(self._obj, green=green, nir=nir, **kwargs)
def mndwi(self, green, swir, **kwargs):
from .multispectral import mndwi
return mndwi(self._obj, green=green, swir=swir, **kwargs)
def ndvi(self, nir, red, **kwargs):
from .multispectral import ndvi
return ndvi(self._obj, nir=nir, red=red, **kwargs)
def evi(self, nir, red, blue, **kwargs):
from .multispectral import evi
return evi(self._obj, nir=nir, red=red, blue=blue, **kwargs)
def arvi(self, nir, red, blue, **kwargs):
from .multispectral import arvi
return arvi(self._obj, nir=nir, red=red, blue=blue, **kwargs)
def savi(self, nir, red, **kwargs):
from .multispectral import savi
return savi(self._obj, nir=nir, red=red, **kwargs)
def nbr(self, nir, swir2, **kwargs):
from .multispectral import nbr
return nbr(self._obj, nir=nir, swir2=swir2, **kwargs)
def sipi(self, nir, red, blue, **kwargs):
from .multispectral import sipi
return sipi(self._obj, nir=nir, red=red, blue=blue, **kwargs)
# ---- Rasterize ----
def rasterize(self, geometries, **kwargs):
from .rasterize import rasterize
ds = self._obj
# Find a 2D variable with y/x dims to use as template
for var in ds.data_vars:
da = ds[var]
if da.ndim == 2 and 'y' in da.dims and 'x' in da.dims:
return rasterize(geometries, like=da, **kwargs)
raise ValueError(
"Dataset has no 2D variable with 'y' and 'x' dimensions "
"to use as rasterize template"
)
# ---- GeoTIFF I/O ----
def to_geotiff(self, path, var=None, **kwargs):
"""Write a Dataset variable as a GeoTIFF.
Parameters
----------
path : str
Output file path.
var : str or None
Variable name to write. If None, uses the first 2D variable
with y/x dimensions.
**kwargs
Passed to :func:`xrspatial.geotiff.to_geotiff`.
"""
from .geotiff import to_geotiff
ds = self._obj
if var is not None:
return to_geotiff(ds[var], path, **kwargs)
for v in ds.data_vars:
da = ds[v]
if da.ndim >= 2 and 'y' in da.dims and 'x' in da.dims:
return to_geotiff(da, path, **kwargs)
raise ValueError(
"Dataset has no variable with 'y' and 'x' dimensions to write"
)
def open_geotiff(self, source, **kwargs):
"""Read a GeoTIFF windowed to this Dataset's spatial extent.
Uses the Dataset's y/x coordinates to compute a pixel window,
then reads only that region from the file.
Parameters
----------
source : str
File path to the GeoTIFF.
**kwargs
Passed to :func:`xrspatial.geotiff.open_geotiff` (except
``window``, which is computed automatically).
Returns
-------
xr.DataArray
The windowed portion of the GeoTIFF.
"""
from .geotiff import open_geotiff, _read_geo_info, _extent_to_window
ds = self._obj
if 'y' not in ds.coords or 'x' not in ds.coords:
raise ValueError(
"Dataset must have 'y' and 'x' coordinates to compute "
"a spatial window"
)
y = ds.coords['y'].values
x = ds.coords['x'].values
y_min, y_max = float(y.min()), float(y.max())
x_min, x_max = float(x.min()), float(x.max())
geo_info, file_h, file_w = _read_geo_info(source)
t = geo_info.transform
# Expand extent by half a pixel so we capture edge pixels
y_min -= abs(t.pixel_height) * 0.5
y_max += abs(t.pixel_height) * 0.5
x_min -= abs(t.pixel_width) * 0.5
x_max += abs(t.pixel_width) * 0.5
window = _extent_to_window(t, file_h, file_w,
y_min, y_max, x_min, x_max)
kwargs.pop('window', None)
return open_geotiff(source, window=window, **kwargs)