0

I'm loading two layers to QGIS maps canvas: the first is a shapefile, and the second is an xml for OSM TileServer.

OSM TileServer - OSM.xml

<GDAL_WMS>
    <Service name="TMS">

        <ServerUrl>https://tile.openstreetmap.org/${z}/${x}/${y}.png</ServerUrl>
        <ImageFormat>image/png</ImageFormat>
    </Service>

    <DataWindow>

        <UpperLeftX>-20037508.34</UpperLeftX>
        <UpperLeftY>20037508.34</UpperLeftY>
        <LowerRightX>20037508.34</LowerRightX>
        <LowerRightY>-20037508.34</LowerRightY>

        <TileLevel>18<TileLevel>
        <TileCountX>1</TileCountX>
        <TileCountY>1</TileCountY>
        <YOrigin>top</YOrigin>

    </DataWindow>

    <Projection>EPSG:3857</Projection>
    
    <BlockSizeX>256</BlockSizeX>
    <BlockSizeY>256</BlockSizeY>

    <ZeroBlockHttpCodes>204,303,400,404,500,501</ZeroBlockHttpCodes>
    <ZeroBlockOnServerException>true</ZeroBlockOnServerException>
    
    <BandsCount>3</BandsCount>
    <DataType>byte</DataType>
    <MaxConnections>3</MaxConnections>

</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, the scale and position are wrong.

This is the cpp code:

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


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

// Globals
QgsMapCanvas * mpMapCanvas;
QList <QgsMapCanvasLayer> myLayerSet;



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

{
    ui->setupUi(this);


//  Instantiate Provider Registry
    QgsProviderRegistry::instance("/usr/local/qgis/lib/qgis/plugins");
    QgsApplication::initQgis();

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

    mpMapCanvas->show();
    
    //load shapfile
    QgsVectorLayer *shapeLayer = new QgsVectorLayer("/home/shp/GSHHS_i_L1.shp", "shp", "ogr");
    QgsMapLayerRegistry::instance()->addMapLayer(shapeLayer, TRUE, TRUE);
    myLayerSet.append(QgsMapCanvasLayer(shapeLayer));
   
  
    // Create a raster layer from a local TMS file
    QgsRasterLayer *tmsLayer = new QgsRasterLayer("./OSM.xml", "OSM_Tile", "gdal", true);
    QgsMapLayerRegistry::instance()->addMapLayer(tmsLayer, TRUE,TRUE); 
    myLayerSet.append(QgsMapCanvasLayer(tmsLayer));

    // Set the Map Canvas Layer Set
    mpMapCanvas->setLayerSet(myLayerSet);

    mpMapCanvas->setExtent(QgsRectangle(120.0, 22.0, 122.0, 25.0));
    mpMapCanvas->refresh();

Both layers load OK but with the scale and position mismatch.

How can I fix this?

1 Answer 1

0

I've found the solution for the above problem.

// Check if OTF CRS transformation is enabled
    if (!mapCanvas->hasCrsTransformEnabled())
    {
        // Enable OTF CRS transformation
        mapCanvas->setCrsTransformEnabled(true);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.