asbinary.ec 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**************************************************************************
  2. *
  3. * asbinary.ec
  4. *
  5. * This program demonstrates how to retrieve spatial data from a database
  6. * in OGIS well-known-binary data format using the Informix Spatial
  7. * DataBlade's ST_AsBinary function.
  8. *
  9. * The following command will likely compile this source:
  10. * esql -g -o asbinary asbinary.ec
  11. *
  12. **************************************************************************/
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <float.h>
  17. EXEC SQL include sqltypes;
  18. EXEC SQL include exp_chk.ec;
  19. /*
  20. * Constants, macros, typedefs, data structures, & functions for
  21. * processing OGIS well-known-binary format data.
  22. */
  23. #include "commfuncs.h"
  24. #include "commfuncs.c"
  25. #include "wkbfuncs.c"
  26. /*
  27. * Local function prototypes
  28. *
  29. */
  30. void draw_polygon (char *wkb_buf);
  31. /*
  32. * Main program
  33. *
  34. */
  35. void main (int argc, char **argv)
  36. {
  37. /* Declare local host variables */
  38. EXEC SQL BEGIN DECLARE SECTION;
  39. int i, n, xid;
  40. short typ;
  41. char dbname[256];
  42. char sql_stmt[200];
  43. var binary query_wkb;
  44. var binary 'st_geometry' fetched_wkb;
  45. EXEC SQL END DECLARE SECTION;
  46. /* Declare other local variables */
  47. int rc;
  48. Geometry geom = {0};
  49. Point pt[30];
  50. int suboffsets[5];
  51. int max_alloced = 0;
  52. char *fetched_wkb_buf;
  53. int fetched_wkb_len;
  54. char *query_wkb_buf;
  55. int query_wkb_len;
  56. int srid;
  57. /* Check for the correct number of arguments entered. */
  58. if (argc < 2)
  59. {
  60. printf ("\nUsage: %s <database> [<srid>]\n", argv[0]);
  61. exit (1);
  62. }
  63. /* Define exception handling routine for EXEC SQL statements */
  64. EXEC SQL whenever sqlerror CALL ignore206;
  65. /* Connect to the database. */
  66. sprintf(dbname, "%s", argv[1]);
  67. EXEC SQL connect to :dbname;
  68. /* Obtain srid to use for inserts */
  69. srid = (argc > 2) ? atoi(argv[2]) : 0;
  70. EXEC SQL DROP TABLE sensitive_areas;
  71. EXEC SQL CREATE TABLE sensitive_areas (id integer, zone ST_Polygon);
  72. sprintf(sql_stmt,
  73. "INSERT INTO sensitive_areas VALUES(1, "
  74. "'%d polygon((22 45,22 58,28 58,28 42,25 42,25 45,22 45))')",srid);
  75. EXEC SQL execute immediate :sql_stmt;
  76. sprintf(sql_stmt,
  77. "INSERT INTO sensitive_areas VALUES(2, "
  78. "'%d polygon((22 18,28 18,28 12,22 12,22 18))')", srid);
  79. EXEC SQL execute immediate :sql_stmt;
  80. sprintf(sql_stmt,
  81. "INSERT INTO sensitive_areas VALUES(3, "
  82. "'%d polygon((42 18,48 18,48 13,42 13,42 18))')", srid);
  83. EXEC SQL execute immediate :sql_stmt;
  84. /* Populate the query polygon point array */
  85. suboffsets[0] = 0;
  86. pt[0].x = 10; pt[0].y = 10;
  87. pt[1].x = 25; pt[1].y = 10;
  88. pt[2].x = 25; pt[2].y = 20;
  89. pt[3].x = 10; pt[3].y = 20;
  90. pt[4].x = 10; pt[4].y = 10;
  91. geom.type = geomPolygon;
  92. geom.num_points = 5;
  93. geom.num_parts = 1;
  94. geom.num_subparts = 1;
  95. geom.suboffsets = suboffsets;
  96. geom.pt = pt;
  97. /* Convert the points to a WKB representation of a polygon. */
  98. geom_to_wkb (&geom, &max_alloced, &query_wkb_len, &query_wkb_buf);
  99. /* Create host variable for WKB representation */
  100. if ((rc = ifx_var_alloc(&query_wkb,query_wkb_len)) < 0)
  101. {
  102. fprintf(stderr, "Error calling ifx_var_alloc.");
  103. exit(1);
  104. }
  105. if ((rc = ifx_var_setdata(&query_wkb,query_wkb_buf,query_wkb_len)) < 0)
  106. {
  107. fprintf(stderr, "Error calling ifx_var_setdata.");
  108. exit(1);
  109. }
  110. if ((rc = ifx_var_setlen(&query_wkb,query_wkb_len)) < 0)
  111. {
  112. fprintf(stderr, "Error calling ifx_var_setlen.");
  113. exit(1);
  114. }
  115. /* Create the SELECT statement. */
  116. sprintf(sql_stmt,
  117. "SELECT ST_AsBinary(zone) "
  118. "FROM sensitive_areas WHERE "
  119. "SE_EnvelopesIntersect(zone,ST_PolyFromWKB(?,%d))", srid);
  120. /* Prepare the SELECT statement. */
  121. EXEC SQL prepare sel_stmt from :sql_stmt;
  122. /* Declare cursor for the SELECT statement. */
  123. EXEC SQL declare sel_curs cursor for sel_stmt;
  124. /* Allocate a statement descriptor. */
  125. EXEC SQL allocate descriptor 'sel_desc';
  126. /* Set the number of input parameters */
  127. n = 1;
  128. EXEC SQL set descriptor 'sel_desc' COUNT = :n;
  129. /* Bind the query WKB to the input parameter */
  130. typ = SQLUDTVAR;
  131. xid = XID_LVARCHAR;
  132. EXEC SQL set descriptor 'sel_desc' VALUE :n
  133. TYPE = :typ,
  134. EXTYPEID = :xid,
  135. DATA = :query_wkb;
  136. /* Open cursor for the SELECT statement. */
  137. EXEC SQL open sel_curs using sql descriptor 'sel_desc';
  138. /* Fetch each polygon within the display window and display it. */
  139. while (1)
  140. {
  141. EXEC SQL fetch sel_curs into :fetched_wkb;
  142. if (sqlca.sqlcode == SQLNOTFOUND)
  143. break;
  144. printf("Fetched row.\n");
  145. if ((fetched_wkb_len = ifx_var_getlen(&fetched_wkb)) == (int) NULL)
  146. {
  147. fprintf(stderr, "Error calling ifx_var_getlen.");
  148. exit(1);
  149. }
  150. if ((fetched_wkb_buf = ifx_var_getdata(&fetched_wkb)) == NULL)
  151. {
  152. fprintf(stderr, "Error calling ifx_var_getdata.");
  153. exit(1);
  154. }
  155. draw_polygon(fetched_wkb_buf);
  156. if ((rc = ifx_var_dealloc(&fetched_wkb)) < 0)
  157. {
  158. fprintf(stderr, "Error calling ifx_var_dealloc.");
  159. exit(1);
  160. }
  161. }
  162. /* Close the result set cursor */
  163. EXEC SQL close sel_curs;
  164. /* Deallocate host variable for WKB representation */
  165. if ((rc = ifx_var_dealloc(&query_wkb)) < 0)
  166. {
  167. fprintf(stderr, "Error calling ifx_var_dealloc.");
  168. exit(1);
  169. }
  170. /* Free resources associated with SELECT statement */
  171. EXEC SQL deallocate descriptor 'sel_desc';
  172. EXEC SQL free sel_curs;
  173. EXEC SQL free sel_stmt;
  174. /* Disconnect from the database */
  175. EXEC SQL disconnect current;
  176. printf( "\nTest Complete\n");
  177. }
  178. /*
  179. * Local function draw_polygon
  180. *
  181. */
  182. void draw_polygon (char *wkb_buf)
  183. {
  184. Geometry geom = {0};
  185. /* Convert the incoming well-known binary byte stream
  186. * into a Geometry data structure. */
  187. wkb_to_geom (wkb_buf, &geom);
  188. /* The remainder of this function is left as an exercise for the reader */
  189. }