使用最近邻方法在PostGIS中绘制一条线

时间:2009-09-24 19:58:07

标签: sql postgresql postgis nearest-neighbor

这是我发送到PostGIS邮件列表的电子邮件的交叉帖子

到目前为止,我努力在一个点和它的预测之间创建一条线 在一条线上的位置已经很长但我几乎就在那里。截至昨天,和 在包括任何最近邻居分析之前,我得到的结果显示在 这张图片:

QGis screenshot

如您所见,粉红色中的每个点都连接到所有投影点,而我只想将每个粉红色x连接到各自的投影。

在IRC上,我建议使用BostonGIS's nearest neighbor method。我向PostgreSQL输入了这个函数并尝试了它,如下所述。我假设我的错误是由于错误的参数类型。我玩了一下,把一些列的类型更改为varchar,但我仍然无法让它工作。

关于我做错的任何想法?有关如何修复它的任何建议吗?

代码:

-- this sql script creates a line table that connects points 

-- convert multi lines into lines

CREATE TABLE exploded_roads AS
SELECT the_geom
FROM (
    SELECT ST_GeometryN(
    the_geom,
    generate_series(1, ST_NumGeometries(the_geom)))
    AS the_geom 
    FROM "StreetCenterLines"
)
AS foo;


-- Create line table that'll connect the centroids to the projected points on exploded lines
CREATE TABLE lines_from_centroids_to_roads (
    the_geom    geometry,
    edge_id SERIAL
);

-- Populate Table
INSERT INTO lines_from_centroids_to_roads ( the_geom )
SELECT
    ST_MakeLine(
        centroids.the_geom,
        (pgis_fn_nn(centroids.the_geom, 1000000, 1,1000, 'exploded_roads' ,'true', 'gid',
            ST_Line_Interpolate_Point(
                exploded_roads.the_geom,
                ST_Line_Locate_Point(
                    exploded_roads.the_geom,
                    centroids.the_geom
                )
            )
        )).*
    )
FROM exploded_roads, fred_city_o6_da_centroids centroids;

DROP TABLE exploded_roads;

错误

NOTICE:  CREATE TABLE will create implicit sequence "lines_from_centroids_to_roads_edge_id_seq" for serial column "lines_from_centroids_to_roads.edge_id"

ERROR:  function pgis_fn_nn(geometry, integer, integer, integer, unknown, unknown, unknown, geometry) does not exist
LINE 28:   (pgis_fn_nn(centroids.the_geom, 1000000, 1,1000, 'exploded...
            ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.


********** Error **********

ERROR: function pgis_fn_nn(geometry, integer, integer, integer, unknown, unknown, unknown, geometry) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 584

2 个答案:

答案 0 :(得分:1)

问题是我认为该函数需要second argument (distguess) to be a double precision而不是整数。尝试1000000.0或尝试强制转换为浮动...

答案 1 :(得分:1)

事实证明我毕竟不需要使用最近的邻居。我分配了一个与我通过

连接线的质心相同的id
-- this sql script creates a line table that connects points 

-- delete existing tables if they exist
DROP TABLE exploded_roads;
DROP TABLE projected_points;
DROP TABLE lines_from_centroids_to_roads;


-- convert multi lines into lines
CREATE TABLE exploded_roads (
    the_geom    geometry,
    edge_id     serial
);

-- insert the linestring that don't need to be converted
INSERT INTO exploded_roads
SELECT the_geom
FROM "StreetCenterLines"
WHERE st_geometrytype(the_geom) = 'ST_LineString';


INSERT INTO exploded_roads
SELECT the_geom
FROM (
    SELECT ST_GeometryN(
    the_geom,
    generate_series(1, ST_NumGeometries(the_geom)))
    AS the_geom 
    FROM "StreetCenterLines"
)
AS foo;





-- create projected points table with ids matching centroid table
CREATE TABLE projected_points (
    the_geom    geometry,
    pid     serial,
    dauid       int
);


-- Populate Table
INSERT INTO projected_points(the_geom, dauid)
SELECT DISTINCT ON ("DAUID")
    ( 
        ST_Line_Interpolate_Point(
            exploded_roads.the_geom,
            ST_Line_Locate_Point(
                exploded_roads.the_geom,
                centroids.the_geom
            )
        )
    ),
    (centroids."DAUID"::int)

FROM exploded_roads, fred_city_o6_da_centroids centroids;


-- Create Line tables
CREATE TABLE lines_from_centroids_to_roads (
    the_geom    geometry,
    edge_id SERIAL
);


-- Populate Line Table
INSERT INTO lines_from_centroids_to_roads(
SELECT
    ST_MakeLine( centroids.the_geom, projected_points.the_geom )
FROM projected_points, fred_city_o6_da_centroids centroids
WHERE projected_points.dauid = centroids."DAUID"::int
);

-- Delete temp tables
--DROP TABLE exploded_roads;
--DROP TABLE projected_points;