Using the Bing aerial image service

While there are many services that provide street map tiles, there are far fewer services that provide 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 opening the Python console plugin within QGIS.

How to do it...

Similar to the approach used for the previous Using the OpenStreetMap service recipe, we will 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. To do this, we need to perform the following steps:

  1. First, we import the GDAL library:
    from osgeo import gdal
    
  2. 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>"""
  3. Now, we create the virtual file path for the XML file:
    vfn = "/vsimem/bing.xml"
    
  4. Then, we turn the XML file into a GDAL virtual file:
    gdal.FileFromMemBuffer(vfn, xml)
    
  5. Now, we can add the file as a QGIS raster layer and check its validity:
    rasterLyr = QgsRasterLayer(vfn, "BING")
    rasterLyr.isValid()
    
  6. Finally, we add the layer to the map:
    QgsMapLayerRegistry.instance().addMapLayers([rasterLyr])
    

How it works...

GDAL has drivers for various map services. The service name for Bing is 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 that it has copyright restrictions. Be sure to read the Bing usage agreement online.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset