In the example above, the -append option allows to import several SHP inside the same table if needed.
This command will add a gid column as the primary key.
Be careful to delete the duplicate geometries, for example when you import data from multiple
source files with overlapping data (such as in the French IGN BDTOPO "département" extracts).
You can use this kind of query to remove the duplicates based on the primary key gid and the IGN BDTOPO key id:
BEGIN;-- Copy data from the route source table-- For example: bdtopo.troncon_de_route-- to create the temporary edges-- with added start and end point point geometries.DROPTABLEIFEXISTStemp_edges;CREATETABLEtemp_edgesASWITHsourceAS(SELECT-- Stores the original data inside a JSON fieldto_jsonb(t.*)ASraw_data,-- Move the geometries nodes to a gridST_SnapToGrid(ST_geometryN(geom,1),0.1)ASgeom-- bdtopo.troncon_de_route is the source road tableFROMbdtopo.troncon_de_routeAStWHERETRUE)SELECTsource.*,-- get the start pointST_StartPoint(geom)ASstart_point,-- get the end pointST_EndPoint(geom)ASend_pointFROMsource;-- Create the needed indexesCREATEINDEXONtemp_edgesUSINGGIST(geom);CREATEINDEXONtemp_edgesUSINGGIST(start_point);CREATEINDEXONtemp_edgesUSINGGIST(end_point);-- Create the temporary nodes from the start and end pointsDROPTABLEIFEXISTStemp_nodes;CREATETABLEtemp_nodesASWITHunion_start_endAS(SELECTraw_data->>'fid'ASstart_of,NULLASend_of,start_pointASgeomFROMtemp_edgesUNIONALLSELECTNULLASstart_of,raw_data->>'fid'ASend_of,end_pointASgeomFROMtemp_edges),distinct_nodesAS(SELECTjson_agg(DISTINCTstart_of)FILTER(WHEREstart_ofISNOTNULL)ASstart_of,json_agg(DISTINCTend_of)FILTER(WHEREend_ofISNOTNULL)ASend_of,geomFROMunion_start_endGROUPBYgeom)SELECT*FROMdistinct_nodes;CREATEINDEXONtemp_nodesUSINGGIST(geom);-- Insert them in the pgrouting.nodes tableTRUNCATEpgrouting.nodesRESTARTIDENTITYCASCADE;INSERTINTOpgrouting.nodes(geom)SELECTgeomFROMtemp_nodes;-- Insert the temporary edges into the pgrouting.edges table-- with additional information about nodes, costs, etc.TRUNCATEpgrouting.edgesRESTARTIDENTITYCASCADE;INSERTINTOpgrouting.edges(label,length,source,target,cost,reverse_cost,source_data,geom)SELECTDISTINCT-- label of the edgee.raw_data->>'nom_voie_ban_gauche'ASlabel,-- lengthST_length(e.geom)AS"length",-- start and end nodes idns.id,ne.id,-- cost based on the lengthCASEWHENe.raw_data->>'sens_de_circulation'in('Sans objet','Double sens','Sens direct')THENST_length(e.geom)ELSE-1ENDAScost,-- reverse cost based on the lengthCASEWHENe.raw_data->>'sens_de_circulation'in('Sans objet','Double sens','Sens inverse')THENST_length(e.geom)ELSE-1ENDASreverse_cost,-- Keep some useful columns from the source tablejsonb_build_object('fid',e.raw_data->'fid','nature',e.raw_data->'nature','importance',e.raw_data->'importance','etat_de_l_objet',e.raw_data->'etat_de_l_objet','largeur_de_chaussee',e.raw_data->'largeur_de_chaussee','prive',e.raw_data->'prive','sens_de_circulation',e.raw_data->'sens_de_circulation','vitesse_moyenne_vl',e.raw_data->'vitesse_moyenne_vl','acces_vehicule_leger',e.raw_data->'acces_vehicule_leger')ASsource_data,-- geometry. Needed for the astar route enginee.geomFROMtemp_edgesASeLEFTJOINpgrouting.nodesASns-- = is faster than ST_EqualsONns.geom=e.start_pointLEFTJOINpgrouting.nodesASneONne.geom=e.end_point;-- Drop the temporary tablesDROPTABLEIFEXISTStemp_nodes;DROPTABLEIFEXISTStemp_edges;COMMIT;-- VACUUM and analyseVACUUMANALYSEpgrouting.nodes;VACUUMANALYSEpgrouting.edges;
If you want to create your project or use another existing project to use this module:
Add the edges and nodes layers with these exact names. You can rename them in the plugin.
The QGIS project must be called pgrouting.qgs or contain a QGIS project variable
called lizmap_pgrouting_enabled with the value yes
You must then create a configuration for Lizmap with the Lizmap plugin. No specific
configuration is needed for the module. You can configure like you want or just click on the
apply button.