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

📄 vb6_tutorial.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 VB6 Bindings 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="vb6_tutorial">GDAL VB6 Bindings Tutorial</a></h1><h2><a class="anchor" name="Intro">Introduction</a></h2>A partial set of Visual Basic 6 bindings have been build for GDAL. Internally these bindings use Declare based calls into the GDAL DLL C API but a set of shadow classes are also provided to provide object oriented access to GDAL services in VB6 similar to those provided in C++.<p>Note that the VB6 bindings are nowhere near comprehensive, nor are they documented. However, in combination with the corresponding C++ class documentation, and the following docs, it should be possible to use GDAL to accomplish a variety of operations. It is not believed that the VB6 bindings will be of any utility with earlier version of VB nor with VB.Net.<p>The classes for which access has been implemented includes <a class="el" href="classGDALDriver.html">GDALDriver</a>, <a class="el" href="classGDALDataset.html">GDALDataset</a>, <a class="el" href="classGDALRasterBand.html">GDALRasterBand</a>, GDALColorTable, OGRSpatialReference and OGRCoordinateTransformation.<p>A mailing list specifically on VB6 GDAL topics has been setup at <a href="http://groups.yahoo.com/group/gdal-vb6-appdev">http://groups.yahoo.com/group/gdal-vb6-appdev</a> .<h2><a class="anchor" name="UsingVB6">Using GDAL VB6 Classes</a></h2>To use VB6 GDAL bindings it is necessary to ensure that GDAL has been built with appropriate C entry points exported using the "stdcall" calling convention. This is the current default, but was not as recently as GDAL 1.2.6. So ensure you get a version more recent than 1.2.6.<p>Then add the GDAL VB6 class and module files to your VB6 project. These come from the <a href="http://www.gdal.org/srctree/vb6">gdal/vb6 directory</a> and include the following key files:<p><ul><li>GDAL.bas - The main user visible module. </li><li>GDALCore.bas - This module is for internal use. </li><li>GDALDriver.cls - The <a class="el" href="classGDALDriver.html">GDALDriver</a> class. </li><li>GDALDataset.cls - The <a class="el" href="classGDALDataset.html">GDALDataset</a> class. </li><li>GDALRasterBand.cls - The <a class="el" href="classGDALRasterBand.html">GDALRasterBand</a> class. </li><li>GDALColorTable.cls - The GDALColorTable class. </li><li>OGRSpatialReference.cls - The OGRSpatialReference class. </li><li>OGRCoordinateTransformation.cls - The OGRCoordinateTransformation class. </li></ul><p>You may need to edit GDALCore.bas, and change occurrences of gdal12.dll to match what your GDAL DLL is called. You can include a full path to the DLL if it can't be guaranteed to be in the current working directory of the application (or the windows system32 directory).<p>You should also be able to load the "test" project from the gdal\vb6\test directory. The test project has test menu items roughly corresponding to the tasks in the following tutorial topics.<h2><a class="anchor" name="TutorialRead">Tutorial - Read Dataset</a></h2>This brief tutorial will demonstrate open a GDAL file, and fetching out some information, about the dataset, and the individual bands. The results are printed to the default from in the following example for simplicity.<p>Before opening the file we need to register the GDAL format drivers. Normally we will just register all the drivers with <a class="el" href="gdal_8h.html#9d40bc998bd6ed07ccde96028e85ae26">GDALAllRegister()</a>.<p><div class="fragment"><pre class="fragment">  Call GDAL.AllRegister()</pre></div><p>Then we need to try and open the dataset. The GDAL.OpenDS() function returns a <a class="el" href="classGDALDataset.html">GDALDataset</a> object, so we dimension an appropriate object for this. GDAL.OpenDS() is the VB6 equivalent of the <a class="el" href="classGDALDataset.html#6764788806a1785c97036d1dba064497">GDALDataset::GDALOpen()</a> function.<p><div class="fragment"><pre class="fragment">  Dim ds As <a class="code" href="classGDALDataset.html">GDALDataset</a>  Set ds = GDAL.OpenDS( <span class="stringliteral">"utm.tif"</span>, GDAL.GA_ReadOnly )</pre></div><p>Then we need to check if the open succeeded, and if not report an error.<p><div class="fragment"><pre class="fragment">  If not ds.IsValid() Then    Call MsgBox( <span class="stringliteral">"Open failed: "</span> &amp; GDAL.GetLastErrorMsg() )    Exit Sub  End If</pre></div><p>If things succeeded, we query width of the image in pixels (XSize), Height of the image in pixels (YSize) and number of bands (BandCount) from the dataset properties.<p><div class="fragment"><pre class="fragment">  Print <span class="stringliteral">"Size: "</span> &amp; ds.XSize &amp; <span class="stringliteral">"x"</span> &amp; ds.YSize &amp; <span class="stringliteral">"x"</span> &amp; ds.BandCount</pre></div><p>Next we read metadata from the dataset using the VB6 equivalent of the <a class="el" href="classGDALMajorObject.html#8ce3bf5795bbebfe9bc643e2152bb360">GDALMajorObject::GetMetadata()</a> method, and report it to the user. Metadata is returned as an array of strings of "name=value" items. Array indices start at zero in the returned array. The domain argument should normally be vbNullString though in specialized circumstances other domains might apply.<p><div class="fragment"><pre class="fragment">  Dim MD As Variant  MD = ds.GetMetadata(vbNullString)  If (UBound(MD) &gt; 0) Then    Print "Metadata:"    For i = 1 To UBound(MD)      Print "  " &amp; MD(i)    Next i  End If</pre></div><p>Parsing the "name=value" strings from GetMetadata() can be a bit of a bother, so if we were looking for specific values we could use GetMetadataItem() and provide a specific item we want to extract. This would extract just the value if it is found, or an empty string otherwise. The GetMetadataItem() is an analog of the C++ <a class="el" href="classGDALMajorObject.html#d17f496c8b6d0c0be6f52437d3ba8a6d">GDALMajorObject::GetMetadataItem()</a> method.<p><div class="fragment"><pre class="fragment">  Dim MDValue As String  MDValue = ds.GetMetadataItem( <span class="stringliteral">"TIFF_DATETIME"</span>, vbNullString )  <span class="keywordflow">if</span> MDValue &lt;&gt; <span class="stringliteral">""</span> Then    Print <span class="stringliteral">"Creation Date: "</span> &amp; MDValue  End If</pre></div><p>The <a class="el" href="classGDALDataset.html#f9593cc241e7d140f5f3c4798a43a668">GDALDataset::GetGeoTransform()</a> method is used to get fetch the affine transformation used to relate pixel/line locations on the image to georeferenced locations in the current coordinate system. In the most common case (image is not rotated or sheared) you can just report the origin (upper left corner) and pixel size from these values. The method returns 0 on success or an error class if it fails, so we only use the return result (placed into the Geotransform array) on success.<p><div class="fragment"><pre class="fragment">  Dim Geotransform(6) As Double  If ds.GetGeoTransform( Geotransform ) = 0 Then     If Geotransform(2) = 0 and Geotransform(4) = 0 Then      Print "Origin: " &amp; Geotransform(0) &amp; "," &amp; Geotransform(3)      Print "Pixel Size: " &amp; Geotransform(1) &amp; "x" &amp; (-1 * Geotransform(5))    End If  End If</pre></div><p>The coordinate system can be fetched using the <a class="el" href="classGDALDataset.html#a42537e1062ce254d124b29ff3ebe857">GDALDataset::GetProjectionRef()</a> analog, GDALDataset.GetProjection(). The returned string is in OpenGIS Well Known Text format. A later example will show how to use an OGRSpatialReference object to reformat the WKT into more readable format and make other use of it.<p><div class="fragment"><pre class="fragment">  Dim WKT As String  WKT = ds.GetProjection()  If Len(WKT) &gt; 0 Then    Print <span class="stringliteral">"Projection: "</span> &amp; WKT  End If</pre></div><p><a class="el" href="classGDALDataset.html">GDALDataset</a> objects have one or more raster bands associated with them. <a class="el" href="classGDALRasterBand.html">GDALRasterBand</a> objects can have metadata (accessed the same as on the <a class="el" href="classGDALDataset.html">GDALDataset</a>) as well as an array of pixel values, and various specialized metadata items like data type, color interpretation, offset/scale. Here we report a few of the items.<p>First we loop over all the bands, fetching a band object for each band and report the band number, and block size.<p><div class="fragment"><pre class="fragment">  For i = 1 To ds.BandCount    Dim band As <a class="code" href="classGDALRasterBand.html">GDALRasterBand</a>               Set band = ds.GetRasterBand(i)    Print <span class="stringliteral">"Band "</span> &amp; i &amp; <span class="stringliteral">" BlockSize: "</span> &amp; band.BlockXSize &amp; <span class="stringliteral">"x"</span> &amp; band.BlockYSize</pre></div><p>The <a class="el" href="classGDALRasterBand.html">GDALRasterBand</a> has a DataType property which has the value returned by the C++ method <a class="el" href="classGDALRasterBand.html#e3ec3bfe18258dd73db3911fed97cc2e">GDALRasterBand::GetRasterDataType()</a>. The returned value is an integer, but may be compared to the predefined constants <a class="el" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a438a66c26861d368e95ba42106ee3ab92">GDAL.GDT_Byte</a>, <a class="el" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a4b8eec11a9f30464690e2602cca2ac5d5">GDAL.GDT_UInt16</a>, <a class="el" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a4cd01d1afd29ffdb9a1cc6c09de61fd2b">GDAL.GDT_Int16</a>, <a class="el" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a41d9deacb35e7b76f14c67d1b699cde33">GDAL.GDT_UInt32</a>, <a class="el" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a4ed358376a9194ff029931af07d4cf6ca">GDAL.GDT_Int32</a>, <a class="el" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a4f5cbd2f96abffd9ac061fc0dced5cbba">GDAL.GDT_Float32</a>, <a class="el" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a469dbfcc12cc807423cb51cf4f9d54983">GDAL.GDT_Float64</a>, <a class="el" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a4e8f87006869e00c4861f865ed9bbaa8e">GDAL.GDT_CInt16</a>, <a class="el" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a42964aa34ef378a3fa2833cfd09c153af">GDAL.GDT_CInt32</a>, <a class="el" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a4554d76abab6d543c77a36e5b59631505">GDAL.GDT_CFloat32</a> and <a class="el" href="gdal_8h.html#22e22ce0a55036a96f652765793fb7a4603dd2d9f0906bd1f8f72dd47c0a56ac">GDAL.GDT_CFloat64</a>. In this case we use the GDAL.GetDataTypeName() method to convert the data type into a name we can show the user.<p><div class="fragment"><pre class="fragment">    Print <span class="stringliteral">"     DataType="</span> &amp; GDAL.GetDataTypeName(band.DataType) _</pre></div><p>We also report the offset, scale, minimum and maximum for the band.<p><div class="fragment"><pre class="fragment">    Print <span class="stringliteral">" Offset="</span> &amp; band.GetOffset() &amp; <span class="stringliteral">" Scale="</span> &amp; band.GetScale() _        &amp; <span class="stringliteral">" Min="</span> &amp; band.GetMinimum() &amp; <span class="stringliteral">" Max="</span> &amp; band.GetMaximum()</pre></div><p>GDALRasterBands can also have GDALColorTable objects associated with them. They are read with the <a class="el" href="classGDALRasterBand.html#2c5cad0f0fc1b6e261d1e965a5b49969">GDALRasterBand::GetColorTable()</a> analog in VB6. Individual RGBA entries should be read into a 4 Integer array.<p><div class="fragment"><pre class="fragment">    Dim ct As GDALColorTable    Set ct = band.GetColorTable()    If ct.IsValid() Then      Dim CEntry(4) As Integer      Print "    Has Color Table, " &amp; ct.EntryCount &amp; " entries"      For iColor = 0 To ct.EntryCount - 1        Call ct.GetColorEntryAsRGB(iColor, CEntry)        Print "      " &amp; iColor &amp; ": " &amp; CEntry(0) &amp; "," &amp; CEntry(1) &amp; "," &amp; CEntry(2) &amp; "," &amp; CEntry(3)      Next iColor    End If</pre></div><p>But of course, the most important contents of a GDAL file is the raster pixel values themselves. The C++ <a class="el" href="classGDALRasterBand.html#5497e8d29e743ee9177202cb3f61c3c7">GDALRasterBand::RasterIO()</a> method is provided in a somewhat simplified form. A predimensioned 1D or 2D array of type Byte, Int, Long, Float or Double is passed to the RasterIO() method along with the band and window to be read. Internally the "buffer size" and datatype is extracted from the dimensions of the passed in buffer.<p>This example dimensions the RawData array to be the size of one scanline of data (XSize x 1) and reads the first whole scanline of data from the file, but only prints out the second and tenth values (since the buffer indexes are zero based).<p><div class="fragment"><pre class="fragment">    Dim err As Long    Dim RawData() As Double    ReDim RawData(ds.XSize) As Double    err = band.RasterIO(GDAL.<a class="code" href="gdal_8h.html#e602fdf251b6b0210a5af5a7cf7623b3b2abfe1fa6e34018b8c692eb48f35cb5">GF_Read</a>, 0, 0, ds.XSize, 1, RawData)    if err = 0 Then      Print "    Data: " &amp; RawData(1) &amp; " " &amp; RawData(9)    End If</pre></div><p>Finally, when done accessing a <a class="el" href="classGDALDataset.html">GDALDataset</a> we can explicitly close it using the CloseDS() method, or just let it fall out of scope in which case it will be closed automatically.<p><div class="fragment"><pre class="fragment">    Call ds.CloseDS()</pre></div><h2><a class="anchor" name="TutorialCreate">Tutorial - Creating Files</a></h2>Next we address creating a new file from an existing file. To create a new file, you have to select a <a class="el" href="classGDALDriver.html">GDALDriver</a> to do the creating. The <a class="el" href="classGDALDriver.html">GDALDriver</a> is essentially an object representing a file format. We fetch it with the GetDriverByName() call from the GDAL module using the driver name.<p><div class="fragment"><pre class="fragment">    Dim Drv As <a class="code" href="classGDALDriver.html">GDALDriver</a>    Call GDAL.AllRegister    Drv = GDALCore.GetDriverByName( <span class="stringliteral">"GTiff"</span> )    If Not Drv.IsValid() Then        Call MsgBox( <span class="stringliteral">"GTiff driver not found "</span> )        Exit Sub    End If</pre></div><p>You could get a list of registered drivers, and identify which support creation something like this:<p><div class="fragment"><pre class="fragment">    drvCount = GDAL.GetDriverCount    For drvIndex = 0 To drvCount - 1        Set Drv = GDAL.GetDriver(drvIndex)        If Drv.GetMetadataItem(GDAL.DCAP_CREATE, <span class="stringliteral">""</span>) = <span class="stringliteral">"YES"</span> _            Or Drv.GetMetadataItem(GDAL.DCAP_CREATECOPY, <span class="stringliteral">""</span>) = <span class="stringliteral">"YES"</span> Then            xMsg = <span class="stringliteral">" (Read/Write)"</span>        Else            xMsg = <span class="stringliteral">" (ReadOnly)"</span>        End If

⌨️ 快捷键说明

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