geomfromwkb.ec 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**************************************************************************
  2. *
  3. * geomfromwkb.ec
  4. *
  5. * This program demonstrates how to insert spatial data into a database
  6. * in OGIS well-known binary format using the Informix Spatial DataBlade's
  7. * ST_GeomFromWkb function.
  8. *
  9. * This program creates a table and inserts several polygons into it.
  10. * For an example program which shows how to insert all spatial types,
  11. * please see load_wkb.ec
  12. *
  13. * The following command will likely compile this source:
  14. * esql -g -o geomfromwkb geomfromwkb.ec
  15. *
  16. **************************************************************************/
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <float.h>
  21. EXEC SQL include sqltypes;
  22. EXEC SQL include exp_chk.ec;
  23. /*
  24. * Constants, macros, typedefs, data structures, & functions for
  25. * processing OGIS well-known-binary format data.
  26. */
  27. #include "commfuncs.h"
  28. #include "commfuncs.c"
  29. #include "wkbfuncs.c"
  30. /*
  31. * Main program
  32. *
  33. */
  34. void main (int argc, char **argv)
  35. {
  36. /* Declare local host variables */
  37. EXEC SQL BEGIN DECLARE SECTION;
  38. int n, xid;
  39. short typ;
  40. char dbname[256];
  41. char sql_stmt[200];
  42. int lot_id;
  43. var binary lot_wkb;
  44. EXEC SQL END DECLARE SECTION;
  45. /* Declare other local variables */
  46. int rc;
  47. Geometry geom = {0};
  48. Point pt[30];
  49. int suboffsets[5];
  50. int max_alloced = 0;
  51. char *lot_wkb_buf;
  52. int lot_wkb_len;
  53. int srid;
  54. /* Check for the correct number of arguments entered. */
  55. if (argc < 2)
  56. {
  57. printf ("\nUsage: %s <database> [<srid>]\n", argv[0]);
  58. exit (1);
  59. }
  60. /* Define exception handling routine for EXEC SQL statements */
  61. EXEC SQL whenever sqlerror CALL ignore206;
  62. /* Connect to the database. */
  63. sprintf(dbname, "%s", argv[1]);
  64. EXEC SQL connect to :dbname;
  65. /* Obtain srid to use for inserts */
  66. srid = (argc > 2) ? atoi(argv[2]) : 0;
  67. EXEC SQL DROP TABLE lots;
  68. EXEC SQL CREATE TABLE lots (lot_id integer, lot ST_Geometry);
  69. /* Initialize entries for non-spatial columns in the table */
  70. lot_id = 1010;
  71. /* Populate the polygon point array */
  72. suboffsets[0] = 0;
  73. pt[0].x = 2; pt[0].y = 57;
  74. pt[1].x = 21.5; pt[1].y = 57;
  75. pt[2].x = 21.5; pt[2].y = 38;
  76. pt[3].x = 2; pt[3].y = 38;
  77. pt[4].x = 2; pt[4].y = 57;
  78. geom.type = geomPolygon;
  79. geom.num_points = 5;
  80. geom.num_parts = 1;
  81. geom.num_subparts = 1;
  82. geom.suboffsets = suboffsets;
  83. geom.pt = pt;
  84. /* Convert the points to a Well Known Binary
  85. * representation of a polygon. */
  86. geom_to_wkb (&geom, &max_alloced, &lot_wkb_len, &lot_wkb_buf);
  87. /* Create the SQL insert statement to populate the lots
  88. * table. The question marks are parameter markers that indicate
  89. * the column values that will be inserted at run time. */
  90. sprintf(sql_stmt,
  91. "INSERT INTO lots (lot_id, lot) "
  92. "VALUES(?, ST_GeomFromWKB(?, %d))", srid);
  93. /* Prepare the SQL statement for execution. */
  94. EXEC SQL prepare ins_stmt from :sql_stmt;
  95. /* Allocate a statement descriptor. */
  96. EXEC SQL allocate descriptor 'ins_desc';
  97. /* Set the number of input parameters */
  98. n = 2;
  99. EXEC SQL set descriptor 'ins_desc' COUNT = :n;
  100. /* Bind the input id to the first input parameter */
  101. n = 1; typ = SQLINT;
  102. EXEC SQL set descriptor 'ins_desc' VALUE :n
  103. TYPE = :typ,
  104. DATA = :lot_id;
  105. /* Create host variable for WKB representation */
  106. if ((rc = ifx_var_alloc(&lot_wkb,lot_wkb_len)) < 0)
  107. {
  108. fprintf(stderr, "Error calling ifx_var_alloc.");
  109. exit(1);
  110. }
  111. if ((rc = ifx_var_setdata(&lot_wkb,lot_wkb_buf,lot_wkb_len)) < 0)
  112. {
  113. fprintf(stderr, "Error calling ifx_var_setdata.");
  114. exit(1);
  115. }
  116. if ((rc = ifx_var_setlen(&lot_wkb,lot_wkb_len)) < 0)
  117. {
  118. fprintf(stderr, "Error calling ifx_var_setlen.");
  119. exit(1);
  120. }
  121. /* Bind the WKB to the second input parameter */
  122. n = 2; typ = SQLUDTVAR; xid = XID_LVARCHAR;
  123. EXEC SQL set descriptor 'ins_desc' VALUE :n
  124. TYPE = :typ,
  125. EXTYPEID = :xid,
  126. DATA = :lot_wkb;
  127. /* Execute the insert statement. */
  128. EXEC SQL execute ins_stmt using sql descriptor 'ins_desc';
  129. /* Free resources associated with the WKB host variable */
  130. if ((rc = ifx_var_dealloc(&lot_wkb)) < 0)
  131. {
  132. fprintf(stderr, "Error calling ifx_var_dealloc.");
  133. exit(1);
  134. }
  135. /* Free resources associated with INSERT statement */
  136. EXEC SQL deallocate descriptor 'ins_desc';
  137. EXEC SQL free ins_stmt;
  138. /* Disconnect from the database */
  139. EXEC SQL disconnect current;
  140. printf( "\nTest Complete\n");
  141. }