如何计算 PostGIS 中线串的最近点?
How to calculate nearest point to a linestring in PostGIS?
我正在按照这个 question 来计算最近的道路(线串)的 POI。我能够计算线串中的最近点,但无法找到从 POI 到线串上最近点(顶点)的距离。
这是我查找最近兴趣点的代码。
CREATE TABLE road(id serial PRIMARY KEY, the_geog geography(LINESTRING,4326) );
CREATE TABLE poi(gid serial PRIMARY KEY, name varchar, city varchar, the_geog geography(POINT,4326) )
值为:
INSERT INTO road (id, the_geog) VALUES (123, ST_GeographyFromText('SRID=4326;LINESTRING(85.280194 23.296728,85.281572 23.297479)') );
计算最近点:
SELECT
poi.name,
poi.city,
ST_AsTEXT(poi.the_geog),
ST_Distance(road.the_geog, poi.the_geog)/1000.0 AS distance_km,
ST_AsTEXT(road.the_geog::geometry)
FROM road, poi
WHERE
road.id = 123
AND ST_DWithin(road.the_geog, poi.the_geog, 10000.0)
ORDER BY
ST_LineLocatePoint(road.the_geog::geometry, poi.the_geog::geometry),
ST_Distance(road.the_geog, poi.the_geog)
;
如果线串表示为:[85.280194 23.296728,85.281572 23.297479]
,我想要这样的结果:
poi vertex distance_km
85.280194 23.296728 85.280001 23.299876 3
85.289673 23.291987 85.281572 23.297479 5
使用ST_DumpPoints()提取直线的顶点:
SELECT id,the_geog, (ST_dumppoints(road.the_geog::GEOMETRY)).geom AS vertex FROM road;
您必须将 GEOGRAPHY 转换为 GEOMETRY,否则 st_dumppoints 不起作用。
您的整个查询可能如下所示:
SELECT road.id AS road_id,
ST_Astext(poi.the_geog) AS poi,
ST_astext(road.vertex) AS vertex,
ST_AsText(road.the_geog) AS line,
ST_Distance(vertex, poi.the_geog) AS distance
FROM (SELECT id,the_geog, (ST_dumppoints(road.the_geog::GEOMETRY)).geom AS vertex
FROM road
) as road,
poi
WHERE road.id = 123
ORDER by ST_Distance(vertex, poi.the_geog);
我正在按照这个 question 来计算最近的道路(线串)的 POI。我能够计算线串中的最近点,但无法找到从 POI 到线串上最近点(顶点)的距离。
这是我查找最近兴趣点的代码。
CREATE TABLE road(id serial PRIMARY KEY, the_geog geography(LINESTRING,4326) );
CREATE TABLE poi(gid serial PRIMARY KEY, name varchar, city varchar, the_geog geography(POINT,4326) )
值为:
INSERT INTO road (id, the_geog) VALUES (123, ST_GeographyFromText('SRID=4326;LINESTRING(85.280194 23.296728,85.281572 23.297479)') );
计算最近点:
SELECT
poi.name,
poi.city,
ST_AsTEXT(poi.the_geog),
ST_Distance(road.the_geog, poi.the_geog)/1000.0 AS distance_km,
ST_AsTEXT(road.the_geog::geometry)
FROM road, poi
WHERE
road.id = 123
AND ST_DWithin(road.the_geog, poi.the_geog, 10000.0)
ORDER BY
ST_LineLocatePoint(road.the_geog::geometry, poi.the_geog::geometry),
ST_Distance(road.the_geog, poi.the_geog)
;
如果线串表示为:[85.280194 23.296728,85.281572 23.297479]
,我想要这样的结果:
poi vertex distance_km
85.280194 23.296728 85.280001 23.299876 3
85.289673 23.291987 85.281572 23.297479 5
使用ST_DumpPoints()提取直线的顶点:
SELECT id,the_geog, (ST_dumppoints(road.the_geog::GEOMETRY)).geom AS vertex FROM road;
您必须将 GEOGRAPHY 转换为 GEOMETRY,否则 st_dumppoints 不起作用。
您的整个查询可能如下所示:
SELECT road.id AS road_id,
ST_Astext(poi.the_geog) AS poi,
ST_astext(road.vertex) AS vertex,
ST_AsText(road.the_geog) AS line,
ST_Distance(vertex, poi.the_geog) AS distance
FROM (SELECT id,the_geog, (ST_dumppoints(road.the_geog::GEOMETRY)).geom AS vertex
FROM road
) as road,
poi
WHERE road.id = 123
ORDER by ST_Distance(vertex, poi.the_geog);