Uso de la consola de Python para preparar mapas sin conexión desde WMS (tile downloader)

El estudio explica cómo preparar mapas fuera de línea a partir de WMS/WFS o cualquier mapa cargado en la vista de mapa QGIS descargando teselas con la ayuda de un script en la consola python. El script también genera un mosaico de todas las teselas for uso posterior.

Accessing raster/vector maps through Web Mapping Service (WMS) service is very common and efficient. It has several advantages such as security, centralized and flexible map styles. Example is Google maps, Yahoo, Bing, MODIS, Landsat, maps published by NASA, NRSC (India). The disadvantage/limitation of WMS is it works via internet or intranet. It may not be possible to get internet connection always especially in the field during survey. Also WMS works slow, compared to any offline image format such as GeoTiff, jpg, img etc.

El uso de QGIS

QGIS provides a tool to connect/access any WMS service. WMS connection is based on simple URL and once connection is established map can be seen on QGIS mapview. If we save the map of visible extent from QGIS mapview as jpg (File > Save as Image) it creates world file which has the geographic coordinate information of particular region E.g. sample.jpg is the map and sample.jpgw have the coordinates. That means the downloaded image is georeferenced. We can download tiles in grid format manually and use later offline. Manual downloading is possible if the area is very small or low resolution images are fine.

QGIS with WMS tiles

QGIS con tiles WMS

Consola Python

A short script was written in “Python Console” with help available on QGIS official website in PyQGIS Developer Cookbook. Script consists of basically 4 components as following,

  1. Coordenada X, Y (superior izquierda e inferior derecha) del primer tile
  2. Tamaño del tile para ser descargado (ejemplo 0.001 X 0.001 grados)
  3. Número de tiles para descargar (calculado con base en la extensión completa del mapa y el tamaño del tile)
  4. Directorio de salida para almacenar las tejas/archivos de salida

The script works in two simple for loops first which defines the extent of map in QGIS map view and run row/columns. Once the map extent is set then map is loaded in QGIS. The delay of 3 seconds (depends on internet connection) was introduced before saving map as jpg in user defined directory. Once the script is executed all the tiles created in directory can be directly loaded in QGIS or single mosaic is prepared from Raster > Miscellaneous > Merge or Build Virtual Raster (Catalog). Both tools generates mosaic but basic difference in Merge creates an image independent of tiles whereas Virtual raster generates a link in all individual tiles (tiles are required).

Python script and directory where tiles are stored

El script de python y el directorio donde los tiles son almacenados

The method has limitation that, changes in map at server end will not reflect in offline (downloaded map). In this case user will have to repeat the process and generate new offline map. Second limitation is the offline map created will be image format; it cannot be used for thematic mapping or image classification. With some extra efforts some landuse maps can be converted to vector with some data loss.

Alcance

The generated mosaic is pure image even if it is downloaded from thematic map (7-8 color). Using advance tool like reclassification, layer/band stacking this raster can be converted to polygon vector. WMS has several requests such as getcapabilities, getmap, getfeatureinfo etc. Using the getgeatureinfo attribute information for each polygon can be retrieved. The script can be converted to plugin so that anyone without scripting knowledge will be able to use the script.

Conclusión

Las siguientes experiencias muestran como QGIS pueden ser usadas a cualquier nivel. Con aptitudes básicas de programación se pueden escribir pequeñas rutinas o complementos para ahorrar tiempo y mejorar la calidad de trabajo. Dentro de QGIS, es posible acceder al repositorio de complementos desarrollados en todo el mundo. El PyQGIS Developer Cookbook (En Inglés) es un excelente recurso para escribir rutinas y con las herramientas de GDAL ahora es mucho más fácil procesar datos ráster.

Autores

This article was contributed May 2014 by Prashant Kadgi, based in Pune (India) and currently works as consultant in GIS APP Consultancy Services. Since 2009 he is using GIS & RS in NRM, forestry, e-governance, software development projects in different regions of India and the world.