asshape.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**************************************************************************
  2. *
  3. * asshape.c
  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. *
  11. * cc -o asshape asshape.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 ESRI shapefile format data.
  29. */
  30. #include "commfuncs.h"
  31. #include "commfuncs.c"
  32. #include "shapefuncs.c"
  33. /*
  34. * Local function prototypes
  35. */
  36. void draw_polygon (char *shape_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 max_alloced = 0;
  48. char fetched_shape_buf[10000];
  49. int fetched_shape_len;
  50. char *query_shape_buf;
  51. int query_shape_len;
  52. int srid;
  53. /* Check for the correct number of arguments entered. */
  54. if (argc < 2)
  55. {
  56. printf ("Usage: %s <datasource> [<srid>]\n", argv[0]);
  57. exit (1);
  58. }
  59. /* Connect to the database. */
  60. server_connect ((UCHAR *) argv[1], &henv, &hdbc);
  61. /* Obtain srid to use for inserts */
  62. srid = (argc > 2) ? atoi(argv[2]) : 0;
  63. /* Allocate memory for the SQL statement handle and
  64. * associate the statement handle with the connection handle. */
  65. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt);
  66. returncode_check (hdbc, (SQLHSTMT)NULL, rc, "SQLAllocHandle");
  67. /* Populate the query polygon point array */
  68. pt[0].x = 10; pt[0].y = 10;
  69. pt[1].x = 25; pt[1].y = 10;
  70. pt[2].x = 25; pt[2].y = 20;
  71. pt[3].x = 10; pt[3].y = 20;
  72. pt[4].x = 10; pt[4].y = 10;
  73. geom.type = geomPolygon;
  74. geom.num_points = 5;
  75. geom.num_parts = 1;
  76. geom.pt = pt;
  77. /* Convert the polygon to an ESRI shape. */
  78. geom_to_shape (&geom, &max_alloced, &query_shape_len, &query_shape_buf);
  79. sprintf(sql_stmt,
  80. "DROP TABLE sensitive_areas");
  81. rc = SQLExecDirect (hstmt, (UCHAR *) sql_stmt, SQL_NTS);
  82. sprintf(sql_stmt,
  83. "CREATE TABLE sensitive_areas (id integer, zone ST_Polygon)");
  84. rc = SQLExecDirect (hstmt, (UCHAR *) sql_stmt, SQL_NTS);
  85. returncode_check (NULL, hstmt, rc, "SQLExecDirect");
  86. sprintf(sql_stmt,
  87. "INSERT INTO sensitive_areas VALUES(1, "
  88. "'%d polygon((22 45,22 58,28 58,28 42,25 42,25 45,22 45))')",srid);
  89. rc = SQLExecDirect (hstmt, (UCHAR *) sql_stmt, SQL_NTS);
  90. returncode_check (NULL, hstmt, rc, "SQLExecDirect");
  91. sprintf(sql_stmt,
  92. "INSERT INTO sensitive_areas VALUES(2, "
  93. "'%d polygon((22 18,28 18,28 12,22 12,22 18))')", srid);
  94. rc = SQLExecDirect (hstmt, (UCHAR *) sql_stmt, SQL_NTS);
  95. returncode_check (NULL, hstmt, rc, "SQLExecDirect");
  96. sprintf(sql_stmt,
  97. "INSERT INTO sensitive_areas VALUES(3, "
  98. "'%d polygon((42 18,48 18,48 13,42 13,42 18))')", srid);
  99. rc = SQLExecDirect (hstmt, (UCHAR *) sql_stmt, SQL_NTS);
  100. returncode_check (NULL, hstmt, rc, "SQLExecDirect");
  101. /* INFORMIX_EXTEST_BEGIN asshape */
  102. /* Create the SQL expression. */
  103. sprintf(sql_stmt,
  104. "SELECT SE_AsShape(zone) "
  105. "FROM sensitive_areas WHERE "
  106. "SE_EnvelopesIntersect(zone,SE_PolyFromShape(?,%d))", srid);
  107. /* Prepare the SQL statement. */
  108. SQLPrepare(hstmt, (UCHAR *)sql_stmt, SQL_NTS);
  109. returncode_check (NULL, hstmt, rc, "SQLPrepare");
  110. /* Bind the query geometry parameter. */
  111. pcbvalue1 = query_shape_len;
  112. SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
  113. SQL_INFX_UDT_LVARCHAR, query_shape_len, 0,
  114. query_shape_buf, query_shape_len, &pcbvalue1);
  115. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  116. /* Execute the query. */
  117. rc = SQLExecute(hstmt);
  118. returncode_check (NULL, hstmt, rc, "SQLExecute");
  119. /* Assign the results of the query (the Zone polygons) to the
  120. fetched_shape_buf variable. */
  121. SQLBindCol (hstmt, 1, SQL_C_BINARY,
  122. fetched_shape_buf, 10000, &fetched_shape_len);
  123. /* Fetch each polygon within the display window and display it. */
  124. while (SQL_SUCCESS == (rc = SQLFetch(hstmt))) {
  125. draw_polygon(fetched_shape_buf);
  126. }
  127. if (rc != SQL_NO_DATA_FOUND) {
  128. returncode_check(NULL, hstmt, rc, "SQLFetch");
  129. }
  130. /* INFORMIX_EXTEST_END asshape */
  131. SQLFreeStmt (hstmt, SQL_CLOSE); /* Close the statement handle */
  132. SQLFreeHandle (SQL_HANDLE_STMT, hstmt); /* Free the statement handle */
  133. SQLDisconnect (hdbc); /* Close the connection */
  134. SQLFreeHandle (SQL_HANDLE_DBC, hdbc); /* Free the database handle */
  135. SQLFreeHandle (SQL_HANDLE_ENV, henv); /* Free the ODBC environment */
  136. printf( "\nTest Complete\n");
  137. }
  138. void draw_polygon (char *shape_buf)
  139. {
  140. Geometry geom = {0};
  141. /* Convert the incoming shapefile format data stream
  142. * into a Geometry data structure. */
  143. shape_to_geom (shape_buf, &geom);
  144. /* The remainder of this function is left as an exercise for the reader */
  145. }