landsat 8 python library
程序员文章站
2022-07-12 23:48:51
...
read the true color
def landsat8_rgb_show(file_path):
"""
show the true color of landsat 8
file_path: the path of the true color file
"""
file_name = os.path.join(file_path, os.path.basename(file_path) + ".tif")
raster_rgb_array = plt.imread(file_name)
plt.imshow(raster_rgb_array)
plt.show()
calculate the metadata of Lansat-8:
def landsat8_read_meta(file_path, show_flag=False):
file_name = os.path.basename(file_path)[:42] + "_MTL" ".txt"
file_name = os.path.join(file_path, os.path.basename(file_path), file_name)
i = 0
metadata = {}
with open(file_name, "r") as mtl_obj:
data = mtl_obj.readline()
cur_first_key = data.split(" = ")[1].split("\n")[0]
metadata[cur_first_key] = {}
while data[:9] != "END_GROUP":
i = i + 1
data = mtl_obj.readline()
cur_sec_key = data.split(" = ")[1].split("\n")[0]
metadata[cur_first_key][cur_sec_key] = {}
data = mtl_obj.readline()
if data == "END\n":
break
while data[:11] != " END_GROUP":
meta_key = data.split(" ")[1].split(" = ")
if meta_key[1].find("\"") != -1:
metadata[cur_first_key][cur_sec_key][meta_key[0]] = meta_key[1].split("\"")[1]
else:
if meta_key[0] == "FILE_DATE":
metadata[cur_first_key][cur_sec_key][meta_key[0]] = meta_key[1].split("\n")[0]
else:
metadata[cur_first_key][cur_sec_key][meta_key[0]] = meta_key[1].split("\n")[0]
data = mtl_obj.readline()
i = i + 1
if i > 3000:
return
if show_flag:
for key in metadata.keys():
metadata_sec = metadata[key]
for key_sec in metadata_sec.keys():
print("\t" + key_sec)
metadata_third = metadata_sec[key_sec]
for key_third in metadata_third.keys():
print("\t\t" + key_third + "\t:\t" + metadata_third[key_third])
return metadata