Categories:

Geospatial Functions

ST_ASGEOJSON

Given a value of type GEOGRAPHY, return the GeoJSON representation of that value.

Syntax

ST_ASGEOJSON( <geography_expression> )

Arguments

geography_expression

The argument must be an expression of type GEOGRAPHY.

Returns

An OBJECT in GeoJSON format.

Examples

The following example demonstrates the ST_ASGEOJSON function:

create table geospatial_table (g GEOGRAPHY);
insert into geospatial_table values
    ('POINT(-122.35 37.55)'), ('LINESTRING(-124.20 42.00, -120.01 41.99)');
select st_asgeojson(g)
    from geospatial_table;
+------------------------+
| ST_ASGEOJSON(G)        |
|------------------------|
| {                      |
|   "coordinates": [     |
|     -122.35,           |
|     37.55              |
|   ],                   |
|   "type": "Point"      |
| }                      |
| {                      |
|   "coordinates": [     |
|     [                  |
|       -124.2,          |
|       42               |
|     ],                 |
|     [                  |
|       -120.01,         |
|       41.99            |
|     ]                  |
|   ],                   |
|   "type": "LineString" |
| }                      |
+------------------------+

Casting the VARIANT output to VARCHAR results in the following:

select st_asgeojson(g)::varchar
    from geospatial_table;
+-------------------------------------------------------------------+
| ST_ASGEOJSON(G)::VARCHAR                                          |
|-------------------------------------------------------------------|
| {"coordinates":[-122.35,37.55],"type":"Point"}                    |
| {"coordinates":[[-124.2,42],[-120.01,41.99]],"type":"LineString"} |
+-------------------------------------------------------------------+