【无标题】

请问大家,如何在python中对输出的图像增加比例尺,指南针,经纬度,图例等信息啊。以下是我的代码,我根据网上别人的方法,主要是Matploblib,想增加这些元素,但是老不成功嘤嘤嘤。

import numpy as np
import rasterio
from rasterio import mask
import fiona
from rasterio.plot import show

def calculate_ndvi(nir_band, red_band):
    ndvi = (nir_band - red_band) / (nir_band + red_band)
    return ndvi


# 读取红光波段和近红外波段的遥感影像数据
with rasterio.open(
        'F:\landsat\LC81220442021003LGN00\LC08_L1TP_122044_20210103_20210103_01_RT_B5.tif') as nir_ds, rasterio.open(
    'F:\landsat\LC81220442021003LGN00\LC08_L1TP_122044_20210103_20210103_01_RT_B4.tif') as red_ds:
    nir_band = nir_ds.read().astype(np.float32)
    red_band = red_ds.read().astype(np.float32)
    # 计算NDVI
    ndvi = calculate_ndvi(nir_band, red_band)
    # 可以保存NDVI结果为GeoTIFF影像
    ndvi_meta = nir_ds.meta
    ndvi_meta.update(dtype=rasterio.float32, count=1)
    with rasterio.open(r'F:\landsat\ndvi.tif', 'w', **ndvi_meta) as ndvi_ds:
        ndvi_ds.write(ndvi)

# 读取行政区的shapefile并转换为GeoJSON格式
shapefile_path = 'C:\\Users\\ASUS\\Desktop\\广东省\\New_Shapefile.shp'
with fiona.open(shapefile_path) as shapefile:
    shapes = [feature["geometry"] for feature in shapefile]
# Read the raster dataset
with rasterio.open(r'F:\landsat\ndvi.tif') as src:
    # Clip the raster using the shapefile geometry
    clipped, clipped_transform = mask.mask(src, shapes, crop=True)
    # Update the metadata for the clipped dataset
    clipped_meta = src.meta.copy()
    clipped_meta.update({
        'height': clipped.shape[1],
        'width': clipped.shape[1],
        'transform': clipped_transform
    })
# Save the clipped raster to a new file
with rasterio.open('F:\landsat\clipped.tif', 'w', **clipped_meta) as dst:
    dst.write(clipped)
# Read the clipped data array
with rasterio.open('F:\\landsat\\clipped.tif') as clipped_ds:
    clipped_data = clipped_ds.read()  # Read the first (and only) band
# Show the clipped data array
show(clipped_data, cmap='viridis')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值