在stackoverflow上看见一篇文章,记录下:❤️原文地址❤️
问题
我有一个Postgres表,它将多边形几何体以特定的格式存储在一列中,类似这样-
0103000020E61000000100000004000000B8627F336B1554405DD602FFA2733A40B8627FA7601554403851F8EBC7723A40B8627FC38F15544036D539E90B733A40B8627F336B1554405DD602FFA2733A40
我知道如何使用 ST_AsText 将这个值转换为WKT,这将得到多边形(Lat Long)。但我想把整列转换成WKT格式。
如何做到这一点?
谢谢!
回答
你试过这个吗?
SELECT ST_AsText(your_geom_column) FROM your_table
在下面的示例中,我将向您展示几种序列化几何图形的方法—两个点编码为4326(WGS84)的数据示例:
CREATE TEMPORARY TABLE tmp (geom GEOMETRY);
INSERT INTO tmp VALUES ('SRID=4326;POINT (1 2)'),
('SRID=4326;POINT (2 4)');
几何图形为 WKB (默认):
SELECT geom FROM tmp;
geom
----------------------------------------------------
0101000020E6100000000000000000F03F0000000000000040
0101000020E610000000000000000000400000000000001040
几何图形如 WKT 和 EWKT (带空间参考系的WKT):
SELECT ST_AsText(geom),ST_AsEWKT(geom) FROM tmp;
st_astext | st_asewkt
------------+----------------------
POINT(1 2) | SRID=4326;POINT(1 2)
POINT(2 4) | SRID=4326;POINT(2 4)
如果你喜欢的话。。
GeoJSON
SELECT ST_AsGeoJSON(geom) FROM tmp;
st_asgeojson
--------------------------------------
{"type":"Point","coordinates":[1,2]}
{"type":"Point","coordinates":[2,4]}
… 甚至
GML
SELECT ST_AsGML(geom) FROM tmp;
st_asgml
-----------------------------------------------------------------------------------
<gml:Point srsName="EPSG:4326"><gml:coordinates>1,2</gml:coordinates></gml:Point>
<gml:Point srsName="EPSG:4326"><gml:coordinates>2,4</gml:coordinates></gml:Point>
… 谷歌地球的爱好者也有他们的乐趣!几何图形如
KML
SELECT ST_AsKML(geom) FROM tmp;
st_askml
-----------------------------------------------
<Point><coordinates>1,2</coordinates></Point>
<Point><coordinates>2,4</coordinates></Point>