1

我目前想用像素 QA 波段掩盖堆叠的 Landsat 8 GeoTIFF 文件,以去除云层和云层阴影。到目前为止,我已经成功地遵循了这里的教程,并使用 EarthPy 和 Rasterio 的组合正确绘制了蒙版场景。生成的蒙版场景是一个 NumPy 蒙版数组。

但是,当我尝试将干净数组导出为 GeoTIFF 文件 (clean.tif) 时,生成的文件包含原始场景而不是蒙版场景文件。以下是我拥有的代码:

from rasterio.plot import plotting_extent
import rasterio as rio
import earthpy.plot as ep
import earthpy.mask as em

# Open mask file
with rio.open("mask.tif") as mask_src:
  mask = mask_src.read()
  mask_meta = mask_src.profile

# Open scene file
with rio.open("scene.tif") as scene_src:
  scene = scene_src.read()
  scene_ext = plotting_extent(scene_src)
  scene_trf = scene_src.transform
  scene_meta = scene_src.profile

# Perform masking
clean = em.mask_pixels(scene, mask)

# Print metadata for mask and scene files
print("masked scene shape => " + str(clean.shape))
print("mask meta => " + str(mask_meta))
print("scene meta => " + str(scene_meta))

# Open and write destination tif file
with rio.open("clean.tif", 'w', **scene_meta) as clean_dst:
  clean_dst.write(clean)

# Open destination tif file
with rio.open("clean.tif") as final_dst:
  final = final_dst.read()
  final_ext = plotting_extent(scene_src)

# Plot mask file
ep.plot_bands(mask)
# Plot scene file
ep.plot_rgb(scene, rgb=[4, 3, 2], extent=scene_ext, stretch=True)
# Plot masked scene
ep.plot_rgb(clean, rgb=[4, 3, 2], extent=scene_ext)
# Plot destination tif file
ep.plot_rgb(final, rgb=[4, 3, 2], extent=final_ext, stretch=True)

这里是文件的情节:

  1. 掩码文件:
    掩码文件

  2. 场景文件:
    场景文件

  3. 绘制蒙版数组:
    绘制的掩码数组

  4. 将掩码数组保存到 geotiff:
    将掩码数组保存到 geotiff

是文件和脚本的 Dropbox 链接。对此我真的摸不着头脑,我很感激关于发生了什么以及如何解决它的任何指示。谢谢你们:)

4

1 回答 1

1

修复它,我需要将从原始场景获取的 NoData 值提供给遮罩数组。这是固定的脚本:

from rasterio.plot import plotting_extent
import rasterio as rio
import earthpy.plot as ep
import earthpy.mask as em

# Open mask file
with rio.open("mask.tif") as mask_src:
  mask = mask_src.read()
  mask_meta = mask_src.profile

# Open scene file
with rio.open("scene.tif") as scene_src:
  scene = scene_src.read()
  scene_ext = plotting_extent(scene_src)
  scene_trf = scene_src.transform
  scene_meta = scene_src.profile
  scene_nodata = scene_src.nodata

# Perform masking
clean = em.mask_pixels(scene, mask)
clean = clean.filled(scene_nodata)

# Print metadata for mask and scene files
print("masked scene shape => " + str(clean.shape))
print("mask meta => " + str(mask_meta))
print("scene meta => " + str(scene_meta))

# Open and write destination tif file
with rio.open("clean.tif", 'w', **scene_meta) as clean_dst:
  clean_dst.write(clean)

# Open destination tif file
with rio.open("clean.tif") as final_dst:
  final = final_dst.read()
  final_ext = plotting_extent(scene_src)

# Plot mask file
ep.plot_bands(mask)
# Plot scene file
ep.plot_rgb(scene, rgb=[4, 3, 2], extent=scene_ext, stretch=True)
# Plot masked scene
ep.plot_rgb(clean, rgb=[4, 3, 2], extent=scene_ext)
# Plot destination tif file
ep.plot_rgb(final, rgb=[4, 3, 2], extent=final_ext, stretch=True)

希望这对面临同样问题的人有用。

于 2020-03-05T07:26:08.143 回答