Adding real-time weather data from OpenWeatherMap

Real-time data is one of the most exciting data types you can add to a modern map. Most data producers make data available through Open GIS Consortium standards. One such example is OpenWeatherMap, which offers an OGC Web Map Service (WMS) for different real-time weather data layers. In this recipe, we'll access this service to access a real-time weather data layer.

Getting ready

To prepare for this recipe, you just need to open the QGIS Python Console by clicking on the Plugins menu and selecting Python Console.

How to do it...

We will add a WMS weather data layer for precipitation to a QGIS map, as follows:

  1. First, we specify the parameters for the service:
    service = 'crs=EPSG:900913&dpiMode=7&featureCount=10&format=image/png&layers=precipitation&styles=&url=http://wms.openweathermap.org/service'
    
  2. Next, we create the raster layer, specifying wms as the type:
    rlayer = QgsRasterLayer(service, "precip", "wms")
    
  3. Finally, we add the precipitation layer to the map:
    QgsMapLayerRegistry.instance().addMapLayers([rlayer])
    

How it works...

A WMS request is typically an HTTP GET request with all of the parameters as part of the URL. In PyQGIS, you use a URL-encoded format and specify the parameters separately from the URL.

The following map image shows the output of the precipitation layer in QGIS:

How it works...
..................Content has been hidden....................

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