代码拉取完成,页面将自动刷新
同步操作将从 fungis/python-gdal-test 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# -*- coding: utf-8 -*-
"""
@File : 02_栅格HDF文件展示.py
@Author : fungis@163.com
@notice : https://zhuanlan.zhihu.com/p/536879741
"""
import os
import numpy as np
import matplotlib
from matplotlib import cm
import matplotlib.pylab as plt # 画图模块
raster_path = './data-use/asc/waterbodyid.asc' # 输入你的asc文件
if os.path.exists(raster_path) is False:
raise Exception('[Errno 2] 该文件不存在: \'' + raster_path + '\'')
num = 6 # 读取前6行(如果是5行的数据,这里修改为5)
metadata_list = [] # 元数据列表
with open(raster_path, 'r') as rows_raster_path:
for i in range(num):
tem_row = str(rows_raster_path.readline()).strip() # 读取两端剔除空格的字符串
list_str = tem_row.split(" ")
item_tem = list_str[-1] # 取list的最后一个元素,即相应的头文件值
metadata_list.append(item_tem)
# 根据读取结果取值
ncols = int(metadata_list[0])
nrows = int(metadata_list[1])
xllcorner = float(metadata_list[2])
yllcorner = float(metadata_list[3])
cellsize = float(metadata_list[4]) # 像元大小
NODATA_value = float(metadata_list[5]) # 空值默认值
xltcorner = xllcorner
yltcorner = yllcorner + cellsize * nrows
# 转换位置信息更改为asc文件制定的脚点
# 坐标信息
tran = (-180.0, 0.5, 0.0, 90.0, 0.0, -0.5)
new_tran = list(tran)
new_tran[0] = xltcorner
new_tran[3] = yltcorner
geo_trans_form = new_tran
lonmin = geo_trans_form[0]
latmin = geo_trans_form[3]
lonmax = geo_trans_form[0] + nrows * geo_trans_form[1] + ncols * geo_trans_form[2]
latmax = geo_trans_form[3] + nrows * geo_trans_form[4] + ncols * geo_trans_form[5]
extent = [lonmin, lonmax, latmin, latmax]
# 矩阵提取
location_array = np.loadtxt(raster_path, skiprows=6)
row = location_array.shape[0] # 矩阵的行数
columns = location_array.shape[1] # 矩阵的列数
plt.title('gdal-ASC-show') # 设置图框标题为gdal-hdf-show
# 间隔色块
cmap = matplotlib.colors.ListedColormap(
['#81ecec', '#00FF00', '#00b894', '#6666FF', '#0984e3', '#0000FF', '#ffeaa7', '#FFFF00', '#fab1a0', '#ff7675',
'#FF0000'])
newcmp = cm.get_cmap('rainbow', 6)
plt.imshow(location_array, extent=extent, interpolation='nearest', cmap=newcmp)
# plt.imshow(location_array, cmap='rainbow') # 绘图,配色为rainbow
plt.colorbar(label='color bar') # 绘制图例
plt.savefig("./results/20230314.01.png", dpi=300)
plt.show()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。