Raster Dataset#
Opening a Raster#
Before opening a GDAL raster dataset, it is necessary to register drivers, then create a data object.
from osgeo import gdal
# Register drivers
gdal.AllRegister()
# TIF file
tif = r"C:\Temp\image.tif"
# Data object
dataset = gdal.Open(tif)
Raster Properties#
Dataset object properties: columns, rows, and band number.
# Get image size
cols = dataset.RasterXSize
rows = dataset.RasterYSize
bands = dataset.RasterCount
# Get projection
proj = dataset.GetProjection()
GetGeoTransform is a method on a dataset object to get projection info as a tuple with 6 numbers.
geotr = dataset.GetGeoTransform() # returns (368000.0, 1000.0, 0.0, 8828000.0, 0.0, -1000.0)
Element |
Description |
|---|---|
|
Top left X |
|
W-E pixel resolution |
|
Rotation, 0 if image is “north up” |
|
Top left Y |
|
Rotation, 0 if image is “north up” |
|
N-S pixel resolution |
Value Properties#
Working with pixel values is limited to one TIF band.
band = dataset.GetRasterBand(1)
# Get NoData value
NoData = band.GetNoDataValue()
# Get min and max
vmin = band.GetMinimum()
vmax = band.GetMaximum()
# Get data type
vtype = band.DataType
# Get colour table
coltab = band.GetColorTable()
# Get statistics: min, max, mean, stddev
stats = band.GetStatistics(1, 1)
Raster data types:
Type |
Description |
|---|---|
|
Eight bit unsigned integer |
|
Sixteen bit unsigned integer |
|
Sixteen bit signed integer |
|
Thirty two bit unsigned integer |
|
Thirty two bit signed integer |
|
Thirty two bit floating point |
|
Sixty four bit floating point |
Raster to Array#
# Convert to a 2D array
data = band.ReadAsArray()