名前

ST_AsGeoJSON — GeoJSON要素としてジオメトリを返します。

概要

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の"geometry"オブジェクトとして返すか、行をGeoJSONの"feature"オブジェクトとして返します (GeoJSON specifications RFC 7946参照)。2次元と3次元の両方のジオメトリに対応しています。GeoJSONはSFS 1.1のジオメトリタイプのみに対応しています (例えば曲線は対応していません)。

maxdecimaldigits引数は、出力で使用される小数部の桁数の最大値を減らすために使われます (デフォルトでは9)。EPSG:4326を使っていて、表示専用でジオメトリを出力する場合には、maxdecimaldigits=6が、多くの地図で良い選択となります。

options引数は、GeoJSON出力にBBOXまたはCRSを追加するために使われます。値は次の通りです。

  • 0: オプションなし

  • 1: GeoJSON BBOX

  • 2: GeoJSON Short CRS (たとえば EPSG:4326)

  • 4: GeoJSON Long CRS (たとえば urn:ogc:def:crs:EPSG:4326)

  • 8: EPSG:4326でない場合にGeoJSON Short CRS (デフォルト)

Availability: 1.3.4

Availability: 1.5.0 ジオグラフィが導入されました。

Changed: 2.0.0 デフォルト引数と名前付き引数に対応しました。

Changed: 3.0.0 レコードの入力に対応しました

Changed: 3.0.0 EPSG:4326以外の場合のSRID出力

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

GeoJSON書式はWebマッピングフレームワークで一般的です。

GeoJSONデータはgeojson.ioで試験と表示が可能です。

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),
  (2, 'two', 'POINT(2 2)'),
  (3, 'three', 'POINT(3 3)'))
AS t(id, name, geom);
st_asgeojson
-----------------------------------------------------------------------------------------------------------------
 {"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"}}

RFC7946に準拠するために、データをEGS84地理座標系に変換するのを忘れないでください。

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]]]}
(1 row)

3次元ジオメトリでも使えます。

SELECT ST_AsGeoJSON('LINESTRING(1 2 3, 4 5 6)');
{"type":"LineString","coordinates":[[1,2,3],[4,5,6]]}

関連情報

ST_GeomFromGeoJSON, ST_AsMVT, ST_AsGeobuf