forked from sargassum-busters/ASI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_SCL.py
152 lines (111 loc) · 4.5 KB
/
plot_SCL.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
# Plots the Scene Classification Layer (SCL) of a Sentinel-2 dataset
# ==============================================================================
import glob
import os
import sys
import numpy as np
from PIL import Image
import rasterio
import Sentinel2
# Requires Pillow
# https://pypi.org/project/Pillow/
# ==============================================================================
def plot_SCL(dataset_path, resolution, show_image=True, save_png=False, save_geotiff=False, out_dir="./"):
""" Plots the SCL layer from a Sentinel-2 dataset
Parameters:
dataset_path : string
The path to the SAFE directory with the Sentinel-2 dataset
resolution : int or string
Sentinel-2 spatial resolution to use
Options:
show_image : boolean (default: true)
Whether to display the image
save_png : boolean (default: false)
Whether to save the image to disk in PNG format at full resolution
save_geotiff : boolean (default: false)
Whether to save the image to disk in GeoTIFF format
out_dir : string (default: "./")
The output path where the image is saved if save_png or save_geotiff
are true. Defaults to current working directory.
Returns:
None
"""
# ----------------------------------------------------------------------------
# The SCL classes
# https://earth.esa.int/web/sentinel/technical-guides/sentinel-2-msi/level-2a/algorithm
SCL_classes = {
0: {"name": "NO_DATA", "color": "#000000"},
1: {"name": "SATURATED_OR_DEFECTIVE", "color": "#fb0c00"},
2: {"name": "DARK_AREA_PIXELS", "color": "#3e3e3e"},
3: {"name": "CLOUD_SHADOWS", "color": "#843900"},
4: {"name": "VEGETATION", "color": "#29ff00"},
5: {"name": "NOT_VEGETATED", "color": "#feff00"},
6: {"name": "WATER", "color": "#1500cd"},
7: {"name": "UNCLASSIFIED", "color": "#767271"},
8: {"name": "CLOUD_MEDIUM_PROBABILITY", "color": "#afacab"},
9: {"name": "CLOUD_HIGH_PROBABILITY", "color": "#d1cfcf"},
10: {"name": "THIN_CIRRUS", "color": "#2ccdff"},
11: {"name": "SNOW", "color": "#fd66ff"}
}
# Convert hex value to RGB tuple
def hex_to_rgb(hx):
hx = hx.lstrip("#")
return tuple(int(hx[i:i+2], 16) for i in (0, 2, 4))
# ----------------------------------------------------------------------------
for c in SCL_classes.keys():
SCL_classes[c]["rgb_color"] = hex_to_rgb(SCL_classes[c]["color"])
# Load mask
SCL_path = Sentinel2.load_SCL(dataset_path, resolution, return_path=True)
SCL_im = rasterio.open(SCL_path, driver='JP2OpenJPEG')
SCL = SCL_im.read(1)
base_name = os.path.splitext(os.path.basename(SCL_path))[0]
print("Loaded {}".format(base_name))
nx, ny = SCL.shape
ntot = nx * ny
print("Size {} x {}, counts:".format(nx, ny, SCL.min(), SCL.max()))
unique, counts = np.unique(SCL, return_counts=True)
mask_counts = dict(zip(unique, counts))
for c, count in mask_counts.items():
print("{:<2} {:<25} {:,} ({:.2f}%)".format(c, SCL_classes[c]["name"], count, 100*count/ntot))
if save_png or show_image:
print("Creating image ...")
img_data = np.zeros((ny, nx, 3), dtype=np.uint8)
for c in SCL_classes.keys():
img_data[SCL == c] = SCL_classes[c]["rgb_color"]
img = Image.fromarray(img_data, 'RGB')
out_fname = base_name + ".png"
out_path = os.path.join(out_dir, out_fname)
img.save(out_path)
print("wrote {}".format(out_path))
if show_image:
print("Displaying in external viewer ...")
img.show()
if save_geotiff:
meta = {}
meta["dtype"] = np.uint8
meta["nodata"] = SCL_im.meta["nodata"]
meta["width"] = SCL_im.meta["width"]
meta["height"] = SCL_im.meta["height"]
meta["crs"] = SCL_im.meta["crs"]
meta["transform"] = SCL_im.meta["transform"]
meta["driver"] = "GTiff"
meta["count"] = 1
out_fname = base_name + ".tif"
out_path = os.path.join(out_dir, out_fname)
with rasterio.open(out_path, "w", **meta) as fout:
fout.write(SCL, 1)
print("Wrote {}".format(out_path))
# ==============================================================================
if __name__ == "__main__":
# The path to the SAFE archive directory to process
dataset_path = sys.argv[1]
# Output directory for all results
out_dir = "./"
# Sentinel-2 spatial resolution to use
resolution = 20
# Show image?
show_image = False
# Save image?
save_png = False
save_geotiff = True
plot_SCL(dataset_path=dataset_path, resolution=resolution, show_image=show_image, save_png=save_png, save_geotiff=save_geotiff, out_dir=out_dir)