Skip to content

Instantly share code, notes, and snippets.

@ismailsunni
Created December 6, 2021 16:31
Show Gist options
  • Save ismailsunni/f51b4e0fbbe89dddea55ca3f0dcd039a to your computer and use it in GitHub Desktop.
Save ismailsunni/f51b4e0fbbe89dddea55ca3f0dcd039a to your computer and use it in GitHub Desktop.
Raw params: [Object: null prototype] {
minimiserCout: 'DISTANCE',
locations: '[{"x": 2.2688699, "y": 48.8155122}, {"x": -2.964521, "y": 48.674905}]'
}
location
{ x: 2.2688699, y: 48.8155122 }
location
{ x: -2.964521, y: 48.674905 }
Cleaned parameters
{
minimize_cost: 'temps',
intervention_mode: 'standard',
transport_mean: 'vehicules_legers',
locations: [ { x: 2.2688699, y: 48.8155122 }, { x: -2.964521, y: 48.674905 } ],
contrainte_ids: [],
viabilite: []
}
Executing (305bd746-3bbd-422f-9df0-4c2564a5b25d): START TRANSACTION;
Executing (305bd746-3bbd-422f-9df0-4c2564a5b25d): CREATE TEMPORARY TABLE temp_edge_proj_point_1 ON COMMIT DROP AS WITH
-- Selection et projection du point
point AS (
SELECT ST_SetSRID(ST_MakePoint(2.2688699, 48.8155122), 4326) as geom
),
-- Récupération du tronçon le plus proche du point de départ
closest_edge AS (
SELECT source AS source, target AS target, cost_time_s1 AS cost, reverse_cost_time_s1 AS reverse_cost, the_geom AS the_geom
FROM reseau_routier, point
WHERE reseau_routier.the_geom && ST_Buffer(point.geom, 0.5)
ORDER BY ST_Distance(reseau_routier."the_geom", point.geom) ASC
LIMIT 1
),
-- Calcul du point du tronçon le plus proche du point de départ
closest_point AS (
SELECT ST_ClosestPoint(closest_edge.the_geom, point.geom) as geom
FROM closest_edge, point
),
-- Division du tronçon en deux parties : avant et après le point le plus proche du point de départ
dump AS (
SELECT(ST_Dump(ST_Split(ST_Snap(closest_edge.the_geom, closest_point.geom, 0.00001), closest_point.geom))).geom as geom
FROM closest_edge, closest_point
),
-- Calcul des coûts respectifs (cost et reverse_cost) de chaque demi-tronçon obtenu en fonction de leur longueur
split AS (
SELECT
CASE WHEN cost > 0 THEN cost * (ST_Length(dump.geom) / ST_Length(closest_edge.the_geom)) ELSE -1 END as new_cost_length,
CASE WHEN reverse_cost > 0 THEN reverse_cost * (ST_Length(dump.geom) / ST_Length(closest_edge.the_geom)) ELSE -1 END as new_reverse_cost_length,
dump.geom
FROM dump, closest_edge
),
-- Construction d'un tronçon (edge) exploitable à partir du demi-tronçon de départ
start_line AS (
SELECT 1*10 as gid, -1 as source, target, cost, reverse_cost, split.geom as the_geom
FROM closest_edge, closest_point
JOIN split ON ST_StartPoint(split.geom) = closest_point.geom
),
-- Construction d'un tronçon (edge) exploitable à partir du demi-tronçon d'arrivée
end_line AS (
SELECT 1*100 as gid, source, -1 as target, cost, reverse_cost, split.geom as the_geom
FROM closest_edge, closest_point
JOIN split ON ST_EndPoint(split.geom) = closest_point.geom
)
SELECT * FROM start_line
UNION
SELECT * FROM end_line;
Executing (305bd746-3bbd-422f-9df0-4c2564a5b25d): CREATE TEMPORARY TABLE temp_edge_proj_point_2 ON COMMIT DROP AS WITH
-- Selection et projection du point
point AS (
SELECT ST_SetSRID(ST_MakePoint(-2.964521, 48.674905), 4326) as geom
),
-- Récupération du tronçon le plus proche du point de départ
closest_edge AS (
SELECT source AS source, target AS target, cost_time_s1 AS cost, reverse_cost_time_s1 AS reverse_cost, the_geom AS the_geom
FROM reseau_routier, point
WHERE reseau_routier.the_geom && ST_Buffer(point.geom, 0.5)
ORDER BY ST_Distance(reseau_routier."the_geom", point.geom) ASC
LIMIT 1
),
-- Calcul du point du tronçon le plus proche du point de départ
closest_point AS (
SELECT ST_ClosestPoint(closest_edge.the_geom, point.geom) as geom
FROM closest_edge, point
),
-- Division du tronçon en deux parties : avant et après le point le plus proche du point de départ
dump AS (
SELECT(ST_Dump(ST_Split(ST_Snap(closest_edge.the_geom, closest_point.geom, 0.00001), closest_point.geom))).geom as geom
FROM closest_edge, closest_point
),
-- Calcul des coûts respectifs (cost et reverse_cost) de chaque demi-tronçon obtenu en fonction de leur longueur
split AS (
SELECT
CASE WHEN cost > 0 THEN cost * (ST_Length(dump.geom) / ST_Length(closest_edge.the_geom)) ELSE -1 END as new_cost_length,
CASE WHEN reverse_cost > 0 THEN reverse_cost * (ST_Length(dump.geom) / ST_Length(closest_edge.the_geom)) ELSE -1 END as new_reverse_cost_length,
dump.geom
FROM dump, closest_edge
),
-- Construction d'un tronçon (edge) exploitable à partir du demi-tronçon de départ
start_line AS (
SELECT 2*10 as gid, -2 as source, target, cost, reverse_cost, split.geom as the_geom
FROM closest_edge, closest_point
JOIN split ON ST_StartPoint(split.geom) = closest_point.geom
),
-- Construction d'un tronçon (edge) exploitable à partir du demi-tronçon d'arrivée
end_line AS (
SELECT 2*100 as gid, source, -2 as target, cost, reverse_cost, split.geom as the_geom
FROM closest_edge, closest_point
JOIN split ON ST_EndPoint(split.geom) = closest_point.geom
)
SELECT * FROM start_line
UNION
SELECT * FROM end_line;
Executing (305bd746-3bbd-422f-9df0-4c2564a5b25d): CREATE TEMPORARY TABLE temp_edge_subset ON COMMIT DROP AS
SELECT gid,
source AS source,
target AS target,
cost_time_s1 AS cost,
reverse_cost_time_s1 AS reverse_cost,
the_geom AS the_geom
FROM reseau_routier
WHERE
(
func_class IN ('1', '2', '3')
AND
reseau_routier.the_geom && ST_Expand(ST_Collect(ARRAY[ST_MakePoint(-2.964521, 48.674905), ST_MakePoint(2.2688699, 48.8155122)]), 0.2)
)
OR
(
func_class IN ('4', '5')
AND
(reseau_routier.the_geom && ST_Expand(ST_MakePoint(-2.964521, 48.674905), 0.2) OR reseau_routier.the_geom && ST_Expand(ST_MakePoint(2.2688699, 48.8155122), 0.2)
)
)
Executing (305bd746-3bbd-422f-9df0-4c2564a5b25d): CREATE TEMPORARY TABLE temp_edge ON COMMIT DROP AS SELECT * FROM temp_edge_proj_point_1 UNION SELECT * FROM temp_edge_proj_point_2 UNION SELECT * FROM temp_edge_subset;
Executing (305bd746-3bbd-422f-9df0-4c2564a5b25d): CREATE TEMPORARY TABLE temp_restriction ON COMMIT DROP AS
SELECT * FROM restrictions WHERE trsp_mean LIKE 'ar_auto'
Executing (305bd746-3bbd-422f-9df0-4c2564a5b25d): CREATE TEMPORARY TABLE temp_route ON COMMIT DROP AS
SELECT
seq,
seq AS _path_seq,
'(' || '-1' || ',' || '-2' || ')' AS path_name,
-1 AS _start_vid,
-2 AS _end_vid,
id1 AS _node,
id2 AS _edge,
cost AS _cost,
lead(id2, 1) over(order by seq) AS next_edge_id
FROM pgr_trsp(
'
SELECT "gid" as id,
source,
target,
cost,
reverse_cost
FROM temp_edge',
-1,
-2,
true,
true,
'SELECT * FROM restrictions WHERE trsp_mean LIKE ''ar_auto''');
Executing (305bd746-3bbd-422f-9df0-4c2564a5b25d): WITH
with_geom AS (
SELECT
seq, temp_route.path_name,
CASE
WHEN temp_route._node = "temp_edge"."source"
THEN "temp_edge"."the_geom"
ELSE ST_Reverse("temp_edge"."the_geom")
END AS path_geom
FROM "temp_edge" JOIN temp_route
ON "temp_edge"."gid" = temp_route._edge
),
one_geom AS (
SELECT path_name, ST_LineMerge(ST_Union(path_geom)) AS path_geom
FROM with_geom
GROUP BY path_name
ORDER BY path_name
),
aggregates AS (
SELECT
path_name, _start_vid, _end_vid,
SUM(_cost) AS agg_cost,
array_agg(_node ORDER BY _path_seq) AS _nodes,
array_agg(_edge ORDER BY _path_seq) AS _edges
FROM temp_route
GROUP BY path_name, _start_vid, _end_vid
ORDER BY _start_vid, _end_vid
)
SELECT row_number() over() as seq,
path_name, _start_vid, _end_vid, agg_cost, _nodes, _edges,
path_geom AS path_geom FROM aggregates JOIN one_geom
USING (path_name)
Executing (305bd746-3bbd-422f-9df0-4c2564a5b25d): SELECT
temp_route.seq,
temp_route._edge,
temp_route.next_edge_id,
temp_route._cost,
next_turn_direction || ' SUR ' || next_edge_name AS next_edge_instruction
FROM temp_route, navigation
WHERE
temp_route._edge = navigation.gid_from
AND (temp_route.next_edge_id = navigation.gid_to OR temp_route._edge = -1);
Executing (305bd746-3bbd-422f-9df0-4c2564a5b25d): COMMIT;
shortest route {
name: 'Routing',
type: 'FeatureCollection',
features: [ { type: 'Feature', geometry: [Object], properties: [Object] } ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment