名前

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

概要

text ST_AsGeoJSON(geometry geom, integer maxdecimaldigits=15, integer options=0);

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

text ST_AsGeoJSON(integer gj_version, geometry geom, integer maxdecimaldigits=15, integer options=0);

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

説明

Geometry JavaScript Object Noation (GeoJSON)要素としてジオメトリを返します (GeoJSON specifications 1.0参照)。2次元と3次元のジオメトリの両方に対応しています。GeoJSONはSFS 1.1ジオメトリタイプのみ対応します (たとえば曲線は対応していません)。

gj_versionパラメタはGeoJSON仕様のメジャーバージョンです。指定した場合1でなければなりません。これはGeoJSONの仕様の版を表現します。

第3引数は、出力の最大有効桁数を減らすために使われることがあります (デフォルトは15です)。

最後の'options'引数は、GeoJSON出力の中にBboxまたはCrsを追加するために使います。与える値は次の通りです。

  • 0: オプションなし (デフォルト値)

  • 1: GeoJSON Bbox

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

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

一つ目の形式: ST_AsGeoJSON(geom) / precision=15 version=1 options=0

二つ目の形式: ST_AsGeoJSON(geom, precision) / version=1 options=0

三つ目の形式: ST_AsGeoJSON(geom, precision, options) / version=1

四つ目の形式: ST_AsGeoJSON(gj_version, geom) / precision=15 options=0

五つ目の形式: ST_AsGeoJSON(gj_version, geom, precision) /options=0

六つ目の形式: ST_AsGeoJSON(gj_version, geom, precision,options)

Availability: 1.3.4

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

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

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

GeoJSON書式は一般的にAjaxマッピングでの使用に効果的です。OpenLayersはこれをサポートする有名なJavaSciprクライアントです。使用例がOpenLayers GeoJSON Exampleにあります。

SELECT ST_AsGeoJSON(the_geom) from fe_edges limit 1;
                                           st_asgeojson
-----------------------------------------------------------------------------------------------------------

{"type":"MultiLineString","coordinates":[[[-89.734634999999997,31.492072000000000],
[-89.734955999999997,31.492237999999997]]]}
(1 row)
--3d point
SELECT ST_AsGeoJSON('LINESTRING(1 2 3, 4 5 6)');

st_asgeojson
-----------------------------------------------------------------------------------------
 {"type":"LineString","coordinates":[[1,2,3],[4,5,6]]}