/* * Title: GeometryToWKT.java 1.00 2002/05/14 * * Licensed Materials - Property of IBM and/or HCL * IBM Informix Spatial DataBlade * (c) Copyright IBM Corporation 2002 All rights reserved. * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved. * * US Government Users Restricted Rights - Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with * IBM Corp. */ import java.sql.*; import java.util.*; import com.ibm.spatial.geom.Geometry; import com.ibm.spatial.srs.CoordRefManager; import com.ibm.spatial.SpatialException; import com.ibm.spatial.io.IfxSQLData; public class GeometryToWKT { static String url = null; Connection conn = null; Map typeMap = null; public GeometryToWKT() throws ClassNotFoundException { // Loading driver System.out.print("Loading JDBC driver..."); Class.forName("com.informix.jdbc.IfxDriver"); System.out.println("ok"); } public static void main(String[] args) { try { GeometryToWKT app = new GeometryToWKT(); app.init(args); app.doRun(); app.cleanup(); } catch (Exception e) { System.out.println("ERROR: " + e.getMessage() + "\n"); e.printStackTrace(); } } void doRun() throws SQLException, SpatialException { /** * Set up a CoordRefManager database connection. This connection * will be used to retrieve spatial reference data associated * with the geometries we're going to read. */ CoordRefManager.getInstance().setConnection(conn); /** * Before retrieving geometry user-defined types, we have to * associate a JDBC custom typemap to the current connection. */ System.out.println("Setting up typemap..."); typeMap = IfxSQLData.enableTypes(conn); System.out.println("enableTypes...ok"); System.out.println(); // Run the query System.out.println("Displaying well-known text..."); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT id, geom FROM geom_t where id >= 0"); while (rs.next()) { Integer id = (Integer)rs.getObject(1); Geometry geo = (Geometry) rs.getObject(2, typeMap); if (geo == null) { System.out.println("NULL"); continue; } // Print out the well-known text representation System.out.println(geo.asText()); } stmt.close(); } // Creates the geometry table and inserts sample data. void init(String args[]) throws SQLException, SpatialException { if (args.length == 0) { if (url == null || url.length() == 0) { throw new RuntimeException( "Connection URL must be provided in " + "order to run this sample program!"); } } else { url = args[0]; } // Getting connection System.out.print("Getting connection..."); conn = DriverManager.getConnection(url); System.out.println("ok\n"); // Setting up the custom typemap System.out.println("Setting up typemap..."); typeMap = IfxSQLData.enableTypes(conn); System.out.println("enableTypes...ok"); System.out.println(); // Drop the test table PreparedStatement pstmt; try { pstmt = conn.prepareStatement("drop table geom_t"); pstmt.executeUpdate(); } catch (SQLException e) { // ignore error when the table does not exist System.out.println("Table does not exist"); } // Creating a table and inserting data System.out.println("Creating table geom_t (id INT, geom ST_Geometry), " + "and inserting data ..."); pstmt = conn.prepareStatement( "CREATE TABLE geom_t (id INT, geom ST_Geometry)"); pstmt.executeUpdate(); System.out.println("Inserting values..."); pstmt = conn.prepareStatement( "INSERT INTO geom_t VALUES(0, NULL::ST_Geometry)"); pstmt.executeUpdate(); pstmt = conn.prepareStatement( "INSERT INTO geom_t VALUES(1, '0 point (-100.0 37.5)')"); pstmt.executeUpdate(); pstmt = conn.prepareStatement( "INSERT INTO geom_t VALUES(2, '0 polygon ((-100 37.5,-100 38.8,-99 39,-99 37.5,-100 37.5))')"); pstmt.executeUpdate(); } void cleanup() throws SQLException { // Drop the test table try { System.out.println("Dropping table geom_t ..."); PreparedStatement pstmt = conn.prepareStatement("drop table geom_t"); pstmt.executeUpdate(); } catch (SQLException e) { // ignore error when the table does not exist System.out.println("Table does not exist"); } // Closing connection System.out.print("Closing connection..."); conn.close(); System.out.println("ok"); } }