Requête spatiale¶
Sommaire
Dans les options de carte, vous pouvez configurer les recherches d’adresses basées sur des services web externes (nominatim, google, IGN France). Lire Options de carte. De plus, vous pouvez ajouter des moyens de recherche spatiale à Lizmap. Cela signifie que vous autoriserez les utilisateurs à effectuer des recherches avec des données spatiales, telles que les pays, les points d’intérêts, etc. Vous avez 2 manières pour ajouter des moyens de recherche dans Lizmap :
- Pour QGIS 2 et QGIS 3, depuis |lizmap_3_2|, vous pouvez créer une table ou une vue
lizmap_search
dans votre base de données PostgreSQL pour stocker les données de recherche de vos projets Lizmap. - Pour QGIS 2 seulement, vous pouvez utiliser le plugin
QuickFinder
pour configurer les données de recherche par projet QGIS.
Recherche PostgreSQL¶
When you have many projects and data, the best solution to provide searching capabilities is to set up a dedicated relation (table or view) inside your database. It’s possible to use a PostgreSQL database to store the search data.
Pré-requis¶
- Une base de données PostgreSQL, accessible depuis Lizmap Web Client.
- PostgreSQL avec les extensions activées :
unaccent
etpg_trgm
(pour des requêtes LIKE efficaces) - A custom function
f_unaccent
which can be used in an index. See this Stack Overflow post for explanation
-- Add the extension pg_trgm
CREATE EXTENSION IF NOT EXISTS pg_trgm;
-- Add the extension unaccent, available with PostgreSQL contrib tools. This is needed to provide searches which are not sensitive to accentuated characters.
CREATE EXTENSION IF NOT EXISTS unaccent;
-- Add the f_unaccent function to be used in the index
CREATE OR REPLACE FUNCTION public.f_unaccent(text)
RETURNS text AS
$func$
SELECT public.unaccent('public.unaccent', $1) -- schema-qualify function and dictionary
$func$ LANGUAGE sql IMMUTABLE;
Note
We choose to use the pg_trgm
extension and this custom f_unaccent
function instead of the Full Text Search (FTS) tool of PostgreSQL, to keep the tool as simple as possible and avoid the need to create FTS « vectors » in your search data.
Créer la table de recherche Lizmap ou la vue¶
The database admin must create a table, view or materialized view called lizmap_search
.
This relation must be accessible in the search_path
(you can put it in the public schema,
or configure the search_path
variable for the database or the user which connects to the database).
La relation lizmap_search
doit contenir les colonnes suivantes :
item_layer
(text). Name of the layer. For example « Countries »item_label
(text). Label to display the results, which is the data to search among. Ex: « France » or « John Doe - Australia ». You can build it from a concatenation of several fields values.item_project
(text). List of Lizmap projects separated by commas. When set, the search will be done only for the specified Lizmap projects. The value can beNULL
if you don’t want to filter per project.item_filter
(text). Username or group name. When given, the results will be filtered by authenticated user login and groups. For example, “admins”. The value can beNULL
if you don’t want to filter per user.geom
(geometry). We advise to store all the geometries with the same SRID.
Voici un exemple de code SQL que vous pouvez utiliser, pour ajouter des données issues de deux tables spatiales différentes dans la recherche lizmap (ici en tant que vue matérialisée pour faciliter une maintenance ultérieure)
DROP MATERIALIZED VIEW IF EXISTS lizmap_search;
CREATE MATERIALIZED VIEW lizmap_search AS
SELECT
'Commune' AS item_layer, -- name of the layer presented to the user
concat(idu, ' - ', tex2) AS item_label, -- the search label is a concatenation between the 'Commune' code (idu) and its name (tex2)
NULL AS item_filter, -- the data will be searchable for every Lizmap user
NULL AS item_project, -- the data will be searchable for every Lizmap maps (published QGIS projects)
geom -- geometry of the 'Commune'. You could also use a simplified version, for example: ST_Envelope(geom) AS geom
FROM cadastre.geo_commune
UNION ALL -- combine the data between the 'Commune' (above) and the 'Parcelles' (below) tables
SELECT
'Parcelles' AS item_layer,
concat(code, ' - ', proprietaire) AS item_label,
'admins' AS item_filter, -- only users in the admins Lizmap group will be able to search among the 'Parcelles'
'cadastre,urban' AS item_project, -- the Parcelles will be available in search only for the cadastre.qgs and urban.qgs QGIS projects
geom
FROM cadastre.parcelle_info
;
Optimisation¶
- Vous devriez utiliser une table, ou une vue matérialisée sur laquelle vous pouvez ajouter des index pour accélérer les requêtes de recherche.
- Nous vous conseillons vivement d’ajouter un index trigramme sur le champ non accentué
item_label
, pour accélérer la requête de recherche :
-- Create the index on the unaccentuated item_label column:
DROP INDEX IF EXISTS lizmap_search_idx;
CREATE INDEX lizmap_search_idx ON lizmap_search USING GIN (f_unaccent(item_label) gin_trgm_ops);
-- You can refresh the materialized view at any time (for example in a cron job) with:
REFRESH MATERIALIZED VIEW lizmap_search;
Avertissement
At present, Lizmap PostgreSQL search cannot use 3D geometries, or geometries with Z or M values.
You have to use the ST_Force2D(geom)
function to convert geometries into 2D geometries.
Configurer l’accès¶
Une fois que cette table (ou vue ou vue matérialisée) est créée dans votre base de données, vous devez vérifier que Lizmap a un accès en lecture dessus.
If your Lizmap instance uses PostgreSQL to store the users, groups and rights, a connection profile already exists for your database. Then you can just add the lizmap_search
relation inside this database (in the public
schema).
If not, or if you need to put the search data in another database (or connect with another PostgreSQL user), you need to add a new database connection profile in Lizmap configuration file lizmap/var/config/profiles.ini.php
.
The new profile is a new jdb prefixed section, called jdb:search. For example, add the following section (please replace the DATABASE_
variables by the correct values):
[jdb:search]
driver="pgsql"
database=DATABASE_NAME
host=DATABASE_HOST
user=DATABASE_USER
password=DATABASE_PASSWORD
; search_path=DATABASE_SCHEMA_WITH_LIZMAP_SEARCH,public
You don’t need to configure the locate by layer tool. The link with Lizmap Web Client is done automatically if you can run the query below successfully in PgAdmin using the same credentials as the Lizmap application. You mustn’t specify the schema where the lizmap_search table is located. It must work without specifying the schema.
SELECT * FROM lizmap_search LIMIT 1;
Vous pouvez maintenant utiliser la barre de recherche par défaut dans Lizmap qui se trouve dans le coin supérieur droit.

Plugin QuickFinder¶
Avertissement
Ceci est pour QGIS 2 seulement. Ce plugin n’est pas disponible sur QGIS 3.
Le but de ce plugin est de fournir une recherche rapide au sein d’un grand volumes de données, en réalisant des recherches dans un fichier qtfs généré par QGIS Desktop.
Pré-requis¶
- Vous devez avoir installé a minima la version « 7.x » de « PHP » sur votre serveur Lizmap.
Configuration¶
Depuis QGIS :
- installer le plugin
QuickFinder
, pour QGIS 2 seulement - choose a layer(s), define the fields to search among, pick the geometry storage format (WKT or Extent) and store Full Text Searchs (FTS) vector into a file database (.qfts). The filename must be identical to the QGIS project filename. Ex:
myproject.qfts
for a QGIS project stored asmyproject.qgs
.
Avertissement
Seuls les formats WKT ou Extent pour le stockage des géométries fonctionne, car le format binaire (WKB) ne peut pas être décodé par LWC.
Dans Lizmap Web Client:
- placez le fichier de base de données à côté du projet QGIS, utilisez l’outil de recherche (champs de saisie) et effectuez un zoom sur la fonction choisie.