-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_map_plot.py
More file actions
59 lines (51 loc) · 1.94 KB
/
Copy pathstack_map_plot.py
File metadata and controls
59 lines (51 loc) · 1.94 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
import gdal
import os
from pathlib import Path
import numpy as np
import plotly.graph_objects as go
import pdb
def test_stack_map_plot():
data_dir = Path("./data")
raster_list = [str(x) for x in data_dir.glob("*.tif")]
stack_map_plot(raster_list)
def stack_map_plot(raster_path_list, label_spec = None, colormap_spec=None):
data = []
zlabel = []
zpos = []
for ii, raster_path in enumerate(raster_path_list):
# Get and filter raster values
band = gdal.Open(raster_path)
band_array = band.ReadAsArray()
nodata = band_array.min()
band_array[band_array == nodata] = np.nan
# Calulate x and y axis from geotransforms: top-left corner and stride from GT, number
# of strides from RasterX/Ysize
gt = band.GetGeoTransform()
X = np.arange(gt[0], gt[0] + gt[1]*band.RasterXSize, gt[1])
Y = np.arange(gt[3], gt[3] + gt[5]*band.RasterYSize*-1, gt[5]*-1) # Geotransforms are a pain..
# Set Z to offsets and build colormap
Z = np.ones(band_array.shape)
Z = Z+(ii*10)
Z[band_array == nodata] = np.nan
nan_colorscale = [[0,'grey'],
[0.01, 'rgba(0,0,0,1)'],
[0.01, 'darkgoldenrod'],
[1, 'mistyrose']]
data.append(go.Surface(x=X, y= Y, z=Z, surfacecolor=band_array, connectgaps = False,
colorscale=nan_colorscale, cmin = band_array.min(), cmax = band_array.max()))
# Setting labels and positions
zlabel.append(os.path.basename(raster_path))
zpos.append(1+(ii*10))
if ii > 0:
data[ii].showscale=False
#Creating figure and setting
fig = go.Figure(data=data)
fig.update_layout(scene = {
"zaxis": {
"ticktext":zlabel,
"tickvals":zpos
},
"xaxis_title": "lon",
"yaxis_title": "lat"
})
fig.show()