polyfromwkb.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**************************************************************************
  2. *
  3. * polyfromwkb.c
  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_PolyFromWkb 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.c
  12. *
  13. * The following command will likely compile this source:
  14. *
  15. * cc -o polyfromwkb polyfromwkb.c \
  16. * -I$INFORMIXDIR/incl/cli -L$INFORMIXDIR/lib/cli -lifcli -lifdmr
  17. *
  18. **************************************************************************/
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <float.h>
  23. #ifdef WINNT
  24. #include <io.h>
  25. #include <windows.h>
  26. #include <conio.h>
  27. #endif
  28. #include <infxcli.h> /* ODBC typedefs and data structures */
  29. #include "odbcutils.c" /* ODBC utility functions */
  30. /*
  31. * Constants, macros, typedefs, data structures, & functions for
  32. * processing OGIS well-known-binary format data.
  33. */
  34. #include "commfuncs.h"
  35. #include "commfuncs.c"
  36. #include "wkbfuncs.c"
  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, pcbvalue2, pcbvalue3;
  45. SDWORD pcbvalue4, pcbvalue5;
  46. Geometry geom = {0};
  47. Point pt[30];
  48. int suboffsets[5];
  49. int max_alloced = 0;
  50. int id;
  51. char *name, *type;
  52. double size;
  53. int name_len, type_len;
  54. char *zone_wkb_buf;
  55. int zone_wkb_len;
  56. int srid;
  57. /* Check for the correct number of arguments entered. */
  58. if (argc < 2)
  59. {
  60. printf ("Usage: %s <datasource> [<srid>]\n", argv[0]);
  61. exit (1);
  62. }
  63. /* Connect to the database. */
  64. server_connect ((UCHAR *) argv[1], &henv, &hdbc);
  65. /* Obtain srid to use for inserts */
  66. srid = (argc > 2) ? atoi(argv[2]) : 0;
  67. /* Allocate memory for the SQL statement handle and
  68. * associate the statement handle with the connection handle. */
  69. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt);
  70. returncode_check (hdbc, (SQLHSTMT)NULL, rc, "SQLAllocHandle");
  71. sprintf(sql_stmt,
  72. "DROP TABLE sensitive_areas");
  73. rc = SQLExecDirect (hstmt, (UCHAR *) sql_stmt, SQL_NTS);
  74. sprintf(sql_stmt,
  75. "CREATE TABLE sensitive_areas (id integer, name varchar(128), "
  76. "size float, type varchar(32), zone ST_Polygon)");
  77. rc = SQLExecDirect (hstmt, (UCHAR *) sql_stmt, SQL_NTS);
  78. returncode_check (NULL, hstmt, rc, "SQLExecDirect");
  79. /* Initialize entries for non-spatial columns in the table */
  80. id = 289;
  81. name = "Johnson County Hospital";
  82. name_len = strlen(name);
  83. size = 102281.91;
  84. type = "hospital";
  85. type_len = strlen(type);
  86. /* Populate the polygon point array */
  87. suboffsets[0] = 0;
  88. pt[0].x = 22000; pt[0].y = 45000;
  89. pt[1].x = 22000; pt[1].y = 58000;
  90. pt[2].x = 28000; pt[2].y = 58000;
  91. pt[3].x = 28000; pt[3].y = 42000;
  92. pt[4].x = 25000; pt[4].y = 42000;
  93. pt[5].x = 25000; pt[5].y = 45000;
  94. pt[6].x = 22000; pt[6].y = 45000;
  95. geom.type = geomPolygon;
  96. geom.num_points = 7;
  97. geom.num_parts = 1;
  98. geom.num_subparts = 1;
  99. geom.suboffsets = suboffsets;
  100. geom.pt = pt;
  101. /* Convert the points to a Well Known Binary
  102. * representation of a polygon. */
  103. geom_to_wkb (&geom, &max_alloced, &zone_wkb_len, &zone_wkb_buf);
  104. /* INFORMIX_EXTEST_BEGIN polyfromwkb */
  105. /* Create the SQL insert statement to populate the sensitive_areas
  106. * table. The question marks are parameter markers that indicate
  107. * the column values that will be inserted at run time. */
  108. sprintf(sql_stmt,
  109. "INSERT INTO sensitive_areas (id, name, size, type, zone) "
  110. "VALUES(?, ?, ?, ?, ST_PolyFromWKB(?, %d))", srid);
  111. /* Prepare the SQL statement for execution. */
  112. rc = SQLPrepare (hstmt, (unsigned char *)sql_stmt, SQL_NTS);
  113. returncode_check (NULL, hstmt, rc, "SQLPrepare");
  114. /* Bind the id to the first parameter. */
  115. pcbvalue1 = 0;
  116. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG,
  117. SQL_INTEGER, 0, 0,
  118. &id, 0, &pcbvalue1);
  119. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  120. /* Bind the name to the second parameter. */
  121. pcbvalue2 = name_len;
  122. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR,
  123. SQL_CHAR, name_len, 0,
  124. name, 0, &pcbvalue2);
  125. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  126. /* Bind the size to the third parameter. */
  127. pcbvalue3 = 0;
  128. rc = SQLBindParameter (hstmt, 3, SQL_PARAM_INPUT, SQL_C_FLOAT,
  129. SQL_REAL, 0, 0,
  130. &size, 0, &pcbvalue3);
  131. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  132. /* Bind the type to the fourth parameter. */
  133. pcbvalue4 = type_len;
  134. rc = SQLBindParameter (hstmt, 4, SQL_PARAM_INPUT, SQL_C_CHAR,
  135. SQL_VARCHAR, type_len, 0,
  136. type, type_len, &pcbvalue4);
  137. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  138. /* Bind the zone geometry to the fifth parameter. */
  139. pcbvalue5 = zone_wkb_len;
  140. rc = SQLBindParameter (hstmt, 5, SQL_PARAM_INPUT, SQL_C_BINARY,
  141. SQL_INFX_UDT_LVARCHAR, zone_wkb_len, 0,
  142. zone_wkb_buf, zone_wkb_len, &pcbvalue5);
  143. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  144. /* Execute the insert statement. */
  145. rc = SQLExecute (hstmt);
  146. returncode_check (NULL, hstmt, rc, "SQLExecute");
  147. /* INFORMIX_EXTEST_END polyfromwkb */
  148. SQLFreeStmt (hstmt, SQL_CLOSE); /* Close the statement handle */
  149. SQLFreeHandle (SQL_HANDLE_STMT, hstmt); /* Free the statement handle */
  150. SQLDisconnect (hdbc); /* Close the connection */
  151. SQLFreeHandle (SQL_HANDLE_DBC, hdbc); /* Free the database handle */
  152. SQLFreeHandle (SQL_HANDLE_ENV, henv); /* Free the ODBC environment */
  153. printf( "\nTest Complete\n");
  154. }