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

📄 warptut.html

📁 gdal库的学习文档
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!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 Warp 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&nbsp;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&nbsp;Pages</span></a></li>  </ul></div><h1><a class="anchor" name="warptut">GDAL Warp API Tutorial</a></h1><h2><a class="anchor" name="warptut_overview">Overview</a></h2>The GDAL Warp API (declared in <a class="el" href="gdalwarper_8h.html">gdalwarper.h</a>) provides services for high performance image warping using application provided geometric transformation functions (GDALTransformerFunc), a variety of resampling kernels, and various masking options. Files much larger than can be held in memory can be warped.<p>This tutorial demonstrates how to implement an application using the Warp API. It assumes implementation in C++ as C and Python bindings are incomplete for the Warp API. It also assumes familiarity with the <a href="gdal_datamodel.html">GDAL Data Model</a>, and the general GDAL API.<p>Applications normally perform a warp by initializing a <a class="el" href="structGDALWarpOptions.html">GDALWarpOptions</a> structure with the options to be utilized, instantiating a <a class="el" href="classGDALWarpOperation.html">GDALWarpOperation</a> based on these options, and then invoking the <a class="el" href="classGDALWarpOperation.html#589a9b74fa370cc9eaf11bdfd9aab2ae">GDALWarpOperation::ChunkAndWarpImage()</a> method to perform the warp options internally using the <a class="el" href="classGDALWarpKernel.html">GDALWarpKernel</a> class.<h2><a class="anchor" name="warptut_simple">A Simple Reprojection Case</a></h2>First we will construct a relatively simple example for reprojecting an image, assuming an appropriate output file already exists, and with minimal error checking.<p><div class="fragment"><pre class="fragment"><span class="preprocessor">#include "<a class="code" href="gdalwarper_8h.html">gdalwarper.h</a>"</span><span class="keywordtype">int</span> main(){    GDALDatasetH  hSrcDS, hDstDS;    <span class="comment">// Open input and output files. </span>    <a class="code" href="gdal_8h.html#9d40bc998bd6ed07ccde96028e85ae26">GDALAllRegister</a>();    hSrcDS = <a class="code" href="gdal_8h.html#e97be045eb4701183ad332ffce29745b">GDALOpen</a>( <span class="stringliteral">"in.tif"</span>, <a class="code" href="gdal_8h.html#045e3967c208993f70257bfd40c9f1d75a021a550b9d5640307d3c0e7e35b732">GA_ReadOnly</a> );    hDstDS = <a class="code" href="gdal_8h.html#e97be045eb4701183ad332ffce29745b">GDALOpen</a>( <span class="stringliteral">"out.tif"</span>, <a class="code" href="gdal_8h.html#045e3967c208993f70257bfd40c9f1d761c6081de474ef2a756982d3c53130a2">GA_Update</a> );    <span class="comment">// Setup warp options. </span>        <a class="code" href="structGDALWarpOptions.html">GDALWarpOptions</a> *psWarpOptions = GDALCreateWarpOptions();    psWarpOptions-&gt;<a class="code" href="structGDALWarpOptions.html#65f68e1cf7ca641c5d0a93762d0416d5">hSrcDS</a> = hSrcDS;    psWarpOptions-&gt;<a class="code" href="structGDALWarpOptions.html#0550f97dd229ae14e7356588a9c74ffb">hDstDS</a> = hDstDS;    psWarpOptions-&gt;<a class="code" href="structGDALWarpOptions.html#9b60196e623b269e8f4905bf9e1792e2">nBandCount</a> = 1;    psWarpOptions-&gt;<a class="code" href="structGDALWarpOptions.html#841df78e52087580fa6c0bd811714160">panSrcBands</a> =         (<span class="keywordtype">int</span> *) CPLMalloc(<span class="keyword">sizeof</span>(<span class="keywordtype">int</span>) * psWarpOptions-&gt;<a class="code" href="structGDALWarpOptions.html#9b60196e623b269e8f4905bf9e1792e2">nBandCount</a> );    psWarpOptions-&gt;<a class="code" href="structGDALWarpOptions.html#841df78e52087580fa6c0bd811714160">panSrcBands</a>[0] = 1;    psWarpOptions-&gt;<a class="code" href="structGDALWarpOptions.html#4997d9eff29876052adb47f7928497f7">panDstBands</a> =         (<span class="keywordtype">int</span> *) CPLMalloc(<span class="keyword">sizeof</span>(<span class="keywordtype">int</span>) * psWarpOptions-&gt;<a class="code" href="structGDALWarpOptions.html#9b60196e623b269e8f4905bf9e1792e2">nBandCount</a> );    psWarpOptions-&gt;<a class="code" href="structGDALWarpOptions.html#4997d9eff29876052adb47f7928497f7">panDstBands</a>[0] = 1;    psWarpOptions-&gt;<a class="code" href="structGDALWarpOptions.html#5cf9b6e00e148b360f659c4e2ac68c01">pfnProgress</a> = <a class="code" href="gdal_8h.html#00e9838f30867a9dbeb65e454d3bea1e">GDALTermProgress</a>;       <span class="comment">// Establish reprojection transformer. </span>    psWarpOptions-&gt;<a class="code" href="structGDALWarpOptions.html#35647845a2629da876f379f82d58a2cd">pTransformerArg</a> =         <a class="code" href="gdal__alg_8h.html#7671696d085085a0bfba3c3df9ffcc0a">GDALCreateGenImgProjTransformer</a>( hSrcDS,                                          <a class="code" href="gdal_8h.html#639a11014cf6c4ff30df6f21d5db9da2">GDALGetProjectionRef</a>(hSrcDS),                                          hDstDS,                                         <a class="code" href="gdal_8h.html#639a11014cf6c4ff30df6f21d5db9da2">GDALGetProjectionRef</a>(hDstDS),                                          FALSE, 0.0, 1 );    psWarpOptions-&gt;<a class="code" href="structGDALWarpOptions.html#8899bacf99abdfd14c8bd590bd6079fa">pfnTransformer</a> = <a class="code" href="gdal__alg_8h.html#109c26234c2f934164e29649353532b6">GDALGenImgProjTransform</a>;    <span class="comment">// Initialize and execute the warp operation. </span>    <a class="code" href="classGDALWarpOperation.html">GDALWarpOperation</a> oOperation;    oOperation.<a class="code" href="classGDALWarpOperation.html#52447611e7e196187a4667842f628d3b">Initialize</a>( psWarpOptions );    oOperation.<a class="code" href="classGDALWarpOperation.html#589a9b74fa370cc9eaf11bdfd9aab2ae">ChunkAndWarpImage</a>( 0, 0,                                   <a class="code" href="gdal_8h.html#4ef08b38a70b6e04f25a81bd82ef0138">GDALGetRasterXSize</a>( hDstDS ),                                   <a class="code" href="gdal_8h.html#e0c0af31441c6bac994f35ac26c82f99">GDALGetRasterYSize</a>( hDstDS ) );    <a class="code" href="gdal__alg_8h.html#5fb383c4e5197e8e37ae1265cca8124d">GDALDestroyGenImgProjTransformer</a>( psWarpOptions-&gt;<a class="code" href="structGDALWarpOptions.html#35647845a2629da876f379f82d58a2cd">pTransformerArg</a> );    GDALDestroyWarpOptions( psWarpOptions );    <a class="code" href="gdal_8h.html#0984222d45a72028fcbbf1f44831ffbc">GDALClose</a>( hDstDS );    <a class="code" href="gdal_8h.html#0984222d45a72028fcbbf1f44831ffbc">GDALClose</a>( hSrcDS );    <span class="keywordflow">return</span> 0;}</pre></div><p>This example opens the existing input and output files (in.tif and out.tif). A <a class="el" href="structGDALWarpOptions.html">GDALWarpOptions</a> structure is allocated (GDALCreateWarpOptions() sets lots of sensible defaults for stuff, always use it for defaulting things), and the input and output file handles, and band lists are set. The panSrcBands and panDstBands lists are dynamically allocated here and will be free automatically by GDALDestroyWarpOptions(). The simple terminal output progress monitor (GDALTermProgress) is installed for reporting completion progress to the user.<p><a class="el" href="gdal__alg_8h.html#7671696d085085a0bfba3c3df9ffcc0a">GDALCreateGenImgProjTransformer()</a> is used to initialize the reprojection transformation between the source and destination images. We assume that they already have reasonable bounds and coordinate systems set. Use of GCPs is disabled.<p>Once the options structure is ready, a <a class="el" href="classGDALWarpOperation.html">GDALWarpOperation</a> is instantiated using them, and the warp actually performed with <a class="el" href="classGDALWarpOperation.html#589a9b74fa370cc9eaf11bdfd9aab2ae">GDALWarpOperation::ChunkAndWarpImage()</a>. Then the transformer, warp options and datasets are cleaned up.<p>Normally error check would be needed after opening files, setting up the reprojection transformer (returns NULL on failure), and initializing the warp.<h2><a class="anchor" name="warptut_options">Other Warping Options</a></h2>The <a class="el" href="structGDALWarpOptions.html">GDALWarpOptions</a> structures contains a number of items that can be set to control warping behavior. A few of particular interest are:<p><ol><li><a class="el" href="structGDALWarpOptions.html#a72d8bd37f896272cd979ce9dc9d65e9">GDALWarpOptions::dfWarpMemoryLimit</a> - Set the maximum amount of memory to be used by the <a class="el" href="classGDALWarpOperation.html">GDALWarpOperation</a> when selecting a size of image chunk to operate on. The value is in bytes, and the default is likely to be conservative (small). Increasing the chunk size can help substantially in some situations but care should be taken to ensure that this size, plus the GDAL cache size plus the working set of GDAL, your application and the operating system are less than the size of RAM or else excessive swapping is likely to interfere with performance. On a system with 256MB of RAM, a value of at least 64MB (roughly 64000000 bytes) is reasonable. Note that this value does <b>not</b> include the memory used by GDAL for low level block caching.<p></li><li>GDALWarpOpations::eResampleAlg - One of GRA_NearestNeighbour (the default, and fastest), GRA_Bilinear (2x2 bilinear resampling) or GRA_Cubic. The GRA_NearestNeighbour type should generally be used for thematic or colormapped images. The other resampling types may give better results for thematic images, especially when substantially changing resolution.<p></li><li><a class="el" href="structGDALWarpOptions.html#304bf40101dbea72b77067071919eb21">GDALWarpOptions::padfSrcNoDataReal</a> - This array (one entry per band being processed) may be setup with a "nodata" value for each band if you wish to avoid having pixels of some background value copied to the destination image.<p></li><li><a class="anchor" name="#warpoptions"></a> <a class="el" href="structGDALWarpOptions.html#0ed77f9917bb96c7a9aabd73d4d06e08">GDALWarpOptions::papszWarpOptions</a> - This is a string list of NAME=VALUE options passed to the warper. See the <a class="el" href="structGDALWarpOptions.html#0ed77f9917bb96c7a9aabd73d4d06e08">GDALWarpOptions::papszWarpOptions</a> docs for all options. Supported values include:<p><ul><li>INIT_DEST=[value] or INIT_DEST=NO_DATA: This option forces the destination image to be initialized to the indicated value (for all bands) or indicates that it should be initialized to the NO_DATA value in padfDstNoDataReal/padfDstNoDataImag. If this value isn't set the destination image will be read and the source warp overlayed on it.<p></li><li>WRITE_FLUSH=YES/NO: This option forces a flush to disk of data after each chunk is processed. In some cases this helps ensure a serial writing of the output data otherwise a block of data may be written to disk each time a block of data is read for the input buffer resulting in a lot of extra seeking around the disk, and reduced IO throughput. The default at this time is NO. </li></ul><p></li></ol><h2><a class="anchor" name="warptut_output">Creating the Output File</a></h2>In the previous case an appropriate output file was already assumed to exist. Now we will go through a case where a new file with appropriate bounds in a new coordinate system is created. This operation doesn't relate specifically to the warp API. It is just using the transformation API.<p><div class="fragment"><pre class="fragment"><span class="preprocessor">#include "<a class="code" href="gdalwarper_8h.html">gdalwarper.h</a>"</span><span class="preprocessor">#include "ogr_spatialref.h"</span>...    GDALDriverH hDriver;    <a class="code" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a4">GDALDataType</a> eDT;    GDALDatasetH hDstDS;    GDALDatasetH hSrcDS;    <span class="comment">// Open the source file. </span>    hSrcDS = <a class="code" href="gdal_8h.html#e97be045eb4701183ad332ffce29745b">GDALOpen</a>( <span class="stringliteral">"in.tif"</span>, <a class="code" href="gdal_8h.html#045e3967c208993f70257bfd40c9f1d75a021a550b9d5640307d3c0e7e35b732">GA_ReadOnly</a> );    CPLAssert( hSrcDS != NULL );        <span class="comment">// Create output with same datatype as first input band. </span>    eDT = <a class="code" href="gdal_8h.html#2edba2a096915aa63e7ca0bf4c383bd0">GDALGetRasterDataType</a>(<a class="code" href="gdal_8h.html#2a74e5e34528589303c1521ebfb9c162">GDALGetRasterBand</a>(hSrcDS,1));    <span class="comment">// Get output driver (GeoTIFF format)</span>    hDriver = <a class="code" href="gdal_8h.html#e8ae868eef1e4773283d137b0a1adfc4">GDALGetDriverByName</a>( <span class="stringliteral">"GTiff"</span> );

⌨️ 快捷键说明

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