postgresql 将Postgres几何格式转换为WKT

elcex8rz  于 2022-12-23  发布在  PostgreSQL
关注(0)|答案(1)|浏览(368)

我有一个Postgres表,它在其中一列中以特定格式存储多边形几何体,类似于以下内容-

0103000020E61000000100000004000000B8627F336B1554405DD602FFA2733A40B8627FA7601554403851F8EBC7723A40B8627FC38F15544036D539E90B733A40B8627F336B1554405DD602FFA2733A40

我知道如何使用ST_AsText将这个值转换为WKT,这将给予我POLYGON((Lat Long))。但是我想将整列转换为WKT格式。
如何做到这一点?
谢谢!

sr4lhrrt

sr4lhrrt1#

你试过这个吗?

SELECT ST_AsText(your_geom_column) FROM your_table

在下面的示例中,我将向您展示几种序列化几何图形的方法。下面是两个点编码为4326(WGS 84)的示例数据:

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>

图书管理员可能还想看看ST_AsMARC21(3.3中添加的)

SELECT ST_AsMARC21(geom) FROM tmp;
                                                                                                                                            st_asmarc21
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 <record xmlns="http://www.loc.gov/MARC21/slim"><datafield tag="034" ind1="1" ind2=" "><subfield code="a">a</subfield><subfield code="d">E0010000</subfield><subfield code="e">E0010000</subfield><subfield code="f">N0020000</subfield><subfield code="g">N0020000</subfield></dataf
ield></record>
 <record xmlns="http://www.loc.gov/MARC21/slim"><datafield tag="034" ind1="1" ind2=" "><subfield code="a">a</subfield><subfield code="d">E0020000</subfield><subfield code="e">E0020000</subfield><subfield code="f">N0040000</subfield><subfield code="g">N0040000</subfield></dataf
ield></record>
(2 rows)

这个列表还在继续!在PostGIS文档中还有其他奇特的方法来序列化几何图形。
演示:db<>fiddle

相关问题