Geoscience Reference
In-Depth Information
#include "gdal_priv.h"
#include "cpl_conv.h"
#include <string>
using namespace std;
int main( int argc, char *argv[])
{
GDALDataset *poDataset;
GDALAllRegister();
//define a string containing the path to our Landsat scene
string landsatFilename="LC82070232013160LGN00_composit.tif";
//open the dataset in read only mode
poDataset = (GDALDataset *) GDALOpen(
landsatFilename.c_str(), GA_ReadOnly );
//perform dataset (read) operations here
//close the dataset
GDALClose( (GDALDataset*) poDataset );
return 0;
}
Notice that GDALOpen does not support the typical C++ std::string type as its
first argument. This is to make a calling from C possible. We therefore need to
convert to a C-style character string via the member function c_str() . The flag
GA_ReadOnly indicates the dataset will not be altered by our program (the alter-
native is GA_Update for read/write access).
Once the dataset has been opened, we can access the actual raster data or other rel-
evant information. This can be done via the member functions of the GDALDataset
class. The following code snippets should go in place of //perform dataset
(read) operations here . In this snippet, we report the bounding box and
the location of the center pixel of the opened image in the coordinate system of the
dataset. This information can be retrieved via the affine transformation coefficients,
available through the member function GetGeoTransform . More information on
the affine transformation parameters can be found in Sect. 8.8 .
//retrieve the six affine transformation parameters as an
array of doubles
double gt[6];
poDataset->GetGeoTransform(gt);
int ncol=poDataset->GetRasterXSize();
int nrow=poDataset->GetRasterYSize();
double pixelSizeX=gt[1];
double pixelSizeY=gt[5];
//calculate bounding box
//if image is not rotated:
//gt[2]=gt[4]=0
//gt[1]=pixel size in X
//gt[5]=pixel size in Y
double ulx=gt[0];
 
 
Search WWH ::




Custom Search