How to Add a Raster Image in QGIS Using Qt, C++, and QGIS API

Introduction

In Geographic Information Systems (GIS), raster data represents information in a grid-based structure such as satellite imagery, aerial photographs, digital elevation models (DEM), or scanned maps. Unlike vector data, which uses points, lines, and polygons, raster data provides continuous coverage across a geographic area.

QGIS, one of the most powerful open-source GIS platforms, provides robust support for raster datasets. Developers can leverage the QGIS API with Qt and C++ to seamlessly integrate raster layers into custom applications. This allows organizations to build tailored GIS solutions while retaining the scalability and flexibility of QGIS.

In this blog, we’ll walk through adding raster imagery into a QGIS-based Qt C++ application using the QGIS API.


QGIS Capabilities with Raster Data

QGIS offers a wide range of raster capabilities, including:

  • Loading raster datasets from multiple formats (GeoTIFF, JPEG, PNG, DEM, etc.)
  • Supporting GDAL (Geospatial Data Abstraction Library) for format compatibility
  • Raster styling and symbology (grayscale, pseudocolor, hillshade, transparency)
  • Analytical tools (resampling, reprojection, raster calculator, contour generation)
  • Integration with PostGIS raster and WMS/WMTS services

By programmatically adding raster data with QGIS API, developers can automate workflows, enable interactive visualization, and build fully customized GIS tools.


Source Code Example

Below is a C++ example of how to add raster data into a QGIS-enabled Qt application:

void MainWindow::addRasterData()
{
    QStringList filePaths = m_objAddMapData->filePathInputLayer;

    if (!filePaths.isEmpty()) {
        for (auto filePath : filePaths) {
            QFileInfo info(filePath);
            QString fileName = info.fileName();

            if (!filePath.isEmpty()) {
                // Create raster layer using GDAL provider
                QgsRasterLayer *rasterLayer = new QgsRasterLayer(filePath, info.baseName(), "gdal");

                if (rasterLayer->isValid()) {
                    qDebug() << "openRasterData: Layer Valid";
                    fileName = info.fileName().split('.')[0];

                    // Add layer to the QGIS map canvas
                    addLayer(rasterLayer);
                }
                else {
                    qDebug() << "Load Raster Failed";
                    qDebug() << "Raster Layer Error: " << rasterLayer->error().message();
                    delete rasterLayer;
                }
            }
        }
    }

    // Clear file input paths after loading
    m_objAddMapData->filePathInputLayer.clear();
}

Explanation

  1. filePaths: A list of raster file paths selected by the user.
  2. QgsRasterLayer: Creates a raster layer using GDAL.
  3. Validation: Ensures the raster is valid before adding.
  4. addLayer(rasterLayer): Adds the valid raster layer to the map canvas.
  5. Error handling: Reports and cleans up invalid raster layers.

This snippet allows seamless integration of raster datasets, making them available for visualization and further analysis in a QGIS-powered application.


Conclusion

Raster imagery is a crucial component of GIS applications, offering detailed spatial information that supports decision-making in domains like remote sensing, defense, agriculture, and urban planning. With the QGIS API and Qt/C++, developers can extend QGIS capabilities to build powerful, domain-specific GIS applications.

If you’re looking to build custom GIS applications that integrate raster, vector, and live data seamlessly, our team at Manya Technologies specializes in delivering scalable, robust, and tailored GIS solutions.

👉 Contact us today to discuss your requirements: Manya Technologies

👉 Check our GIS solutions: PrithviGIS


Scroll to Top