Introducing Crunchy Data Warehouse: A next-generation Postgres-native data warehouse. Crunchy Data Warehouse Learn more

Waiting for PostGIS 3: ST_AsGeoJSON(record)

Avatar for Paul Ramsey

Paul Ramsey

2 min read

With PostGIS 3.0, it is now possible to generate GeoJSON features directly without any intermediate code, using the new ST_AsGeoJSON(record) function.

The GeoJSON format is a common transport format, between servers and web clients, and even between components of processing chains. Being able to create useful GeoJSON is important for integrating different parts in a modern geoprocessing application.

PostGIS has had an ST_AsGeoJSON(geometry) for forever, but it does slightly less than most users really need: it takes in a PostGIS geometry, and outputs a GeoJSON "geometry object".

The GeoJSON geometry object is just the shape of the feature, it doesn't include any of the other information about the feature that might be included in the table or query. As a result, developers have spent a lot of time writing boiler-plate code to wrap the results of ST_AsGeoJSON(geometry) up with the columns of a result tuple to create GeoJSON "feature objects".

The ST_AsGeoJSON(record) function looks at the input tuple, and takes the first column of type geometry to convert into a GeoJSON geometry. The rest of the columns are added to the GeoJSON features in the properties member.

SELECT ST_AsGeoJSON(subq.*) AS geojson
FROM (
  SELECT ST_Centroid(geom), type, admin
  FROM countries
  WHERE name = 'Canada'
) AS subq
{
	"type": "Feature",
	"geometry": {
		"type": "Point",
		"coordinates": [-98.2939042718784, 61.3764628013483]
	},
	"properties": {
		"type": "Sovereign country",
		"admin": "Canada"
	}
}

Using GeoJSON output it's easy to stream features directly from the database into an OpenLayers or Leaflet web map, or to consume them with ogr2ogr for conversion to other geospatial formats.