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!")
prj.instance().addMapLayer(ras)
qmc.setExtent(ras.extent())
layers.append(ras)
qmc.setLayers(layers)
else:
print("Unable to load basemap.")
C++ version:
QString url = "type=xyz&url=https://a.tile.openstreetmap.org";
url.append("/%7Bz%7D/%7Bx%7D/%7By%7D.png&zmax=19&zmin=0&crs=EPSG3857");
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!";
prj->instance()->addMapLayer(ras);
qmc->setExtent(ras->extent());
layers.append(ras);
qmc->setLayers(layers);
} 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)
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.QgsProviderRegistryagain to see if it changes the list and also if behaviour for loading layers has changed too.