ST_AsGeoJSON function

The ST_AsGeoJSON function takes a geometry as input and returns its representation in the GeoJson format.

If the specified geometry is null then null is returned.

Syntax

Read syntax diagramSkip visual syntax diagram ST_AsGeoJSON ( geometry )

Parameter

geometry
A value of type ST_Geometry or one of its subtypes to be converted to the corresponding GeoJSON format.

Return type

CLOB(2G)

Examples

Example 1:
The following example illustrates the result for a point geometry in the spatial reference system with srs_id 4326 (the default):

CREATE TABLE sample_points (id integer, geometry ST_Point)

INSERT INTO sample_points VALUES (1100, ST_Point(10, 20))

SELECT id, cast(ST_AsGeoJSON(geometry) AS varchar(400)) AS json_point FROM sample_points
Result:

ID    JSON_POINT
----  ------------------------------------------------------------------
1100  { "type": "POINT", "coordinates": [10.000000000,20.000000000] }
Example 2:
In the following example, geometries of type linestring are inserted with the spatial reference system id 4326 (the default) and listed as geojson:

CREATE TABLE SAMPLE_Linestring (id integer, geometry ST_LineString)

INSERT INTO SAMPLE_Linestring VALUES (2232, ST_LineString('linestring(120 55, 125 60, 130 65, 135 70)') )

INSERT INTO SAMPLE_Linestring VALUES (2233, ST_LineString('linestring(125 55, 130 55, 130 60, 130 65, 135 65)') )

SELECT id, cast(ST_AsGeoJSON(geometry) AS varchar(400)) AS json_line FROM SAMPLE_Linestring
Result:

ID     JSON_LINE
-----  --------------------------------------------------------------------------------------------------
2232   {"type":"LINESTRING","coordinates":[[120.000000000,55.000000000],[125.000000000,60.000000000],
                                           [130.000000000,65.000000000],[135.000000000,70.000000000]]}
2233   {"type":"LINESTRING","coordinates":[[125.000000000,55.000000000],[130.000000000,55.000000000],
                                           [130.000000000,60.000000000],[130.000000000,65.000000000],
                                           [135.000000000,65.000000000]]}