Clone wiki

kealib / Developing with KEA and VC++

Developing with KEA and VC++

After successful installation of a KEA-enabled GDAL, we can now try to use it programmatically with C++.

1. Create new VS project

Use Visual Studio to create a new Win32 Console Application (the name of our sample project is "keainfo").

2. Add example .cpp files

Replace the created keainfo.cpp, stdafx.h and stdafx.cpp with the ones provided here:

// keainfo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
using namespace std;

#include "gdal_priv.h"
#include "cpl_conv.h" // for CPLMalloc()

char *pszFilename = "O:\\KEA\\utm.kea"; // ADJUST THIS PATH

int main()
{
    GDALDataset  *poDataset;

    GDALAllRegister();

    poDataset = (GDALDataset *) GDALOpen( pszFilename, GA_ReadOnly );
    if( poDataset == NULL )
    {
        cout << "Opening of kea file failed";
    } else {
              cout << "Opening of KEA file successful \n";
       }
           double        adfGeoTransform[6];

    printf( "Driver: %s/%s\n",
            poDataset->GetDriver()->GetDescription(), 
            poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) );

    printf( "Size is %dx%dx%d\n", 
            poDataset->GetRasterXSize(), poDataset->GetRasterYSize(),
            poDataset->GetRasterCount() );

    if( poDataset->GetProjectionRef()  != NULL )
        printf( "Projection is `%s'\n", poDataset->GetProjectionRef() );

    if( poDataset->GetGeoTransform( adfGeoTransform ) == CE_None )
    {
        printf( "Origin = (%.6f,%.6f)\n",
                adfGeoTransform[0], adfGeoTransform[3] );

        printf( "Pixel Size = (%.6f,%.6f)\n",
                adfGeoTransform[1], adfGeoTransform[5] );
    }

       cout<<"Press Return to exit...";
    cin.sync();
    cin.ignore();
}

Edit the path to utm.kea (from https://bitbucket.org/chchrsc/kealib/downloads/utm.kea) (in keainfo.cpp) to reflect where it is on your system.

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

// TODO: reference additional headers your program requires here
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <sstream>

#include "gdal_priv.h"
#include "cpl_conv.h" // for CPLMalloc()
#include <gdal_rat.h>
#include <cstdlib> // for putenv()
// stdafx.cpp : source file that includes just the standard includes
// keainfo.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file

3. Additional settings

Now we have to edit our project settings (under Properties):

Configuration Properties -> C/C++ -> General -> Additional Include Directories.

  • Set the path to the GDAL include directory on your system.

Configuration Properties -> C/C++ > Linker > General > Additional Library Directories.

  • Set the path to the gdal lib directory (where gdal_i.lib is located).

Configuration Properties -> C/C++ > Linker > Input > Additional Dependencies.

  • Add gdal_i.lib to the list

4. Compile

[we hope for the best...]

4.1 Other possible requirements might be complier dependent

Issue: LNK2005, LNK1169 errors

Might be resolved by the following settings:

Configuration Properties -> C/C++ -> Precompiled Headers -> Precompiled Header

  • Set to Not Using Precompiled Headers (or if that fails, make it blank).

5. Run

You should see the following on a console:

Opening of KEA file successful
Driver: KEA/KEA Image Format (.kea)
Size is 512x512x1
Projection is `PROJCS["NAD27 / UTM zone 11N",GEOGCS["NAD27",DATUM["North_America
n_Datum_1927",SPHEROID["Clarke 1866",6378206.4,294.9786982139006,AUTHORITY["EPSG
","7008"]],AUTHORITY["EPSG","6267"]],PRIMEM["Greenwich",0],UNIT["degree",0.01745
32925199433],AUTHORITY["EPSG","4267"]],PROJECTION["Transverse_Mercator"],PARAMET
ER["latitude_of_origin",0],PARAMETER["central_meridian",-117],PARAMETER["scale_f
actor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],U
NIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","26711"]]'
Origin = (440720.000000,3751320.000000)
Pixel Size = (60.000000,-60.000000)
Press Return to exit...

See http://www.gdal.org/gdal_tutorial.html to get started with GDAL.

Updated