1

I'm loading two layers to QGIS maps canvas: the first is a shapefile, with EPSG:4326 crs. The second is an xml for Goolge TileServer, with EPSG:3857 crs.

Google TileServer - Hybrid.xml

<Service name="TMS">
    <!-- <ServerUrl>http://mt.google.com/vt/lyrs=m&amp;x=${x}&amp;y=${y}&amp;z=${z}</ServerUrl> --> <!-- Map -->
    <!-- <ServerUrl>http://mt.google.com/vt/lyrs=s&amp;x=${x}&amp;y=${y}&amp;z=${z}</ServerUrl> --> <!-- Satellite -->
    <ServerUrl>http://mt.google.com/vt/lyrs=y&amp;x=${x}&amp;y=${y}&amp;z=${z}</ServerUrl> --> <!-- Hybrid -->
    <!-- <ServerUrl>http://mt.google.com/vt/lyrs=t&amp;x=${x}&amp;y=${y}&amp;z=${z}</ServerUrl> --> <!-- Terrain -->
    <!-- <ServerUrl>http://mt.google.com/vt/lyrs=p&amp;x=${x}&amp;y=${y}&amp;z=${z}</ServerUrl> --> <!-- Terrain, Streets and Water  -->
</Service>
<DataWindow>
    <UpperLeftX>-20037508.34</UpperLeftX>
    <UpperLeftY>20037508.34</UpperLeftY>
    <LowerRightX>20037508.34</LowerRightX>
    <LowerRightY>-20037508.34</LowerRightY>
    <TileLevel>20</TileLevel>
    <TileCountX>1</TileCountX>
    <TileCountY>1</TileCountY>
    <YOrigin>top</YOrigin>
</DataWindow>
<Projection>EPSG:3857</Projection>
<BlockSizeX>256</BlockSizeX>
<BlockSizeY>256</BlockSizeY>
<BandsCount>3</BandsCount>
<MaxConnections>5</MaxConnections>
<Cache>
    <Depth>2</Depth>
    <Extension>.jpg</Extension>
</Cache>
</GDAL_WMS> 

When I load both layers directly in QGIS, the scales match OK and the layers align correctly. When I load them using QGIS C++ API, they don´t.

This is the cpp code:

#include "mainwindow.h"
#include "ui_mainwindow.h"

// M_PI patch
#ifndef M_PI
    #define M_PI 3.14159265358979323846
#endif

// noexcept patch
#ifndef no_except_work_around_H
#define no_except_work_around_H
#if (_MSC_VER <= 1800)
#include <xkeycheck.h>
#define noexcept
#endif
#endif //no_except_work_around_H

// QGIS Includes
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgsmaplayerregistry.h>
#include <qgsvectorlayer.h>
#include <qgsrasterlayer.h>
#include <qgsmapcanvas.h>

// Globals
QgsMapCanvas * mypMapCanvas;
QList <QgsMapCanvasLayer> myLayerSet;
QDir appDir;
QString myAppDir;
QString myPluginsDir;
QString myBaseDir;
QString appData;


MainWindow::MainWindow(QWidget *parent) :
    // Member initializers
    QMainWindow(parent),
    ui(new Ui::MainWindow)

{
    ui->setupUi(this);

    // App paths
    appDir = QApplication::applicationDirPath();
//    appDir.cdUp();
    myAppDir = appDir.absolutePath() + "/";
    myPluginsDir = myAppDir + "plugins/";
    myBaseDir = myAppDir + "base/";

    QgsApplication::setPrefixPath(myAppDir);
    QgsApplication::setPluginPath(myPluginsDir);

//  Instantiate Provider Registry
    QgsProviderRegistry::instance(myPluginsDir);
    QgsApplication::initQgis();

//     Create the Map Canvas
    mypMapCanvas = new QgsMapCanvas(0, 0);
    mypMapCanvas->enableAntiAliasing(true);
    mypMapCanvas->setCanvasColor(QColor(255, 255, 255));
    mypMapCanvas->setCrsTransformEnabled(true);

    ui->HL1->addWidget(mypMapCanvas);

//  Add vector layer
    QgsVectorLayer * myVLayer = new QgsVectorLayer("D:/GIS/base/departamentos.shp", "Deptos", "ogr", true);
    QgsMapLayerRegistry::instance()->addMapLayer(myVLayer, true);
    myLayerSet.append(QgsMapCanvasLayer(myVLayer, true));
    mypMapCanvas->setLayerSet(myLayerSet);
    mypMapCanvas->zoomToFullExtent();

    //  Add raster layer
    QgsRasterLayer * myRLayer = new QgsRasterLayer("D:/GIS/base/Google TileServer - Satellite.xml", "Google", "gdal", true);
    QgsMapLayerRegistry::instance()->addMapLayer(myRLayer, true);
    myLayerSet.append(QgsMapCanvasLayer(myRLayer, true));
    mypMapCanvas->freeze(false);
    mypMapCanvas->setLayerSet(myLayerSet);
    mypMapCanvas->setVisible(true);
    mypMapCanvas->refresh();

}

MainWindow::~MainWindow()
{
    delete ui;
}

Both layers load OK but with a scale mismatch.

In QT Creator, the Application Output says:

c:\src\qgis\src\core\qgscoordinatereferencesystem.cpp(296) : (QgsCoordinateReferenceSystem::loadFromDb) [3ms] failed : /resources/srs.db does not exist!

The complete log is here.

It seems there's a path issue with srs.db that I can't solve, and then, the crs or the scale is not correctly set for the raster or the vector layer. I don't know how the QGIS API handles the providers' (ogr or gdal) paths.

All what I did was of no use:

  • Set the GDAL_DATA environment variable to the directory where srs.db is. Now I'm using Qgis 2.18.16 API and the file is in C:/OSGeo4W/apps/qgis-rel-dev/resources. Bu the way, I'm using Windows 10.
  • Copy srs.db to the output directory (debug and release).
  • Copy srs.db to the directory set by QgsApplication::setPrefixPath

Nothing worked. Any suggestion?

Where should I use setCrsTransformEnabled(true)?

  • with QgsMapCanvas?
  • with QgsMapSettings?
  • with QgsTracer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.