This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
l8_ovr.py
105 lines (70 loc) · 3.08 KB
/
l8_ovr.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
"""remotepixel.l8_ovr"""
import base64
from io import BytesIO
from concurrent import futures
import numpy as np
from PIL import Image
from rio_toa import reflectance
from remotepixel import utils
np.seterr(divide='ignore', invalid='ignore')
landsat_bucket = 's3://landsat-pds'
def worker(args):
"""
"""
address, band, meta, ovrSize, ndvi = args
MR = float(utils.landsat_mtl_extract(meta, f'REFLECTANCE_MULT_BAND_{band}'))
AR = float(utils.landsat_mtl_extract(meta, f'REFLECTANCE_ADD_BAND_{band}'))
E = float(utils.landsat_mtl_extract(meta, 'SUN_ELEVATION'))
matrix = utils.get_overview(address, ovrSize)
matrix = reflectance.reflectance(matrix, MR, AR, E, src_nodata=0)
if not ndvi:
imgRange = np.percentile(matrix[matrix > 0], (2, 98)).tolist()
matrix = np.where(matrix > 0, utils.linear_rescale(matrix, in_range=imgRange, out_range=[1, 255]), 0).astype(np.uint8)
return matrix
def create(scene, bands=[4, 3, 2], img_format='jpeg', ovrSize=512):
"""
"""
if img_format not in ['png', 'jpeg']:
raise UserWarning(f'Invalid {img_format} extension')
scene_params = utils.landsat_parse_scene_id(scene)
meta_data = utils.landsat_get_mtl(scene)
landsat_address = f'{landsat_bucket}/{scene_params["key"]}'
args = ((f'{landsat_address}_B{band}.TIF', band, meta_data, ovrSize, False) for band in bands)
out = np.zeros((4, ovrSize, ovrSize), dtype=np.uint8)
with futures.ThreadPoolExecutor(max_workers=3) as executor:
out[0:3] = list(executor.map(worker, args))
out[-1] = np.all(np.dstack(out[:3]) != 0, axis=2).astype(np.uint8) * 255
img = Image.fromarray(np.dstack(out))
sio = BytesIO()
if img_format == 'jpeg':
img = img.convert('RGB')
img.save(sio, 'jpeg', subsampling=0, quality=100)
else:
img.save(sio, 'png', compress_level=0)
sio.seek(0)
return base64.b64encode(sio.getvalue()).decode()
def create_ndvi(scene, img_format='jpeg', ovrSize=512):
"""
"""
if img_format not in ['png', 'jpeg']:
raise UserWarning(f'Invalid {img_format} extension')
scene_params = utils.landsat_parse_scene_id(scene)
meta_data = utils.landsat_get_mtl(scene)
landsat_address = f'{landsat_bucket}/{scene_params["key"]}'
bands = [4, 5]
args = ((f'{landsat_address}_B{band}.TIF', band, meta_data, ovrSize, True) for band in bands)
with futures.ThreadPoolExecutor(max_workers=3) as executor:
out = list(executor.map(worker, args))
ratio = np.where((out[1] * out[0]) > 0, np.nan_to_num((out[1] - out[0]) / (out[1] + out[0])), -1)
ratio = np.where(ratio > -1, utils.linear_rescale(ratio, in_range=[-1, 1], out_range=[1, 255]), 0).astype(np.uint8)
cmap = list(np.array(utils.get_colormap()).flatten())
img = Image.fromarray(ratio, 'P')
img.putpalette(cmap)
sio = BytesIO()
if img_format == 'jpeg':
img = img.convert('RGB')
img.save(sio, 'jpeg', subsampling=0, quality=100)
else:
img.save(sio, 'png', compress_level=0)
sio.seek(0)
return base64.b64encode(sio.getvalue()).decode()