Bing Aerial Image Service: QGIS Python Programming CookBook

By: PACKT Books

Last updated:

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”>

ย ย ย ย ย ย ย ย ย ย  <ServerUrl>

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย  http://a${server_num}.ortho.tiles.virtualearth.net/tiles/a${quadkey}.jpeg?g=90

ย ย ย ย ย ย ย ย ย ย  </ServerUrl>

ย ย ย  </Service>

ย ย ย  <MaxConnections>4</MaxConnections>

ย ย ย  <Cache/>

</GDAL_WMS>”””

  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”)

rasterLyr.isValid()

  1. Finally we add the layer to the map:

QgsMapLayerRegistry.instance().addMapLayers([rasterLyr])

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ย โ€“>

Photo of author
PACKT Books