📄 warptut.html
字号:
CPLAssert( hDriver != NULL ); <span class="comment">// Get Source coordinate system. </span> <span class="keyword">const</span> <span class="keywordtype">char</span> *pszSrcWKT, *pszDstWKT = NULL; pszSrcWKT = <a class="code" href="gdal_8h.html#639a11014cf6c4ff30df6f21d5db9da2">GDALGetProjectionRef</a>( hSrcDS ); CPLAssert( pszSrcWKT != NULL && strlen(pszSrcWKT) > 0 ); <span class="comment">// Setup output coordinate system that is UTM 11 WGS84. </span> OGRSpatialReference oSRS; oSRS.SetUTM( 11, TRUE ); oSRS.SetWellKnownGeogCS( <span class="stringliteral">"WGS84"</span> ); oSRS.exportToWkt( &pszDstWKT ); <span class="comment">// Create a transformer that maps from source pixel/line coordinates</span> <span class="comment">// to destination georeferenced coordinates (not destination </span> <span class="comment">// pixel line). We do that by omitting the destination dataset</span> <span class="comment">// handle (setting it to NULL). </span> <span class="keywordtype">void</span> *hTransformArg; hTransformArg = <a class="code" href="gdal__alg_8h.html#7671696d085085a0bfba3c3df9ffcc0a">GDALCreateGenImgProjTransformer</a>( hSrcDS, pszSrcWKT, NULL, pszDstWKT, FALSE, 0, 1 ); CPLAssert( hTransformArg != NULL ); <span class="comment">// Get approximate output georeferenced bounds and resolution for file. </span> <span class="keywordtype">double</span> adfDstGeoTransform[6]; <span class="keywordtype">int</span> nPixels=0, nLines=0; CPLErr eErr; eErr = <a class="code" href="gdal__alg_8h.html#816819e7495bfce06dbd110f7c57af65">GDALSuggestedWarpOutput</a>( hSrcDS, GDALGenImgProjTransform, hTransformArg, adfDstGeoTransform, &nPixels, &nLines ); CPLAssert( eErr == CE_None ); <a class="code" href="gdal__alg_8h.html#5fb383c4e5197e8e37ae1265cca8124d">GDALDestroyGenImgProjTransformer</a>( hTransformArg ); <span class="comment">// Create the output file. </span> hDstDS = <a class="code" href="gdal_8h.html#f68516793118967e1292519cbd66442c">GDALCreate</a>( hDriver, <span class="stringliteral">"out.tif"</span>, nPixels, nLines, <a class="code" href="gdal_8h.html#1b9f888aac1cb4dbc99dc1dc023174b7">GDALGetRasterCount</a>(hSrcDS), eDT, NULL ); CPLAssert( hDstDS != NULL ); <span class="comment">// Write out the projection definition. </span> <a class="code" href="gdal_8h.html#145f2be5db1ac31a07a9d4389f4ace65">GDALSetProjection</a>( hDstDS, pszDstWKT ); <a class="code" href="gdal_8h.html#e93448112c1a7e69f2764c1aa3c6c8b5">GDALSetGeoTransform</a>( hDstDS, adfDstGeoTransform ); <span class="comment">// Copy the color table, if required.</span> GDALColorTableH hCT; hCT = <a class="code" href="gdal_8h.html#b4ebf9ba142ed1847cfb04143fb75c3e">GDALGetRasterColorTable</a>( <a class="code" href="gdal_8h.html#2a74e5e34528589303c1521ebfb9c162">GDALGetRasterBand</a>(hSrcDS,1) ); <span class="keywordflow">if</span>( hCT != NULL ) <a class="code" href="gdal_8h.html#de5abc76e229097e6799ab58414aeaed">GDALSetRasterColorTable</a>( <a class="code" href="gdal_8h.html#2a74e5e34528589303c1521ebfb9c162">GDALGetRasterBand</a>(hDstDS,1), hCT ); ... proceed with warp as before ...</pre></div><p>Some notes on this logic:<p><ul><li>We need to create the transformer to output coordinates such that the output of the transformer is georeferenced, not pixel line coordinates since we use the transformer to map pixels around the source image into destination georeferenced coordinates.<p></li><li>The <a class="el" href="gdal__alg_8h.html#816819e7495bfce06dbd110f7c57af65">GDALSuggestedWarpOutput()</a> function will return an adfDstGeoTransform, nPixels and nLines that describes an output image size and georeferenced extents that should hold all pixels from the source image. The resolution is intended to be comparable to the source, but the output pixels are always square regardless of the shape of input pixels.<p></li><li>The warper requires an output file in a format that can be "randomly" written to. This generally limits things to uncompressed formats that have an implementation of the Create() method (as opposed to CreateCopy()). To warp to compressed formats, or CreateCopy() style formats it is necessary to produce a full temporary copy of the image in a better behaved format, and then CreateCopy() it to the desired final format.<p></li><li>The Warp API copies only pixels. All colormaps, georeferencing and other metadata must be copied to the destination by the application.<p></li></ul><h2><a class="anchor" name="warptut_perfomance">Performance Optimization</a></h2>There are a number of things that can be done to optimize the performance of the warp API.<p><ol><li>Increase the amount of memory available for the Warp API chunking so that larger chunks can be operated on at a time. This is the <a class="el" href="structGDALWarpOptions.html#a72d8bd37f896272cd979ce9dc9d65e9">GDALWarpOptions::dfWarpMemoryLimit</a> parameter. In theory the larger the chunk size operated on the more efficient the I/O strategy, and the more efficient the approximated transformation will be. However, the sum of the warp memory and the GDAL cache should be less than RAM size, likely around 2/3 of RAM size.<p></li><li>Increase the amount of memory for GDAL caching. This is especially important when working with very large input and output images that are scanline oriented. If all the input or output scanlines have to be re-read for each chunk they intersect performance may degrade greatly. Use <a class="el" href="gdal_8h.html#dfb1e95703ee577f012935869852d96c">GDALSetCacheMax()</a> to control the amount of memory available for caching within GDAL.<p></li><li>Use an approximated transformation instead of exact reprojection for each pixel to be transformed. This code illustrates how an approximated transformation could be created based on a reprojection transformation, but with a given error threshold (dfErrorThreshold in output pixels).<p><div class="fragment"><pre class="fragment"> hTransformArg = <a class="code" href="gdal__alg_8h.html#4ec403b75384f0a71130eb009078426f">GDALCreateApproxTransformer</a>( GDALGenImgProjTransform, hGenImgProjArg, dfErrorThreshold ); pfnTransformer = <a class="code" href="gdal__alg_8h.html#766ccb23b021d30d86908c08ad8d1668">GDALApproxTransform</a>;</pre></div><p></li><li>When writing to a blank output file, use the INIT_DEST option in the <a class="el" href="structGDALWarpOptions.html#0ed77f9917bb96c7a9aabd73d4d06e08">GDALWarpOptions::papszWarpOptions</a> to cause the output chunks to be initialized to a fixed value, instead of being read from the output. This can substantially reduce unnecessary IO work.<p></li><li>Use tiled input and output formats. Tiled formats allow a given chunk of source and destination imagery to be accessed without having to touch a great deal of extra image data. Large scanline oriented files can result in a great deal of wasted extra IO.<p></li><li>Process all bands in one call. This ensures the transformation calculations don't have to be performed for each band.<p></li><li>Use the <a class="el" href="classGDALWarpOperation.html#a4fcccc201832f6f692fd290848cf4f1">GDALWarpOperation::ChunkAndWarpMulti()</a> method instead of <a class="el" href="classGDALWarpOperation.html#589a9b74fa370cc9eaf11bdfd9aab2ae">GDALWarpOperation::ChunkAndWarpImage()</a>. It uses a separate thread for the IO and the actual image warp operation allowing more effective use of CPU and IO bandwidth. For this to work GDAL needs to have been built with multi-threading support (default on Win32, --with-pthreads on Unix).<p></li><li>The resampling kernels vary is work required from nearest neighbour being least, then bilinear then cubic. Don't use a more complex resampling kernel than needed.<p></li><li>Avoid use of esoteric masking options so that special simplified logic case be used for common special cases. For instance, nearest neighbour resampling with no masking on 8bit data is highly optimized compared to the general case.<p></li></ol><h2><a class="anchor" name="warptut_other_opts">Other Masking Options</a></h2>The <a class="el" href="structGDALWarpOptions.html">GDALWarpOptions</a> include a bunch of esoteric masking capabilities, for validity masks, and density masks on input and output. Some of these are not yet implemented and others are implemented but poorly tested. Other than per-band validity masks it is advised that these features be used with caution at this time.<p> <p>$Id: warptut.dox 10252 2006-11-09 14:53:18Z fwarmerdam $</p> <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 + -