SQL:如何在 PostGIS table 的线形文件中获取给定坐标的 source/target?

SQL: How to get source/target of given coordinates in a line shapefile in PostGIS table?

我在 PostGIS 中加载了一个线形文件,并在 pgRouting 中使用 pgr_createTopology 通过添加源和目标来制作 table routable(分配线源编号的 2 个端节点和目标数量)。下图是table的一部分:

现在我有一些坐标的端节点,它们属于table中的线,我想知道有多少个sources/targets分别对应这些坐标。

比如上面的table,假设259463.392, 2737830.062line id=1line id=2的端点之一,那么这个坐标有source/target=175

我是 SQL 的新手,尝试了一些查询但出现错误:

SELECT ST_AsText(geom) from source;
   FROM public.tc_line15_split;

错误:

ERROR:  syntax error at or near "FROM"
LINE 2:        FROM public.tc_line15_split;
               ^
********** Error **********

ERROR: syntax error at or near "FROM"
SQL state: 42601
Character: 45

更新#1

我想如果列source/target包含节点坐标信息,我就知道我想要什么,但似乎不是,它们只是包含数字的列。

我得到的顶点 table 如下:

我使用以下查询获取了下面的 table:

select source, target, st_astext(geom) as geom from public.tc_line15_split;

而且我还在寻找是否可以通过上面的 2 tables 满足我的需求。

所以我尝试了下面的查询并在给定坐标附近得到了 2 条线:

select id from tc_line15_split 
where st_dwithin(geom, st_setsrid(st_makepoint(259463.392, 2737830.062), 3826), 0.1);

后来我从第一个图的table中发现坐标分别是id 170/51的source/target=54,但是还是效率低

我想知道有没有办法找到相同的 source/target 数字,在本例中 line id=51line id=170 都包含,在我发现给定坐标位于这两行?


更新#2

基于顶点table,我使用以下查询获取给定坐标的相应源编号,也就是点id:

select id from tc_line15_split_vertices_pgr 
where st_dwithin(the_geom, st_setsrid(st_makepoint(259463.392, 2737830.062), 3826), 0.1);

列出table:

select source, target, st_astext(geom) as geom from public.tc_line15_split;

你还应该有一个顶点 table,其中包含像 id 和 geom 这样的列,如果你想找到离某个位置最近的节点,你可以使用类似的东西:

select id from vertices where st_dwithin(geom, st_setsrid(st_makepoint(x,y), 3826), tol);

其中 x,y 是您的坐标,tol 是搜索半径。

如果您想要实现的是计算出有多少网络边共享一个公共节点,那么实现此目的的最简单方法是使用由 [=19= 生成的顶点 table ].根据文档,它生成一个 table 与你的边 table 同名,但附加了 _vertices_pgr

如果您然后 运行 pgr_analyzeGraph() 方法,它将使用每个顶点的统计数据填充 my_table_vertices_pgr table 内的空列。其中一项统计数据是 cnt 列,它显示特定顶点被任何相邻边共享的次数。

编辑:

关于您问题的其他一些方面:

您问题中的第一个查询 returns 由于语法错误,这应该有效:

SELECT ST_AsText(geom) FROM public.tc_line15_split;

关于更新 #1 -> 节点坐标未显式存储在顶点 table 中,但您可以使用此查询为特定节点 ID(例如 15)检索它们:

SELECT ST_AsText(ST_Centroid(the_geom))
    FROM tc_line15_split_vertices_pgr
    WHERE id = 15

或者,如果您只需要 X 和 Y:

SELECT ST_X(the_geom), ST_Y(the_geom)
    FROM tc_line15_split_vertices_pgr
    WHERE id = 15