Bing Aerial Image Service: QGIS Python Programming CookBook

| | ,

Learn how to use the Bing Aerial Image Service in QGIS using Python from this section as part of this preview chapter, Creating Dynamic Maps from QGIS Python Programming CookBook. With 140 short, reusable recipes to automate geospatial processes in QGIS, the QGIS Python Programming CookBook teaches readers how to use Python and QGIS to create and transform data, produce appealing GIS visualizations, and build complex map layouts.

Using the Bing Aerial Image Service

While there are many services providing street map tiles, there are far fewer services providing imagery services. One excellent free service for both maps, and more importantly, imagery is Microsoft’s Bing Map Services. We can access Bing imagery programmatically in QGIS using GDAL’s WMS capability coupled with virtual files.

Getting ready

You don’t need to do any preparation for this recipe other than to open the Python Console plugin within QGIS.

How to do it…

Similar to the approach used for the previous OpenStreetMaps recipe, we create an XML file as a string to describe the service, turn it into a GDAL virtual file, and load it as a raster in QGIS.

  1. First we import the GDAL library:

fromosgeo import gdal

  1. Next we create the XML file describing the bing service as a string:

xml = “””<GDAL_WMS>

<Service name=”VirtualEarth”>








  1. Now we create the virtual file path for the XML file:

vfn = “/vsimem/bing.xml”

  1. Then we turn the XML file into a GDAL virtual file:

gdal.FileFromMemBuffer(vfn, xml)

  1. Now we can add the file as a QGIS raster layer and check its validity:

rasterLyr = QgsRasterLayer(vfn, “BING”)


  1. Finally we add the layer to the map:


How it works…

GDAL has drivers for various map services. The one for Bing is called VirtualEarth. The ${} clauses in the server URL provide placeholders which will be replaced with instance-specific data when GDAL downloads styles. When using this data you should be aware it has copyright restrictions. Be sure to read the Bing usage agreement online.

QGIS Python Programming CookBook

<– Previous: OpenStreetMap Service | Next: OpenWeatherMap –>

Enter your email to receive the weekly GIS Lounge newsletter: