⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gdal_tutorial.html

📁 gdal库的学习文档
💻 HTML
📖 第 1 页 / 共 3 页
字号:
            <a class="code" href="gdal_8h.html#2b36986db233e04861c43a6d3ae893e2">GDALComputeRasterMinMax</a>((GDALRasterBandH)poBand, TRUE, adfMinMax);        printf( <span class="stringliteral">"Min=%.3fd, Max=%.3f\n"</span>, adfMinMax[0], adfMinMax[1] );                <span class="keywordflow">if</span>( poBand-&gt;<a class="code" href="classGDALRasterBand.html#20f7cee65f2a3578dc702fe2477bfc5c">GetOverviewCount</a>() &gt; 0 )            printf( <span class="stringliteral">"Band has %d overviews.\n"</span>, poBand-&gt;<a class="code" href="classGDALRasterBand.html#20f7cee65f2a3578dc702fe2477bfc5c">GetOverviewCount</a>() );        <span class="keywordflow">if</span>( poBand-&gt;<a class="code" href="classGDALRasterBand.html#2c5cad0f0fc1b6e261d1e965a5b49969">GetColorTable</a>() != NULL )            printf( <span class="stringliteral">"Band has a color table with %d entries.\n"</span>,                      poBand-&gt;<a class="code" href="classGDALRasterBand.html#2c5cad0f0fc1b6e261d1e965a5b49969">GetColorTable</a>()-&gt;GetColorEntryCount() );</pre></div><p>In C: <div class="fragment"><pre class="fragment">        GDALRasterBandH hBand;        <span class="keywordtype">int</span>             nBlockXSize, nBlockYSize;        <span class="keywordtype">int</span>             bGotMin, bGotMax;        <span class="keywordtype">double</span>          adfMinMax[2];                hBand = <a class="code" href="gdal_8h.html#2a74e5e34528589303c1521ebfb9c162">GDALGetRasterBand</a>( hDataset, 1 );        <a class="code" href="gdal_8h.html#174cd06c70a4534f4f00d0427f77f45a">GDALGetBlockSize</a>( hBand, &amp;nBlockXSize, &amp;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>(<a class="code" href="gdal_8h.html#2edba2a096915aa63e7ca0bf4c383bd0">GDALGetRasterDataType</a>(hBand)),                <a class="code" href="gdal_8h.html#d22541da614932e0c34028fe6fc08074">GDALGetColorInterpretationName</a>(                    <a class="code" href="gdal_8h.html#ec13128878a5f4e4a96605c4b6c71d6f">GDALGetRasterColorInterpretation</a>(hBand)) );        adfMinMax[0] = <a class="code" href="gdal_8h.html#3a2e32244e8e906238171efa0af767ba">GDALGetRasterMinimum</a>( hBand, &amp;bGotMin );        adfMinMax[1] = <a class="code" href="gdal_8h.html#403a61b92434a77717488915a5e615cb">GDALGetRasterMaximum</a>( hBand, &amp;bGotMax );        <span class="keywordflow">if</span>( ! (bGotMin &amp;&amp; bGotMax) )            <a class="code" href="gdal_8h.html#2b36986db233e04861c43a6d3ae893e2">GDALComputeRasterMinMax</a>( hBand, TRUE, adfMinMax );        printf( <span class="stringliteral">"Min=%.3fd, Max=%.3f\n"</span>, adfMinMax[0], adfMinMax[1] );                <span class="keywordflow">if</span>( <a class="code" href="gdal_8h.html#16b185b8a771a65cc26077e3b54887bc">GDALGetOverviewCount</a>(hBand) &gt; 0 )            printf( <span class="stringliteral">"Band has %d overviews.\n"</span>, <a class="code" href="gdal_8h.html#16b185b8a771a65cc26077e3b54887bc">GDALGetOverviewCount</a>(hBand));        <span class="keywordflow">if</span>( <a class="code" href="gdal_8h.html#b4ebf9ba142ed1847cfb04143fb75c3e">GDALGetRasterColorTable</a>( hBand ) != NULL )            printf( <span class="stringliteral">"Band has a color table with %d entries.\n"</span>,                      GDALGetColorEntryCount(                         <a class="code" href="gdal_8h.html#b4ebf9ba142ed1847cfb04143fb75c3e">GDALGetRasterColorTable</a>( hBand ) ) );</pre></div><p>In Python (note several bindings are missing): <div class="fragment"><pre class="fragment">        band = dataset.GetRasterBand(1)        print 'Band Type=',gdal.GetDataTypeName(band.DataType)        min = band.GetMinimum()        max = band.GetMaximum()        <span class="keywordflow">if</span> min is not None and max is not None:            (min,max) = ComputeRasterMinMax(1)        print 'Min=%.3f, Max=%.3f' % (min,max)        <span class="keywordflow">if</span> band.GetOverviewCount() &gt; 0:            print 'Band has ', band.GetOverviewCount(), ' overviews.'        <span class="keywordflow">if</span> not band.GetRasterColorTable() is None:            print 'Band has a color table with ', \            band.GetRasterColorTable().GetCount(), ' entries.'</pre></div><h2><a class="anchor" name="gdal_tutorial_read">Reading Raster Data</a></h2>There are a few ways to read raster data, but the most common is via the <a class="el" href="classGDALRasterBand.html#5497e8d29e743ee9177202cb3f61c3c7">GDALRasterBand::RasterIO()</a> method. This method will automatically take care of data type conversion, up/down sampling and windowing. The following code will read the first scanline of data into a similarly sized buffer, converting it to floating point as part of the operation.<p>In C++: <div class="fragment"><pre class="fragment">        <span class="keywordtype">float</span> *pafScanline;        <span class="keywordtype">int</span>   nXSize = poBand-&gt;<a class="code" href="classGDALRasterBand.html#46f78e79da622039a670107ae5a94f02">GetXSize</a>();        pafScanline = (<span class="keywordtype">float</span> *) CPLMalloc(<span class="keyword">sizeof</span>(<span class="keywordtype">float</span>)*nXSize);        poBand-&gt;<a class="code" href="classGDALRasterBand.html#5497e8d29e743ee9177202cb3f61c3c7">RasterIO</a>( <a class="code" href="gdal_8h.html#e602fdf251b6b0210a5af5a7cf7623b3b2abfe1fa6e34018b8c692eb48f35cb5">GF_Read</a>, 0, 0, nXSize, 1,                           pafScanline, nXSize, 1, <a class="code" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a4f5cbd2f96abffd9ac061fc0dced5cbba">GDT_Float32</a>,                           0, 0 );</pre></div><p>In C: <div class="fragment"><pre class="fragment">        <span class="keywordtype">float</span> *pafScanline;        <span class="keywordtype">int</span>   nXSize = <a class="code" href="gdal_8h.html#25c74f44562ceb26b2efe2f1f28f9b5f">GDALGetRasterBandXSize</a>( hBand );        pafScanline = (<span class="keywordtype">float</span> *) CPLMalloc(<span class="keyword">sizeof</span>(<span class="keywordtype">float</span>)*nXSize);        <a class="code" href="gdal_8h.html#f26fead53c02f8035150cc710c156752">GDALRasterIO</a>( hBand, <a class="code" href="gdal_8h.html#e602fdf251b6b0210a5af5a7cf7623b3b2abfe1fa6e34018b8c692eb48f35cb5">GF_Read</a>, 0, 0, nXSize, 1,                       pafScanline, nXSize, 1, <a class="code" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a4f5cbd2f96abffd9ac061fc0dced5cbba">GDT_Float32</a>,                       0, 0 );</pre></div><p>In Python:<p><div class="fragment"><pre class="fragment">        scanline = band.ReadRaster( 0, 0, band.XSize, 1, \                                     band.XSize, 1, <a class="code" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a4f5cbd2f96abffd9ac061fc0dced5cbba">GDT_Float32</a> )</pre></div><p>Note that the returned scanline is of type string, and contains xsize*4 bytes of raw binary floating point data. This can be converted to Python values using the <b>struct</b> module from the standard library:<p><div class="fragment"><pre class="fragment">        <span class="keyword">import</span> <span class="keyword">struct</span><span class="keyword"></span><span class="keyword">        </span>tuple_of_floats = <span class="keyword">struct</span>.unpack(<span class="charliteral">'f'</span> * b2.XSize, scanline)</pre></div><p>The RasterIO call takes the following arguments. <div class="fragment"><pre class="fragment">CPLErr <a class="code" href="classGDALRasterBand.html#5497e8d29e743ee9177202cb3f61c3c7">GDALRasterBand::RasterIO</a>( <a class="code" href="gdal_8h.html#e602fdf251b6b0210a5af5a7cf7623b3">GDALRWFlag</a> eRWFlag,                                 <span class="keywordtype">int</span> nXOff, <span class="keywordtype">int</span> nYOff, <span class="keywordtype">int</span> nXSize, <span class="keywordtype">int</span> nYSize,                                 <span class="keywordtype">void</span> * pData, <span class="keywordtype">int</span> nBufXSize, <span class="keywordtype">int</span> nBufYSize,                                 <a class="code" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a4">GDALDataType</a> eBufType,                                 <span class="keywordtype">int</span> nPixelSpace,                                 <span class="keywordtype">int</span> nLineSpace )</pre></div><p>Note that the same RasterIO() call is used to read, or write based on the setting of eRWFlag (either GF_Read or GF_Write). The nXOff, nYOff, nXSize, nYSize argument describe the window of raster data on disk to read (or write). It doesn't have to fall on tile boundaries though access may be more efficient if it does.<p>The pData is the memory buffer the data is read into, or written from. It's real type must be whatever is passed as eBufType, such as GDT_Float32, or GDT_Byte. The RasterIO() call will take care of converting between the buffer's data type and the data type of the band. Note that when converting floating point data to integer RasterIO() rounds down, and when converting source values outside the legal range of the output the nearest legal value is used. This implies, for instance, that 16bit data read into a GDT_Byte buffer will map all values greater than 255 to 255, <b>the data is not scaled!</b><p>The nBufXSize and nBufYSize values describe the size of the buffer. When loading data at full resolution this would be the same as the window size. However, to load a reduced resolution overview this could be set to smaller than the window on disk. In this case the RasterIO() will utilize overviews to do the IO more efficiently if the overviews are suitable.<p>The nPixelSpace, and nLineSpace are normally zero indicating that default values should be used. However, they can be used to control access to the memory data buffer, allowing reading into a buffer containing other pixel interleaved data for instance.<h2><a class="anchor" name="gdal_tutorial_close">Closing the Dataset</a></h2>Please keep in mind that <a class="el" href="classGDALRasterBand.html">GDALRasterBand</a> objects are <em>owned</em> by their dataset, and they should never be destroyed with the C++ delete operator. GDALDataset's can be closed either by calling <a class="el" href="gdal_8h.html#0984222d45a72028fcbbf1f44831ffbc">GDALClose()</a> or using the delete operator on the <a class="el" href="classGDALDataset.html">GDALDataset</a>. Either will result in proper cleanup, and flushing of any pending writes.<h2><a class="anchor" name="gdal_tutorial_creation">Techniques for Creating Files</a></h2>New files in GDAL supported formats may be created if the format driver supports creation. There are two general techniques for creating files, using CreateCopy() and Create(). The CreateCopy method involves calling the CreateCopy() method on the format driver, and passing in a source dataset that should be copied. The Create method involves calling the Create() method on the driver, and then explicitly writing all the metadata, and raster data with separate calls. All drivers that support creating new files support the CreateCopy() method, but only a few support the Create() method.<p>To determine if a particular format supports Create or CreateCopy it is possible to check the DCAP_CREATE and DCAP_CREATECOPY metadata on the format driver object. Ensure that <a class="el" href="gdal_8h.html#9d40bc998bd6ed07ccde96028e85ae26">GDALAllRegister()</a> has been called before calling GetDriverByName(). In this example we<p>In C++: <div class="fragment"><pre class="fragment"><span class="preprocessor">#include "<a class="code" href="cpl__string_8h.html">cpl_string.h</a>"</span>...    <span class="keyword">const</span> <span class="keywordtype">char</span> *pszFormat = <span class="stringliteral">"GTiff"</span>;    <a class="code" href="classGDALDriver.html">GDALDriver</a> *poDriver;    <span class="keywordtype">char</span> **papszMetadata;    poDriver = GetGDALDriverManager()-&gt;<a class="code" href="classGDALDriverManager.html#c26308c182440c8abd584040fa89bd4f">GetDriverByName</a>(pszFormat);    <span class="keywordflow">if</span>( poDriver == NULL )        exit( 1 );    papszMetadata = poDriver-&gt;<a class="code" href="classGDALMajorObject.html#8ce3bf5795bbebfe9bc643e2152bb360">GetMetadata</a>();    <span class="keywordflow">if</span>( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATE, FALSE ) )        printf( <span class="stringliteral">"Driver %s supports Create() method.\n"</span>, pszFormat );    <span class="keywordflow">if</span>( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATECOPY, FALSE ) )        printf( <span class="stringliteral">"Driver %s supports CreateCopy() method.\n"</span>, pszFormat );</pre></div><p>In C: <div class="fragment"><pre class="fragment"><span class="preprocessor">#include "<a class="code" href="cpl__string_8h.html">cpl_string.h</a>"</span>...    <span class="keyword">const</span> <span class="keywordtype">char</span> *pszFormat = <span class="stringliteral">"GTiff"</span>;    <a class="code" href="classGDALDriver.html">GDALDriver</a> hDriver = <a class="code" href="gdal_8h.html#e8ae868eef1e4773283d137b0a1adfc4">GDALGetDriverByName</a>( pszFormat );    <span class="keywordtype">char</span> **papszMetadata;    <span class="keywordflow">if</span>( hDriver == NULL )        exit( 1 );    papszMetadata = <a class="code" href="gdal_8h.html#25929a66e99d92c2788485ecf13e2e82">GDALGetMetadata</a>( hDriver, NULL );    <span class="keywordflow">if</span>( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATE, FALSE ) )        printf( <span class="stringliteral">"Driver %s supports Create() method.\n"</span>, pszFormat );    <span class="keywordflow">if</span>( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATECOPY, FALSE ) )        printf( <span class="stringliteral">"Driver %s supports CreateCopy() method.\n"</span>, pszFormat );</pre></div><p>In Python:<p><div class="fragment"><pre class="fragment">    format = <span class="stringliteral">"GTiff"</span>    driver = gdal.GetDriverByName( format )    metadata = driver.GetMetadata()    <span class="keywordflow">if</span> metadata.has_key(gdal.DCAP_CREATE) \       and metadata[gdal.DCAP_CREATE] == 'YES':        print 'Driver %s supports Create() method.' % format    if metadata.has_key(gdal.DCAP_CREATECOPY) \       and metadata[gdal.DCAP_CREATECOPY] == 'YES':

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -