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

📄 gdal_vrttut.html

📁 gdal库的学习文档
💻 HTML
📖 第 1 页 / 共 3 页
字号:
      <span class="keywordtype">char</span> szFilterSourceXML[10000];      <a class="code" href="classGDALRasterBand.html">GDALRasterBand</a> *poBand = poVRTDS-&gt;<a class="code" href="classGDALDataset.html#d96adcf07f2979ad176e37a7f8638fb6">GetRasterBand</a>( nBand );      sprintf( szFilterSourceXML,         <span class="stringliteral">"&lt;KernelFilteredSource&gt;"</span>        <span class="stringliteral">"  &lt;SourceFilename&gt;%s&lt;/SourceFilename&gt;1&lt;SourceBand&gt;%d&lt;/SourceBand&gt;"</span>        <span class="stringliteral">"  &lt;Kernel&gt;"</span>        <span class="stringliteral">"    &lt;Size&gt;3&lt;/Size&gt;"</span>        <span class="stringliteral">"    &lt;Coefs&gt;0.111 0.111 0.111 0.111 0.111 0.111 0.111 0.111 0.111&lt;/Coefs&gt;"</span>        <span class="stringliteral">"  &lt;/Kernel&gt;"</span>        <span class="stringliteral">"&lt;/KernelFilteredSource&gt;"</span>,         pszSourceFilename, nBand );              poBand-&gt;<a class="code" href="classGDALMajorObject.html#3e157735f6ff6e11935c2a2dbcc24c92">SetMetadataItem</a>( <span class="stringliteral">"source_0"</span>, szFilterSourceXML, <span class="stringliteral">"vrt_sources"</span> );  }</pre></div><p><h2>Using Derived Bands</h2><p>A specialized type of band is a 'derived' band which derives its pixel information from its source bands. With this type of band you must also specify a pixel function, which has the responsibility of generating the output raster. Pixel functions are created by an application and then registered with GDAL using a unique key.<p>Using derived bands you can create VRT datasets that manipulate bands on the fly without having to create new band files on disk. For example, you might want to generate a band using four source bands from a nine band input dataset (x0, x3, x4, and x8):<p><div class="fragment"><pre class="fragment">  band_value = sqrt((x3*x3+x4*x4)/(x0*x8));</pre></div><p>You could write the pixel function to compute this value and then register it with GDAL with the name "MyFirstFunction". Then, the following VRT XML could be used to display this derived band:<p><div class="fragment"><pre class="fragment">&lt;VRTDataset rasterXSize=<span class="stringliteral">"1000"</span> rasterYSize=<span class="stringliteral">"1000"</span>&gt;  &lt;VRTRasterBand dataType=<span class="stringliteral">"Float32"</span> band=<span class="stringliteral">"1"</span> subClass=<span class="stringliteral">"VRTDerivedRasterBand"</span>&gt;&gt;    &lt;Description&gt;Magnitude&lt;/Description&gt;    &lt;PixelFunctionType&gt;MyFirstFunction&lt;/PixelFunctionType&gt;    &lt;SimpleSource&gt;      &lt;SourceFilename relativeToVRT=<span class="stringliteral">"1"</span>&gt;nine_band.dat&lt;/SourceFilename&gt;      &lt;SourceBand&gt;1&lt;/SourceBand&gt;      &lt;SrcRect xOff=<span class="stringliteral">"0"</span> yOff=<span class="stringliteral">"0"</span> xSize=<span class="stringliteral">"1000"</span> ySize=<span class="stringliteral">"1000"</span>/&gt;      &lt;DstRect xOff=<span class="stringliteral">"0"</span> yOff=<span class="stringliteral">"0"</span> xSize=<span class="stringliteral">"1000"</span> ySize=<span class="stringliteral">"1000"</span>/&gt;    &lt;/SimpleSource&gt;    &lt;SimpleSource&gt;      &lt;SourceFilename relativeToVRT=<span class="stringliteral">"1"</span>&gt;nine_band.dat&lt;/SourceFilename&gt;      &lt;SourceBand&gt;4&lt;/SourceBand&gt;      &lt;SrcRect xOff=<span class="stringliteral">"0"</span> yOff=<span class="stringliteral">"0"</span> xSize=<span class="stringliteral">"1000"</span> ySize=<span class="stringliteral">"1000"</span>/&gt;      &lt;DstRect xOff=<span class="stringliteral">"0"</span> yOff=<span class="stringliteral">"0"</span> xSize=<span class="stringliteral">"1000"</span> ySize=<span class="stringliteral">"1000"</span>/&gt;    &lt;/SimpleSource&gt;    &lt;SimpleSource&gt;      &lt;SourceFilename relativeToVRT=<span class="stringliteral">"1"</span>&gt;nine_band.dat&lt;/SourceFilename&gt;      &lt;SourceBand&gt;5&lt;/SourceBand&gt;      &lt;SrcRect xOff=<span class="stringliteral">"0"</span> yOff=<span class="stringliteral">"0"</span> xSize=<span class="stringliteral">"1000"</span> ySize=<span class="stringliteral">"1000"</span>/&gt;      &lt;DstRect xOff=<span class="stringliteral">"0"</span> yOff=<span class="stringliteral">"0"</span> xSize=<span class="stringliteral">"1000"</span> ySize=<span class="stringliteral">"1000"</span>/&gt;    &lt;/SimpleSource&gt;    &lt;SimpleSource&gt;      &lt;SourceFilename relativeToVRT=<span class="stringliteral">"1"</span>&gt;nine_band.dat&lt;/SourceFilename&gt;      &lt;SourceBand&gt;9&lt;/SourceBand&gt;      &lt;SrcRect xOff=<span class="stringliteral">"0"</span> yOff=<span class="stringliteral">"0"</span> xSize=<span class="stringliteral">"1000"</span> ySize=<span class="stringliteral">"1000"</span>/&gt;      &lt;DstRect xOff=<span class="stringliteral">"0"</span> yOff=<span class="stringliteral">"0"</span> xSize=<span class="stringliteral">"1000"</span> ySize=<span class="stringliteral">"1000"</span>/&gt;    &lt;/SimpleSource&gt;  &lt;/VRTRasterBand&gt;&lt;/VRTDataset&gt;</pre></div><p>In addition to the subclass specification (VRTDerivedRasterBand) and the PixelFunctionType value, there is another new parameter that can come in handy: SourceTransferType. Typically the source rasters are obtained using the data type of the derived band. There might be times, however, when you want the pixel function to have access to higher resolution source data than the data type being generated. For example, you might have a derived band of type "Float", which takes a single source of type "CFloat32" or "CFloat64", and returns the imaginary portion. To accomplish this, set the SourceTransferType to "CFloat64". Otherwise the source would be converted to "Float" prior to calling the pixel function, and the imaginary portion would be lost.<p><div class="fragment"><pre class="fragment">&lt;VRTDataset rasterXSize=<span class="stringliteral">"1000"</span> rasterYSize=<span class="stringliteral">"1000"</span>&gt;  &lt;VRTRasterBand dataType=<span class="stringliteral">"Float32"</span> band=<span class="stringliteral">"1"</span> subClass=<span class="stringliteral">"VRTDerivedRasterBand"</span>&gt;&gt;    &lt;Description&gt;Magnitude&lt;/Description&gt;    &lt;PixelFunctionType&gt;MyFirstFunction&lt;/PixelFunctionType&gt;    &lt;SourceTransferType&gt;<span class="stringliteral">"CFloat64"</span>&lt;/SourceTransferType&gt;    ...</pre></div><p><h3>Writing Pixel Functions</h3><p>To register this function with GDAL (prior to accessing any VRT datasets with derived bands that use this function), an application calls GDALAddDerivedBandPixelFunc with a key and a GDALDerivedPixelFunc:<p><div class="fragment"><pre class="fragment">    <a class="code" href="gdal_8h.html#e27e95ca0d86b0775d5c379e954828cf">GDALAddDerivedBandPixelFunc</a>(<span class="stringliteral">"MyFirstFunction"</span>, TestFunction);</pre></div><p>A good time to do this is at the beginning of an application when the GDAL drivers are registered.<p>GDALDerivedPixelFunc is defined with a signature similar to IRasterIO:<p><dl compact><dt><b>Parameters:</b></dt><dd>  <table border="0" cellspacing="2" cellpadding="0">    <tr><td valign="top"></td><td valign="top"><em>papoSources</em>&nbsp;</td><td>A pointer to packed rasters; one per source. The datatype of all will be the same, specified in the eSrcType parameter.</td></tr>    <tr><td valign="top"></td><td valign="top"><em>nSources</em>&nbsp;</td><td>The number of source rasters.</td></tr>    <tr><td valign="top"></td><td valign="top"><em>pData</em>&nbsp;</td><td>The buffer into which the data should be read, or from which it should be written. This buffer must contain at least nBufXSize * nBufYSize words of type eBufType. It is organized in left to right, top to bottom pixel order. Spacing is controlled by the nPixelSpace, and nLineSpace parameters.</td></tr>    <tr><td valign="top"></td><td valign="top"><em>nBufXSize</em>&nbsp;</td><td>The width of the buffer image into which the desired region is to be read, or from which it is to be written.</td></tr>    <tr><td valign="top"></td><td valign="top"><em>nBufYSize</em>&nbsp;</td><td>The height of the buffer image into which the desired region is to be read, or from which it is to be written.</td></tr>    <tr><td valign="top"></td><td valign="top"><em>eSrcType</em>&nbsp;</td><td>The type of the pixel values in the papoSources raster array.</td></tr>    <tr><td valign="top"></td><td valign="top"><em>eBufType</em>&nbsp;</td><td>The type of the pixel values that the pixel function must generate in the pData data buffer.</td></tr>    <tr><td valign="top"></td><td valign="top"><em>nPixelSpace</em>&nbsp;</td><td>The byte offset from the start of one pixel value in pData to the start of the next pixel value within a scanline. If defaulted (0) the size of the datatype eBufType is used.</td></tr>    <tr><td valign="top"></td><td valign="top"><em>nLineSpace</em>&nbsp;</td><td>The byte offset from the start of one scanline in pData to the start of the next.</td></tr>  </table></dl><dl class="return" compact><dt><b>Returns:</b></dt><dd>CE_Failure on failure, otherwise CE_None.</dd></dl><div class="fragment"><pre class="fragment"><span class="keyword">typedef</span> CPLErr(*GDALDerivedPixelFunc)(<span class="keywordtype">void</span> **papoSources, <span class="keywordtype">int</span> nSources, <span class="keywordtype">void</span> *pData,                        <span class="keywordtype">int</span> nXSize, <span class="keywordtype">int</span> nYSize,                        <a class="code" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a4">GDALDataType</a> eSrcType, <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>The following is an implementation of the pixel function:<p><div class="fragment"><pre class="fragment"><span class="preprocessor">#include "<a class="code" href="gdal_8h.html">gdal.h</a>"</span>CPLErr TestFunction(<span class="keywordtype">void</span> **papoSources, <span class="keywordtype">int</span> nSources, <span class="keywordtype">void</span> *pData,                    <span class="keywordtype">int</span> nXSize, <span class="keywordtype">int</span> nYSize,                    <a class="code" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a4">GDALDataType</a> eSrcType, <a class="code" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a4">GDALDataType</a> eBufType,                    <span class="keywordtype">int</span> nPixelSpace, <span class="keywordtype">int</span> nLineSpace){    <span class="keywordtype">int</span> ii, iLine, iCol;    <span class="keywordtype">double</span> pix_val;    <span class="keywordtype">double</span> x0, x3, x4, x8;    <span class="comment">/* ---- Init ---- */</span>    <span class="keywordflow">if</span> (nSources != 4) <span class="keywordflow">return</span> CE_Failure;    <span class="comment">/* ---- Set pixels ---- */</span>    <span class="keywordflow">for</span>( iLine = 0; iLine &lt; nYSize; iLine++ ) {        <span class="keywordflow">for</span>( iCol = 0; iCol &lt; nXSize; iCol++ ) {            ii = iLine * nXSize + iCol;            <span class="comment">/* Source raster pixels may be obtained with SRCVAL macro */</span>            x0 = <a class="code" href="gdal_8h.html#61bb460bb8dad455391cfa2bc98cd3d8">SRCVAL</a>(papoSources[0], eSrcType, ii);            x3 = <a class="code" href="gdal_8h.html#61bb460bb8dad455391cfa2bc98cd3d8">SRCVAL</a>(papoSources[1], eSrcType, ii);            x4 = <a class="code" href="gdal_8h.html#61bb460bb8dad455391cfa2bc98cd3d8">SRCVAL</a>(papoSources[2], eSrcType, ii);            x8 = <a class="code" href="gdal_8h.html#61bb460bb8dad455391cfa2bc98cd3d8">SRCVAL</a>(papoSources[3], eSrcType, ii);            pix_val = sqrt((x3*x3+x4*x4)/(x0*x8));            <a class="code" href="gdal_8h.html#14e39ec8cc3a962913673f984e0d5398">GDALCopyWords</a>(&amp;pix_val, <a class="code" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a469dbfcc12cc807423cb51cf4f9d54983">GDT_Float64</a>, 0,                          ((GByte *)pData) + nLineSpace * iLine +                          iCol * nPixelSpace, eBufType, nPixelSpace, 1);        }    }    <span class="comment">/* ---- Return success ---- */</span>    <span class="keywordflow">return</span> CE_None;}</pre></div> <hr>Generated for GDAL by <a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.1.</body></html>

⌨️ 快捷键说明

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