Optimization

Conceitos gerais

Rendering speed is crucial for a WebGIS application, more than in a desktop application:

  • os utilizadores web esperam ter tudo disponível quase imediatamente

  • each user can sends requests to the same application; if you have tens or hundreds of users, you can easily understand that optimising your web application is an important task.

You have to think about a web publication for many users rather than the display of a map to a single user.

By default, for each QGIS layer you add to your Lizmap project, you can choose from the Lizmap plugin whether to toggle the layer visibility on (checkbox Toggled) at the startup of the application. You have to be careful not to abuse this feature, because if the project contains e.g. 30 layers, Lizmap at startup will send a request to QGIS server for each of them.

If the checkbox Single tile is ticked, this will request 30 images of the size of your browser window. If not, Lizmap, through OpenLayers, will request 30 series of tiles (about 250 by 250 pixels). Each tile is an image, and is created as a function of the total window size and zooming level. Therefore, subsequent users will zoom in the same area, the tiles already generated will be reused.

The tiles can be cached with two non exclusive systems:

  • Server side, on the machine where QGIS server and Lizmap are installed. If the tile has been requested and generated earlier, and not expired, Lizmap will reuse it and send it to the client, avoiding a new request to QGIS server.

  • Client side: tiles will be saved in the browser cache and reused until they expire. This avoid both the request to QGIS server and the internet traffic.

The server cache has to be generated. Read Gestão da cache como administrador to know how to generate the cache.

Para otimizar o desempenho, é importante entender como o Lizmap usa os mosaicos para serem apresentados.

Let’s say you have a screen of 1280 by 768 pixels. If you have all your layers tiled, Lizmap has therefore to show about 5 by 3 = 15 tiles (256 by 256 pixel each) per layer, and more for a larger screen. If surrounding tiles are only partially shown, the total number will be even greater. An average of 20 tiles per layer is a reasonable estimate. With 30 layers, as in our example, this will mean a total of about 20 by 30 = 600 tiles (therefore, 600 requests to Lizmap server) per user, at each startup of Lizmap and for every zoom & pan. If you have 10 concurrent users, this gets quite heavy for the server, if the cache has not been generated previously, and QGIS server has therefore to create them. The time required for each tile will depend heavily on the performance of the server and the complexity of the project.

O tamanho de cada mosaico dependerá de:

  • o tipo de dados (raster único ou vetor, ou combinação de várias camadas)

  • o formato de imagem escolhido (PNG, JPEG)

A typical tile could be around 30 Kb. In our example, the client will therefore download about 20 by 30 = 600 Kb per layer, which, for 30 layers, will give a grand total of about 18 Mb, which is heavy both for the server (lots of connection bandwidth consumed) and for the users (long delay, even with a reasonably fast connection).

Estes cálculos mostram claramente que para alcançar bons desempenhos no mapeamento web é necessário fazer escolhas e simplificar o máximo possível.

Por exemplo, se alguém prestar atenção à abordagem do Google Maps ou serviços similares, é óbvio que, além de terem servidores poderosos, eles simplificaram ao máximo possível: apenas uma série de mosaicos como camada base e muito poucas camadas adicionais (e nem todas ao mesmo tempo). Mesmo que não seja possível criar um mapa tão simples, é importante, no entanto, saber quais camadas que devem ser mostradas na inicialização do mapa e quais os compromissos aceitáveis para os seus utilizadores.

Se o seu projeto tem 50 camadas passíveis de ligar e desligar, a grande maioria dos seus utilizadores nunca irá selecionar a maior parte delas. É claro que existem casos reais de uso em que as camadas individuais devem ser apresentadas seletivamente e, portanto, não é possível agrupá-las para reduzir o número de camadas mostradas.

Tips

Para otimizar o máximo possível a sua aplicação, sugerimos que:

  • Crie projetos QGIS separados e, consequentemente, mapas Lizmap diferentes para diferentes objetivos, agrupando os dados em temas lógicos. Por exemplo, um mapa sobre o desenvolvimento urbano com provavelmente 10 camadas e um sobre ambiente, com cerca de 5 camadas, geralmente são mais legíveis, e muito mais rápido, do que um único projeto complexo com todos os dados. Adicionar uma pequena imagem para cada projeto ajudará os utilizadores a selecionar o projeto mais relevante à primeira vista. Também é possível partilhar algumas das camadas entre os diferentes projetos, através do mecanismo de incorporação do QGIS.

  • Use the option Maps only in the administrator web interface. This option allows the user to switch automatically from one map to another, through the button Home, maintaining as much as possible the localization and the zooming level. In this case, the Lizmap welcome page with the list of projects and their thumbnails is not displayed, and the user is directed automatically to one of the projects, at the administrator choice.

  • Do not show all the layers at startup (deactivate the checkbox Toggled as described above). Only very important layers should be visible by default, and users should activate only the layer they need. This allow a sensible reduction in the number of requests, and of the total network traffic.

  • Create groups of layers, and use the option Group as layer in Lizmap plugin. Generally a series of layers of the same general theme can be displayed as a whole, with an appropriate choice of styles. In this case, Lizmap will only show one checkbox for the whole group, and more importantly it will request only one series of tiles for the whole group, thus reducing the number of tiles and server requests, and the total volume of data to be downloaded. The legend of the group will be displayed.

  • Use the option Single tile for some layers. In this case, Lizmap will request only one image per layer, of about the size of the screen, instead of a series of tiles. This will therefore greatly reduce the number of requests to the server. For instance, in our example above, without the optimizations described, if all the layers are displayed, every user will request 30 images (one per layer) for every zoom or pan, instead of 480. The total size of data to be downloaded is however similar. On the other hand, different users will be very unlikely to request exactly the same image, therefore using a cache is pointless in this case, and is avoided by Lizmap (the two options are mutually exclusive). The optimal choice (single tile vs. tiled) is different for different layers. For instance, a complex base layer, created by combining 15 individual layers, will be best used as a group (Group as layer), tiled and cached. A simple linear layer, like a series of bus lines, can be displayed as a single tile.

  • Use the option Hide checkboxes for groups: this avoids the users to click on a group with e.g. 20 layers without really needing it, thus firing a big series of requests to the server. In any case, avoiding groups of more than 5-10 layers is usually good practice.

  • Otimize os dados e o projeto QGIS. Como mencionado acima, a publicação de um mapa na Internet vai mudar o seu ponto de vista: como já referido, é necessário ter em atenção que muitos utilizares podem fazer solicitações ao servidor em paralelo, dessa forma evitar a sobrecarga é crucial para:

    • criar um índice espacial para todas as suas camadas vetoriais

    • criar pirâmides para todas as suas camadas raster (exceto para as muito pequenas)

    • apresentar apenas dados na escala apropriada: por exemplo, apresentar uma camada detalhada de construções a 1:500.000 não faz sentido, pois a imagem é praticamente ilegível e exige demasiado esforço do servidor

    • use simplified version of a layer to display it at different scales. You can then group the original layer (to be displayed e.g. around 1:1,000) with the simplified versions (to be displayed e.g. around 1:10,000, 1:50,000, etc.), and Goup as a layer to let the user see this as a single layer, using the most appropriate data at each scale

    • Tenha cuidado com a reprojeção On The Fly (OTF). Se, por exemplo, apresentar dados em Lambert 93 (EPSG:2154) sobre um mapa base do OpenStreetmap ou Google (em Pseudo Mercator, EPSG:3857), o QGIS Server necessitará de reprojetar rasters e vetores antes de gerar o mapa. Isso pode ter um impacto nos tempos de renderização para camadas grandes e complexas. Em França, pode evitar a reprojeção utilizando o mapa de base IGN Géoportail diretamente em EPSG:2154

    • esteja ciente do fato de que certas opções de renderização (p.ex., rótulos, expressões, etc.) podem ser muito exigentes relativamente ao servidor

    • se utilizar o PostGIS, otimize-o: adicione sempre índices espaciais, índices para campos filtrados, para chaves estrangeiras, parâmetros apropriados para a configuração do PostgreSQL, possivelmente uma ligação via socket Unix em vez de TCP/IP (pode fazer isso através da utilização de serviços), etc.

    • use an appropriate image format. For the base layers, where you do not need transparency JPEG is usually the best option: the tiles will be smaller, and faster to download. For other layers, try smaller depth PNGs (16bit or 8bit): for some symbols, the visual result may be the same, and the tiles smaller. Have a check to see if the image quality is acceptable in your case

    • Use server side simplification if possible. Read Server side simplification.

  • Atualize seu servidor. Esta é sempre uma opção, mas muitas vezes inútil se não otimizar o seu projeto como descrito acima. Em qualquer caso, um servidor de gama baixa (por exemplo, 2 Gb de RAM, 2 núcleos a 2,2 GHz) é inadequado. Um quad-core rápido com 8 Gb de RAM é o mínimo razoável. Evite instalar o servidor QGIS e o Lizmap em Windows, é mais complexo e mais lento.

  • Lizmap avoids the automatic download of the legends at startup and at every zoom level. This will be done exclusively on demand, if the legend is displayed, thus saving one request per layer for each zoom.

Em detalhe: como ativar as caches

In Lizmap plugin ‣ Layers, you can enable for each layer or group the cache (client and server side) for generated images.

Server side

This feature is not compatible with the option Single tile. Lizmap Web Client can dynamically create a cache tiles on the server. This cache is the storage of the images already generated by QGIS-Server on the server. The Lizmap Web Client application automatically generates the cache as the tiles are requested. Enable caching can greatly lighten the load on the server, since we do not want more QGIS-Server tiles that have already been made.

To activate it, you must:

  • check the box Server tile cache

  • specify the expiration time of the cache server in seconds: Expiration (seconds). 0 means no expiration on the server, the tile will be kept on the server until the cache is cleared.

The Metatile option allows you to specify image size in addition for generating a tile. The principle of Metatile is to request the server for a bigger image than hoped, to cut it to the size of the request and return it to the Web client. This method avoids truncated labels at the edges and discontinuities between tiles, but is more resource intensive. The default value is 3,3, an image whose width and height are equal to 5 times the width and height request. This option is useless for rasters.

Nota

For vector layers with metatiles, you have to set label position to a fixed position. If the label is very long, do not forget to use a word wrap function or to increase the metatile size a little bit. So labels are displayed in the same area.

Client side

The Browser client cache option allows you to specify an expiration time for the tiles in the Web browser (Mozilla Firefox, Chrome, Internet Explorer, Opera, etc.) cache in seconds. When browsing the Lizmap map with the browser, it stores displayed tiles in its cache. Enable client cache can greatly optimize Lizmap because the browser does not re-request the server for tiles already in cache that are not expired.

Aviso

Values 0 and 1 are equivalent and do not activate the option.

Sugerimos que defina o valor máximo (1 mês, equivalente a 24 x 3600 x 30 = 2,592,000 segundos), exceto, obviamente, para camadas cujos dados mudam frequentemente.

Nota

  • A cache deve ser ativada apenas em renderização real, quando desejar colocar o projeto em produção.

  • These two cache systems, server and client, are completely independent of one another. But of course, it is interesting to use the two together to optimize the application and free server resources.

Centralizar a cache com a integração de grupos e camadas de um projeto principal

In QGIS, it is possible to integrate in a project, groups or layers from another project (which will be called «parent»). This technique is interesting because it allows you to set the properties of the layers once in a project and use them in several other, for example for baselayers. In the «child» projects that integrate these layers, it is not possible to change these properties.

Lizmap uses this feature to centralize the tiles cache. For all child projects using integrated layers of the parent project, Lizmap requests QGIS-Server tiles from the parent project, not from child projects. The cache will be centralized at the parent project and all child projects that use layers benefit the shared cache.

Para utilizar este recurso, deve:

  • publicar o projeto QGIS pai com o Lizmap

    • you must choose the right announced extent in project properties ‣ QGIS Server, because this extent will be reused identically in child projects.

    • you must configure the cache for the layers to integrate. Also, note the options chosen here (image format, metatile, expiration) for use as such in child projects.

    • It is possible to hide the project from the main page of Lizmap with the check box in Map ‣ Hide the project Web Client Lizmap.

  • open the child project and integrate layers or groups in this project, for example orthophoto. Then you must:

    • verify that the announced extent in QGIS project properties ‣ QGIS Server is exactly the same as the parent project.

    • configurar a cache para a camada integrada com exatamente as mesmas opções que as selecionadas a partir do projeto pai: tamanho da imagem, expiração, mosaico grande.

    • definir o id Lizmap do repositório de origem do projeto pai (de acordo com o configurado na interface de administração do Lizmap Web Client).

    • the code of the «Source project» (the name of the parent QGIS project without the .qgs extension) is automatically entered for layers and integrated groups.

  • Publish the child project to the Lizmap Web Client as usual.