0

我做 pgRouting 并且我需要将整数结果从我的SELECT.

SELECT ST_AsGeoJSON(ST_Transform(way, 4326)) AS geometry
  FROM pgr_dijkstra(
'SELECT osm_id AS id, source, target, st_length(way) as cost FROM planet_osm_roads',
34, 3000, false
  ) as di
  JOIN planet_osm_roads pt
  ON di.edge = pt.osm_id ;

这是可行的,但是如果我想用我的街道的节点号替换例如 dijkstra 函数中的节点 34,请使用以下查询:

SELECT pl.source::integer 
FROM planet_osm_roads pl 
WHERE pl.name LIKE ''street_name'' 
LIMIT 1

并在一起:

SELECT ST_AsGeoJSON(ST_Transform(way, 4326)) AS geometry
  FROM pgr_dijkstra(
'SELECT osm_id AS id, source, target, st_length(way) as cost FROM planet_osm_roads',
'SELECT pl.source::integer FROM planet_osm_roads pl WHERE pl.name LIKE ''street_name'' LIMIT 1',
 3000, false
  ) as di
  JOIN planet_osm_roads pt
  ON di.edge = pt.osm_id ;

它将失败并出现错误:

ERROR:  function pgr_dijkstra(unknown, unknown, integer, boolean) is not unique
LINE 93:   FROM pgr_dijkstra(
                ^
HINT:  Could not choose a best candidate function. You might need to add explicit type casts.

我认为,这是因为我的选择查询返回带有行 ID 和列名的 sql 结果。但也许还有另一个问题。

如何仅将其输出为单个整数?

在此处输入图像描述

4

2 回答 2

1

不要将查询作为字符串传递:

SELECT ST_AsGeoJSON(ST_Transform(way, 4326)) AS geometry
FROM pgr_dijkstra(
  'SELECT osm_id AS id, source, target, st_length(way) as cost FROM planet_osm_roads',
  (SELECT pl.source::integer FROM planet_osm_roads pl WHERE pl.name LIKE 'street_name' LIMIT 1),
   3000, false
) as di
  JOIN planet_osm_roads pt
  ON di.edge = pt.osm_id;

或者使用派生表

SELECT ST_AsGeoJSON(ST_Transform(way, 4326)) AS geometry
FROM (
  SELECT pl.source::integer as source
  FROM planet_osm_roads pl 
  WHERE pl.name 
  LIKE 'street_name' LIMIT 1
) pl 
  join lateral pgr_dijkstra(
    'SELECT osm_id AS id, source, target, st_length(way) as cost FROM planet_osm_roads',
    pl.source,
    3000, false
  ) as di on true
  JOIN planet_osm_roads pt ON di.edge = pt.osm_id;
于 2018-10-18T08:43:50.493 回答
1

顶点是整数参数,您试图将文本放在那里。只需将 ' 更改为 ()

SELECT ST_AsGeoJSON(ST_Transform(way, 4326)) AS geometry
  FROM pgr_dijkstra('SELECT osm_id AS id, source, target, st_length(way) as cost FROM planet_osm_roads',
(SELECT pl.source::integer FROM planet_osm_roads pl WHERE pl.name LIKE ''street_name'' LIMIT 1),
 3000, false
  ) as di
  JOIN planet_osm_roads pt
  ON di.edge = pt.osm_id ;

这是一种方法,另一种方法是使用街道名称作为参数编写函数,并将您的 geojson 作为输出。

于 2018-10-18T08:45:04.267 回答