Article Directory
0 Background and configuration environment
0.1 Background
GDAL (Geospatial Data Abstraction Library) is an open source raster spatial data conversion library in X / MIT license agreement. It is expressed in a variety of file formats supported by the use of abstract data model. It also has a series of command-line tools for data conversion and processing.
This open source raster spatial data conversion and database interfaces with many other languages, for the python, he has a corresponding third-party packages GDAL, download and install already mentioned in the previous article.
Objective: Python may use third-party package of: sensing the GDAL read and write data, to facilitate batch processing.
0.2 Configuration Environment
Computer Systems: Win7x64
Python Version: 3.6.4
GDAL Version: 2.3.2
Reading 1
1.1 TIFF format
Tagged Image File Format (Tag Image File Format, abbreviated TIFF) is a flexible bitmap format, mainly used to store images, including photographs and art, including drawing. It was originally printed with a PostScript developed by the Aldus and Microsoft. TIFF and JPEG and PNG with a popular high color image format.
TIFF files with .tif extension.
def tif_read(tifpath, bandnum):
"""
Use GDAL to read data and transform them into arrays.
:param tifpath:tif文件的路径
:param bandnum:需要读取的波段
:return:该波段的数据,narray格式。len(narray)是行数,len(narray[0])列数
"""
image = gdal.Open(tifpath) # 打开该图像
if image == None:
print(tifpath + "该tif不能打开!")
return
lie = image.RasterXSize # 栅格矩阵的列数
hang = image.RasterYSize # 栅格矩阵的行数
im_bands = image.RasterCount # 波段数
im_proj = image.GetProjection() # 获取投影信息
im_geotrans = image.GetGeoTransform() # 仿射矩阵
print('该tif:{}个行,{}个列,{}层波段, 取出第{}层.'.format(hang, lie, im_bands, bandnum))
band = image.GetRasterBand(bandnum) # Get the information of band num.
band_array = band.ReadAsArray(0,0,lie,hang) # Getting data from zeroth rows and 0 columns
# band_df = pd.DataFrame(band_array)
del image # 减少冗余
return band_array, im_proj, im_geotrans
2 write
2.1 TIFF format
TIFF format data formats: Byete, int16, uint16, int32 , uint32, float32, float64 more than seven kinds.
First, you must determine the format of the data to write on demand.
def tif_write(self, filename, im_data, im_proj, im_geotrans):
"""
gdal数据类型包括
gdal.GDT_Byte,
gdal.GDT_UInt16, gdal.GDT_Int16, gdal.GDT_UInt32, gdal.GDT_Int32,
gdal.GDT_Float32, gdal.GDT_Float64
:param filename: 存出文件名
:param im_data: 输入数据
:param im_proj: 投影信息
:param im_geotrans: 放射变换信息
:return: 0
"""
if 'int8' in im_data.dtype.name: # 判断栅格数据的数据类型
datatype = gdal.GDT_Byte
elif 'int16' in im_data.dtype.name:
datatype = gdal.GDT_UInt16
else:
datatype = gdal.GDT_Float32
# 判读数组维数
if len(im_data.shape) == 3:
im_bands, im_height, im_width = im_data.shape
else:
im_bands, (im_height, im_width) = 1,im_data.shape # 多维或1.2维
#创建文件
driver = gdal.GetDriverByName("GTiff") #数据类型必须有,因为要计算需要多大内存空间
dataset = driver.Create(filename, im_width, im_height, im_bands, datatype)
dataset.SetGeoTransform(im_geotrans) #写入仿射变换参数
dataset.SetProjection(im_proj) #写入投影
if im_bands == 1:
dataset.GetRasterBand(1).WriteArray(im_data) #写入数组数据
else:
for i in range(im_bands):
dataset.GetRasterBand(i+1).WriteArray(im_data[i])
del dataset
3 shows
3.1 TIFF format
# 这个展示的效果并不是太好,当做示意图用
def tif_display(self,im_data):
"""
:param im_data: 影像数据,narray
:return: 展出影像
"""
# plt.imshow(im_data,'gray') # 必须规定为显示的为什么图像
plt.imshow(im_data) # 必须规定为显示的为什么图像
plt.xticks([]), plt.yticks([]) # 隐藏坐标线
plt.show() # 显示出来,不要也可以,但是一般都要了