GDAL API Tutorial. Before opening a GDAL supported raster datastore it is necessary to register drivers.
There is a driver for each supported format. Normally this is accomplished with the GDALAllRegister() function which attempts to register all known drivers, including those auto-loaded from .so files using GDALDriverManager::AutoLoadDrivers(). If for some applications it is necessary to limit the set of drivers it may be helpful to review the code from gdalallregister.cpp. Python automatically calls GDALAllRegister() when the gdal module is imported. Once the drivers are registered, the application should call the free standing GDALOpen() function to open a dataset, passing the name of the dataset and the access desired (GA_ReadOnly or GA_Update).
In C++: int main() if( poDataset == NULL ) In C: if( hDataset == NULL ) In Python: import gdal from gdalconst import * if dataset is None: adfGeoTransform adfGeoTransform adfGeoTransform adfGeoTransform adfGeoTransform adfGeoTransform int bGotMin, bGotMax; Raster Layers — Python GDAL/OGR Cookbook 1.0 documentation. Close a raster dataset This recipe shows how to close a raster dataset.
It is useful in the middle of a script, to recover the resources held by accessing the dataset, remove file locks, etc. It is not necessary at the end of the script, as the Python garbage collector will do the same thing automatically when the script exits. Further discussion of this topic can be found in this GIS StackExchange question. import gdal # open datasetds = gdal.Open('test.tif') # close datasetds = None Get Raster Band Get a raster band. From osgeo import gdalimport sys# this allows GDAL to throw Python Exceptionsgdal.UseExceptions() try: src_ds = gdal.Open( "INPUT.tif" )except RuntimeError, e: print 'Unable to open INPUT.tif' print e sys.exit(1) try: srcband = src_ds.GetRasterBand(1)except RuntimeError, e: # for example, try GetRasterBand(10) print 'Band ( %i ) not found' % band_num print e sys.exit(1) Loop Through All Raster Bands Get Raster Band Information Polygonize a Raster Band Calculate zonal statistics.
GeoExamples: Raster classification with GDAL Python. There is a new entry about this topic, with a much more efficient code: Classifying a raster means assigning a set of discrete values from the original continuous raster data.
You can think about it as colorizing a raster file using data intervals, but it has many other uses, of course. We will use the Seamless Data Warehouse page from the USGS to get some data. Just open the following page and use the download section tools to get the data from the place you prefer: The result is a file in the sub folder with a number. So be sure that the GDAL python bindings are installed and execute the script: To classify the values is necessary to check for each classification value, until it fits to the interval.
And how the classified file looks like. A nice exercice would be generating the coloured png's directly from the python script from a color file. Gdal_reclassify/gdal_reclassify.py at master · chiatt/gdal_reclassify. Raster Layers — Python GDAL/OGR Cookbook 1.0 documentation. GeoExamples: GDAL performance: raster classification using NumPy. I wrote some time ago two posts about raster classification and how to colorize a raster using GDAL.
Both examples work well, but they are terribly inefficient and won't process a really big raster. In this post, I'll show how to work properly with rasters, specially with big rasters or when the processing time matters, such as on the fly raster generation processes. Before continuing, I recommend to read this course about Geoprocessing by Chris Garrard, that helped me a lot. As usual, you can find all the code at GitHub The original code To learn about the major errors in my former posts, the best is to look at the code: Reading the raster without using the ReadAsArray function (lines 26 to 29). New code The following code has all the changes together, but at the GitHub you can find different versions applying them one by one: At line 46, you can see how ReadAsArray works. Performance improvements. Chiatt/gdal_reclassify. GDAL/OGR user docs. GDAL Utilities gdalinfogdal_translategdaladdogdalwarpgdaltindexgdalbuildvrtgdal_contourgdaldemrgb2pct.pypct2rgb.pygdal_merge.pygdal2tiles.pygdal_rasterizegdaltransformnearblackgdal_retile.py gdal_grid gdal_proximity.pygdal_polygonize.pygdal_sieve.pygdal_fillnodata.pygdallocationinfogdal-configGDAL Raster Formats AIRSAR -- AIRSAR Polarimetric Format BAG --- Bathymetry Attributed Grid See Also:BLX -- Magellan BLX Topo File FormatBMP --- Microsoft Windows Device Independent BitmapCOSAR -- TerraSAR-X Complex SAR Data Product DODS -- OPeNDAP Grid Client DTED -- Military Elevation DataECW -- ERDAS Compress Wavelets (.ecw)ELAS - Earth Resources Laboratory Applications Software Epsilon - Wavelet compressed images NetCDF: Network Common Data Form NITF -- National Imagery Transmission FormatOGDI -- OGDI Bridge OZI -- OZF2/OZFX3 raster See alsoJAXA PALSAR Processed Products PCIDSK --- PCI Geomatics Database File Geospatial PDFPDS -- Planetary Data SystemSee Also:Rasdaman GDAL driver.