CoordRefCreate.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Title: CoordRefCreate.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.*;
  16. import com.ibm.spatial.srs.*;
  17. import com.ibm.spatial.*;
  18. public class CoordRefCreate {
  19. static String url = null;
  20. Connection conn = null;
  21. CoordRefManager crm = null;
  22. public CoordRefCreate() throws ClassNotFoundException {
  23. // Loading JDBC driver
  24. System.out.print("Loading JDBC driver...");
  25. Class.forName("com.informix.jdbc.IfxDriver");
  26. System.out.println("ok");
  27. }
  28. public static void main(String[] args) {
  29. try {
  30. CoordRefCreate app = new CoordRefCreate();
  31. app.init(args);
  32. app.doRun();
  33. app.cleanup();
  34. } catch (Exception e) {
  35. System.out.println("ERROR: " + e.getMessage() + "\n");
  36. e.printStackTrace();
  37. }
  38. }
  39. void doRun() throws SpatialException, ClassNotFoundException {
  40. /**
  41. * Set up a CoordRefManager database connection. This connection
  42. * will be used to retrieve spatial reference data associated
  43. * with the geometries we're going to read.
  44. */
  45. crm = CoordRefManager.getInstance();
  46. crm.setConnection(conn);
  47. /**
  48. * Search the database for a coordinate reference with
  49. * the authoritative name/id: "EPSG:4326"
  50. */
  51. System.out.println("Search database for all the spatial references " +
  52. "identified by 'EPSG:4326'");
  53. CoordRef epsg4326 = null;
  54. for (Enumeration e = crm.findByAuth("EPSG", new Integer(4326));
  55. e.hasMoreElements(); ) {
  56. epsg4326 = (CoordRef) e.nextElement();
  57. System.out.println(epsg4326);
  58. }
  59. /**
  60. * Note that we should not use epsg4326 and associate it to a
  61. * geometry object, as this is not stored in the CoordRefManager
  62. * "in-use" cache. Instead, we should use the object returned by
  63. * the get() method.
  64. */
  65. CoordRef cr4326 = (epsg4326 != null) ? crm.get(epsg4326.getSrid()) : null;
  66. /**
  67. * This object is read-only: calling any of the set methods will
  68. * result in an exception.
  69. */
  70. if (cr4326 != null) {
  71. try {
  72. cr4326.setAuthName("Dummy");
  73. } catch (UnsupportedOperationException e) {
  74. System.out.println("Can't modify this coordinate reference");
  75. }
  76. }
  77. // Search for all the coordinate reference objects in the database
  78. System.out.println("\nGetting all the CoordRefs in the database ...");
  79. for (Enumeration allCrs = crm.find(new CoordRef(), 0);
  80. allCrs.hasMoreElements() ;) {
  81. CoordRef cr = (CoordRef) allCrs.nextElement();
  82. System.out.println("Srid = " + cr.getSrid() +
  83. " Description = " + cr.getDescription() +
  84. "\nSrtext = " + cr.getSrtext() + "\n");
  85. }
  86. /**
  87. * In order to create and store a new coordinate reference, we first
  88. * construct a new CoordRef object. This object is only a placeholder
  89. * for all the data that needs to be passed to the coordinate
  90. * reference manager. Do not associate it with geometry objects.
  91. */
  92. System.out.println("Creating a new CoordRef object ...");
  93. CoordRef cr = new CoordRef();
  94. cr.setXY(-100, -100, 100);
  95. cr.setAuthName("TEST");
  96. cr.setAuthSrid(new Integer(100));
  97. cr.setDescription("Spatial Java API Sample");
  98. cr.setSrtext("UNKNOWN");
  99. System.out.println("Source CoordRef:");
  100. System.out.println("Srid=" + cr.getSrid() +
  101. " Description= " + cr.getDescription() +
  102. "\nProjection= " + cr.getSrtext() + "\n");
  103. /**
  104. * We put the coordinate reference into the CoordRefManager object,
  105. * which will serialize it to the database.
  106. * The object returned is an immutable coordinate reference, which
  107. * may be associated with geometry objects (it is already stored in
  108. * the cache).
  109. */
  110. System.out.println("Storing the new CoordRef ...");
  111. CoordRef crNew = crm.put(cr);
  112. System.out.println("The srid of the new object is: " + crNew.getSrid());
  113. /**
  114. * The object returned by the previous operation is immutable:
  115. * calling any of the set methods will result in an exception.
  116. */
  117. try {
  118. crNew.setAuthName("Dummy");
  119. } catch (UnsupportedOperationException e) {
  120. System.out.println("Can't modify the returned coordinate reference");
  121. }
  122. /**
  123. * Remove the coordinate reference from the cache and database.
  124. */
  125. System.out.println("Deleting the coordinate reference with srid = " +
  126. crNew.getSrid());
  127. crm.remove(crNew.getSrid());
  128. }
  129. void init(String args[]) throws SQLException, SpatialException {
  130. if (args.length == 0) {
  131. if (url == null || url.length() == 0) {
  132. System.out.println("ERROR: connection URL must be provided in " +
  133. "order to run this sample program!");
  134. return;
  135. }
  136. } else {
  137. url = args[0];
  138. }
  139. // Getting connection
  140. System.out.print("Getting connection...");
  141. conn = DriverManager.getConnection(url);
  142. System.out.println("Ok\n");
  143. }
  144. void cleanup() throws SQLException {
  145. crm.setConnection(null);
  146. // Closing connection
  147. System.out.print("Closing connection...");
  148. conn.close();
  149. System.out.println("ok");
  150. }
  151. }