背景
最近有一個需求,需要將WGS84轉成火星座標系。個人覺得在代碼中逐個點座標進行轉換,太麻煩,而且效率低。PostGIS的st_transform雖然可以進行座標轉換,但是不支持國內這些座標系。最後在網上找了有人編寫的一個***pg-coordtransform
***庫,可以在火星座標系、百度座標系、WGS84座標系、CGCS2000座標系之間互轉,部署也很簡單。
使用過程
- 部署PostGIS(已有直接下一步)
- 直接把github
)上的sql拿下來運行一下,然後就可以用了
-- 如果轉換後結果爲null,查看geom的srid是否爲4326或者4490
WGS84轉GCJ02
select geoc_wgs84togcj02(geom) from test_table
GCJ02轉WGS84
select geoc_gcj02towgs84(geom) from test_table
WGS84轉BD09
select geoc_wgs84tobd09(geom) from test_table
BD09轉WGS84
select geoc_bd09towgs84(geom) from test_table
CGCS2000轉GCJ02
select geoc_cgcs2000togcj02(geom) from test_table
GCJ02轉CGCS2000
select geoc_gcj02tocgcs2000(geom) from test_table
CGCS2000轉BD09
select geoc_cgcs2000tobd09(geom) from test_table
BD09轉CGCS2000
select geoc_bd09tocgcs2000(geom) from test_table
GCJ02轉BD09
select geoc_gcj02tobd09(geom) from test_table
BD09轉GCJ02
select geoc_bd09togcj02(geom) from test_table
使用demo
select st_asgeojson(geoc_wgs84togcj02(st_setsrid(shape,4326))) from dt_cbdk where shape is not null limit 1
- 原來的座標系統必須爲4326或4490,否在轉換結果爲null
- 可以使用st_setsrid()給空間字段添加srid