PostGIS 从 POLYGON 中提取坐标

PostGIS extract coordinates from POLYGON

我正在尝试从我使用 PostGIS 扩展创建的 PostgreSQL 中的多边形列中提取纬度/经度值,我尝试使用这样的查询获取坐标

查询

ST_AsText(coverage_area) as coverage_area 但是它的输出 returns 它不是我使用的方便形式。

输出

POLYGON((37.9615819622 23.7216281890869,37.9617173039801 23.7193965911865,37.9633413851658 23.717679977417,37.964559422483 23.7147617340087,37.9644240860015 23.7116718292236,37.9615819622 23.7216281890869))

我需要这样的输出:

37.9615819622 23.7216281890869,37.9617173039801 23.7193965911865,37.9633413851658 23.717679977417,37.964559422483 23.7147617340087,37.9644240860015 23.7116718292236, 37.9615819622 23.7216281890869

我还搜索了 PostGIS 文档,我唯一找到的是 ST_AsGeoJSON 也没有帮助我... 还有其他人遇到过这个问题吗?

谢谢。

注意: 我知道我可以创建一个正则表达式规则并去掉括号,但我想避免这种情况并找到一种方法来 return "clean" 对坐标

使用函数st_dumppoints to extract all points of a geometry and the functions ST_x and ST_y提取点的坐标。然后使用 array_agg 和 array_to_string 构建所有坐标为

的行

试试这个查询:

SELECT array_to_string(array_agg, ',') FROM 
(SELECT array_agg( ST_x(geom)||' '||ST_y(geom))  FROM 
    (SELECT (ST_dumppoints(coverage_area)).geom FROM your_table
    ) AS foo_1
) AS foo_2;

也可以使用postgresqlstring functions来简化查询。

SELECT substring(left(St_astext(coverage_area),-2),10) FROM tablename;

这将删除开头的 "POLYGON((" 和结尾的“))”。

修剪是最简单的方法:

select btrim(st_astext(coverage_area), 'POLYGON()') from some_table;

如果您还需要更大的灵活性,包括线串、点和类似物:

select regexp_replace(st_astext(coverage_area), '[A-Z()]', '', 'g')

将 GeoJson 几何体转换为 json 然后提取坐标:

SELECT 
  ST_AsGeoJSON(geom) :: json->'coordinates' AS coordinates
FROM
  your_table;

请参阅 ST_AsGeoJSON docs as well as the JSON functions reference 中 PostgreSQL 的示例部分。