I would like to add a OSM Standard Basemap to my project through a python script running in PyCharm, outside of QGIS. Within QGIS, I use QuickMapServices and add an OSM Standard Map but I can not seem to find anywhere how to retrieve the information from this plugin. Ultimately, I would like to know some code from a script that adds a base map to my project. I have tried using the code below but it says it is valid. However, no map appears in my project.

    import glob
    import json
    import math
    import os
    import sys
    from qgis.core import *
    from PyQt5.QtGui import QColor
    from PyQt5.QtGui import QFont
    from qgis.core import QgsRasterLayer
    from qgis.core import QgsVectorLayer
    from qgis.core import QgsApplication
    from qgis.core import QgsProcessingFeedback
    from qgis.analysis import QgsNativeAlgorithms
    from qgis._gui import QgsMapCanvas
    os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = r'C:\OSGEO4W64\apps\Qt5\plugins'
    os.environ['PATH'] += r';C:\OSGEO4W64\apps\qgis\bin;C:\OSGEO4W64\apps\Qt5\bin'
    os.environ['PROJ_LIB'] = 'C:\\OSGeo4W64\\share\\proj'
    QgsApplication.setPrefixPath("C:/OSGeo4W64/bin/qgis-bin.exe", True)
    qgs = QgsApplication([], True)
    from qgis import processing
    from processing.core.Processing import Processing
    # Write your code here to load some layers, use processing
    # algorithms, etc.
    DistInt = {}
    # Add Base Map
    urlWithParams = 'type=xyz&url=https://a.tile.openstreetmap.org/%7Bz%7D/%7Bx%7D/%7By%7D.png&zmax=19&zmin=0&crs=EPSG3857'
    rlayer2 = iface.addRasterLayer(urlWithParams, 'OpenStreetMap', 'wms')
    if rlayer2.isValid():
        print('invalid layer')
  • I have add the lines in my script I have used before adding my base map, above. I have just taken a snippet as my script is over 400 lines long. I have added QgsMapCanvas but not bridge? Thanks. – LiamHems Jan 13 at 15:26
  • Did you create QMainWindow? Where do you define QgsMapCanvas? – Kadir Şahbaz Jan 13 at 15:43
  • I have not created a QMainWindow. I have only imported QgsMapCanvas from qgis._gui. – LiamHems Jan 13 at 15:49
  • Did you add other layers (shapefile for example) to the project and did they appear? – Kadir Şahbaz Jan 13 at 15:50
  • 1
    Integrated your code in a sample I maintain with everything working with your layer gist.github.com/ThomasG77/f711853e5fb81c746d2a1af0b2a9ecf5 I see at least 1 obvious error: iface.addRasterLayer should be QgsRasterLayer (adapt import). iface not available in standalone application. You may adapt my script to troubleshot your own issue. PS: on Linux so, less environment variables than you – ThomasG77 Feb 1 at 22:17

We don't know that the other parts of your code look like. So it is hard to provide a perfect answer. But the followings will give you an idea. It worked for me in the project with GUI.

from qgis.gui import QgsLayerTreeMapCanvasBridge


canvas = QgsMapCanvas()
root = QgsProject.instance().layerTreeRoot()
bridge = QgsLayerTreeMapCanvasBridge(root, canvas)

  • Sorry, I am a little bit confused on how to implement this into my project, so that I can see the base map within my project? Where in my script would I add this code? Thanks – LiamHems Jan 14 at 10:47
  • The first line to "imports", the last two lines underneath where you define QgsMapCanvas. – Kadir Şahbaz Jan 14 at 10:59
  • I have added the first line where my imports are and then under my code where I have canvas = QgsMapCanvas(), I have changed the code to self.canvas = QgsMapCanvas() and added the two lines below that. However, it now says that name 'self' is not defined... can I import the module self? – LiamHems Jan 14 at 11:07
  • Please provide a minimal reproducible code. Otherwise, it is hard to say something, We don't know how the script file are structured. I thought you use a class, so I added self. Try to remove selfs. – Kadir Şahbaz Jan 14 at 11:10
  • I have tried to remove self but still no luck unfortunately. Sorry I know it is hard to understand how the script is structured without the full script. – LiamHems Jan 14 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.