-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapping_funcs.py
1203 lines (1030 loc) · 41.6 KB
/
mapping_funcs.py
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
'''
silly little functions for mapping projects
(C) tlohde
'''
from contourpy import contour_generator
import cartopy.crs as ccrs
import geopandas as gpd
from itertools import groupby, chain
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection, PolyCollection
from matplotlib.colors import (LinearSegmentedColormap,
Normalize,
BoundaryNorm,
ListedColormap)
from matplotlib.cm import ScalarMappable
import numpy as np
import pystac_client
from pystac.extensions.eo import EOExtension as eo
from pyproj.database import query_utm_crs_info
from pyproj.aoi import AreaOfInterest
import pyproj
import planetary_computer
from rasterio.enums import Resampling
import re
import rioxarray as rio
from scipy.interpolate import griddata
import shapely
from shapely import LineString
from shapelysmooth import taubin_smooth
from skimage import color
import stackstac
from tqdm import tqdm
from typing import Literal
import xarray as xr
import xrspatial as xrs
from xrspatial.classify import natural_breaks, equal_interval, reclassify
def validate_type(func, locals):
'''
validate inputs to function
'''
for var, var_type in func.__annotations__.items():
if var == 'return':
continue
if not any([isinstance(locals[var], vt) for vt in [var_type]]):
raise TypeError(
f'{var} must be (/be one of): {var_type} not a {locals[var]}'
)
class Utils():
'''
general utilities for working with raster
and vector datasets
'''
def twoD_interp(img: np.ndarray) -> np.ndarray:
'''
2d interpolation. useful for filling in gaps in
digital elevation models
input - 2d numpy arr with gaps as `np.nan`
output - filled 2d numpy arr
'''
assert len(img.shape) == 2, 'input must be 2d array'
validate_type(Utils.twoD_interp, locals=locals())
h, w = img.shape[:2]
mask = np.isnan(img)
xx, yy = np.meshgrid(np.arange(w), np.arange(h))
known_x = xx[~mask]
known_y = yy[~mask]
known_z = img[~mask]
missing_x = xx[mask]
missing_y = yy[mask]
interp_vals = griddata((known_x, known_y),
known_z, (missing_x, missing_y),
method='cubic', fill_value=np.nan)
interpolated = img.copy()
interpolated[missing_y, missing_x] = interp_vals
return interpolated
def get_local_utm(geom: shapely.geometry):
'''
get epsg code for utm zone input geometry is in
assumes shapely geometry is lat/lon epsg4326 coords
'''
_minx, _miny, _maxx, _maxy = geom.bounds
_utms = query_utm_crs_info(
'WGS84',
area_of_interest=AreaOfInterest(
west_lon_degree=_minx,
south_lat_degree=_miny,
east_lon_degree=_maxx,
north_lat_degree=_maxy
)
)
return ccrs.epsg(_utms[0].code)
def shapely_reprojector(geo: shapely.geometry,
src_crs: int = 3413,
target_crs: int = 4326):
"""
reproject shapely point (geo) from src_crs to target_crs
avoids having to create geopandas series to handle crs transformations
"""
assert isinstance(geo,
(shapely.geometry.polygon.Polygon,
shapely.geometry.linestring.LineString,
shapely.geometry.point.Point)
), 'geo must be shapely geometry'
transformer = pyproj.Transformer.from_crs(
src_crs,
target_crs,
always_xy=True
)
if isinstance(geo, shapely.geometry.point.Point):
_x, _y = geo.coords.xy
return shapely.Point(*transformer.transform(_x, _y))
elif isinstance(geo, shapely.geometry.linestring.LineString):
_x, _y = geo.coords.xy
return shapely.LineString(zip(*transformer.transform(_x, _y)))
elif isinstance(geo, shapely.geometry.polygon.Polygon):
_x, _y = geo.exterior.coords.xy
return shapely.Polygon(zip(*transformer.transform(_x, _y)))
def bezier(ls: shapely.geometry.linestring.LineString,
interval: tuple[float, int] = 100):
'''
make bezier curve from shapely linestring
'''
def get_segs(ls):
num_segs = len(ls.coords)-1
segs = [
LineString([ls.coords[i],
ls.coords[i+1]])
for i in range(num_segs)
]
return num_segs, segs
def get_points(segs, step):
return [seg.interpolate(step, normalized=True) for seg in segs]
points = []
_num_segs, _segs = get_segs(ls)
if len(_segs) == 1:
return ls
for _step in [i/interval for i in range(interval+1)]:
_tmp_segs = _segs
for _ in range(_num_segs-1):
_points = get_points(_tmp_segs, _step)
_ls = LineString(_points)
_, _tmp_segs = get_segs(_ls)
points.append(_ls.interpolate(_step, normalized=True))
return LineString(points)
class DEM():
@staticmethod
def get_copernicus_dem(geom: shapely.geometry.polygon.Polygon,
res: int = 30,
rprj: bool = True,
prj=None,
interp: bool = True
):
'''
get Copernicus Global DEM from planetary computer stac catalog
inputs:
geom - shapely geometry (polygon / box)
res - int: resolution of DEM (either 30, or 90, default 30)
rprj - bool: whether or not to reprojct the dem
prj - projection to reprject to
interp - whether or not to interpolate nans
returns: xarray instance of DEM
clipped to envelope of `geom`
if reprojected then not only clipped but also aligned
with nan's interpolated
'''
validate_type(DEM.get_copernicus_dem, locals=locals())
catalog = pystac_client.Client.open(
"https://planetarycomputer.microsoft.com/api/stac/v1",
modifier=planetary_computer.sign_inplace,
)
search = catalog.search(collections=[f'cop-dem-glo-{res}'],
intersects=geom.envelope)
items = search.item_collection()
if len(items) > 0:
dem = (stackstac.stack(
planetary_computer.sign(items))
.mean(dim='time', skipna=True)
.squeeze()
.rio.write_crs(4326)
.rio.clip_box(*geom.bounds)
)
# for reprojecting
# if user specifies prj (as int epsg code)
# reproejct to that, otherwise get local utm crs
if rprj:
if prj:
if isinstance(prj, int):
prj = ccrs.epsg(prj)
else:
prj = Utils.get_local_utm(geom)
demprj = dem.rio.reproject(
prj,
nodata=np.nan,
resampling=Resampling.bilinear
).rio.write_crs(prj)
# fix wonky edges
# trimming a few rows off of t/b l/r edge as needed
tb1 = np.nonzero(~np.isnan(demprj.data[:, 0]))[0][0]
tb2 = np.nonzero(~np.isnan(demprj.data[:, -1]))[0][0]
rl1 = np.nonzero(~np.isnan(demprj.data[0, :]))[0][0]
rl2 = np.nonzero(~np.isnan(demprj.data[-1, :]))[0][0]
if tb1 != tb2:
rows = slice(*sorted([tb1, tb2]))
else:
rows = slice(0, demprj.data.shape[0])
if rl1 != rl2:
cols = slice(*sorted([rl1, rl2]))
else:
cols = slice(0, demprj.data.shape[1])
demprj = demprj[rows, cols]
# interpolate NaNs
if interp:
demprj.data = Utils.twoD_interp(demprj.data)
return demprj.drop(['proj:epsg',
'band',
'gsd',
'epsg',
'platform',
'proj:shape'])
else:
return dem
print('could not find a DEM')
return None
class Ridges():
'''
class for making ridge plots from DEMs
inputs : shapely polygon with coords in lat/lon (epsg 4326)
'''
def __init__(self, aoi: shapely.Polygon, **kwargs):
assert isinstance(
aoi, shapely.geometry.polygon.Polygon), 'aoi not a polygon'
self.aoi = aoi
self.res = kwargs.get('res', 90)
self.rprj = kwargs.get('rprj', True)
# get local utm projection
_gds = gpd.GeoSeries(aoi, crs=4326)
self.prj = _gds.estimate_utm_crs()
# unpack kwargs
self.smooth_dict = kwargs.get('smooth_dict', {'x': 5})
self.step = kwargs.get('step', None)
self.cmap = kwargs.get('cmap', None)
if isinstance(self.cmap,
(ListedColormap,
LinearSegmentedColormap)
):
pass
elif isinstance(self.cmap, str):
self.cmap = plt.colormaps[self.cmap]
else:
print('reverting to default colormap')
self.cmap = plt.colormaps['Grays']
self.figsize = kwargs.get('figsize', [10, 10])
self.annotate = kwargs.get('annotate', True)
self.title = kwargs.get('title', None)
self.vert_exag = kwargs.get('vert_exag', 0.15)
self.fc = kwargs.get('fc', 'w')
self.ec = kwargs.get('ec', 'k')
self.lw = kwargs.get('lw', 0.5)
self.ffval = kwargs.get('ffval', -10)
self.alpha_p = kwargs.get('alpha_p', 1)
self.textc = kwargs.get('textc', 'w')
self.font = kwargs.get('font', 'dejavu sans mono')
self.get_dem()
self.smooth()
self.transects()
self.plotter(ec=self.ec,
fc=self.fc,
vert_exag=self.vert_exag,
lw=self.lw,
ffval=self.ffval,
alpha_p=self.alpha_p,
annotate=self.annotate)
def get_dem(self):
'''
get digital elevation model
'''
self.dem = DEM.get_copernicus_dem(geom=self.aoi,
res=self.res,
rprj=self.rprj,
prj=self.prj)
def smooth(self):
# smooth DEM along x-axis
self.dem_smooth = self.dem.rolling(self.smooth_dict,
min_periods=3,
center=True).mean(skipna=True)
self.Z = self.dem_smooth.data
# Z[np.isnan(Z)] = 0
self.X, self.Y = np.meshgrid(self.dem_smooth.x.data,
self.dem_smooth.y.data)
def transects(self, **kwargs):
'''
sample elevation along transects
'''
def divide_at_nan(q):
# for splitting transects at nan values
groups = []
uniquekeys = []
for k, g in groupby(q, lambda x: np.any(np.isnan(x))):
groups.append(list(g)) # Store group iterator as a list
uniquekeys.append(k)
return [groups[i] for i, tf in
enumerate(uniquekeys) if
(not tf) & (len(groups[i]) > 1)]
# if step kwarg not supplied calcuate transect spacing
# such that there will be 50 transects in total
if not self.step:
self.step = len(self.dem_smooth.y.data) // 50
# sample transects
# slicing x, y and z arrays
self.z_sub = self.Z[
[i for i in range(0, len(self.dem_smooth.y.data), self.step)],
:
]
self.x_sub = self.X[
[i for i in range(0, len(self.dem_smooth.y.data), self.step)],
:
]
self.y_sub = self.dem_smooth.y.data[::self.step]
# get pairs of x,z coords along each transect
# and ensure they are sorted along x-axis
verts = [list(zip(self.x_sub[i, :],
self.z_sub[i, :]))
for i in range(self.z_sub.shape[0])]
verts = [sorted(v, key=lambda tup: tup[0]) for v in verts]
# if any NaN values are encountered split the transect there
# and resume transect at next non-NaN value
# list of lists - some which may be lists themselves
divided = [divide_at_nan(x) for x in verts]
# unpack to be just a list of lists
self.cleaned = list(chain(*divided))
# dividing transects, increases the number of transects,
# so need to add additional y-coordinate
# values to plot along - i.e. duplicate y-values of transects
# that have been split
# if multiple splits duplicate y coords multiple times
insert_idxs = [
[i]*(len(c)-1) for i, c in enumerate(divided) if len(c) > 1
]
insert_idxs = [val for sublist in insert_idxs for val in sublist]
self.y_sub = np.insert(
self.y_sub, insert_idxs, self.y_sub[insert_idxs]
)
# set colors
self.facecolors = self.cmap(
np.linspace(0, 1, len(self.y_sub))
)
self.edgecolors = plt.colormaps['Greys'](
np.linspace(0, 1, len(self.y_sub))
)
def plotter(self, **kwargs):
ec = kwargs.get('ec', self.ec)
fc = kwargs.get('fc', self.fc)
vert_exag = kwargs.get('vert_exag', self.vert_exag)
lw = kwargs.get('lw', self.lw)
ffval = kwargs.get('ffval', self.ffval)
alpha_p = kwargs.get('alpha_p', self.alpha_p)
annotate = kwargs.get('annotate', True)
def polygon_under_graph(x, y, fill=0.):
"""
Construct the vertex list which defines the polygon filling the
space under the (x, y) line graph.
This assumes x is in ascending order.
"""
return [(x[0], fill), *zip(x, y), (x[-1], fill)]
# ffval = -10 # z coordinate of bottom polygon
# lw = 0.5 # line width
# alpha_p = 1
# this sets the 'front' polygon to have lower edge of zero,
# whereas other polygons have lower edge of ffval
ff = np.zeros(self.y_sub.shape[0])
ff[0:-1] = ffval
# make polygons
polys = [polygon_under_graph(*zip(*c),
fill=ff[i])
for i, c in enumerate(self.cleaned)]
self.collection = PolyCollection(polys,
facecolors=self.facecolors,
alpha=alpha_p,
edgecolors=ec,
linewidths=lw)
self.fig, self.ax = plt.subplots(figsize=self.figsize,
subplot_kw={'projection': '3d'})
self.ax.add_collection3d(self.collection, zs=self.y_sub, zdir='y')
self.ax.set_xlim(np.nanmin(self.x_sub), np.nanmax(self.x_sub))
self.ax.set_zlim(np.nanmin(self.z_sub), np.nanmax(self.z_sub))
self.ax.set_ylim(np.nanmin(self.y_sub), np.nanmax(self.y_sub))
# self.ax.set_axis_off()
self.ax.patch.set_facecolor(fc)
self.ax.yaxis.set_pane_color(fc)
self.ax.xaxis.set_pane_color(fc)
self.ax.zaxis.set_pane_color(fc)
self.fig.patch.set_facecolor(fc)
self.ax.grid(False)
self.ax.set_box_aspect([1, 1, vert_exag])
self.ax.view_init(elev=40, azim=270, roll=0)
if annotate:
self.ax.set_title(self.title,
font=self.font,
fontsize=40, # 'xx-large',
color=self.textc,
loc='left',
x=0.2,
y=0.77)
self.ax.text(np.nanmin(self.x_sub),
np.nanmin(self.y_sub),
0.05 * np.nanmax(self.z_sub[-1, :]),
' by:tlohde', 'x',
ha='left',
va='bottom',
fontsize=5, # 'xx-small',
font='dejavu sans mono')
self.ax.text(np.nanmax(self.x_sub),
np.nanmin(self.y_sub),
0.05 * np.nanmax(self.z_sub[-1, :]),
'Copernicus Global DEM, ESA (2021)',
'x',
ha='right',
va='bottom',
fontsize=5, # 'xx-small',
font='dejavu sans mono')
self.ax.set_axis_off()
class Flow():
'''
class for making flowy topography maps
input dem
**kwargs:
'step' - [in crs units] size of step to take between
samples
reps - how many steps to take
cmap - colormap
N - how many points to randomly seed
'''
def __init__(self,
dem,
**kwargs):
self.dem = dem
self.aspect = xrs.aspect(dem)
self.slope = xrs.slope(dem)
self.epsg = self.dem.rio.crs.to_epsg()
self.prj = ccrs.epsg(self.epsg)
self.N = kwargs.get('N', None)
self.step = kwargs.get('step', int(1.5 * dem.rio.resolution()[0]))
self.reps = kwargs.get('reps', 50)
self.gradient_threshold = kwargs.get('gradient_threshold', 5)
self.cmap = plt.get_cmap(kwargs.get('cmap', 'twilight'))
self.make_points()
self.follow_aspect(step=self.step, reps=self.reps)
# self.make_line_collection()
def make_points(self):
'''
randomly scatter N points across domain
'''
_minx, _miny, _maxx, _maxy = self.dem.rio.bounds()
if not self.N:
self.N = int(np.multiply(*self.dem.shape) / 10)
self.x = np.random.uniform(_minx, _maxx, self.N)
self.y = np.random.uniform(_miny, _maxy, self.N)
def trim_and_validate(ls):
if ls is None:
pass
elif ls.is_valid:
return ls
else:
x, y = ls.coords.xy
nans = np.nonzero(~np.isnan(x) | ~np.isnan(y))[0]
x = [x[i] for i in nans]
y = [y[i] for i in nans]
if len(x) > 1:
return LineString(zip(x, y))
else:
pass
def follow_aspect(self, step, reps):
'''
trace downhill path - in aspect direction
'''
_xpoints = self.x.copy()
_ypoints = self.y.copy()
_x = self.x.copy()
_y = self.y.copy()
for q in tqdm(range(reps)):
x_da = xr.DataArray(_x, dims=['index'])
y_da = xr.DataArray(_y, dims=['index'])
theta = self.aspect.sel(
x=x_da,
y=y_da,
method='nearest'
)
grad = self.slope.sel(
x=x_da,
y=y_da,
method='nearest'
)
_dx = xr.where((theta.isnull())
| (theta <= 0)
| (grad.isnull())
| (grad < self.gradient_threshold),
np.nan,
step * np.sin(np.deg2rad(theta)))
_dy = xr.where((theta.isnull())
| (theta <= 0)
| (grad.isnull())
| (grad < self.gradient_threshold),
np.nan,
step * np.cos(np.deg2rad(theta)))
if _dy.isnull().sum().item() == _dy.shape[0]:
# print(f'jumping out after {q}/{reps}')
break
_x += _dx
_y += _dy
_xpoints = np.vstack([_xpoints, _x])
_ypoints = np.vstack([_ypoints, _y])
# make linestrings
self.linestrings = [LineString(zip(
_xpoints[:, i], _ypoints[:, i]))
for i in range(_xpoints.shape[1])
]
# TODO FIX THIS HIDEOUS MESS
# problem with 'None' linestrings and trim_and_validate returning None
# so have to to list comprehension thing twice.
self.linestrings = [ls for ls in self.linestrings if ls is not None]
# ensure the are tidy, valid, and not too short
self.linestrings = [Flow.trim_and_validate(ls)
for ls in self.linestrings]
self.linestrings = [ls for ls in self.linestrings if ls is not None]
self.linestrings = [ls for ls in self.linestrings
if (ls.is_valid) & (ls.length > self.step * 3)]
# smooth the linestring
self.smooth_linestrings = [
taubin_smooth(ls) for ls in self.linestrings
]
# ensure smoothed are tidy, valid and not too short
self.smooth_linestrings = [Flow.trim_and_validate(ls)
for ls in self.smooth_linestrings]
self.smooth_linestrings = [
ls for ls in self.smooth_linestrings if ls is not None
]
self.smooth_linestrings = [
ls for ls in self.smooth_linestrings
if (ls.is_valid) & (ls.length > self.step * 3)
]
def make_line_collection(self, smoothed=True):
def LineString_to_LineCollection(ls, lsb=60):
_sgmnts = []
_azis = []
_colors = []
_alphas = []
_cnorm = Normalize(0, 360)
length = len(ls.coords)
for i, ps in enumerate(
zip(
ls.coords[:-1], ls.coords[1:]
)):
_p1, _p2 = ps
_sgmnts.append(
([_p1, _p2])
)
_xy_diff = np.array([_p2[0] - _p1[0],
_p2[1] - _p1[1]])
_azi = (90
- np.rad2deg(np.arctan2(_xy_diff[1],
_xy_diff[0]))
- lsb)
if _azi < 0:
_azi += 360
_azis.append(_azi)
_colors.append(self.cmap(_cnorm(_azi)))
_alphas.append(1 - i/length)
return _sgmnts, _azis, _colors, _alphas
segments = []
azimuths = []
colors = []
alphas = []
if smoothed:
lines_to_use = self.smooth_linestrings
else:
lines_to_use = self.linestrings
for _ls in lines_to_use:
seg, azi, clr, alp = LineString_to_LineCollection(_ls)
segments += seg
azimuths += azi
colors += clr
alphas += alp
self.segments = segments
self.azimuths = azimuths
self.colors = colors
self.alphas = alphas
def plot(self, ax, lw):
_lc = LineCollection(self.segments,
linewidths=lw,
colors=self.colors,
alpha=self.alphas)
ax.add_collection(_lc)
ax.set(xlim=(self.dem.x.min(), self.dem.x.max()),
ylim=(self.dem.y.min(), self.dem.y.max()))
class Tanaka():
'''
class for constructing tanaka contours
inputs:
dem - digital elevation model - xarray dataarray
method - for classifying elevation bands
eqi - equal interval
nbk - natural breaks
default (eqi)
k - number breaks (int), or list of break points
lsb - light source bearing for illuminting the contours
(default 300)
'''
def __init__(self,
dem: xr.core.dataarray.DataArray,
method: Literal["eqi", "nbk"] = "eqi",
k: tuple[int, list] = 10,
lsb: int = 300):
self.dem = dem
self.method = method
self.k = k
self.lsb = lsb
self.contours = {'cg': None,
'lines': [],
'segments': [],
'azimuths': [],
'widths': [],
'alphas': []}
self.break_reclassify()
self.generate_contours()
self.style_lines()
self.epsg = self.dem.rio.crs.to_epsg()
self.prj = ccrs.epsg(self.epsg)
def break_reclassify(self):
'''
reclassifying DEM, either using user
input (where k is a list), or
by using xrspatial's equal_interval or natural_breaks
'''
if isinstance(self.k, list):
_new_vals = list(range(len(self.k)))
self.classif = reclassify(self.dem,
bins=self.k,
new_values=_new_vals)
else:
if self.method not in ['eqi', 'nbk']:
raise ValueError(
f'{self.method} is not valid method. input \
either "eqi" for equal interval, \
or "nbk" for natural breaks')
if self.method == 'eqi':
self.classif = equal_interval(self.dem, k=self.k)
elif self.method == 'nbk':
self.classif = natural_breaks(self.dem, k=self.k)
# construct dictionary that maps between classified groups
# and the elevation range they span
self.break_dict = {}
for n in np.unique(self.classif.data):
_min = xr.where(self.classif == n, self.dem, np.nan).min().item()
_max = xr.where(self.classif == n, self.dem, np.nan).max().item()
self.break_dict[n] = (_min, _max)
def generate_contours(self):
'''
generates contour lines at each break
'''
self.contours['cg'] = contour_generator(x=self.dem.x.values,
y=self.dem.y.values,
z=self.dem.data)
_breaks = [self.break_dict[0][0]] \
+ [b[1] for b in self.break_dict.values()]
for _b in _breaks:
self.contours['lines'] += [
LineString(_l) for _l in self.contours['cg'].lines(_b)
]
def style_lines(self):
'''
# for each individual contour line, split contour at
# every node and determine azimuth
# and from azimuth define line width
# from contourpy docs:
# Contour line segments are directed with higher z on the left,
# hence closed line loops are oriented anticlockwise if
# they enclose a region that is higher then the contour level,
# or clockwise if they enclose a region that is lower
# than the contour level.
# This assumes a right-hand coordinate system.
# i *think* this means my azimuth calcs are okay
'''
self.lsb = 360-self.lsb # lsb == light source bearing
for _line in tqdm(self.contours['lines']):
# calculate azimuth
for _p1, _p2 in zip(_line.coords, _line.coords[1:]):
self.contours['segments'].append(
LineString([_p1, _p2])
)
_xy_diff = np.array([_p2[0] - _p1[0],
_p2[1] - _p1[1]])
_azi = (90
- np.rad2deg(np.arctan2(_xy_diff[1],
_xy_diff[0]))
- self.lsb)
if _azi < 0:
_azi += 360
self.contours['azimuths'].append(_azi)
self.contours['widths'].append(
0.5*(0.05 + np.abs(np.cos(np.deg2rad(_azi))))
)
self.contours['alphas'] += [(i+2)/len(_line.coords)
for i in range(len(_line.coords)-1)]
def plot_tanaka(self,
ax: matplotlib.axes._axes.Axes,
cmap=False):
'''
create line collection of tanaka contours
and add to matplotlib axes, ax
'''
_segs = [list(map(tuple, zip(*s.coords.xy)))
for s in self.contours['segments']]
if not cmap:
cmap = LinearSegmentedColormap.from_list('wkw', ['w', 'k', 'w'])
else:
cmap = plt.get_cmap(cmap)
_cnorm = Normalize(0, 360)
_colors = cmap(_cnorm(self.contours['azimuths']))
# alphas = (widths - min(widths)) / (max(widths) - min(widths))
# if not alphas:
# colors[:, -1] = alphas
_linecol = LineCollection(_segs,
linewidths=self.contours['widths'],
colors=_colors)
ax.add_collection(_linecol)
_segbounds = np.array(
[seg.bounds for seg in self.contours['segments']]
)
_minx, _miny = np.min(_segbounds[:, [0, 1]], axis=0)
_maxx, _maxy = np.max(_segbounds[:, [2, 3]], axis=0)
ax.set_xlim(_minx, _maxx)
ax.set_ylim(_miny, _maxy)
class SatelliteImage():
'''
quick grab of stack of satellite images
holds lazy stack of satellite images
inputs:
`aoi` - (shapely polygon)
`collection` - list of planetary computer stac catalogs to search
default: `['sentinel-2-l2a', 'landsat-c2-l2']`
`datetime` (str) format must be "yyyy-mm-dd/yyyy-mm-dd"
`resolution` (int) cell size in metres of returned stack
`cloud` (int) (between 0 and 100) % cloud cover filter
`epsg` (int) target projection
`months` (list) - list of ints of months to include
'''
def __init__(self,
aoi: shapely.geometry.polygon.Polygon,
collection: list = ['sentinel-2-l2a',
'landsat-c2-l2'],
datetime: str = "2020-01-01/2030-12-31",
resolution: int = 10,
cloud: int = 25,
epsg: int = None,
months: list = [],
):
validate_type(SatelliteImage, locals=locals())
if not re.match(
'^[0-9]{4}-[0-9]{2}-[0-9]{2}/[0-9]{4}-[0-9]{2}-[0-9]{2}$',
datetime
):
raise ValueError("datetime format must be 'yyyy-mm-dd/yyyy-mm/dd'")
self.aoi = aoi
self.collection = collection
self.datetime = datetime
self.resolution = resolution
self.cloud = cloud
self.epsg = epsg
if self.epsg:
self.prj = ccrs.epsg(self.epsg)
self.months = months
self.get_full_stack()
self.get_stack_contains_aoi()
self.get_least_cloudy()
self.clip()
def get_full_stack(self):
'''
get lazy instance of all items found
in the either the chosen epsg, if supplied,
or the most common crs in the stack
'''
catalog = pystac_client.Client.open(
"https://planetarycomputer.microsoft.com/api/stac/v1",
modifier=planetary_computer.sign_inplace)
search = catalog.search(
collections=self.collection,
intersects=self.aoi,
datetime=self.datetime,
query={"eo:cloud_cover": {'lt': self.cloud}}
)
self.items = search.item_collection()
# filter by months
if len(self.months) == 0:
print(f'found {len(self.items)} items')
else:
# filter items by months
self.items = [
item for item in self.items
if int(item.properties['datetime'].split('-')[1])
in self.months
]
print(f'found {len(self.items)} items in months: {self.months}')
# if target epsg not specified select most common projection in items
# found and use that
if not self.epsg:
_epsgs = [item.properties['proj:epsg'] for item in self.items]
_unq, _cnts = np.unique(_epsgs, return_counts=True)
self.epsg = int(_unq[np.argmax(_cnts)])
self.prj = ccrs.epsg(self.epsg)
# create stack
self.full_stack = stackstac.stack(
self.items,
epsg=self.epsg,
resolution=self.resolution
)
# count of images by satellite platform (landsat / sentinel)
self.platform_count_dict = dict(
zip(*np.unique(self.full_stack.platform,
return_counts=True))
)
def get_stack_contains_aoi(self):
'''
identify items that completely contain the aoi
(if any) and get lazy stack of those items
'''
_bounding_boxes = [shapely.geometry.shape(item.geometry)
for item in self.items]
self.items_containing_aoi = [item for bbox, item
in zip(_bounding_boxes, self.items) if
bbox.contains_properly(self.aoi)
]
if len(self.items_containing_aoi) == 0:
print('no scenes completely contain aoi')
self.containing_stack = None
else:
print(f'{len(self.items_containing_aoi)} contain aoi')
self.containing_stack = stackstac.stack(
self.items_containing_aoi,
epsg=self.epsg,
resolution=self.resolution
)
def get_least_cloudy(self):
'''
identify least cloudy item and get lazy array
if there are scenes that completely contain aoi
pick one of those, otherwise go for least cloudy
of all items
'''
_dict = {'full': self.items,
'contains': self.items_containing_aoi}
if len(self.items_containing_aoi) == 0:
_which = 'full'
else:
_which = 'contains'
self.item_least_cloud = min(
_dict[_which],
key=lambda item: eo.ext(item).cloud_cover
)