📄 gdal_tutorial.html
字号:
print 'Driver %s supports CreateCopy() method.' % format</pre></div><p>Note that a number of drivers are read-only and won't support Create() or CreateCopy().<h2><a class="anchor" name="gdal_tutorial_createcopy">Using CreateCopy()</a></h2>The <a class="el" href="classGDALDriver.html#d0f7a33b0bd7f9d685bbd90d04fde629">GDALDriver::CreateCopy()</a> method can be used fairly simply as most information is collected from the source dataset. However, it includes options for passing format specific creation options, and for reporting progress to the user as a long dataset copy takes place. A simple copy from the a file named pszSrcFilename, to a new file named pszDstFilename using default options on a format whose driver was previously fetched might look like this:<p>In C++: <div class="fragment"><pre class="fragment"> <a class="code" href="classGDALDataset.html">GDALDataset</a> *poSrcDS = (<a class="code" href="classGDALDataset.html">GDALDataset</a> *) <a class="code" href="gdal_8h.html#e97be045eb4701183ad332ffce29745b">GDALOpen</a>( pszSrcFilename, <a class="code" href="gdal_8h.html#045e3967c208993f70257bfd40c9f1d75a021a550b9d5640307d3c0e7e35b732">GA_ReadOnly</a> ); <a class="code" href="classGDALDataset.html">GDALDataset</a> *poDstDS; poDstDS = poDriver-><a class="code" href="classGDALDriver.html#d0f7a33b0bd7f9d685bbd90d04fde629">CreateCopy</a>( pszDstFilename, poSrcDS, FALSE, NULL, NULL, NULL ); <span class="keywordflow">if</span>( poDstDS != NULL ) <span class="keyword">delete</span> poDstDS;</pre></div><p>In C: <div class="fragment"><pre class="fragment"> GDALDatasetH hSrcDS = <a class="code" href="gdal_8h.html#e97be045eb4701183ad332ffce29745b">GDALOpen</a>( pszSrcFilename, <a class="code" href="gdal_8h.html#045e3967c208993f70257bfd40c9f1d75a021a550b9d5640307d3c0e7e35b732">GA_ReadOnly</a> ); GDALDatasetH hDstDS; hDstDS = <a class="code" href="gdal_8h.html#c9812351af9d1d6ed2f1ea51cd49ef51">GDALCreateCopy</a>( hDriver, pszDstFilename, hSrcDS, FALSE, NULL, NULL, NULL ); <span class="keywordflow">if</span>( hDstDS != NULL ) <a class="code" href="gdal_8h.html#0984222d45a72028fcbbf1f44831ffbc">GDALClose</a>( hDstDS );</pre></div><p>In Python:<p><div class="fragment"><pre class="fragment"> src_ds = gdal.Open( src_filename ) dst_ds = driver.CreateCopy( dst_filename, src_ds, 0 )</pre></div><p>Note that the CreateCopy() method returns a writeable dataset, and that it must be closed properly to complete writing and flushing the dataset to disk. In the Python case this occurs automatically when "dst_ds" goes out of scope. The FALSE (or 0) value used for the bStrict option just after the destination filename in the CreateCopy() call indicates that the CreateCopy() call should proceed without a fatal error even if the destination dataset cannot be created to exactly match the input dataset. This might be because the output format does not support the pixel datatype of the input dataset, or because the destination cannot support writing georeferencing for instance.<p>A more complex case might involve passing creation options, and using a predefined progress monitor like this:<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="keywordtype">char</span> **papszOptions = NULL; papszOptions = CSLSetNameValue( papszOptions, <span class="stringliteral">"TILED"</span>, <span class="stringliteral">"YES"</span> ); papszOptions = CSLSetNameValue( papszOptions, <span class="stringliteral">"COMPRESS"</span>, <span class="stringliteral">"PACKBITS"</span> ); poDstDS = poDriver-><a class="code" href="classGDALDriver.html#d0f7a33b0bd7f9d685bbd90d04fde629">CreateCopy</a>( pszDstFilename, poSrcDS, FALSE, papszOptions, GDALTermProgress, NULL ); <span class="keywordflow">if</span>( poDstDS != NULL ) <span class="keyword">delete</span> poDstDS;</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="keywordtype">char</span> **papszOptions = NULL; papszOptions = CSLSetNameValue( papszOptions, <span class="stringliteral">"TILED"</span>, <span class="stringliteral">"YES"</span> ); papszOptions = CSLSetNameValue( papszOptions, <span class="stringliteral">"COMPRESS"</span>, <span class="stringliteral">"PACKBITS"</span> ); hDstDS = <a class="code" href="gdal_8h.html#c9812351af9d1d6ed2f1ea51cd49ef51">GDALCreateCopy</a>( hDriver, pszDstFilename, hSrcDS, FALSE, papszOptions, GDALTermProgres, NULL ); <span class="keywordflow">if</span>( hDstDS != NULL ) <a class="code" href="gdal_8h.html#0984222d45a72028fcbbf1f44831ffbc">GDALClose</a>( hDstDS );</pre></div><p>In Python:<p><div class="fragment"><pre class="fragment"> src_ds = gdal.Open( src_filename ) dst_ds = driver.CreateCopy( dst_filename, src_ds, 0, [ 'TILED=YES', 'COMPRESS=PACKBITS' ] )</pre></div><h2><a class="anchor" name="gdal_tutorial_create">Using Create()</a></h2>For situations in which you are not just exporting an existing file to a new file, it is generally necessary to use the <a class="el" href="classGDALDriver.html#191dc4a5c8f48c1dea4083c711b8f7c4">GDALDriver::Create()</a> method (though some interesting options are possible through use of virtual files or in-memory files). The Create() method takes an options list much like CreateCopy(), but the image size, number of bands and band type must be provided explicitly. <p>In C++: <div class="fragment"><pre class="fragment"> <a class="code" href="classGDALDataset.html">GDALDataset</a> *poDstDS; <span class="keywordtype">char</span> **papszOptions = NULL; poDstDS = poDriver-><a class="code" href="classGDALDriver.html#191dc4a5c8f48c1dea4083c711b8f7c4">Create</a>( pszDstFilename, 512, 512, 1, <a class="code" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a438a66c26861d368e95ba42106ee3ab92">GDT_Byte</a>, papszOptions );</pre></div><p>In C: <div class="fragment"><pre class="fragment"> GDALDatasetH hDstDS; <span class="keywordtype">char</span> **papszOptions = NULL; hDstDS = <a class="code" href="gdal_8h.html#f68516793118967e1292519cbd66442c">GDALCreate</a>( hDriver, pszDstFilename, 512, 512, 1, <a class="code" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a438a66c26861d368e95ba42106ee3ab92">GDT_Byte</a>, papszOptions );</pre></div><p>In Python:<p><div class="fragment"><pre class="fragment"> dst_ds = driver.Create( dst_filename, 512, 512, 1, gdal.GDT_Byte )</pre></div><p>Once the dataset is successfully created, all appropriate metadata and raster data must be written to the file. What this is will vary according to usage, but a simple case with a projection, geotransform and raster data is covered here.<p>In C++: <div class="fragment"><pre class="fragment"> <span class="keywordtype">double</span> adfGeoTransform[6] = { 444720, 30, 0, 3751320, 0, -30 }; OGRSpatialReference oSRS; <span class="keywordtype">char</span> *pszSRS_WKT = NULL; <a class="code" href="classGDALRasterBand.html">GDALRasterBand</a> *poBand; GByte abyRaster[512*512]; poDstDS-><a class="code" href="classGDALDataset.html#0fe0f81d65d84557b5d71ddc024faa02">SetGeoTransform</a>( adfGeoTransform ); oSRS.SetUTM( 11, TRUE ); oSRS.SetWellKnownGeogCS( <span class="stringliteral">"NAD27"</span> ); oSRS.exportToWkt( &pszSRS_WKT ); poDstDS-><a class="code" href="classGDALDataset.html#e2d2e231f6d632f8c2b2cf0078a01150">SetProjection</a>( pszSRS_WKT ); CPLFree( pszSRS_WKT ); poBand = poDstDS-><a class="code" href="classGDALDataset.html#d96adcf07f2979ad176e37a7f8638fb6">GetRasterBand</a>(1); poBand-><a class="code" href="classGDALRasterBand.html#5497e8d29e743ee9177202cb3f61c3c7">RasterIO</a>( <a class="code" href="gdal_8h.html#e602fdf251b6b0210a5af5a7cf7623b37f8fbb849495a05f2281b9a6fac0e7e2">GF_Write</a>, 0, 0, 512, 512, abyRaster, 512, 512, <a class="code" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a438a66c26861d368e95ba42106ee3ab92">GDT_Byte</a>, 0, 0 ); <span class="keyword">delete</span> poDstDS;</pre></div><p>In C: <div class="fragment"><pre class="fragment"> <span class="keywordtype">double</span> adfGeoTransform[6] = { 444720, 30, 0, 3751320, 0, -30 }; OGRSpatialReferenceH hSRS; <span class="keywordtype">char</span> *pszSRS_WKT = NULL; GDALRasterBandH hBand; GByte abyRaster[512*512]; <a class="code" href="gdal_8h.html#e93448112c1a7e69f2764c1aa3c6c8b5">GDALSetGeoTransform</a>( hDstDS, adfGeoTransform ); hSRS = OSRNewSpatialReference( NULL ); OSRSetUTM( hSRS, 11, TRUE ); OSRSetWellKnownGeogCS( hSRS, <span class="stringliteral">"NAD27"</span> ); OSRExportToWkt( hSRS, &pszSRS_WKT ); OSRDestroySpatialReference( hSRS ); <a class="code" href="gdal_8h.html#145f2be5db1ac31a07a9d4389f4ace65">GDALSetProjection</a>( hDstDS, pszSRS_WKT ); CPLFree( pszSRS_WKT ); hBand = <a class="code" href="gdal_8h.html#2a74e5e34528589303c1521ebfb9c162">GDALGetRasterBand</a>( hDstDS, 1 ); <a class="code" href="gdal_8h.html#f26fead53c02f8035150cc710c156752">GDALRasterIO</a>( hBand, <a class="code" href="gdal_8h.html#e602fdf251b6b0210a5af5a7cf7623b37f8fbb849495a05f2281b9a6fac0e7e2">GF_Write</a>, 0, 0, 512, 512, abyRaster, 512, 512, <a class="code" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a438a66c26861d368e95ba42106ee3ab92">GDT_Byte</a>, 0, 0 ); <a class="code" href="gdal_8h.html#0984222d45a72028fcbbf1f44831ffbc">GDALClose</a>( hDstDS );</pre></div><p>In Python:<p><div class="fragment"><pre class="fragment"> <span class="keyword">import</span> Numeric, osr dst_ds.SetGeoTransform( [ 444720, 30, 0, 3751320, 0, -30 ] ) srs = osr.SpatialReference() srs.SetUTM( 11, 1 ) srs.SetWellKnownGeogCS( 'NAD27' ) dst_ds.SetProjection( srs.ExportToWkt() ) raster = Numeric.zeros( (512, 512) ) dst_ds.GetRasterBand(1).WriteArray( raster )</pre></div><p> <p>$Id: gdal_tutorial.dox 10108 2006-10-17 22:30:22Z mloskot $</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 + -