GeometryToWKT.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Title: GeometryToWKT.java 1.00 2002/05/14
  3. *
  4. * Licensed Materials - Property of IBM and/or HCL
  5. * IBM Informix Spatial DataBlade
  6. * (c) Copyright IBM Corporation 2002 All rights reserved.
  7. * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved.
  8. *
  9. * US Government Users Restricted Rights - Use, duplication or
  10. * disclosure restricted by GSA ADP Schedule Contract with
  11. * IBM Corp.
  12. */
  13. import java.sql.*;
  14. import java.util.*;
  15. import com.ibm.spatial.geom.Geometry;
  16. import com.ibm.spatial.srs.CoordRefManager;
  17. import com.ibm.spatial.SpatialException;
  18. import com.ibm.spatial.io.IfxSQLData;
  19. public class GeometryToWKT {
  20. static String url = null;
  21. Connection conn = null;
  22. Map typeMap = null;
  23. public GeometryToWKT() throws ClassNotFoundException {
  24. // Loading driver
  25. System.out.print("Loading JDBC driver...");
  26. Class.forName("com.informix.jdbc.IfxDriver");
  27. System.out.println("ok");
  28. }
  29. public static void main(String[] args) {
  30. try {
  31. GeometryToWKT app = new GeometryToWKT();
  32. app.init(args);
  33. app.doRun();
  34. app.cleanup();
  35. } catch (Exception e) {
  36. System.out.println("ERROR: " + e.getMessage() + "\n");
  37. e.printStackTrace();
  38. }
  39. }
  40. void doRun() throws SQLException, SpatialException {
  41. /**
  42. * Set up a CoordRefManager database connection. This connection
  43. * will be used to retrieve spatial reference data associated
  44. * with the geometries we're going to read.
  45. */
  46. CoordRefManager.getInstance().setConnection(conn);
  47. /**
  48. * Before retrieving geometry user-defined types, we have to
  49. * associate a JDBC custom typemap to the current connection.
  50. */
  51. System.out.println("Setting up typemap...");
  52. typeMap = IfxSQLData.enableTypes(conn);
  53. System.out.println("enableTypes...ok");
  54. System.out.println();
  55. // Run the query
  56. System.out.println("Displaying well-known text...");
  57. Statement stmt = conn.createStatement();
  58. ResultSet rs = stmt.executeQuery("SELECT id, geom FROM geom_t where id >= 0");
  59. while (rs.next()) {
  60. Integer id = (Integer)rs.getObject(1);
  61. Geometry geo = (Geometry) rs.getObject(2, typeMap);
  62. if (geo == null) {
  63. System.out.println("NULL");
  64. continue;
  65. }
  66. // Print out the well-known text representation
  67. System.out.println(geo.asText());
  68. }
  69. stmt.close();
  70. }
  71. // Creates the geometry table and inserts sample data.
  72. void init(String args[]) throws SQLException, SpatialException {
  73. if (args.length == 0) {
  74. if (url == null || url.length() == 0) {
  75. throw new RuntimeException(
  76. "Connection URL must be provided in " +
  77. "order to run this sample program!");
  78. }
  79. } else {
  80. url = args[0];
  81. }
  82. // Getting connection
  83. System.out.print("Getting connection...");
  84. conn = DriverManager.getConnection(url);
  85. System.out.println("ok\n");
  86. // Setting up the custom typemap
  87. System.out.println("Setting up typemap...");
  88. typeMap = IfxSQLData.enableTypes(conn);
  89. System.out.println("enableTypes...ok");
  90. System.out.println();
  91. // Drop the test table
  92. PreparedStatement pstmt;
  93. try {
  94. pstmt = conn.prepareStatement("drop table geom_t");
  95. pstmt.executeUpdate();
  96. } catch (SQLException e) {
  97. // ignore error when the table does not exist
  98. System.out.println("Table does not exist");
  99. }
  100. // Creating a table and inserting data
  101. System.out.println("Creating table geom_t (id INT, geom ST_Geometry), " +
  102. "and inserting data ...");
  103. pstmt = conn.prepareStatement(
  104. "CREATE TABLE geom_t (id INT, geom ST_Geometry)");
  105. pstmt.executeUpdate();
  106. System.out.println("Inserting values...");
  107. pstmt = conn.prepareStatement(
  108. "INSERT INTO geom_t VALUES(0, NULL::ST_Geometry)");
  109. pstmt.executeUpdate();
  110. pstmt = conn.prepareStatement(
  111. "INSERT INTO geom_t VALUES(1, '0 point (-100.0 37.5)')");
  112. pstmt.executeUpdate();
  113. pstmt = conn.prepareStatement(
  114. "INSERT INTO geom_t VALUES(2, '0 polygon ((-100 37.5,-100 38.8,-99 39,-99 37.5,-100 37.5))')");
  115. pstmt.executeUpdate();
  116. }
  117. void cleanup() throws SQLException {
  118. // Drop the test table
  119. try {
  120. System.out.println("Dropping table geom_t ...");
  121. PreparedStatement pstmt = conn.prepareStatement("drop table geom_t");
  122. pstmt.executeUpdate();
  123. } catch (SQLException e) {
  124. // ignore error when the table does not exist
  125. System.out.println("Table does not exist");
  126. }
  127. // Closing connection
  128. System.out.print("Closing connection...");
  129. conn.close();
  130. System.out.println("ok");
  131. }
  132. }