Name

ST_AsGeoJSON — 将几何图形作为GeoJSON元素返回。

Synopsis

text ST_AsGeoJSON(record feature, text geomcolumnname, integer maxdecimaldigits=9, boolean pretty_bool=false);

text ST_AsGeoJSON(geometry geom, integer maxdecimaldigits=9, integer options=8);

text ST_AsGeoJSON(geography geog, integer maxdecimaldigits=9, integer options=0);

描述

将几何图形作为GeoJSON“几何图形”返回,或将行作为GeoJSON“要素”返回。(请参阅 GeoJSON规范RFC 7946 )。同时支持2D和3D几何图形。GeoJSON仅支持SFS 1.1几何体类型(例如,不支持曲线)。

这个 maxdecimaldigits 参数可用于减少输出中使用的最大小数位数(默认为9)。如果您使用的是EPSG:4326并且输出的几何图形仅用于显示, maxdecimaldigits =6对于许多贴图来说可能是一个很好的选择。

[Warning]

使用 最大小数位数 参数可能会导致输出几何图形无效。为了避免这种使用 ST_ReducePrecision 首先要有一个合适的网格大小。

这个 options 参数可用于在GeoJSON输出中添加BBox或CRS:

  • 0:表示无选项

  • 1:GeoJSON BBox

  • 2:GeoJSON短CRS(例如EPSG:4326)

  • 4:GeoJSON长CRS(例如urn:ogc:def:crs:epsg::4326)

  • 8:GeoJSON短CRS,如果不是EPSG:4326(默认)

GeoJSON规范规定,使用右手规则来确定多边形的方向,有些客户端要求使用此方向。这可以通过使用 ST_ForcePolygonCCW 。该规范还要求几何图形采用WGS84坐标系(SRID=4326)。如有必要,可以使用将几何图形投影到WGS84中 ST_TransformST_Transform( geom, 4326 )

GeoJSON可在线测试和查看,网址为 geojson.io geojsonlint.com 。它得到了Web地图框架的广泛支持:

可用性:1.3.4

可用性:引入了1.5.0地理支持。

已更改:2.0.0支持默认参数和命名参数。

更改:3.0.0支持记录作为输入

已更改:3.0.0如果不是EPSG:4326,则输出SRID。

This function supports 3d and will not drop the z-index.

示例

生成FeatureCollection:

SELECT json_build_object(
    'type', 'FeatureCollection',
    'features', json_agg(ST_AsGeoJSON(t.*)::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]}, "properties": {"id": 1, "name": "one"}}, {"type": "Feature", "geometry": {"type":"Point","coordinates":[2,2]}, "properties": {"id": 2, "name": "two"}}, {"type": "Feature", "geometry": {"type":"Point","coordinates":[3,3]}, "properties": {"id": 3, "name": "three"}}]}

生成特征:

SELECT ST_AsGeoJSON(t.*)
FROM (VALUES (1, 'one', 'POINT(1 1)'::geometry)) AS t(id, name, geom);
st_asgeojson
-----------------------------------------------------------------------------------------------------------------
 {"type": "Feature", "geometry": {"type":"Point","coordinates":[1,1]}, "properties": {"id": 1, "name": "one"}}

使用生成要素的另一种方法 id 属性是使用JSONB函数和运算符:

SELECT jsonb_build_object(
    'type',       'Feature',
    'id',         id,
    'geometry',   ST_AsGeoJSON(geom)::jsonb,
    'properties', to_jsonb( t.* ) - 'id' - 'geom'
    ) AS json
FROM (VALUES (1, 'one', 'POINT(1 1)'::geometry)) AS t(id, name, geom);
json
-----------------------------------------------------------------------------------------------------------------
 {"id": 1, "type": "Feature", "geometry": {"type": "Point", "coordinates": [1, 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]]}

另请参阅

ST_GeomFromGeoJSON, ST_ForcePolygonCCW , ST_Transform