1.gdal package Introduction
gdal open source spatial data processing packet, which supports more than 100 types of raster data, covering all major GIS and RS data formats, including Arc / Info ASCII Grid (asc), GeoTiff (tiff), Erdas Imagine Images (img), ASCII DEM (dem) and other formats.
2. Install the package gdal
(1) Find this link and download the package gdal: https://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame (select the corresponding gdal package according to their own systems and python version)
(2) at a dos handover command to package the folder, pip install and run the install package name.
3. Read raster data
# Import gdal package
from osgeo import gdal
# Import numpy package (support matrix operation and high dimensional array, the array also provides a number of functions and matrix operations)
import numpy as np
#open a file
dataset=gdal.Open("fdem.tif")
Number of columns in a grid matrix #
im_width = dataset.RasterXSize
The number of rows of the matrix grid #
im_height = dataset.RasterYSize
# Number of bands
im_bands = dataset.RasterCount
# Affine matrix, geodetic coordinates of the upper left pixel and pixel resolution.
# Total of six parameters, tables represent the upper left corner x coordinate points; the image resolution in the east-west direction; if north upward, the rotation angle of the map, the x-axis and the line parallel to the image represented by 0; y coordinate of the upper left corner;
# If the north upward, the rotation angle of the map, 0 represents a column parallel with the y axis of the image; resolution map north-south direction.
im_geotrans = dataset.GetGeoTransform()
# Map projection information
im_proj = dataset.GetProjection()
# Reads the value of a certain pixel point
# (1) reads a band, the parameters for the index number of the band, the band index number from 1 to start (this image I open only one band)
band=dataset.GetRasterBand(1)
# (2) ReadAsArray (<xoff>, <yoff>, <xsize>, <ysize>), read out from the (XOFF, Yoff) starts, the size (xsize, ysize) matrix. The following is a read entire image
im_datas=band.ReadAsArray(0,0,im_width,im_height)
# (3) obtaining one or several pixels value (see lines 10 to 14 and column data 20 to 25)
data = im_datas [10: 15.20: 26]
# Free up memory. Failure to do so, display the file has been occupied when you open the image in arcgis or envi
del dataset