I used GDAL to georeference a static image. However, when I load it in the map canvas (QGIS API), the image gets blurry when I zoom in. I need to generate map tiles but I cannot figure out how I would do that in C++. I read about gdal2tiles but I am still confused about how I would use the resulting .pngs. Can someone point me to any kind of example code or material that would make things clearer?
#include <gdal_priv.h>
#include <cpl_conv.h>
#include <cpl_error.h>
#include <ogr_spatialref.h>
using namespace std;
#include <string>
#include <cmath>
#include <QString>
#include <QDebug>
typedef struct Bounds{
Bounds() : north(0), east(0), south(0), west(0) {}
double north;
double east;
double south;
double west;
}Bounds;
void GeoRasterizer::geoReference(const QString &source,
const Bounds &bd,
int imgWd, int imgHt,
const QString &target)
{
OGRSpatialReference osr;
CPLErr error;
char *wktProj = (char*)malloc(sizeof(char) * 50);
GDALAllRegister();
GDALDataset *srcDS = (GDALDataset*)GDALOpen(source.toStdString().c_str(), GA_ReadOnly);
CPLAssert( srcDS != NULL );
qDebug() << "Source " << source << " opened" << endl;
GDALDriver *dstDrv = GetGDALDriverManager()->GetDriverByName("GTiff");
CPLAssert( dstDrv != NULL );
qDebug() << "Driver opened!" << endl;
GDALDataset *dstDS = dstDrv->CreateCopy(target.toStdString().c_str(), srcDS, 0, NULL, NULL, NULL);
CPLAssert( dstDS != NULL );
qDebug() << "Target " << target << " opened" << endl;
//Set the map projection
osr.importFromEPSG(4326);
osr.exportToWkt(&wktProj);
error = GDALSetProjection(dstDS, wktProj);
CPLAssert( error != CE_None );
qDebug() << "Projection set to " << wktProj << endl;
//Set the map georeferencing
double mapWidth = abs(bd.east - bd.west);
double mapHeight = abs(bd.north - bd.south);
double transform[6] = { bd.west, mapWidth / imgWd, 0, bd.north, 0, -mapHeight / imgHt };
error = GDALSetGeoTransform(dstDS, transform);
CPLAssert( error != CE_None );
qDebug() << "Transform set!" << endl;
GDALFlushCache(dstDS);
GDALClose(dstDS);
GDALClose(srcDS);
}
imgWdandimgHt? You may be decreasing cell resolution (i.e. losing data) when you assign the geotransform. – khafen May 01 '17 at 21:21