Constructor functions

Constructor functions are spatial functions that you can use to build spatial objects from the following input formats: well-known text (WKT) representation, well-known binary (WKB) representation, ESRI shape representation, and Geography Markup Language (GML).

Constructor functions have the same name as the geometry data type of the column into which the data will be inserted. These functions operate consistently on each of the input data exchange formats.

Spatial support includes the following constructor functions:
  • ST_POINT
  • ST_LINESTRING
  • ST_POLYGON
  • ST_MULTIPOINT
  • ST_MULTILINESTRING
  • ST_MULTIPOLYGON

The ST_Point function also takes X, Y, Z, and M coordinate values as input.

Functions that operate on data exchange formats

This section provides the syntax for calling functions that operate on data exchange formats, describes the input parameters for the functions, and identifies the type of geometry that these functions return.

Syntax for functions operating on data exchange formats

Read syntax diagramSkip visual syntax diagramdb2gse.geometry_type(wktwkbshapegml,srs_id)

Parameters for functions operating on data exchange formats

db2gse
Name of the schema to which the spatial data types supplied by IBM Spatial Support for Db2 for z/OS belong.
geometry_type
One of the following constructor functions:
  • ST_Point
  • ST_LineString
  • ST_Polygon
  • ST_MultiPoint
  • ST_MultiLineString
  • ST_MultiPolygon
wkt
A value of type CLOB(8M) that contains the well-known text representation of the geometry.
wkb
A value of type BLOB(4M) that contains the well-known binary representation of the resulting linestring. If the well-known binary representation is null, then null is returned.
shape
A value of type BLOB(4M) that contains the shape representation of the resulting linestring. If the shape representation is null, then null is returned.
gml
A value of type CLOB(8M) that contains the GML representation of the geometry.
srs_id
A value of type INTEGER that identifies the spatial reference system for the resulting geometry.

Return Type for functions operating on data exchange formats

geometry_type

If geometry_type is ST_Geometry, the dynamic type of the returned geometry type corresponds to the geometry indicated by the input value.

If geometry_type is any other type, the dynamic type of the returned geometry type corresponds to the function name. If the geometry indicated by the input value does not match the function name or the name of one of its subtypes, an error is returned.

A function that creates geometries from coordinates

The ST_Point function creates geometries not only from data exchange formats, but also from numeric coordinate values—a very useful capability if your location data is already stored in your database.

Syntax for db2gse.ST_Point

Read syntax diagramSkip visual syntax diagramdb2gse.ST_Point(coordinates,srs_id )
coordinates
Read syntax diagramSkip visual syntax diagramx_coordinate,y_coordinate ,z_coordinate,m_coordinate

Parameters for db2gse.ST_Point

x_coordinate
A value of type DOUBLE that specifies the X coordinate for the resulting point.
y_coordinate
A value of type DOUBLE that specifies the Y coordinate for the resulting point.
z_coordinate
A value of type DOUBLE that specifies the Z coordinate for the resulting point.

If the z_coordinate parameter is omitted, the resulting point will not have a Z coordinate.

m_coordinate
A value of type DOUBLE that specifies the M coordinate for the resulting point.

If the m_coordinate parameter is omitted, the resulting point will not have a measure.

srs_id
A value of type INTEGER that identifies the spatial reference system for the resulting point.

If srs_id does not identify a spatial reference system listed in the catalog view DB2GSE.ST_SPATIAL_REFERENCE_SYSTEMS, an exception condition is raised.

Return type for db2gse.ST_Point

db2gse.ST_Point

Examples

This topic provides examples of code for invoking constructor functions, code for creating tables to contain the output of constructor functions, and code for retrieving the output.

The following example inserts a row into the SAMPLE_GEOMETRY table with ID 100 and a point value with an X coordinate of 30, a Y coordinate of 40, and in spatial reference system 1 using the coordinate representation and well-known text (WKT) representation. It then inserts another row with ID 200 and a linestring value with the coordinates indicated.


CREATE TABLE sample_geometry (id INT, geom db2gse.ST_Geometry);

INSERT INTO sample_geometry(id, geom)
       VALUES(100,db2gse.ST_Geometry(db2gse.ST_Point('POINT(30 40)',1)));

INSERT INTO sample_geometry(id, geom)
       VALUES(200,db2gse.ST_Geometry(db2gse.ST_Linestring('linestring(50 50, 
								100 100)', 1)));

If you know that the spatial column can only contain ST_Point values, you can use the following example, which inserts two points. Attempting to insert a linestring or any other type which is not a point results in an SQL error. The first insert creates a point geometry from the well-known-text representation (WKT). The second insert creates a point geometry from numeric coordinate values. These input values could also be selected from existing table columns.

CREATE TABLE sample_points (id INT, geom db2gse.ST_Point);

INSERT INTO sample_points(id, geom)
       VALUES(100,db2gse.ST_Point('point(30 40)', 1));

INSERT INTO sample_points(id, geom)
       VALUES(101,db2gse.ST_Point(50, 50, 1));

The following example uses embedded SQL and assumes that the application fills the data areas with the appropriate values.

EXEC SQL BEGIN DECLARE SECTION;
   sqlint32 id = 0;
   SQL TYPE IS CLOB(10000) wkt_buffer;
   SQL TYPE IS BLOB(10000) wkb_buffer;
   SQL TYPE IS BLOB(10000) shape_buffer;
EXEC SQL END DECLARE SECTION;

// * Application logic to read into buffers goes here */

EXEC SQL INSERT INTO sample_geometry(id, geom)
         VALUES(:id, db2gse.ST_Geometry(:wkt_buffer,1));

EXEC SQL INSERT INTO sample_geometry(id, geom)
         VALUES:id, db2gse.ST_Geometry(:wkb_buffer,1));

EXEC SQL INSERT INTO sample_geometry(id, geom)
         VALUES(:id, db2gse.ST_Geometry(:shape_buffer,1));

The following sample Java™ code uses JDBC to insert point geometries using the WKT representation to specify the geometry and using the X, Y numeric coordinate values to specify the geometries.

String ins1 = "INSERT into sample_geometry (id, geom) 
              VALUES(?, db2gse.ST_Point(CAST( ? 
              as VARCHAR(128)), 1))";
PreparedStatement pstmt = con.prepareStatement(ins1);
pstmt.setInt(1, 100);                    // id value
pstmt.setString(2, "point(32.4 50.7)");  // wkt value
int rc = pstmt.executeUpdate();

String ins2 = "INSERT into sample_geometry (id, geom)                 
              VALUES(?, db2gse.ST_Point(CAST( ? as double), 
              CAST(? as double), 1))";
pstmt = con.prepareStatement(ins2);
pstmt.setInt(1, 200);         // id value
pstmt.setDouble(2, 40.3);     // lat
pstmt.setDouble(3, -72.5);    // long
rc = pstmt.executeUpdate();

Conversion to well-known text (WKT) representation

Text representations are CLOB values representing ASCII character strings. They allow geometries to be exchanged in ASCII text form.

The ST_AsText function converts a geometry value stored in a table to a WKT string. The following example uses a simple command-line query to select the values that were previously inserted into the SAMPLE_GEOMETRY table. (This example returns SQL code 445, because the data has been truncated.)


SELECT id, VARCHAR(db2gse.ST_AsText(geom), 50) AS WKTGEOM 
FROM sample_geometry;

ID 		WKTGEOM
------ 	--------------------------------
   100   POINT ( 30.00000000 40.00000000)
   200   LINESTRING ( 50.00000000 50.00000000, 100.00000000 100.00000000)

The following example uses embedded SQL to select the values that were previously inserted into the SAMPLE_GEOMETRY table.


 EXEC SQL BEGIN DECLARE SECTION;
 sqlint32 id = 0;
 SQL TYPE IS CLOB(10000) wkt_buffer;
 short wkt_buffer_ind = -1;
 EXEC SQL END DECLARE SECTION;

 EXEC SQL
 SELECT id, db2gse.ST_AsText(geom)
 INTO :id, :wkt_buffer :wkt_buffer_ind
 FROM sample_geometry
 WHERE id = 100;

In addition, IBM Spatial Support for Db2 for z/OS provides other functions that convert geometries to and from well-known text representations. The following additional functions implement the Open Geospatial Consortium (OGC) standard Simple Features SQL and the ISO standard SQL multimedia and application packages - Part 3: Spatial:

  • ST_GeomFromText
  • ST_WKTToSQL

Conversion to well-known binary (WKB) representation

The WKB representation consists of binary data structures that must be BLOB values.

These BLOB values represent binary data structures that must be managed by an application program written in a programming language that Db2 supports and for which Db2 has a language binding.

The ST_AsBinary function converts a geometry value stored in a table to the well-known binary (WKB) representation, which can be fetched into a BLOB variable in program storage. The following example uses embedded SQL to select the values that were previously inserted into the SAMPLE_GEOMETRY table.


EXEC SQL BEGIN DECLARE SECTION;
   sqlint32 id = 0;    
   SQL TYPE IS BLOB(10000) wkb_buffer;
   short wkb_buffer_ind = -1;
EXEC SQL END DECLARE SECTION;

EXEC SQL
   SELECT id, db2gse.ST_AsBinary(geom)
   INTO :id, :wkb_buffer :wkb_buffer_ind
   FROM sample_geometry
   WHERE id = 200;

In addition, IBM Spatial Support for Db2 for z/OS provides other functions that convert geometries to and from well-known binary representations. The following additional functions implement the Open Geospatial Consortium (OGC) standard Simple Features SQL and the ISO standard SQL multimedia and application packages - Part 3: Spatial:

  • ST_GeomFromWKB
  • ST_WKBToSQL

Conversion to ESRI shape representation

The ESRI shape representation consists of binary data structures that must be managed by an application program written in a supported language.

The ST_AsShape function converts a geometry value stored in a table to the ESRI shape representation, which can be fetched into a BLOB variable in program storage. The following example uses embedded SQL to select the values that were previously inserted into the SAMPLE_GEOMETRY table.


EXEC SQL BEGIN DECLARE SECTION;
   sqlint32 id;
   SQL TYPE IS BLOB(10000) shape_buffer;
EXEC SQL END DECLARE SECTION;

EXEC SQL
   SELECT id, db2gse.ST_AsShape(geom)
   INTO :id, :shape_buffer
   FROM sample_geometry;

Conversion to Geography Markup Language (GML) representation

Geography Markup Language (GML) representations are ASCII strings. GML representations allow geometries to be exchanged in ASCII text format.

The ST_AsGML function converts a geometry value that is stored in a table to a GML text string. The following example selects the values that were previously inserted into the SAMPLE_GEOMETRY table. The results shown in the example are reformatted for readability. The spacing in your results might vary according to your online display.


SELECT id, VARCHAR(db2gse.ST_AsGML(geom, 'EPSG', 4269), 500) AS GMLGEOM 
FROM sample_geometry;

ID           GMLGEOM
-----------  ----------------------------------------------------------
        100 <gml:Point srsName="EPSG:4269">
            <gml:coord><gml:X>30</gml:X><gml:Y>40</gml:Y></gml:coord>
            </gml:Point>
        200 <gml:LineString srsName="EPSG:4269">
            <gml:coord><gml:X>50</gml:X><gml:Y>50</gml:Y></gml:coord>
            <gml:coord><gml:X>100</gml:X><gml:Y>100</gml:Y></gml:coord>
            </gml:LineString>