wiratama
wiratama

Reputation: 43

How to obtain latitude and longitude from geotiff image using Gdal C++

I want to obtain the latitude and longitude from geotiff image by using Gdal C++. Most source and tutorial to get these coordinates usually use C and Phyton. As we know, the Gdal source of these are different in C++. Is there any documentation or tutorial how to get this kind of location using C++?. Or anyone know how to obtain these coordinates in Gdal C++?

Upvotes: 3

Views: 1035

Answers (1)

Richard
Richard

Reputation: 61289

Like so:

GDALDataset *fin = (GDALDataset*)GDALOpen(filename.c_str(), GA_ReadOnly);
if(fin==NULL)
  throw std::runtime_error("Could not open file '"+filename+"' with GDAL!");

std::vector<double> geotransform(6);
if(fin->GetGeoTransform(geotransform.data())!=CE_None)
  throw std::runtime_error("Could not get a geotransform!");

//From https://www.gdal.org/gdal_datamodel.html "Affine GeoTransform"
//    Xgeo = GT(0) + Xpixel*GT(1) + Yline*GT(2)
//    Ygeo = GT(3) + Xpixel*GT(4) + Yline*GT(5)

int x = 0;
int y = 0;

double lng=geotransform[0] + x*geotransform[1] + y*geotransform[2];
double lat=geotransform[3] + x*geotransform[4] + y*geotransform[5];

GDAL doesn't have a C++ interface, so you do most of the work as though you're in C.

Upvotes: 1

Related Questions