IBM Support

Retrieving a polygon from a table using the Spatial DataBlade JAVA API

Troubleshooting


Problem

This article contains sample code which demonstrates how to pass a polygon as an argument to a SELECT statement in order to find all the rows for this polygon from a table using the spatial datablade JAVA API.

Resolving The Problem


This example program has been tested with IBM Informix Dynamic Server, Version 9.40, IBM Informix JDBC Driver, Version 3.00.JC3, and the IBM Informix Spatial DataBlade module, Version 8.20.

The "nz" table was created using the following SQL statement:

   CREATE TABLE nz (id integer, shape st_geometry);


Sample code:

import java.sql.*;
import java.io.*;
import com.ibm.spatial.geom.Geometry;
import com.ibm.spatial.srs.CoordRefManager;
import com.ibm.spatial.SpatialException;
import com.ibm.spatial.io.IfxSQLData;
import com.ibm.spatial.geom.*;
import com.ibm.spatial.srs.*;
import com.ibm.spatial.*;
import com.informix.jdbc.*;


public class spatest
{

 public static void main(String[] args) throws SpatialException, ClassNotFoundException
 {
   try {

          // load the Informix JDBC driver & get the connection
    Class.forName("com.informix.jdbc.IfxDriver");
    String url = "";  // set to correct url, something like

                         // "jdbc:informix-sqli://myhost:1526/proddb:INFORMIX
                         // SERVER=prod_tcp;user=informix;password=informix";
    Connection conn = DriverManager.getConnection (url);

          // get the custom type map associated with the spatial data types
    java.util.Map typeMap=IfxSQLData.enableTypes(conn);


          // set the CoordRefManager connection
    CoordRefManager crm=CoordRefManager.getInstance();
    crm.setConnection(conn);

    CoordRef cr = new CoordRef();
    GeometryFactory gf=GeometryFactory.getInstance(cr);

          // define the SQL query
    String query = "";

          String tableName="nz";
          query += "SELECT id, shape::st_geometry" + " ";
         query += "FROM " + tableName + " ";
         query += "WHERE ST_CONTAINS (shape, ST_PolyFromText (?, ?))";

    PreparedStatement pstmt = conn.prepareStatement(query);

         // Create Polygon

          double[] xy = {1,1, 2,2, 1,2, 1,1};
         double[] z = null;

          double[] m = null;
          int[] partOffsets = null;
          int[] subPartOffsets = {0, 4};
         Geometry geo = gf.createGeometry("Polygon", xy, z, m, partOffsets, subPartOffsets);

         String geo_string = geo.asText();
         String srid = "1";

         pstmt.setString(1, geo_string);
         pstmt.setString(2, srid);

          // run a query and fetch the spatial data
         ResultSet rs = pstmt.executeQuery();

    while (rs.next()) {
Integer id = (Integer)rs.getObject(1);
Geometry g = (Geometry) rs.getObject(2, typeMap);
if (g == null) {
      System.out.println("NULL");
      continue;
}
System.out.println("ID:" + id);
System.out.println("polygon:" + g.toString());
IfxGeometry dbgeo = (IfxGeometry)g;
        /* ... */
    }
    pstmt.close();
}

catch (ClassNotFoundException e) {                

          System.out.println("++++++" + e); }
 
catch (SQLException e) {
    System.out.println("2++++++" + e.getErrorCode() + ":" + e.getMessage());
    e.printStackTrace();
}
catch (Exception e) {
        System.out.println("ERROR: failed to load Informix JDBC driver.");
   e.printStackTrace();
   return;
}
}

}


Preparing to Run this Program:

Before you run this spatial Java API program, set your CLASSPATH environment variable to include:

- The IBM Informix JDBC Driver, ifxjdbc.jar, in the directory where you installed the driver.
- The Spatial DataBlade Java API, spatial.jar, in the directory where you installed the DataBlade module, $INFORMIXDIR/extend/spatial.version.

[{"Product":{"code":"SSAVH9","label":"Informix Spatial DataBlade"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"Informix Spatial DataBlade Module","Platform":[{"code":"PF002","label":"AIX"},{"code":"PF025","label":"Platform Independent"},{"code":"PF008","label":"DYNIX\/ptx"},{"code":"PF010","label":"HP-UX"},{"code":"PF015","label":"IRIX"},{"code":"PF016","label":"Linux"},{"code":"PF027","label":"Solaris"},{"code":"PF033","label":"Windows"}],"Version":"8.2","Edition":"","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Document Information

Modified date:
06 April 2023

UID

swg21254380