GeometryToArray.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Title: GeometryToArray.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 GeometryToArray {
  19. static String url = null;
  20. Connection conn = null;
  21. CoordRefManager crm = null;
  22. public GeometryToArray() throws ClassNotFoundException {
  23. // Loading 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. GeometryToArray app = new GeometryToArray();
  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. /**
  40. * Creates several geometries using a factory, then reads coordinate
  41. * data out.
  42. */
  43. void doRun() throws SpatialException, ClassNotFoundException {
  44. // Creating a factory object
  45. System.out.println("Initializing the factory ...");
  46. CoordRef cr = crm.get(100);
  47. GeometryFactory factory = GeometryFactory.getInstance(cr);
  48. // Creating a point
  49. System.out.println("\nCreating a Point geometry ...");
  50. Geometry geo = factory.createPoint(100, 100);
  51. System.out.println(geo.toString());
  52. /**
  53. * Reading coordinate data using IfxGeometry.toCoordArray() and
  54. * IfxGeometry.toPointArray().
  55. */
  56. System.out.println("Reading XY coordinate data using IfxGeometry.toCoordArray()" +
  57. " and IfxGeometry.toPointArray() ...");
  58. System.out.println("COORD_XY : " + dumpCoordsXY(geo));
  59. System.out.println("Points : " + dumpPoints(geo));
  60. // Creating a multipolygon
  61. System.out.println("\nCreating a MultiPolygon zm geometry...");
  62. double[] xy = {0, 0, 3, 7, 10, 10, 7, 3, 0, 0, 1, 1, 4, 6, 9, 9, 6, 4, 1, 1,
  63. 20, 20, 23, 27, 30, 30, 27, 23, 20, 20};
  64. double[] z = {0, 5, 10, 5, 0, 1, 4, 9, 4, 1, 0, 5, 10, 5, 0};
  65. double[] m = {0, 5, 10, 5, 0, 1, 4, 9, 4, 1, 0, 5, 10, 5, 0};
  66. int[] partOffsets = {0, 2, 3};
  67. int[] subPartOffsets = {0, 5, 10, 15};
  68. geo = factory.createGeometry("MultiPolygon", xy, z, m, partOffsets,
  69. subPartOffsets);
  70. System.out.println(geo.toString());
  71. /**
  72. * Reading coordinate data using IfxGeometry.toCoordArray() and
  73. * IfxGeometry.toPointArray().
  74. */
  75. System.out.println("Reading XY coordinate data using IfxGeometry.toCoordArray()" +
  76. " and IfxGeometry.toPointArray() ...");
  77. System.out.println("COORD_XY : " + dumpCoordsXY(geo));
  78. System.out.println("Points : " + dumpPoints(geo));
  79. }
  80. /**
  81. * Read coordinate data using toCoordArray().
  82. */
  83. StringBuffer dumpCoordsXY(Geometry g) {
  84. IfxGeometry geo = (IfxGeometry)g;
  85. double buf[] = new double[10];
  86. StringBuffer sb = new StringBuffer(1000);
  87. // For each part and subpart, read coordinates into buf
  88. for(int part = 0; part < geo.numParts(); part++) {
  89. for(int subpart = 0; subpart < geo.numSubParts(part); subpart++) {
  90. for (int position = 0, points = geo.numPoints(part, subpart), read = 0;
  91. points > 0;
  92. points -= read) {
  93. read = geo.toCoordArray(buf, 0, IfxGeometry.COORD_XY,
  94. position, part, subpart);
  95. if (read == 0) break;
  96. // Write coordinates into string buffer
  97. for (int i = 0; i < read; i++) {
  98. sb.append(buf[i * 2]).append(' ').append(buf[i*2+1]);
  99. sb.append(',');
  100. }
  101. // Update position of the next point to read
  102. position += read;
  103. }
  104. }
  105. }
  106. return sb;
  107. }
  108. /**
  109. * Reads coordinate data using toPointArray().
  110. */
  111. StringBuffer dumpPoints(Geometry g) {
  112. IfxGeometry geo = (IfxGeometry)g;
  113. StringBuffer sb = new StringBuffer(1000);
  114. CoordPoint buf[] = new CoordPoint.Double[10];
  115. for (int i = 0; i < buf.length; i++)
  116. buf[i] = new CoordPoint.Double();
  117. for(int part = 0; part < geo.numParts(); part++) {
  118. for(int subpart = 0; subpart < geo.numSubParts(part); subpart++) {
  119. for (int position = 0, points = geo.numPoints(part, subpart), read = 0;
  120. points > 0;
  121. points -= read) {
  122. read = geo.toPointArray(buf, 0, position, part, subpart);
  123. if (read == 0) break;
  124. // Write point coordinates into string buffer
  125. for (int i = 0; i < read; i++)
  126. sb.append(buf[i].getX()).append(' ').append(buf[i].getY()).append(',');
  127. // Update position of the next point to read
  128. position += read;
  129. }
  130. }
  131. }
  132. return sb;
  133. }
  134. /**
  135. * Creates a spatial reference with srid=100 and inserts it into
  136. * the SPATIAL_REFERENCES table.
  137. */
  138. void init(String args[]) throws SQLException, SpatialException {
  139. if (args.length == 0) {
  140. if (url == null || url.length() == 0) {
  141. throw new RuntimeException(
  142. "Connection URL must be provided in " +
  143. "order to run this sample program!");
  144. }
  145. } else {
  146. url = args[0];
  147. }
  148. // Getting connection
  149. System.out.print("Getting connection...");
  150. conn = DriverManager.getConnection(url);
  151. System.out.println("ok\n");
  152. // Delete the SRS with srid=100
  153. PreparedStatement pstmt;
  154. try {
  155. pstmt = conn.prepareStatement("delete from spatial_references where srid=100");
  156. pstmt.executeUpdate();
  157. } catch (SQLException e) {
  158. // ignore error
  159. System.out.println("SRS with srid=100 does not exist");
  160. }
  161. // Insert a new SRS
  162. System.out.println("Inserting a SRS with srid=100 ...");
  163. pstmt = conn.prepareStatement(
  164. "insert into sde.spatial_references values" +
  165. "(100,NULL,NULL,NULL,-1000.0,-1000.0,1000,-1000.0,1000,-1000.0,1000,'UNKNOWN')");
  166. pstmt.executeUpdate();
  167. // Setting the CoordRefManager connection
  168. crm = CoordRefManager.getInstance();
  169. crm.setConnection(conn);
  170. crm.refresh();
  171. }
  172. void cleanup() throws SQLException {
  173. // Delete the SRS with srid=100
  174. PreparedStatement pstmt;
  175. System.out.println("Deleting the SRS with srid=100 ...");
  176. try {
  177. pstmt = conn.prepareStatement("delete from spatial_references where srid=100");
  178. pstmt.executeUpdate();
  179. } catch (SQLException e) {
  180. // ignore error
  181. System.out.println("SRS with srid=100 does not exist");
  182. }
  183. // Closing connection
  184. System.out.print("Closing connection...");
  185. conn.close();
  186. System.out.println("ok");
  187. }
  188. }