pointfromwkb.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**************************************************************************
  2. *
  3. * pointfromwkb.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_PointFromWkb 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 pointfromwkb pointfromwkb.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. Geometry geom = {0};
  46. Point pt[30];
  47. int max_alloced = 0;
  48. int site_id;
  49. char *name;
  50. int name_len;
  51. char *location_wkb_buf;
  52. int location_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. /* ignore results */
  69. rc = SQLExecDirect (hstmt, (UCHAR *) "DROP TABLE hazardous_sites", SQL_NTS);
  70. rc = SQLExecDirect (hstmt, (UCHAR *) "CREATE TABLE hazardous_sites (site_id integer, name varchar(128), location ST_Point)", SQL_NTS);
  71. returncode_check (NULL, hstmt, rc, "SQLExecDirect");
  72. /* Initialize entries for non-spatial columns in the table */
  73. site_id = 59;
  74. name = "Landmark Industrial";
  75. name_len = strlen(name);
  76. /* Populate the point */
  77. #if 0
  78. pt[0].x = 48235; pt[0].y = 29746;
  79. #else
  80. pt[0].x = 10; pt[0].y = 20;
  81. #endif
  82. geom.type = geomPoint;
  83. geom.num_points = 1;
  84. geom.num_parts = 1;
  85. geom.pt = pt;
  86. /* Convert the point to a Well Known Binary representation. */
  87. geom_to_wkb (&geom, &max_alloced, &location_wkb_len, &location_wkb_buf);
  88. /* INFORMIX_EXTEST_BEGIN pointfromwkb */
  89. /* Create the SQL insert statement to populate the hazardous_sites
  90. * table. The question marks are parameter markers that indicate
  91. * the column values that will be inserted at run time. */
  92. sprintf(sql_stmt,
  93. "INSERT INTO hazardous_sites (site_id, name, location) "
  94. "VALUES(?, ?, ST_PointFromWKB(?, %d))", srid);
  95. /* Prepare the SQL statement for execution. */
  96. rc = SQLPrepare (hstmt, (unsigned char *)sql_stmt, SQL_NTS);
  97. returncode_check (NULL, hstmt, rc, "SQLPrepare");
  98. /* Bind the site_id to the first parameter. */
  99. pcbvalue1 = 0;
  100. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG,
  101. SQL_INTEGER, 0, 0,
  102. &site_id, 0, &pcbvalue1);
  103. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  104. /* Bind the name to the second parameter. */
  105. pcbvalue2 = name_len;
  106. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR,
  107. SQL_CHAR, name_len, 0,
  108. name, 0, &pcbvalue2);
  109. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  110. /* Bind the location geometry to the third parameter. */
  111. pcbvalue3 = location_wkb_len;
  112. rc = SQLBindParameter (hstmt, 3, SQL_PARAM_INPUT, SQL_C_BINARY,
  113. SQL_INFX_UDT_LVARCHAR, location_wkb_len, 0,
  114. location_wkb_buf, location_wkb_len, &pcbvalue3);
  115. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  116. /* Execute the insert statement. */
  117. rc = SQLExecute (hstmt);
  118. returncode_check (NULL, hstmt, rc, "SQLExecute");
  119. /* INFORMIX_EXTEST_END pointfromwkb */
  120. SQLFreeStmt (hstmt, SQL_CLOSE); /* Close the statement handle */
  121. SQLFreeHandle (SQL_HANDLE_STMT, hstmt); /* Free the statement handle */
  122. SQLDisconnect (hdbc); /* Close the connection */
  123. SQLFreeHandle (SQL_HANDLE_DBC, hdbc); /* Free the database handle */
  124. SQLFreeHandle (SQL_HANDLE_ENV, henv); /* Free the ODBC environment */
  125. printf( "\nTest Complete\n");
  126. }