This recipe creates a set of Internet web map tiles from your QGIS map. What's interesting about this recipe is that once the static map tiles are generated, you can serve them up locally or from any web-accessible directory using the client-side browser's JavaScript without the need of a map server, or you can serve them (for example, distribute them on a portable USB drive).
You will need to download the zipped shapefile from https://geospatialpython.googlecode.com/svn/countries.zip.
Unzip the shapefile to a directory named shapes
in your qgis_data
directory. Next, create a directory called tilecache
in your qgis_data
directory. You will also need to install the QTiles plugin using the QGIS Plugin Manager. This plugin is experimental, so make sure that the Show also experimental plugins checkbox is checked in the QGIS Plugin Manager's Settings tab.
We will load the shapefile and randomly color each country. We'll then manipulate the QTiles plugin using Python to generate map tiles for 5
zoom levels' worth of tiles. To do this, we need to perform the following steps:
from PyQt4.QtCore import * from PyQt4.QtGui import * import qtiles import random
def randomColor(mix=(255,255,255)): red = random.randrange(0,256) green = random.randrange(0,256) blue = random.randrange(0,256) r,g,b = mix red = (red + r) / 2 green = (green + g) / 2 blue = (blue + b) / 2 return (red, green, blue)
def done(): print "FINISHED!!"
shp = "/qgis_data/shapes/countries.shp" dir = "/qgis_data/tilecache"
layer = QgsVectorLayer(shp, "Countries", "ogr")
field = 'CNTRY_NAME'
features = layer.getFeatures()
categories = [] for feature in features: country = feature[field] sym = QgsSymbolV2.defaultSymbol(layer.geometryType()) r,g,b = randomColor() sym.setColor(QColor(r,g,b,255)) category = QgsRendererCategoryV2(country, sym, country) categories.append(category)
renderer = QgsCategorizedSymbolRendererV2(field, categories) layer.setRendererV2(renderer) QgsMapLayerRegistry.instance().addMapLayer(layer)
canvas = iface.mapCanvas() layers = canvas.mapSettings().layers() extent = canvas.extent() minZoom = 0 maxZoom = 5 width = 256 height = 256 transp = 255 quality = 70 format = "PNG" outputPath = QFileInfo(dir) rootDir = "countries" antialiasing = False tmsConvention = True mapUrl = False viewer = True
QTiles
plugin. We'll create a thread object and pass it all of the tile settings previously mentioned:tt = qtiles.tilingthread.TilingThread(layers, extent, minZoom, maxZoom, width, height, transp, quality, format, outputPath, rootDir, antialiasing, tmsConvention, mapUrl, viewer)
tt.processFinished.connect(done)
tt.start()
countries.html
and a directory named countries
.countries.html
page to open it in a browser.You can generate up to 16 zoom levels with this plugin. After eight zoom levels, the tile generation process takes a long time and the tile set becomes quite large on the filesystem, totaling hundreds of megabytes. One way to avoid creating a lot of files is to use the mbtiles format, which stores all the data in a single file. However, you need a web application using GDAL to access it.
You can see a working example of the output recipe stored in a github.io
web directory at http://geospatialpython.github.io/qgis/tiles/countries.html.