I am trying to write a C++ version of a custom QGIS (v 3.10) application I initially wrote in Python that overlays data onto OpenStreetMap. I am able to do this successfully by loading online tiles via URL as a raster layer in Python using WMS provider; however the same URL does not work when using the C++ API, despite being able to load other raster files (e.g. GeoTIFF).

Code in Python:

url = 'type=xyz&url=https://a.tile.openstreetmap.org'
url += '/%7Bz%7D/%7Bx%7D/%7By%7D.png&zmax=19&zmin=0&crs=EPSG3857'
prj = QgsProject()
qmc = QgsMapCanvas()
layers = []
ras = QgsRasterLayer(url,'OpenStreetMap','wms')

if ras.isValid():
    print("Basemap loaded successfully!")

     print("Unable to load basemap.")

C++ version:

QString url = "type=xyz&url=https://a.tile.openstreetmap.org";
QgsProject() *prj = new QgsProject();
QgsMapCanvas *qmc = new QgsMapCanvas();
QList <QgsMapLayer *> layers;
QgsRasterLayer *ras = new QgsRasterLayer(url,'OpenStreetMap','wms');

if ( ras.isValid() )
    qDebug() << "Basemap loaded successfully!";
 } else
     qDebug() << "Unable to load basemap.";

I get the successful message and loaded map in Python and the invalid message and no map in C++. I have tried using other URL's but nothing has worked thus far. If there are plugins available that might help, but I would still need to write workable code without the use of the QGIS gui itself (as in most examples using plugins).

EDIT Using the advice of @ThomasG77 below, I made the following amendment:

QStringList plist = QgsProviderRegistry::instance()->providerList();
for ( int i=0; i<plist.length(); i++ )
    cout << plist.at(i).toStdString() << endl;

which yields the following output:

gdal; memory; mesh_memory; ogr

Do I have to manually add the OSM/WSM provider in my .pro file or environmental variable? Using Fedora Linux (31)

  • What are your loaded providers in your standalone Python, in your C++? In PyQGIS, within QGIS, I do QgsProviderRegistry.instance().providerList() to check. How fo you load your Python standalone project and your C++, in particular env variables? Providers are maybe not "well" loaded.
    – ThomasG77
    Commented Apr 6, 2021 at 15:33
  • Well the qgis libraries/headers are added to my .pro file for the C++ standalone and I have them added to my .bashrc (using Fedora) in my LD_LIBRARY_PATH variable.
    – skew_t_pie
    Commented Apr 6, 2021 at 16:16
  • @ThomasG77 see my edit for the output of the C++ equivalent using QgsProvderRegistry
    – skew_t_pie
    Commented Apr 6, 2021 at 16:32
  • About "OSM/WSM provider in my .pro file or environmental variable". Just try and retest again your QgsProviderRegistry again to see if it changes the list and also if behaviour for loading layers has changed too.
    – ThomasG77
    Commented Apr 6, 2021 at 18:26
  • Nothing has changed. The Python QgsProviderRegistry shows essentially all drivers: ['DB2', 'WFS', 'arcgisfeatureserver', 'arcgismapserver', 'delimitedtext', 'gdal', 'geonode', 'gpx', 'grass', 'grassraster', 'mdal', 'memory', 'mesh_memory', 'mssql', 'ogr', 'ows', 'postgres', 'spatialite', 'virtual', 'wcs', 'wms']. Both have the same .bashrc/.bash_profile environment settings. Only difference is I have python library/plugin paths added to the PyQGIS app paths.
    – skew_t_pie
    Commented Apr 6, 2021 at 18:53


Your Answer

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

Browse other questions tagged or ask your own question.