def plot_month_hist(): fdir = this_root + '1km月值_1978_1985_1995_2005_2018\\' fdir = fdir.decode('gbk') flist = os.listdir(fdir) for year in ['1978','1985','1995','2005','2018']: plt.figure(figsize=(3*4, 4*4)) flag = 0 for f in flist: if year in f: if f.endswith('.tif'): print(f) # flag += 1 # a = a.decode('gbk') if not '2005' in f: mon = f.split('_')[-3] # print(mon) else: mon = f.split('_')[-2] # print(mon) array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array(fdir + f) hist = [] for i in range(len(array)): for j in range(len(array[0])): val = array[i][j] if 0 <= val <= 100: hist.append(val) flag+=1 plt.subplot(4,3,flag) plt.hist(hist,bins=40,normed=1) plt.title(mon) plt.savefig(this_root+year+'.png',dpi=300)
def plot_year_hist(): fdir = this_root + '1km月值_1978_1985_1995_2005_2018\\' fdir = fdir.decode('gbk') flist = os.listdir(fdir) flag = 0 # plt.figure(figsize=(5,25)) flag = 0 for f in flist: if f.endswith('.tif'): # flag+=1 if not '-08-01_' in f: continue flag += 1 # a = a.decode('gbk') print(f) year = f.split('_')[1].split('.')[0] array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array(fdir + f) hist = [] for i in range(len(array)): for j in range(len(array[0])): val = array[i][j] if 0 <= val <= 100: hist.append(val) plt.subplot(1,5,flag) plt.hist(hist,bins=40,normed=1) plt.title(year) if flag == 5: break # plt.xticks([]) # plt.yticks([] plt.show()
def quanqu(): fdir = r'D:\FVC\FVC_1km_new\FVC-year_1km_clipped\\' # fdir = fdir.decode('gbk') flist = os.listdir(fdir) fw = open(fdir+'quanqu.csv','w') for f in flist: if f.endswith('.tif'): # print(f) year = f.split('.')[0].split('fvc')[1] # print(a.decode('gbk')) # exit() array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array(fdir+f) # plt.imshow(array) # plt.colorbar() # plt.show() fenmu = 0. gao = 0. zhonggao = 0. zhong = 0. zhongdi = 0. di = 0. for i in range(len(array)): for j in range(len(array[0])): val = array[i][j]*100 if 0<=val<=100: fenmu += 1. if 75<val<=100: gao+=1. elif 60<val<=75: zhonggao+=1. elif 45<val<=60: zhong+=1. elif 30<val<=45: zhongdi+=1. elif 0<=val<=30: di+=1. else: print(val) gao_ratio = round(gao/fenmu,4)*100. zhonggao_ratio = round(zhonggao/fenmu,4)*100. zhong_ratio = round(zhong/fenmu,4)*100. zhongdi_ratio = round(zhongdi/fenmu,4)*100. di_ratio = round(di/fenmu,4)*100. a = '{}年内蒙古植被高覆盖区面积为{}平方千米,占全区总面积比例为{}%,' \ '植被中高覆盖区面积为{}平方千米,占全区总面积比例为{}%,' \ '植被中覆盖区面积为{}平方千米,占全区总面积比例为{}%,' \ '植被中低覆盖区面积为{}平方千米,占全区总面积比例为{}%,' \ '植被低覆盖区面积为{}平方千米,占全区总面积比例为{}%'\ .format(year,gao,gao_ratio, zhonggao,zhonggao_ratio, zhong,zhong_ratio, zhongdi,zhongdi_ratio, di,di_ratio) b='{},'*5 c=b.format(gao,zhonggao,zhong,zhongdi,di) # print(c) print(a.decode('gbk')) fw.write(','.join(c.split(',')) + '\n')
def gen_dem_npy(): # for the purpose of generate non standardized dem dic tif = r'E:\MODIS\DEM\china_1km_dem_wgs1984_resample.tif' array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array( tif) np.save(r'E:\MODIS\DEM\china_1km_dem_wgs1984_resample_non_normalize', array)
def gen_normalize_dem_tif(): # for the purpose of examination arr_dem = np.load(r'E:\MODIS\DEM\dem_normalize.npy') plt.imshow(arr_dem) plt.colorbar() plt.show() tif = r'E:\MODIS\DEM\china_1km_dem_wgs1984_resample.tif' array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array( tif) to_raster.array2raster( r'E:\MODIS\DEM\china_1km_dem_wgs1984_resample_normalize.tif', originX, originY, pixelWidth, pixelHeight, arr_dem)
def plot_fenqu(): fdir = 'E:\\FVC内蒙古植被覆盖数据\\30m年值_1978_1985_1995_2005_2018_clipped\\'.decode('gbk') outdir = this_root+'png\\' if not os.path.isdir(outdir): os.makedirs(outdir) shp_name = ['北方风沙区', '北方土石山区', '东北黑土区', '西北黄土高原区'] flist = os.listdir(fdir) for y in ['1978','1985','1995','2005','2018']: arrs = [] names = [] for z in shp_name: for f in flist: if f.endswith('.tif'): zone = f.split('_')[-1].split('.')[0].encode('gbk') year = f.split('_')[1].split('.')[0] if zone == z and y == year: array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array(fdir+f) arrs.append(array) names.append(z.decode('gbk')) fig, axs = plt.subplots(2, 2, figsize=(9,7)) images = [] flag = 0 for i in range(2): for j in range(2): arr = arrs[flag] # classified = classify_arr(arr) # exit() arr = np.ma.masked_where(arr>120,arr) # sns. # plt.imshow() cmap = ["#734e00", "#a87001", "#effa91", "#b1cc63", "#5d8c22"] cmap_ = col.LinearSegmentedColormap.from_list('mycol',cmap) cm.register_cmap(cmap=cmap_) images.append(axs[i,j].imshow(arr, cmap='mycol',vmin=0,vmax=100)) # images.append(axs[i, j].imshow(arr,'jet')) axs[i, j].set_title(names[flag]) axs[i,j].set_xticks([]) axs[i,j].set_yticks([]) # fig.yticks([]) # fig.yticks([]) flag += 1 fig.suptitle(y+'年内蒙古4大区域植被盖度(%)'.decode('gbk')) fig.colorbar(images[0], ax=axs, orientation='horizontal', shrink=0.7, pad=0.05) plt.show() # plt.savefig(outdir+y+'.png',dpi=300) pass
def modify_2018_1km_tif(): fdir = u'E:\\FVC内蒙古植被覆盖数据\\1km月值_1978_1985_1995_2005_2018\\' flist = os.listdir(fdir) selected = [] for f in flist: if f.endswith('.tif') and '_2018-' in f: print(f) selected.append(f) selected = selected[::2] print() for f in selected: # print(f) array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array(fdir+f) break # arrs.append(array) maxarr = np.zeros_like(array) for f in selected: print(f) array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array(fdir+f) maxarr = np.maximum(maxarr,array) to_raster.array2raster(fdir+'2018_new.tif',originX, originY, pixelWidth, pixelHeight,maxarr)
def chutu(): ### 内存不足 无法运行 #### fdir = u'E:\\FVC内蒙古植被覆盖数据\\30m月值_1978_1985_1995_2005_2018\\' flist = os.listdir(fdir) for f in flist: if f.endswith('.tif'): array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array(fdir+f) array = np.array(array) array = np.ma.masked_where(array<0,array) array = np.ma.masked_where(array>100,array) plt.imshow(array,'BrBG',vmin=0,vmax=100) # plt.colorbar() plt.xticks([]) plt.yticks([]) plt.title(f) plt.savefig(f+'.png',dpi=300) exit()
def cal_trend_30m(): fdir = u'E:\\FVC内蒙古植被覆盖数据\\30m年值_1978_1985_1995_2005_2018\\' # save_file = 'E:\\FVC内蒙古植被覆盖数据\\npy\\1km月值' flist = os.listdir(fdir) row = 66189 col = 80941 print('create void dic') for r in tqdm(range(row)): void_dic = {} for c in range(col): void_dic['%d.%d'%(r,c)] = [] for f in flist: print(f) if f.endswith('.tif'): array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array(fdir + f) print(np.shape(array)) exit()
def contour(): fdir = u'E:\\FVC内蒙古植被覆盖数据\\1km年值_1978_1985_1995_2005_2018\\' outdir = u'E:\\FVC内蒙古植被覆盖数据\\tif\\' flist = os.listdir(fdir) arr_sum = 0 for f in flist: if f.endswith('.tif'): print(f) array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array(fdir + f) array = np.array(array,dtype='float64') arr_sum += array arr_mean = arr_sum/5. grid = arr_mean > -9999 arr_mean[np.logical_not(grid)] = 255 # plt.imshow(arr_mean) # plt.colorbar() # plt.show() # exit() to_raster.array2raster(outdir+'mean.tif',originX, originY, pixelWidth, pixelHeight,arr_mean)
def cal_trend(): fdir = u'E:\\FVC内蒙古植被覆盖数据\\1km年值_1978_1985_1995_2005_2018\\' save_file = 'E:\\FVC内蒙古植被覆盖数据\\npy\\1km年值_new' flist = os.listdir(fdir) arrs = [] for y in [1978,1985,1995,2005,2018]: for f in flist: if f.endswith('.tif') and str(y) in f: print(f) # continue array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array(fdir+f) arrs.append(array) # exit() _,row,col = np.shape(arrs) trend = [] for r in tqdm(range(row)): temp = [] for c in range(col): vals = [] for arr in arrs: val = arr[r][c] vals.append(val) if vals[0] < 0: temp.append(-999999) continue else: fit = stats.linregress(range(len(vals)),vals) slope = float(fit.slope) temp.append(slope) # print stats.pearsonr(range(len(vals)),vals) trend.append(temp) trend = np.array(trend) # plt.imshow(trend) # plt.colorbar() # plt.show() # print('saving') np.save(save_file,trend)
def mean_1km(): fdir = r'E:\1km_fvc_monthly\glass\\' out_dir = r'E:\1km_fvc_annual\glass\\' flist = os.listdir(fdir) one_year = {} for y in range(2000, 2017): one_year[str(y)] = [] for f in flist: year = f.split('_')[1].split('-')[0] for y in range(2000, 2017): if year == str(y): one_year[year].append(f) for y in one_year: print(y) array_sum = 0. flag = 0. for mon in one_year[y]: array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array( fdir + mon) # array = np.ma.masked_where(array == 255, array) # plt.imshow(array, 'jet') # plt.colorbar() # plt.show() array_sum += array flag += 1. array_mean = array_sum / flag # array_mean = np.ma.masked_where(array_mean>254,array_mean) newRasterfn = out_dir + y + '.tif' longitude_start = originX latitude_start = originY pixelWidth = pixelWidth pixelHeight = pixelHeight array = array_mean to_raster.array2raster(newRasterfn, longitude_start, latitude_start, pixelWidth, pixelHeight, array)
def data_transform(self): # 不可并行,内存不足 mode = 'SWE_max' # mode = 'SWE_avg' fdir = this_root+'GLOBSWE\\tif\\'+mode+'\\' outdir = this_root+'GLOBSWE\\per_pix\\'+mode+'\\' analysis.Tools().mk_dir(outdir,force=True) # 将空间图转换为数组 # per_pix_data flist = os.listdir(fdir) date_list = [] for y in range(1982,2016): for mon in range(1,13): if mon in range(5,10): continue date_list.append('{}{}'.format(y,'%02d'%mon)) # for i in date_list: # print i # exit() all_array = [] for d in tqdm(date_list, 'loading...'): for f in flist: # if not d in f: # continue if f.endswith('.tif'): if f.split('.')[0] == d: array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array(fdir + f) all_array.append(array) # print len(all_array) # exit() row = len(all_array[0]) col = len(all_array[0][0]) void_dic = {} void_dic_list = [] for r in range(row): for c in range(col): void_dic['%03d.%03d' % (r, c)] = [] void_dic_list.append('%03d.%03d' % (r, c)) # print(len(void_dic)) # exit() for r in tqdm(range(row), 'transforming...'): for c in range(col): for arr in all_array: val = arr[r][c] void_dic['%03d.%03d' % (r, c)].append(val) # for i in void_dic_list: # print(i) # exit() flag = 0 temp_dic = {} for key in tqdm(void_dic_list, 'saving...'): flag += 1 # print('saving ',flag,'/',len(void_dic)/100000) temp_dic[key] = void_dic[key] if flag % 10000 == 0: # print('\nsaving %02d' % (flag / 10000)+'\n') np.save(outdir + 'per_pix_dic_%03d' % (flag / 10000), temp_dic) temp_dic = {} np.save(outdir + 'per_pix_dic_%03d' % 0, temp_dic)
fdir = r'F:\FVC内蒙古植被覆盖数据\1km月值_1978_1985_1995_2005_2018\\' save_dir = r'F:\FVC内蒙古植被覆盖数据\1km_monthly\\' flist = os.listdir(fdir) time_init = time.time() flag = 0 for f in flist: time_start = time.time() if not f.endswith('.tif'): continue # print(f) # exit() file_name = (fdir+f).decode('gbk').encode('utf-8') array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array(file_name) new_array = [] flag1 = 0 for i in array: flag1 += 1 # print flag1,'/',len(array) temp = [] for j in i: if j < -999: val = 255 elif -999 < j < 0: val = 0 elif 0 <= j <=1: val = int(j*100) elif j > 1:
def fenqu(): # fdir = r'D:\FVC\FVC_1km_new\4fenqu\\'.decode('gbk') # fdir = r'D:\FVC\重点区域范围\1km_fenqu_clipped\\'.decode('gbk') fdir = r'E:\FVC内蒙古植被覆盖数据\FVC_D\重点1\1km_clipped\\'.decode('gbk') # shp_name = ['北方风沙区', '北方土石山区', '东北黑土区', '西北黄土高原区'] # shp_name = ['太行山山地丘陵区', '宁蒙覆沙黄土丘陵区', '晋陕蒙丘陵沟壑区', '燕山及辽西山地丘陵区'] # shp_name = ['内蒙古草地', '鄂尔多斯市', '内蒙古农牧交错带'] shp_name = ['赤峰市', '鄂尔多斯', '通辽市', '准格尔旗'] flist = os.listdir(fdir) fw = open(fdir+'result_.csv','w') for f in flist: if f.endswith('.tif'): # print(f.decode('gbk')) zone = f.split('_')[-1].split('.')[0].encode('gbk') # year = f.split('_')[0].split('fvc')[1] year = f.split('_')[0] # print(year) # exit() # print(zone) array, originX, originY, pixelWidth, pixelHeight = to_raster.raster2array(fdir + f) # plt.imshow(array) # plt.colorbar() # plt.show() fenmu = 0. gao = 0. zhonggao = 0. zhong = 0. zhongdi = 0. di = 0. # plt.imshow(array) # plt.show() for i in range(len(array)): for j in range(len(array[0])): val = array[i][j] if 0 <= val <= 100: fenmu += 1. if 75 < val <= 100: gao += 1. elif 60 < val <= 75: zhonggao += 1. elif 45 < val <= 60: zhong += 1. elif 30 < val <= 45: zhongdi += 1. elif 0 <= val <= 30: di += 1. else: print(val) gao_ratio = round(gao / fenmu, 4) * 100. zhonggao_ratio = round(zhonggao / fenmu, 4) * 100. zhong_ratio = round(zhong / fenmu, 4) * 100. zhongdi_ratio = round(zhongdi / fenmu, 4) * 100. di_ratio = round(di / fenmu, 4) * 100. gao = int(gao) zhonggao = int(zhonggao) zhong = int(zhong) zhongdi = int(zhongdi) di = int(di) a = '{}年内蒙古{}植被高覆盖区面积为{}平方千米,占全区总面积比例为{}%;' \ '植被中高覆盖区面积为{}平方千米,占全区总面积比例为{}%;' \ '植被中覆盖区面积为{}平方千米,占全区总面积比例为{}%;' \ '植被中低覆盖区面积为{}平方千米,占全区总面积比例为{}%;' \ '植被低覆盖区面积为{}平方千米,占全区总面积比例为{}%。' \ .format(year,zone,gao, gao_ratio, zhonggao, zhonggao_ratio, zhong, zhong_ratio, zhongdi, zhongdi_ratio, di, di_ratio) b = '{},'*12 c = b.format(year,zone,gao, gao_ratio, zhonggao, zhonggao_ratio, zhong, zhong_ratio, zhongdi, zhongdi_ratio, di, di_ratio) print(a.decode('gbk')) fw.write(','.join(c.split(',')[2:])+'\n')