The ST_GeomFromKML() function

The ST_GeomFromKML() function takes a KML fragment and returns an ST_Geometry corresponding to the fragment.

Syntax

ST_GeomFromKML(kml_string lvarchar) 

Return type

Depends on the KML fragment type, as shown in the following table.

Table 1. KML fragment to return type mapping
KML fragment Return type
Point ST_Point
LineString ST_LineString
Polygon ST_Polygon
MultiGeometry plus Point ST_MultiPoint
MultiGeometry plus LineString ST_MultiLineString
MultiGeometry plus Polygon ST_MultiPolygon

Example

The geometry_test table contains the INTEGER gid column, which uniquely identifies each row, and the geom column, which stores the geometry:
CREATE TABLE geometry_test (gid INTEGER, geom ST_Geometry);
The following INSERT statements insert the data into the gid and geom columns of the geometry_test table. The ST_GeomFromKML() function converts the KML text representation of each geometry into its corresponding instantiable subclass:
INSERT INTO geometry_test VALUES(1,ST_GeomFromKML('<Point><coordinates>
                                 10.02,20.01</coordinates></Point>',4))

INSERT INTO geometry_test VALUES(2,ST_GeomFromKML('<LineString><coordinates>
                                 10.01,20.01 20.01,30.01 30.01,40.01
                                 </coordinates></LineString>',4));