The ST_AsKML() function

The ST_AsKML() function returns the Keyhole Markup Language (KML) representation of an ST_Geometry spatial type.

ST_AsKML() can also take an optional parameter that describes KML shape attributes.

Syntax

ST_AsKML(p ST_Geometry)
ST_AsKML(p ST_Geometry, attributes lvarchar)

The attributes parameter can contain one or more KML shape attributes, in XML format. The following table describes common KML shape attributes.

Table 1. KML shape attributes
Attribute Description
<extrude> A Boolean value that specifies if the geometry is connected to the ground (1) or not (0). To extrude a geometry, the value of the <altitudeMode> attribute must be either relativeToGround or absolute and the Z coordinate within the <coordinates> element must be greater than zero (0).

If this attribute is not specified, it is false (0).

<tessellate> A Boolean value that specifies if the geometry should follow the terrain (1) or not (0). To enable tessellation for the geometry, the value for the <altitudeMode> attribute must be clampToGround.

If this attribute is not specified, it is false (0).

<altitudeMode> Specifies how Z coordinates in the <coordinates> element are interpreted. Possible values are:
clampToGround (default)
Indicates to ignore the altitude specification in the geometry and place it at ground level.
relativeToGround
Interprets the altitude specification in the geometry and places it at that altitude above the ground.
absolute
Interprets the altitude specification in the geometry and places it at that altitude above sea level.

The Spatial DataBlade® module does not check whether the attributes are valid. If any of the attributes are not valid, the resulting KML fragment might not be valid when it is received by the application.

Return type

LVARCHAR

Example

EXECUTE FUNCTION ST_AsKML(ST_PointFromText('POINT(-83.54354 34.23425)',4))
Output:
<Point>
        <coordinates>-83.5435399663,34.2342500677</coordinates>
</Point>
In this example, ST_AsKML() contains KML shape attributes to specify that the geometry is connected to the ground:
EXECUTE FUNCTION ST_AsKML(ST_PointFromText('POINT Z
                         (-83.54523 34.214312 100)',4),
                         '<extrude>1</extrude><altitudeMode>
                         clampToGround</altitudeMode>');
Output:
<Point>
       <extrude>1</extrude>
       <altitudeMode>clampToGround</altitudeMode>
       <coordinates>-83.545522957,34.2143120335,100</coordinates>
</Point>