import sys
import gdal
from gdalconst import *
import numpy as np
from osr import SpatialReference
fn = r'H:\JinJiang_interpret\1. pro_data_par\a.image\b.raster_lable\119042LC0820130703_raster.tif'
print(fn)
ds = gdal.Open(fn,GA_ReadOnly)
band = ds.GetRasterBand(1)
data=band.ReadAsArray()
#获得栅格数据的唯一值
unique_values = np.unique(np.array(ds.GetRasterBand(1).ReadAsArray()))
print(unique_values)
#把唯一值变成列表
unique_values_list = unique_values.tolist()
print(unique_values_list)
#把栅格图读取成list
data_list = sum(data.tolist(), [])
#看看栅格图里面的唯一值都对应的数量
unique_values_counts = []
for i in unique_values_list:
unique_values_counts.append(list.count(i))
print(unique_values_counts)
用data.tolist()弄出来的是tm的有子列表的列表,还用用sum函数去再搞一下,妈的,搞死心态了!
|