The ST_GeomFromGML() function

The ST_GeomFromGML() function takes a GML2 or GML version 3 string representation and an optional spatial reference ID and returns a geometry object.

If the srsName attribute is specified in the GML string, a corresponding entry must exist in the spatial_references table, or it must be specified as UNKNOWN or DEFAULT. The GML representation can include Z and M measures, but must include the appropriate srsDimension attribute. The following table describes the corresponding geometry values for each srsDimension value.

Table 1. Geometry values for srsDimension values
srsDimension value Geometry value
2 A geometry value with only X and Y coordinate values.
3 A geometry value with X, Y, and Z coordinate values in GML version 3 or using the <coordinates> tag in GML 2.

A geometry value with X, Y and measure values if the <X>, <Y>, and <M> tags are used in the <coord> elements of the GML representation.

4 A geometry value with X, Y, Z, and measure coordinate values.

Syntax

ST_GeomFromGML(gml_string lvarchar) 
ST_GeomFromGML(gml_string lvarchar, SRID integer) 

For SRID, specify 2 if the GML conforms to GML version 2, or specify 3 for GML3. The default is 3.

Return type

ST_Geometry

Example

The geometry_test table contains the INTEGER gid column, which uniquely identifies each row, and the g1 column, which stores the geometry:
CREATE TABLE geometry_test (gid smallint, g1 ST_Geometry);
The following INSERT statements insert the data into the gid and g1 columns of the geometry_test table. The ST_GeomFromGML() function converts the GML text representation of each geometry into its corresponding instantiable subclass:
INSERT INTO geometry_test VALUES (
1,
ST_GeomFromGML(‘<gml:Point srsName="DEFAULT" srsDimension="2">
  <gml:pos>10.02 20.01</gml:pos></gml:Point>',1000)) ;

INSERT INTO geometry_test VALUES (
2,
ST_GeomFromGML(‘<gml:LineString srsName="DEFAULT" srsDimension="2">
  <gml:posList dimension="2">10.01 20.01 10.01 30.01 10.01 40.01
  </gml:posList>
  </gml:LineString>',1000)) ;

INSERT INTO geometry_test VALUES (
3,
ST_GeomFromGML(‘<gml:Polygon srsName="DEFAULT" srsDimension="2">
  <gml:exterior>
  <gml:LinearRing>
  <gml:posList dimension="2">
    10.02 20.01 19.15 33.94 25.02 34.15 11.92 35.64 10.02 20.01
  </gml:posList>
  </gml:LinearRing>
  </gml:exterior>
  </gml:Polygon>',1000)) ;

INSERT INTO geometry_test VALUES (
4,
ST_GeomFromGML(‘<gml:MultiPoint srsName="DEFAULT" srsDimension="2">
  <gml:PointMember>
  <gml:Point srsName="DEFAULT" srsDimension="2">
  <gml:pos>10.02 20.01</gml:pos>
  </gml:Point>
  </gml:PointMember><gml:PointMember>
  <gml:Point srsName="DEFAULT" srsDimension="2">
  <gml:pos>10.32 23.98</gml:pos>
  </gml:Point>
  </gml:PointMember>
  <gml:PointMember>
  <gml:Point srsName="DEFAULT" srsDimension="2">
  <gml:pos>11.92 25.64</gml:pos>
  </gml:Point>
  </gml:PointMember>
  </gml:MultiPoint>',1000)) ;

INSERT INTO geometry_test VALUES (
5,
ST_GeomFromGML(‘<gml:MultiLineString srsName="DEFAULT" srsDimension="2">
  <gml:LineStringMember>
  <gml:LineString srsName="DEFAULT" srsDimension="2">
  <gml:posList dimension="2">10.02 20.01 10.32 23.98 11.92 25.64
  </gml:posList>
  </gml:LineString>
  </gml:LineStringMember>
  <gml:LineStringMember>
  <gml:LineString srsName="DEFAULT" srsDimension="2">
  <gml:posList dimension="2">9.55 23.75 15.36 30.11</gml:posList>
  </gml:LineString>
  </gml:LineStringMember>
  </gml:MultiLineString>',1000)) ;

INSERT INTO geometry_test VALUES(
6,
ST_GeomFromGML(‘<gml:MultiPolygon srsName="DEFAULT" srsDimension="2">
  <gml:PolygonMember><gml:Polygon srsName="DEFAULT" srsDimension="2">
  <gml:exterior>
  <gml:LinearRing>
  <gml:posList dimension="2">
    10.02 20.01 19.15 33.94 25.02 34.15 11.92 35.64 10.02 20.01
  </gml:posList>
  </gml:LinearRing>
  </gml:exterior>
  </gml:Polygon>
  </gml:PolygonMember>
  <gml:PolygonMember>
  <gml:Polygon srsName="DEFAULT" srsDimension="2">
  <gml:exterior><gml:LinearRing>
  <gml:posList dimension="2">
    51.71 21.73 73.36 27.04 71.52 32.87 52.43 31.9 51.71 21.73
  </gml:posList>
  </gml:LinearRing>
  </gml:exterior>
  </gml:Polygon>
  </gml:PolygonMember>
  </gml:MultiPolygon>',1000)) ;