请问大家,如何在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')