Creating a KML image overlay for a raster

GoogleEarth is one of the most widely available geospatial viewers in existence. The XML data format used by GoogleEarth for geospatial data is called KML. The Open Geospatial Consortium adopted KML as a data standard. Converting rasters into a KML overlay compressed in a KMZ archive file is a very popular way to make data available to end users who know how to use GoogleEarth.

Getting ready

We will use the SatImage raster again available at the following URL if you haven't downloaded it from previous recipes:

https://geospatialpython.googlecode.com/files/SatImage.zip

Place this raster in your /qgis_data/rasters directory.

How to do it...

In this recipe, we'll create a KML document describing our image. Then we'll convert the image to a JPEG in memory using GDAL's specialized virtual file system and write all of the contents directly to a KMZ file using Python's zipfile module.

  1. Start QGIS.
  2. From the Plugins menu select Python Console
  3. We need to import the gdal module as well as the Python zipfile module:
    from osgeo import gdal
    import zipfile
    
  4. Next, we'll open our satellite image in gdal:
    srcf = "/qgis_data/rasters/Satimage.tif"
    
  5. Now, we'll create a variable with our virtualized file name, using the GDAL virtual file naming convention beginning with vismem:
    vfn = "/vsimem/satimage.jpg"
    
  6. We create the JPEG gdal driver object for the output format:
    drv = gdal.GetDriverByName('JPEG')
    
  7. Now, we can open the source file:
    src = gdal.Open(srcf)
    
  8. Then, we can copy that source file to our virtual JPEG:
    tgt = drv.CreateCopy(vfn, src)
    
  9. Now, we are going to create a raster layer in QGIS for our raster, just for the benefit of it calculating the image's extent:
    rasterLyr = QgsRasterLayer(srcf, "SatImage")
    
  10. Next, we get the layer's extent:
    e = rasterLyr.extent()
    
  11. Next, we format our KML document template and insert the image extents:
    kml = """<?xml version="1.0" encoding="UTF-8"?>
    <kml xmlns="http://www.opengis.net/kml/2.2">
      <Document>
        <name>QGIS KML Example</name>
        <GroundOverlay>
            <name>SatImage</name>
            <drawOrder>30</drawOrder>
            <Icon>
              <href>SatImage.jpg</href>
            </Icon>
            <LatLonBox>
              <north>%s</north>
              <south>%s</south>
              <east>%s</east>
              <west>%s</west>
            </LatLonBox>
        </GroundOverlay>
      </Document>
    </kml>""" %(e.yMaximum(), e.yMinimum(), e.xMaximum(), e.xMinimum())
  12. Now, we open our virtual JPEG in GDAL and prepare it for reading:
    vsifile = gdal.VSIFOpenL(vfn,'r')
    gdal.VSIFSeekL(vsifile, 0, 2)
    vsileng = gdal.VSIFTellL(vsifile)
    gdal.VSIFSeekL(vsifile, 0, 0)
    
  13. Finally, we write our KML document and virtual JPEG into a zipped KMZ file:
    z = zipfile.ZipFile("/qgis_data/rasters/satimage.kmz", "w", zipfile.ZIP_DEFLATED)
    z.writestr("doc.kml", kml)
    z.writestr("SatImage.jpg", gdal.VSIFReadL(1, vsileng, vsifile))
    z.close()
    
  14. Now, open the KMZ file in GoogleEarth and verify it looks like the following screenshot:
    How to do it...

How it works...

KML is a straightforward XML format. There are entire libraries in Python dedicated to reading and writing it, but for a simple export to share an image or two, the PyQGIS console is more than adequate. While we run this script in the QGIS Python interpreter, it could be run outside of QGIS using just GDAL.

There's more...

The Orfeo Toolbox has a processing algorithm called otb:imagetokmzexport which has a much more sophisticated KMZ export tool for images.

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

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