NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

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.";
 }

(Note: the layer list takes QgsMapLayer pointers in C++ but couldn't escape the vectors using &lt/&gt). 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).

asked 06 Apr '21, 15:45

raisehellpraisedale's gravatar image

raisehellpra...
11112
accept rate: 0%

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×710
×287
×67
×39
×10

question asked: 06 Apr '21, 15:45

question was seen: 2,765 times

last updated: 07 Apr '21, 08:32

NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum