asbinary.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**************************************************************************
  2. *
  3. * asbinary.c
  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. *
  11. * cc -o asbinary asbinary.c \
  12. * -I$INFORMIXDIR/incl/cli -L$INFORMIXDIR/lib/cli -lifcli -lifdmr
  13. *
  14. **************************************************************************/
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <float.h>
  19. #ifdef WINNT
  20. #include <io.h>
  21. #include <windows.h>
  22. #include <conio.h>
  23. #endif
  24. #include <infxcli.h> /* ODBC typedefs and data structures */
  25. #include "odbcutils.c" /* ODBC utility functions */
  26. /*
  27. * Constants, macros, typedefs, data structures, & functions for
  28. * processing OGIS well-known-binary format data.
  29. */
  30. #include "commfuncs.h"
  31. #include "commfuncs.c"
  32. #include "wkbfuncs.c"
  33. /*
  34. * Local function prototypes
  35. */
  36. void draw_polygon (char *wkb_buf);
  37. void main (int argc, char **argv)
  38. {
  39. SQLHDBC hdbc;
  40. SQLHENV henv;
  41. SQLHSTMT hstmt;
  42. char sql_stmt[200];
  43. int rc;
  44. SDWORD pcbvalue1;
  45. Geometry geom = {0};
  46. Point pt[30];
  47. int suboffsets[5];
  48. int max_alloced = 0;
  49. char fetched_wkb_buf[10000];
  50. int fetched_wkb_len;
  51. char *query_wkb_buf;
  52. int query_wkb_len;
  53. int srid;
  54. /* Check for the correct number of arguments entered. */
  55. if (argc < 2)
  56. {
  57. printf ("Usage: %s <datasource> [<srid>]\n", argv[0]);
  58. exit (1);
  59. }
  60. /* Connect to the database. */
  61. server_connect ((UCHAR *) argv[1], &henv, &hdbc);
  62. /* Obtain srid to use for inserts */
  63. srid = (argc > 2) ? atoi(argv[2]) : 0;
  64. /* Allocate memory for the SQL statement handle and
  65. * associate the statement handle with the connection handle. */
  66. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt);
  67. returncode_check (hdbc, (SQLHSTMT)NULL, rc, "SQLAllocHandle");
  68. sprintf(sql_stmt,
  69. "DROP TABLE sensitive_areas");
  70. rc = SQLExecDirect (hstmt, (UCHAR *) sql_stmt, SQL_NTS);
  71. sprintf(sql_stmt,
  72. "CREATE TABLE sensitive_areas (id integer, zone ST_Polygon)");
  73. rc = SQLExecDirect (hstmt, (UCHAR *) sql_stmt, SQL_NTS);
  74. returncode_check (NULL, hstmt, rc, "SQLExecDirect");
  75. sprintf(sql_stmt,
  76. "INSERT INTO sensitive_areas VALUES(1, "
  77. "'%d polygon((22 45,22 58,28 58,28 42,25 42,25 45,22 45))')",srid);
  78. rc = SQLExecDirect (hstmt, (UCHAR *) sql_stmt, SQL_NTS);
  79. returncode_check (NULL, hstmt, rc, "SQLExecDirect");
  80. sprintf(sql_stmt,
  81. "INSERT INTO sensitive_areas VALUES(2, "
  82. "'%d polygon((22 18,28 18,28 12,22 12,22 18))')", srid);
  83. rc = SQLExecDirect (hstmt, (UCHAR *) sql_stmt, SQL_NTS);
  84. returncode_check (NULL, hstmt, rc, "SQLExecDirect");
  85. sprintf(sql_stmt,
  86. "INSERT INTO sensitive_areas VALUES(3, "
  87. "'%d polygon((42 18,48 18,48 13,42 13,42 18))')", srid);
  88. rc = SQLExecDirect (hstmt, (UCHAR *) sql_stmt, SQL_NTS);
  89. returncode_check (NULL, hstmt, rc, "SQLExecDirect");
  90. /* Populate the query polygon point array */
  91. suboffsets[0] = 0;
  92. pt[0].x = 10; pt[0].y = 10;
  93. pt[1].x = 25; pt[1].y = 10;
  94. pt[2].x = 25; pt[2].y = 20;
  95. pt[3].x = 10; pt[3].y = 20;
  96. pt[4].x = 10; pt[4].y = 10;
  97. geom.type = geomPolygon;
  98. geom.num_points = 5;
  99. geom.num_parts = 1;
  100. geom.num_subparts = 1;
  101. geom.suboffsets = suboffsets;
  102. geom.pt = pt;
  103. /* Convert the points to a Well Known Binary
  104. * representation of a polygon. */
  105. geom_to_wkb (&geom, &max_alloced, &query_wkb_len, &query_wkb_buf);
  106. /* INFORMIX_EXTEST_BEGIN asbinary */
  107. /* Create the SQL expression. */
  108. sprintf(sql_stmt,
  109. "SELECT ST_AsBinary(zone) "
  110. "FROM sensitive_areas WHERE "
  111. "SE_EnvelopesIntersect(zone,ST_PolyFromWKB(?,%d))", srid);
  112. /* Prepare the SQL statement. */
  113. SQLPrepare(hstmt, (UCHAR *) sql_stmt, SQL_NTS);
  114. returncode_check (NULL, hstmt, rc, "SQLPrepare");
  115. /* Bind the query shape parameter. */
  116. pcbvalue1 = query_wkb_len;
  117. SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
  118. SQL_INFX_UDT_LVARCHAR, query_wkb_len, 0,
  119. query_wkb_buf, query_wkb_len, &pcbvalue1);
  120. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  121. /* Execute the query. */
  122. rc = SQLExecute(hstmt);
  123. returncode_check (NULL, hstmt, rc, "SQLExecute");
  124. /* Assign the results of the query (the buildingfootprint polygons)
  125. * to the fetched_binary variable. */
  126. SQLBindCol (hstmt, 1, SQL_C_BINARY,
  127. fetched_wkb_buf, 10000, &fetched_wkb_len);
  128. /* Fetch each polygon within the display window and display it. */
  129. while (1)
  130. {
  131. rc = SQLFetch(hstmt);
  132. if (rc == SQL_NO_DATA_FOUND)
  133. break;
  134. else
  135. returncode_check(NULL, hstmt, rc, "SQLFetch");
  136. draw_polygon(fetched_wkb_buf);
  137. }
  138. /* Close the result set cursor */
  139. SQLCloseCursor(hstmt);
  140. /* INFORMIX_EXTEST_END asbinary */
  141. SQLFreeStmt (hstmt, SQL_CLOSE); /* Close the statement handle */
  142. SQLFreeHandle (SQL_HANDLE_STMT, hstmt); /* Free the statement handle */
  143. SQLDisconnect (hdbc); /* Close the connection */
  144. SQLFreeHandle (SQL_HANDLE_DBC, hdbc); /* Free the database handle */
  145. SQLFreeHandle (SQL_HANDLE_ENV, henv); /* Free the ODBC environment */
  146. printf( "\nTest Complete\n");
  147. }
  148. void draw_polygon (char *wkb_buf)
  149. {
  150. Geometry geom = {0};
  151. /* Convert the incoming well-known binary byte stream
  152. * into a Geometry data structure. */
  153. wkb_to_geom (wkb_buf, &geom);
  154. /* The remainder of this function is left as an exercise for the reader */
  155. }