numpy 使用Rasterio导出多波段Earthpy遮罩GeoTIFF,生成原始非遮罩文件

zynd9foi  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(135)

我目前想用Pixel QA波段遮罩一个堆叠的Landsat 8 GeoTIFF文件,以移除云和云阴影。到目前为止,我已经成功地遵循了教程here,并使用EarthPy和Rasterio的组合正确绘制了蒙版场景。生成的遮罩场景是NumPy遮罩阵列。
但是,当我尝试将干净的阵列导出为GeoTIFF文件(干净。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.掩码文件:

1.场景文件:

1.绘制的掩码阵列:

1.已将掩码数组保存到geotiff:

Here是指向文件和脚本的Dropbox链接。我真的抓伤了我的头,我感谢任何关于发生了什么以及如何修复它的指示。谢谢大家:)

slwdgvem

slwdgvem1#

修复了它,我需要将从原始场景获取的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)

希望这对那些面临同样问题的人有帮助。

相关问题