GEOG 868
Spatial Database Management

Geometry Outputs

PrintPrint

PostGIS offers a number of functions for converting geometries between different forms. We saw ST_AsText() in a few examples from earlier in the lesson. Here we'll look at a few others.

ST_AsBinary()
This function outputs the geometry in Well-Known Binary (WKB) format, as laid out in the OGC specification. Outputting geometries in this format is sometimes necessary to interoperate with third-party applications. Here we output the NYC points in WKB format:

SELECT ST_AsBinary(geom) FROM nyc_poi.pts;

ST_AsEWKB()
One of the shortcomings of ST_AsBinary() is that it doesn't provide the geometry's SRID as part of its output. That's where the ST_AsEWKB() function comes in (the "E" in "EWKB" stands for extended). Note that the SRID is binary-encoded just like the coordinates so you shouldn't expect to be able to read the SRID in the output. Here we use ST_AsEWKB() on the same NYC points:

SELECT ST_AsEWKB(geom) FROM nyc_poi.pts;

ST_AsText()
As we've seen in a number of examples this function can be used to output geometries in a human-readable format.

ST_AsEWKT()
Just as the ST_AsBinary() function does not include SRID the same is true of ST_AsText(). ST_AsEWKT() outputs the same text as ST_AsText() but it also includes the SRID. For example:

SELECT ST_AsEWKT(geom) FROM nyc_poi.pts;

Other output functions
Other formats supported by PostGIS include GeoJSON (ST_AsGeoJSON), Geography Markup Language (ST_AsGML), Keyhole Markup Language (ST_AsKML) and scalable vector graphics (ST_AsSVG). Consult the PostGIS documentation for details on using these functions.