Skip to content
🎉 Apache Sedona 1.8.1 is now available! Check out the new features and improvements.

ST_AsGeoJSON

Introduction: Return the GeoJSON string representation of a geometry.

The type parameter (Since: v1.6.1) takes the following options -

  • "Simple" (default): Returns a simple GeoJSON geometry.
  • "Feature": Wraps the geometry in a GeoJSON Feature.
  • "FeatureCollection": Wraps the Feature in a GeoJSON FeatureCollection.

Format:

ST_AsGeoJSON (A: Geometry)

ST_AsGeoJSON (A: Geometry, type: String)

Since: v1.3.0

SQL Example (Simple GeoJSON):

SELECT ST_AsGeoJSON(ST_GeomFromWKT('POLYGON((1 1, 8 1, 8 8, 1 8, 1 1))'))

Output:

{
  "type":"Polygon",
  "coordinates":[
    [[1.0,1.0],
      [8.0,1.0],
      [8.0,8.0],
      [1.0,8.0],
      [1.0,1.0]]
  ]
}

SQL Example (Feature GeoJSON):

Output:

{
  "type":"Feature",
  "geometry": {
      "type":"Polygon",
      "coordinates":[
        [[1.0,1.0],
          [8.0,1.0],
          [8.0,8.0],
          [1.0,8.0],
          [1.0,1.0]]
      ]
  }
}

SQL Example (FeatureCollection GeoJSON):

Output:

{
  "type":"FeatureCollection",
  "features": [{
    "type":"Feature",
    "geometry": {
      "type":"Polygon",
      "coordinates":[
        [[1.0,1.0],
          [8.0,1.0],
          [8.0,8.0],
          [1.0,8.0],
          [1.0,1.0]]
      ]
    }
  }
  ]
}