123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- 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 {
-
- 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 {
-
- CoordRefManager.getInstance().setConnection(conn);
-
- System.out.println("Setting up typemap...");
- typeMap = IfxSQLData.enableTypes(conn);
- System.out.println("enableTypes...ok");
- System.out.println();
-
- 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;
- }
-
- System.out.println(geo.asText());
- }
- stmt.close();
- }
-
- 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];
- }
-
- System.out.print("Getting connection...");
- conn = DriverManager.getConnection(url);
- System.out.println("ok\n");
-
- System.out.println("Setting up typemap...");
- typeMap = IfxSQLData.enableTypes(conn);
- System.out.println("enableTypes...ok");
- System.out.println();
-
- PreparedStatement pstmt;
- try {
- pstmt = conn.prepareStatement("drop table geom_t");
- pstmt.executeUpdate();
- } catch (SQLException e) {
-
- System.out.println("Table does not exist");
- }
-
- 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 {
-
- try {
- System.out.println("Dropping table geom_t ...");
- PreparedStatement pstmt = conn.prepareStatement("drop table geom_t");
- pstmt.executeUpdate();
- } catch (SQLException e) {
-
- System.out.println("Table does not exist");
- }
-
- System.out.print("Closing connection...");
- conn.close();
- System.out.println("ok");
- }
- }
|