将Postgres几何格式转换为WKT

时间:2018-04-18 17:00:43

标签: postgresql postgis

我有一个Postgres表,它在其中一个列中以特定格式存储多边形几何,如下所示 -

0103000020E61000000100000004000000B8627F336B1554405DD602FFA2733A40B8627FA7601554403851F8EBC7723A40B8627FC38F15544036D539E90B733A40B8627F336B1554405DD602FFA2733A40

我知道如何使用ST_AsText将此单个值转换为WKT,这将为我提供POLYGON((Lat Long))。但我想将整列转换为WKT格式。

如何实现这一目标?

谢谢!

1 个答案:

答案 0 :(得分:5)

你试过这个吗?

SELECT ST_AsText(your_geom_column) FROM your_table

示例:

CREATE TEMPORARY TABLE tmp (geom GEOMETRY);
INSERT INTO tmp VALUES (ST_GeomFromText('POINT (1 2)')),
                       (ST_GeomFromText('POINT (2 4)'));

几何图形为EWKT

db=# SELECT geom FROM tmp;
                    geom                    
--------------------------------------------
 0101000000000000000000F03F0000000000000040
 010100000000000000000000400000000000001040

几何图形为 WKT

db=# SELECT ST_AsText(geom) FROM tmp;
 st_astext  
------------
 POINT(1 2)
 POINT(2 4)

如果您喜欢GeoJSON ..

db=# SELECT ST_AsGeoJSON(geom) FROM tmp;    
             st_asgeojson             
--------------------------------------
 {"type":"Point","coordinates":[1,2]}
 {"type":"Point","coordinates":[2,4]}

..甚至GML

db=# SELECT ST_AsGML(geom) FROM tmp;    
                           st_asgml                            
---------------------------------------------------------------
 <gml:Point><gml:coordinates>1,2</gml:coordinates></gml:Point>
 <gml:Point><gml:coordinates>2,4</gml:coordinates></gml:Point>