如何使用PostgreSQL中的字段创建多边形?

时间:2017-01-06 17:25:40

标签: postgresql

我没有PostGIS,因此我使用内置函数。 我有以下查询正常工作。 但是,我需要从表中找到这些值

select area(circle( (polygon '(43.5042,-96.8391),(33.508,-112.1254),(33.4912,-111.9237),(33.4912,-111.9237),(33.4912,-111.9237),(38,-97)' ) ))

以下查询返回错误。 有关如何从表中的字段获取多边形的任何帮助吗?

select id, polygon '('||array_to_string(array_agg('('||latdec_src||','||longdec_src||')'),',') ||')'  from access where id='99999' and time >= '2017-01-03' and time < '2017-01-04' group by 1

select id, polygon array_agg('('||latdec_src||','||longdec_src||')')  from access where id='99999' and time >= '2017-01-03' and time < '2017-01-04' group by 1


select id, polygon array_agg(point(latdec_src,longdec_src))  from acesss where id='99999' and time >= '2017-01-03' and time < '2017-01-04' group by 1

1 个答案:

答案 0 :(得分:0)

你可以做类似的事情:

WITH points (lat, lng, sort_order) AS
(
    VALUES 
       (43.5042, -96.8391, 1),
       (33.508, -112.1254, 2),
       (33.4912,-111.9237, 3),
       (33.4912,-111.9237, 4),
       (33.4912,-111.9237, 5),
       (38,     -97,       6)

)
SELECT
    area(circle(polygon( string_agg(one_point, ',') ))) AS area
FROM    (
    SELECT
        '(' || lat || ',' || lng || ')' AS one_point
    FROM
        points
    ORDER BY
        sort_order
    ) AS s0 ;

如果我很了解你的表(你的定义可以包含在你的问题中),那就是

SELECT
    area(circle(polygon( string_agg(one_point, ',') ))) AS area
FROM    (
    SELECT
        '(' || latdec_src || ',' || longdec_src || ')' AS one_point
    FROM
        access 
    WHERE  
        id ='99999' and time >= '2017-01-03' and time < '2017-01-04'
    ORDER BY
        time
    ) AS s0 ;

(如果您只使用经度和纬度值,我真的不知道这是否会给您一个区域;但这是另一个故事。)