ST_Transform

返回一个新的 GEOMETRY,其坐标转换为 srid 实参使用的空间参考系统标识符 (SRID)。

此函数支持以下转换:

  • EPSG 4326 (WGS84) 到 EPSG 3857 (Web Mercator)

  • EPSG 3857 (Web Mercator) 到 EPSG 4326 (WGS84)

对于 EPSG 4326 (WGS84),除非坐标处于以下范围内,否则转换会失败:

  • 经度限制:-572 到 +572
  • 纬度限制:-89.9999999 到 +89.9999999

行为类型

不可变

语法

ST_Transform( g1, srid )

参数

g1
GEOMETRY 类型的空间对象。
srid
要将空间对象转换到的空间参考系统标识符 (SRID),类型为 INTEGER。

返回

GEOMETRY

支持的数据类型

示例

以下示例显示了如何将数据从 Web Mercator (3857) 转换为 WGS84 (4326):

=> SELECT ST_AsText(ST_Transform(STV_GeometryPoint(7910240.56433, 5215074.23966, 3857), 4326));
        ST_AsText
-------------------------
 POINT (71.0589 42.3601)
(1 row)

以下示例显示了如何将表中的线串数据从 WGS84 (4326) 转换为 Web Mercator (3857):

=> CREATE TABLE transform_line_example (g GEOMETRY);
CREATE TABLE
=> COPY transform_line_example (gx FILLER LONG VARCHAR, g AS ST_GeomFromText(gx, 4326)) FROM STDIN;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> LINESTRING(0 0, 1 1, 2 2, 3 4)
>> \.
=> SELECT ST_AsText(ST_Transform(g, 3857)) FROM transform_line_example;
                                                        ST_AsText
-------------------------------------------------------------------------------------------------------------------------
 LINESTRING (0 -7.08115455161e-10, 111319.490793 111325.142866, 222638.981587 222684.208506, 333958.47238 445640.109656)
(1 row)

以下示例显示了如何将表中的点数据从 WGS84 (4326) 转换为 Web Mercator (3857):

=> CREATE TABLE transform_example (x FLOAT, y FLOAT, srid INT);
CREATE TABLE
=> COPY transform_example FROM STDIN;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> 42.3601|71.0589|4326
>> 122.4194|37.7749|4326
>> 94.5786|39.0997|4326
>> \.
=> SELECT ST_AsText(ST_Transform(STV_GeometryPoint(x, y, srid), 3857)) FROM transform_example;
              ST_AsText
-------------------------------------
 POINT (4715504.76195 11422441.5961)
 POINT (13627665.2712 4547675.35434)
 POINT (10528441.5919 4735962.8206)
(3 rows)