簡體   English   中英

如何使用多邊形提取柵格堆棧中每個波段的像素值?

[英]How to extract pixel value of every band in a rasterstack using a polygon?

我正在嘗試從具有多個特征的多邊形中提取柵格的像素值。 我的柵格是一個有 4 個波段的柵格堆棧。 我已經找到了如何為整個光柵執行此操作,但我需要每個波段的信息。 有什么提示嗎?

from rasterstats import zonal_stats
import os
import numpy as np 
import statistics
 
shapefile = Class1.shp
geotiff = tile_105.tif

# calculate all zonal stats
stats = zonal_stats(shapefile, geotiff)

# extract the mean and store it
single_mean = [f['mean'] for f in stats]

val_list = []

# only store the positive values. 
for val in single_mean:
    if val != None :
        val = [float(val)]
      
        val_list.append(val[0])

all_mean = statistics.mean(val_list)

rasterio 讀取[多波段光柵]!

https://rasterio.readthedocs.io/en/latest/topics/reading.html

import rasterio

dataset = rasterio.open('path_to_your_multiband.tif')

您可能還想通過定義裁剪來定義 AOI 的范圍 window ((row_start, row_stop), (col_start, col_stop))

window = ((20, 50), (10, 40))
val_list[]
with rasterio.open(dataset) as src:
    # Create zero array (you may want to set dtype too) for negative values
    # only store the positive values. 
    array = np.zeros((window[0][1] - window[0][0],
                      window[1][1] - window[1][0],
                      len(bands)))
    # Fill the array
    for i, band in enumerate(bands):
        array[:,:,i] = src.read(band, window=window) 
        val_list.append(i[0])

all_mean = statistics.mean(val_list)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM