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++.
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.
A mailing list specifically on VB6 GDAL topics has been setup at http://groups.yahoo.com/group/gdal-vb6-appdev .
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.
Then add the GDAL VB6 class and module files to your VB6 project. These come from the gdal/vb6 directory and include the following key files:
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).
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.
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.
Before opening the file we need to register the GDAL format drivers. Normally we will just register all the drivers with GDALAllRegister().
Then we need to try and open the dataset. The GDAL.OpenDS() function returns a GDALDataset object, so we dimension an appropriate object for this. GDAL.OpenDS() is the VB6 equivalent of the GDALDataset::GDALOpen() function.
Then we need to check if the open succeeded, and if not report an error.
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.
Next we read metadata from the dataset using the VB6 equivalent of the GDALMajorObject::GetMetadata() 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.
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++ GDALMajorObject::GetMetadataItem() method.
The GDALDataset::GetGeoTransform() 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.
The coordinate system can be fetched using the GDALDataset::GetProjectionRef() 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.
GDALDataset objects have one or more raster bands associated with them. GDALRasterBand objects can have metadata (accessed the same as on the GDALDataset) 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.
First we loop over all the bands, fetching a band object for each band and report the band number, and block size.
The GDALRasterBand has a DataType property which has the value returned by the C++ method GDALRasterBand::GetRasterDataType(). The returned value is an integer, but may be compared to the predefined constants GDAL.GDT_Byte, GDAL.GDT_UInt16, GDAL.GDT_Int16, GDAL.GDT_UInt32, GDAL.GDT_Int32, GDAL.GDT_Float32, GDAL.GDT_Float64, GDAL.GDT_CInt16, GDAL.GDT_CInt32, GDAL.GDT_CFloat32 and GDAL.GDT_CFloat64. In this case we use the GDAL.GetDataTypeName() method to convert the data type into a name we can show the user.
We also report the offset, scale, minimum and maximum for the band.
GDALRasterBands can also have GDALColorTable objects associated with them. They are read with the GDALRasterBand::GetColorTable() analog in VB6. Individual RGBA entries should be read into a 4 Integer array.
But of course, the most important contents of a GDAL file is the raster pixel values themselves. The C++ GDALRasterBand::RasterIO() 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.
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).
Finally, when done accessing a GDALDataset 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.
Next we address creating a new file from an existing file. To create a new file, you have to select a GDALDriver to do the creating. The GDALDriver is essentially an object representing a file format. We fetch it with the GetDriverByName() call from the GDAL module using the driver name.
You could get a list of registered drivers, and identify which support creation something like this:
Once we have the driver object, the simplest way of creating a new file is to use CreateCopy(). This tries to create a copy of the input file in the new format. A complete segment (without any error checking) would look like the following. The CreateCopy() method corresponds to the C++ method GDALDriver::CreateCopy(). The VB6 implementation does not support the use of progress callbacks.
This is nice and simple, but sometimes we need to create a file with more detailed control. So, next we show how to create a file and then copy pieces of data to it "manually". The GDALDriver::Create() analog is Create().
In some cases we may want to provide some creation options, which is demonstrated here. Creation options (like metadata set through the SetMetadata() method) are arrays of Strings.
When copying the GeoTransform, we take care to check that reading the geotransform actually worked. Most methods which return CPLErr in C++ also return it in VB6. A return value of 0 will indicate success, and non-zero is failure.
Copy the projection. Even if GetProjection() fails we get an empty string which is safe enough to set on the target. Similarly for metadata.
Next we loop, processing bands, and copy some common data items.
Then, if one is available, we copy the palette.
Finally, the meat and potatoes. We copy the image data. We do this one scanline at a time so that we can support very large images without require large amounts of RAM. Here we use a Double buffer for the scanline, but if we knew in advance the type of the image, we could dimension a buffer of the appropriate type. The RasterIO() method internally knows how to convert pixel data types, so using Double ensures all data types (except for complex) are properly preserved, though at the cost of some extra data conversion internally.
The GDAL VB6 bindings also include limited support for use of the OGRSpatialReference and OGRCoordinateTransformation classes. The OGRSpatialReference represents a coordinate system and can be used to parse, manipulate and form WKT strings, such as those returned by the GDALDataset.GetProjection() method. The OGRCoordinateTransformation class provides a way of reprojecting between two coordinate systems.
The following example shows how to report the corners of an image in georeferenced and geographic (lat/long) coordinates. First, we open the file, and read the geotransform.
Next, we fetch the coordinate system, and if it is non-empty we try to instantiate an OGRSpatialReference from it.
If the coordinate system is projected it will have a PROJECTION node. In that case we build a new coordinate system which is the corresponding geographic coordinate system. So for instance if the "srs" was UTM 11 WGS84 then it's corresponding geographic coordinate system would just be WGS84. Once we have these two coordinate systems, we build a transformer to convert between them.
Next we call a helper function to report each corner, and the center. We pass in the name of the corner, the pixel/line location at the corner, and the geotransform and transformer object.
The ReportCorner subroutine starts by computing the corresponding georeferenced x and y location using the pixel/line coordinates and the geotransform.
Next, if we have a transformer, we use it to compute a corresponding latitude and longitude.
Then we report the corner location in georeferenced, and if we have it geographic coordinates.
$Id: vb6_tutorial.dox 13528 2008-01-14 19:37:42Z warmerdam $