📄 gdal_tutorial.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>GDAL: GDAL API Tutorial</title><link href="doxygen.css" rel="stylesheet" type="text/css"><link href="tabs.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.5.1 --><div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li><a href="files.html"><span>Files</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> </ul></div><h1><a class="anchor" name="gdal_tutorial">GDAL API Tutorial</a></h1><h2><a class="anchor" name="gdal_tutorial_open">Opening the File</a></h2>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 <a class="el" href="gdal_8h.html#9d40bc998bd6ed07ccde96028e85ae26">GDALAllRegister()</a> function which attempts to register all known drivers, including those auto-loaded from .so files using <a class="el" href="classGDALDriverManager.html#77417ede570b33695e5b318fbbdb1968">GDALDriverManager::AutoLoadDrivers()</a>. If for some applications it is necessary to limit the set of drivers it may be helpful to review the code from <a href="gdalallregister.cpp.html">gdalallregister.cpp</a>.<p>Once the drivers are registered, the application should call the free standing <a class="el" href="gdal_8h.html#e97be045eb4701183ad332ffce29745b">GDALOpen()</a> function to open a dataset, passing the name of the dataset and the access desired (GA_ReadOnly or GA_Update).<p>In C++: <div class="fragment"><pre class="fragment"><span class="preprocessor">#include "gdal_priv.h"</span><span class="keywordtype">int</span> main(){ <a class="code" href="classGDALDataset.html">GDALDataset</a> *poDataset; <a class="code" href="gdal_8h.html#9d40bc998bd6ed07ccde96028e85ae26">GDALAllRegister</a>(); poDataset = (<a class="code" href="classGDALDataset.html">GDALDataset</a> *) <a class="code" href="gdal_8h.html#e97be045eb4701183ad332ffce29745b">GDALOpen</a>( pszFilename, <a class="code" href="gdal_8h.html#045e3967c208993f70257bfd40c9f1d75a021a550b9d5640307d3c0e7e35b732">GA_ReadOnly</a> ); <span class="keywordflow">if</span>( poDataset == NULL ) { ...; }</pre></div><p>In C: <div class="fragment"><pre class="fragment"><span class="preprocessor">#include "<a class="code" href="gdal_8h.html">gdal.h</a>"</span><span class="keywordtype">int</span> main(){ GDALDatasetH hDataset; <a class="code" href="gdal_8h.html#9d40bc998bd6ed07ccde96028e85ae26">GDALAllRegister</a>(); hDataset = <a class="code" href="gdal_8h.html#e97be045eb4701183ad332ffce29745b">GDALOpen</a>( pszFilename, <a class="code" href="gdal_8h.html#045e3967c208993f70257bfd40c9f1d75a021a550b9d5640307d3c0e7e35b732">GA_ReadOnly</a> ); <span class="keywordflow">if</span>( hDataset == NULL ) { ...; }</pre></div><p>In Python: <div class="fragment"><pre class="fragment"> <span class="keyword">import</span> gdal from gdalconst <span class="keyword">import</span> * dataset = gdal.Open( filename, <a class="code" href="gdal_8h.html#045e3967c208993f70257bfd40c9f1d75a021a550b9d5640307d3c0e7e35b732">GA_ReadOnly</a> ) <span class="keywordflow">if</span> dataset is None: ...</pre></div><p>Note that if <a class="el" href="gdal_8h.html#e97be045eb4701183ad332ffce29745b">GDALOpen()</a> returns NULL it means the open failed, and that an error messages will already have been emitted via <a class="el" href="cpl__error_8h.html#ad2b98dd58e4de706a245faddac90403">CPLError()</a>. If you want to control how errors are reported to the user review the <a class="el" href="cpl__error_8h.html#ad2b98dd58e4de706a245faddac90403">CPLError()</a> documentation. Generally speaking all of GDAL uses <a class="el" href="cpl__error_8h.html#ad2b98dd58e4de706a245faddac90403">CPLError()</a> for error reporting. Also, note that pszFilename need not actually be the name of a physical file (though it usually is). It's interpretation is driver dependent, and it might be an URL, a filename with additional parameters added at the end controlling the open or almost anything. Please try not to limit GDAL file selection dialogs to only selecting physical files.<h2><a class="anchor" name="gdal_tutorial_dataset">Getting Dataset Information</a></h2>As described in the <a href="gdal_datamodel.html">GDAL Data Model</a>, a <a class="el" href="classGDALDataset.html">GDALDataset</a> contains a list of raster bands, all pertaining to the same area, and having the same resolution. It also has metadata, a coordinate system, a georeferencing transform, size of raster and various other information.<p><div class="fragment"><pre class="fragment"> adfGeoTransform[0] <span class="comment">/* top left x */</span> adfGeoTransform[1] <span class="comment">/* w-e pixel resolution */</span> adfGeoTransform[2] <span class="comment">/* rotation, 0 if image is "north up" */</span> adfGeoTransform[3] <span class="comment">/* top left y */</span> adfGeoTransform[4] <span class="comment">/* rotation, 0 if image is "north up" */</span> adfGeoTransform[5] <span class="comment">/* n-s pixel resolution */</span></pre></div><p>If we wanted to print some general information about the dataset we might do the following:<p>In C++: <div class="fragment"><pre class="fragment"> <span class="keywordtype">double</span> adfGeoTransform[6]; printf( <span class="stringliteral">"Driver: %s/%s\n"</span>, poDataset-><a class="code" href="classGDALDataset.html#86db47943e47ae71e97d6ba7a30213d0">GetDriver</a>()-><a class="code" href="classGDALMajorObject.html#f769554a8632dda9f99db325664a37cf">GetDescription</a>(), poDataset-><a class="code" href="classGDALDataset.html#86db47943e47ae71e97d6ba7a30213d0">GetDriver</a>()-><a class="code" href="classGDALMajorObject.html#d17f496c8b6d0c0be6f52437d3ba8a6d">GetMetadataItem</a>( GDAL_DMD_LONGNAME ) ); printf( <span class="stringliteral">"Size is %dx%dx%d\n"</span>, poDataset-><a class="code" href="classGDALDataset.html#2fc4b67ab71fd335171b4a5cd9a87be1">GetRasterXSize</a>(), poDataset-><a class="code" href="classGDALDataset.html#aee2f1b5adabf0b768ed19be15c00f3d">GetRasterYSize</a>(), poDataset-><a class="code" href="classGDALDataset.html#01ed7cc3d711651470212dac01af69a0">GetRasterCount</a>() ); <span class="keywordflow">if</span>( poDataset-><a class="code" href="classGDALDataset.html#a42537e1062ce254d124b29ff3ebe857">GetProjectionRef</a>() != NULL ) printf( <span class="stringliteral">"Projection is `%s'\n"</span>, poDataset-><a class="code" href="classGDALDataset.html#a42537e1062ce254d124b29ff3ebe857">GetProjectionRef</a>() ); <span class="keywordflow">if</span>( poDataset-><a class="code" href="classGDALDataset.html#f9593cc241e7d140f5f3c4798a43a668">GetGeoTransform</a>( adfGeoTransform ) == CE_None ) { printf( <span class="stringliteral">"Origin = (%.6f,%.6f)\n"</span>, adfGeoTransform[0], adfGeoTransform[3] ); printf( <span class="stringliteral">"Pixel Size = (%.6f,%.6f)\n"</span>, adfGeoTransform[1], adfGeoTransform[5] ); }</pre></div><p>In C: <div class="fragment"><pre class="fragment"> GDALDriverH hDriver; <span class="keywordtype">double</span> adfGeoTransform[6]; hDriver = <a class="code" href="gdal_8h.html#115e56894fda0a51ad99820b1bc38391">GDALGetDatasetDriver</a>( hDataset ); printf( <span class="stringliteral">"Driver: %s/%s\n"</span>, GDALGetDriverShortName( hDriver ), GDALGetDriverLongName( hDriver ) ); printf( <span class="stringliteral">"Size is %dx%dx%d\n"</span>, <a class="code" href="gdal_8h.html#4ef08b38a70b6e04f25a81bd82ef0138">GDALGetRasterXSize</a>( hDataset ), <a class="code" href="gdal_8h.html#e0c0af31441c6bac994f35ac26c82f99">GDALGetRasterYSize</a>( hDataset ), <a class="code" href="gdal_8h.html#1b9f888aac1cb4dbc99dc1dc023174b7">GDALGetRasterCount</a>( hDataset ) ); <span class="keywordflow">if</span>( <a class="code" href="gdal_8h.html#639a11014cf6c4ff30df6f21d5db9da2">GDALGetProjectionRef</a>( hDataset ) != NULL ) printf( <span class="stringliteral">"Projection is `%s'\n"</span>, <a class="code" href="gdal_8h.html#639a11014cf6c4ff30df6f21d5db9da2">GDALGetProjectionRef</a>( hDataset ) ); <span class="keywordflow">if</span>( <a class="code" href="gdal_8h.html#df94718221d264fc798043e13adf8c8f">GDALGetGeoTransform</a>( hDataset, adfGeoTransform ) == CE_None ) { printf( <span class="stringliteral">"Origin = (%.6f,%.6f)\n"</span>, adfGeoTransform[0], adfGeoTransform[3] ); printf( <span class="stringliteral">"Pixel Size = (%.6f,%.6f)\n"</span>, adfGeoTransform[1], adfGeoTransform[5] ); }</pre></div><p>In Python: <div class="fragment"><pre class="fragment"> print 'Driver: ', dataset.GetDriver().ShortName,<span class="charliteral">'/'</span>, \ dataset.GetDriver().LongName print 'Size is ',dataset.RasterXSize,<span class="charliteral">'x'</span>,dataset.RasterYSize, \ <span class="charliteral">'x'</span>,dataset.RasterCount print 'Projection is ',dataset.GetProjection() geotransform = dataset.GetGeoTransform() <span class="keywordflow">if</span> not geotransform is None: print 'Origin = (',geotransform[0], <span class="charliteral">','</span>,geotransform[3],<span class="charliteral">')'</span> print 'Pixel Size = (',geotransform[1], <span class="charliteral">','</span>,geotransform[5],<span class="charliteral">')'</span></pre></div><h2><a class="anchor" name="gdal_tutorial_band">Fetching a Raster Band</a></h2>At this time access to raster data via GDAL is done one band at a time. Also, there is metadata, blocksizes, color tables, and various other information available on a band by band basis. The following codes fetches a <a class="el" href="classGDALRasterBand.html">GDALRasterBand</a> object from the dataset (numbered 1 through GetRasterCount()) and displays a little information about it.<p>In C++: <div class="fragment"><pre class="fragment"> <a class="code" href="classGDALRasterBand.html">GDALRasterBand</a> *poBand; <span class="keywordtype">int</span> nBlockXSize, nBlockYSize; <span class="keywordtype">int</span> bGotMin, bGotMax; <span class="keywordtype">double</span> adfMinMax[2]; poBand = poDataset-><a class="code" href="classGDALDataset.html#d96adcf07f2979ad176e37a7f8638fb6">GetRasterBand</a>( 1 ); poBand-><a class="code" href="classGDALRasterBand.html#f2ee6fa0f675d7d52bc19f826d161ad6">GetBlockSize</a>( &nBlockXSize, &nBlockYSize ); printf( <span class="stringliteral">"Block=%dx%d Type=%s, ColorInterp=%s\n"</span>, nBlockXSize, nBlockYSize, <a class="code" href="gdal_8h.html#33b543bd0b1e36598abb7fa9fd39add7">GDALGetDataTypeName</a>(poBand-><a class="code" href="classGDALRasterBand.html#e3ec3bfe18258dd73db3911fed97cc2e">GetRasterDataType</a>()), <a class="code" href="gdal_8h.html#d22541da614932e0c34028fe6fc08074">GDALGetColorInterpretationName</a>( poBand-><a class="code" href="classGDALRasterBand.html#772e1232ac07944e4c6bce3c66f98103">GetColorInterpretation</a>()) ); adfMinMax[0] = poBand-><a class="code" href="classGDALRasterBand.html#aabf419931d0f505428f91cff54085cc">GetMinimum</a>( &bGotMin ); adfMinMax[1] = poBand-><a class="code" href="classGDALRasterBand.html#bd5f6f0b339155484343d887998e29f5">GetMaximum</a>( &bGotMax ); <span class="keywordflow">if</span>( ! (bGotMin && bGotMax) )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -