asshape.ec 6.0 KB

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