ST_AsGeoJSON — 以 GeoJSON 格式傳回幾何或要素。
text ST_AsGeoJSON(
record feature, text geom_column="", integer maxdecimaldigits=9, boolean pretty_bool=false, text id_column='')
;
text ST_AsGeoJSON(
geometry geom, integer maxdecimaldigits=9, integer options=8)
;
text ST_AsGeoJSON(
geography geog, integer maxdecimaldigits=9, integer options=0)
;
以 GeoJSON "geometry" 物件形式傳回幾何,或以 GeoJSON "feature" 物件形式傳回列。
產生的 GeoJSON 幾何和要素表示符合 GeoJSON 規格 RFC 7946,除非解析的幾何參考的 CRS 不是 WGS84 經緯度(EPSG:4326, urn:ogc:def:crs:OGC::CRS84);GeoJSON 幾何物件預設會附加一個簡短的 CRS SRID 識別符。 2D 和 3D 幾何都支援。GeoJSON 僅支援 SFS 1.1 幾何類型(例如,不支援曲線)。
geom_column
參數用於區分多個幾何欄。如果省略,將會決定記錄中的第一個幾何欄。反之,傳遞參數會節省欄類型查找。
maxdecimaldigits
引數可用於減少輸出中使用的小數位數上限(預設為 9)。如果您正在使用 EPSG:4326 並且僅為了顯示而輸出幾何,則對於許多地圖而言,maxdecimaldigits
=6 可能是一個不錯的選擇。
![]() |
|
使用 |
options
引數可用於在 GeoJSON 輸出中新增 BBOX 或 CRS。
0:表示無選項
1:GeoJSON BBOX
2:GeoJSON 簡短 CRS(例如 EPSG:4326)
4:GeoJSON 長 CRS(例如 urn:ogc:def:crs:EPSG::4326)
8:如果不是 EPSG:4326,則使用 GeoJSON 簡短 CRS(預設)
id_column
參數用於設定傳回的 GeoJSON 要素的 "id" 成員。根據 GeoJSON RFC,當要素具有常用的識別符時(例如主鍵),應使用此參數。未指定時,產生的要素將不會取得 "id" 成員,並且除了幾何之外的任何欄(包括任何潛在的鍵)都只會進入要素的 "properties" 成員。
GeoJSON 規格指出多邊形使用右手規則定向,並且某些用戶端需要此方向。這可以通過使用 ST_ForcePolygonCCW 來確保。該規範還要求幾何形狀位於 WGS84 坐標系統(SRID = 4326)中。如有必要,可以使用 ST_Transform 將幾何投影到 WGS84 中:ST_Transform( geom, 4326 )
。
可以在 geojson.io 和 geojsonlint.com 上線上測試和查看 GeoJSON。它受到網路地圖框架的廣泛支援。
可用性:1.3.4
可用性:引入了 1.5.0 地理支援。
已變更:2.0.0 支援預設引數和具名引數。
已變更:3.0.0 支援將記錄作為輸入
已變更:3.0.0 如果不是 EPSG:4326,則輸出 SRID。
已變更:3.5.0 允許指定包含要素 ID 的欄
此函數支援 3d,並且不會捨棄 z 索引。
產生 FeatureCollection
SELECT json_build_object( 'type', 'FeatureCollection', 'features', json_agg(ST_AsGeoJSON(t.*, id_column => 'id')::json) ) FROM ( VALUES (1, 'one', 'POINT(1 1)'::geometry), (2, 'two', 'POINT(2 2)'), (3, 'three', 'POINT(3 3)') ) as t(id, name, geom);
{"type" : "FeatureCollection", "features" : [{"type": "Feature", "geometry": {"type":"Point","coordinates":[1,1]}, "id": 1, "properties": {"name": "one"}}, {"type": "Feature", "geometry": {"type":"Point","coordinates":[2,2]}, "id": 2, "properties": {"name": "two"}}, {"type": "Feature", "geometry": {"type":"Point","coordinates":[3,3]}, "id": 3, "properties": {"name": "three"}}]}
產生要素
SELECT ST_AsGeoJSON(t.*, id_column => 'id') FROM (VALUES (1, 'one', 'POINT(1 1)'::geometry)) AS t(id, name, geom);
st_asgeojson ----------------------------------------------------------------------------------------------------------------- {"type": "Feature", "geometry": {"type":"Point","coordinates":[1,1]}, "id": 1, "properties": {"name": "one"}}
請記住將您的資料轉換為 WGS84 經度、緯度,以符合 GeoJSON 規格
SELECT ST_AsGeoJSON(ST_Transform(geom,4326)) from fe_edges limit 1;
st_asgeojson ----------------------------------------------------------------------------------------------------------- {"type":"MultiLineString","coordinates":[[[-89.734634999999997,31.492072000000000], [-89.734955999999997,31.492237999999997]]]}
支援 3D 幾何
SELECT ST_AsGeoJSON('LINESTRING(1 2 3, 4 5 6)');
{"type":"LineString","coordinates":[[1,2,3],[4,5,6]]}
可以使用 Options 引數在 GeoJSON 輸出中新增 BBOX 和 CRS
SELECT ST_AsGeoJSON(ST_SetSRID('POINT(1 1)'::geometry, 4326), 9, 4|1);
{"type":"Point","crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:EPSG::4326"}},"bbox":[1.000000000,1.000000000,1.000000000,1.000000000],"coordinates":[1,1]}