I have the following problem: I have a data set of latitude and longitude points, where I want to calculate shortest distances between all of them.
While I found a solution, which works when explicitly specifying the parameters, using a query to get these parameters currently fails. My query looks as follows:
with data as(
SELECT testlonglat.*,f as id
FROM testlonglat, LATERAL get_closest_node( long,lat) f),
sourced as (
SELECT array_agg(id) from data where source = true),
targetd as (
SELECT array_agg(id) from data where source = false)
SELECT *
FROM sourced,
targetd,
LATERAL pgr_dijkstra(
'SELECT id,source,target,length::float8 AS cost FROM us1'::text,
sourced,
targetd,
false)
The first subquery to the variable data get a node id for every lat/long coordinate. The succeeding sourced and targetd just divide this large dataset into source and target nodes and concatenate all rows into an array. The resulting dataset only contains 1 records with an array.
The main query applies pgr_dijkstra to find shortest routes between all source and all destination nodes. The signature of this algorithm is
pgr_dijkstra(text,anyarray,anyarray,boolean)
The stated query however produces records and tries to call the following function
pgr_dijkstra(text,record,record,boolean)
Is there a way, to solve this issue? Explicitly casting the records variables to any array (e.g.,sourced::bigint[]) only throws another exception, stating that this cast is not possible.
Thank you for your help.
CREATE TABLE source AS SELECT array_aggand try changing the data type. But you should show us one working example with a literal array and compare with the one you have in the SELECT`sourced.array_agg, targetd.array_aggas parameters -- you could also use an explicit column alias too, in multiple places (I.e.WITH sourced(sourced) AS (...)orSELECT array_agg(id) AS sourced).