Start using GDAL in Python
From the utility provided by GDAL, many programs have suffixes of .py
, which fully demonstrates that the Python language has been widely used in the development of GDAL.
Import GDAL in Python
To use GDAL in Python, you only need to import the gdal
module. In earlier versions (before 1.5), GDAL was imported using the following statement:
>>> import gdal
But after GDAL became a sub-project of OSGEO, its code was reorganized. In GDAL RFC17 file , implemented Python’s new namespace osgeo
and gdal
and ogr
are both included under this namespace.
RFC (Request For Comments), which means “request for comment”, a series of documents scheduled by number. When an organization or group develops a set of standards or proposes a plan for a certain standard, and wants to consult outsiders, it will issue an RFC on the Internet. Those who are interested in this issue can read it. The RFC also put forward its own opinions.
For Python 1.6 and later versions, it is recommended to import using the following statement:
>>> from osgeo import gdal
Of course, early versions also support importing GDAL modules, but in some versions there is a discard warning:
>>> import gdal
usr(/lib/python2.6/dist-packages/osgeo/gdal.py:99: ...
In the latest version, the discard warning disappeared.
To maintain compatibility, you can use the following statement to import the gdal
module:
>>> try:
... import gdal
... except:
... from osgeo import gdal
In addition to the gdal
package, there is also a gdalconst
package that is also imported. gdalconst
is also a package for osgeo
, which binds some constants used in GDAL. The constants in gdalconst
are prefixed, trying to minimize the conflict with other modules. So the gdalconst
can be imported directly like this:
>>> from osgeo.gdalconst import *
Raster Data Driver in GDAL
To read certain types of data, you must first load the Driver, which initializes an object so that it “knows” some data structure. The following statement can be used to register all data drivers at one time, but only read but not write:
>>> gdal.AllRegister()
Instead of registering a type of data driver separately, you can either read or write, or create a data set. (This ultimately depends on whether GDAL has been implemented). Some different types of data-driven encodings have already been introduced in the table [tab:gdaL_format].
The following statement registers Erdas’s raster data type.
>>> driver = gdal.GetDriverByName('HFA')
>>> driver.Register()
5
You can use the following statement to determine whether the driver registered successfully.
>>> driver = gdal.GetDriverByName('GeoTiff')
>>> driver == None
True
The above registration failed because there is no data format named GeoTiff
(the correct format is GTiff
).
View the data formats supported by the system
GDAL can not only use GetDriverByName()
to get the driver, but also GetDriver()
. The following code gets the names of all the drivers supported by the system.
>>> drv_count = gdal.GetDriverCount()
>>> drv_count
202
For different Linux distributions, and the version of GDAL installed and the compilation options are different, the results of the above program are different. So in general, avoid using gdal.GetDriver()
and use the gdal.GetDriverByName()
function to get the driver.
It can be seen that in different Debian distributions, the number of GDAL support drivers is still relatively large (table [tab_gdal_drv_num]). This aspect shows that the development of GDAL is still very fast, on the other hand, it also shows that GDAL is now relatively mature.
Debian version |
Number of Drivers Supported |
---|---|
Debian Squeeze |
88 |
Debian Wheezy |
114 |
Debian Jessie |
120 |
Debian Stretch |
202 |
>>> for idx in range(10):
... driver = gdal.GetDriver(idx)
... print( "%10s: %s" % (driver.ShortName, driver.LongName))
...
VRT: Virtual Raster
GTiff: GeoTIFF
NITF: National Imagery Transmission Format
RPFTOC: Raster Product Format TOC format
ECRGTOC: ECRG TOC format
HFA: Erdas Imagine Images (.img)
SAR_CEOS: CEOS SAR Image
CEOS: CEOS Image
JAXAPALSAR: JAXA PALSAR Product Reader (Level 1.1/1.5)
GFF: Ground-based SAR Applications Testbed File Format (.gff)
In line 4 above, the index value is used directly to get the driver, and on the fifth line, the name of the driver is printed. The driver has two properties: ShortName
and LongName
. ShortName
is consistent with the raster data format defined in GDAL, and LongName
is descriptive text.