2

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);
}
  • Maybe this post could give a help? – mgri Apr 25 '17 at 14:22
  • There must be some other issue that makes blur. If you do not need tiles for some other reason, why not to correct the real problem? I suppose that now you do not read right resolution when you zoom in. – user30184 Apr 25 '17 at 14:24
  • @user30184 I have posted the code I use to georeference the image. Do I need to change something? – user2522981 Apr 26 '17 at 05:51
  • Please tell why do you need tiles? If you open a static geotiff with QGIS you should see that you can zoom in and out without blurring. – user30184 Apr 26 '17 at 07:54
  • I don't need tiles. I just assumed that since it is blurring, I might need them. However, if there is a way to do it without involving tiles, I would prefer that. I posted the code so I could find out if I am doing something wrong. – user2522981 Apr 26 '17 at 08:36
  • Blurring makes me think that you load the image first to map canvas and when you zoom in you are stretching those same pixels. Instead you should reload the image, or actually only that part of image that suits with the new bounding box and scale. – user30184 Apr 29 '17 at 14:04
  • What are your values for imgWd and imgHt? You may be decreasing cell resolution (i.e. losing data) when you assign the geotransform. – khafen May 01 '17 at 21:21
  • I am using the image size. I cross checked with the properties of the file. – user2522981 May 04 '17 at 13:48

0 Answers0