2

I am writing a simple program to display raster data using qgis api C++.
When I open raster data that has different coordinate systems, the on-the-fly feature does not work on my mapCanvas.
Can someone help me?

OpenGisData::OpenGisData(QWidget *parent)
: QMainWindow(parent)
{
    setupUi(this);
    QWidget* centralWidget = OpenGisDataClass::centralWidget;
    mMapCanvas = new QgsMapCanvas(centralWidget);
    QGridLayout* gridLayout = new QGridLayout;
    gridLayout->addWidget(mMapCanvas);
    centralWidget->setLayout(gridLayout);
    connect(actionOpen, &QAction::triggered, this, &OpenGisData::open);
}
void OpenGisData::open()
{
    QString fileName = QFileDialog::getOpenFileName(this, "Open data", "D:\\Test");
    if (!fileName.isEmpty())
    {
        QgsRasterLayer* rasterLayer = new QgsRasterLayer(fileName);
        if (rasterLayer && rasterLayer->isValid())
        {
            QList<QgsMapLayer*> layers = mMapCanvas->layers();
            layers.append(rasterLayer);

            mMapCanvas->setLayers(layers);
            mMapCanvas->zoomToFullExtent();
        }
    }
}

1 Answer 1

0

I solved my problem by adding the following code:

connect(QgsProject::instance(), &QgsProject::crsChanged, [=] {
    QgsCoordinateReferenceSystem crs = QgsProject::instance()->crs();
    mMapCanvas->setDestinationCrs(crs);
});

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.