mpointfromwkb.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**************************************************************************
  2. *
  3. * mpointfromwkb.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_MpointFromWkb 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 mpointfromwkb mpointfromwkb.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. char *genus, *species;
  49. int genus_len, species_len;
  50. char *sitings_wkb_buf;
  51. int sitings_wkb_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. rc = SQLExecDirect (hstmt, (UCHAR *) "DROP TABLE species_sitings", SQL_NTS);
  68. rc = SQLExecDirect (hstmt, (UCHAR *) "CREATE TABLE species_sitings (id integer,genus varchar(128), species varchar(128), sitings ST_MultiPoint)", SQL_NTS);
  69. returncode_check (NULL, hstmt, rc, "SQLExecDirect");
  70. /* Initialize entries for non-spatial columns in the table */
  71. genus = "Falco"; genus_len = strlen(genus);
  72. species = "punctatus"; species_len = strlen(species);
  73. /* Populate the multipoint */
  74. pt[0].x = 1; pt[0].y = 2;
  75. pt[1].x = 3; pt[1].y = 4;
  76. pt[2].x = 5; pt[2].y = 6;
  77. pt[3].x = 7; pt[3].y = 8;
  78. pt[4].x = 9; pt[4].y = 10;
  79. geom.type = geomMultiPoint;
  80. geom.num_points = 5;
  81. geom.num_parts = 5;
  82. geom.pt = pt;
  83. /* Convert the points to a Well Known Binary
  84. * representation of a multipoint. */
  85. geom_to_wkb (&geom, &max_alloced, &sitings_wkb_len, &sitings_wkb_buf);
  86. /* INFORMIX_EXTEST_BEGIN mpointfromwkb */
  87. /* Create the SQL insert statement to populate the species_sitings
  88. * table. The question marks are parameter markers that indicate
  89. * the column values that will be inserted at run time. */
  90. sprintf(sql_stmt,
  91. "INSERT INTO species_sitings (species,genus,sitings) "
  92. "VALUES(?, ?, ST_MpointFromWKB(?, %d))", srid);
  93. /* Prepare the SQL statement for execution. */
  94. rc = SQLPrepare (hstmt, (unsigned char *)sql_stmt, SQL_NTS);
  95. returncode_check (NULL, hstmt, rc, "SQLPrepare");
  96. /* Bind the species to the first parameter. */
  97. pcbvalue1 = species_len;
  98. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR,
  99. SQL_CHAR, species_len, 0,
  100. species, species_len, &pcbvalue1);
  101. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  102. /* Bind the genus to the second parameter. */
  103. pcbvalue2 = genus_len;
  104. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR,
  105. SQL_CHAR, genus_len, 0,
  106. genus, genus_len, &pcbvalue2);
  107. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  108. /* Bind the sitings geometry to the third parameter. */
  109. pcbvalue3 = sitings_wkb_len;
  110. rc = SQLBindParameter (hstmt, 3, SQL_PARAM_INPUT, SQL_C_BINARY,
  111. SQL_INFX_UDT_LVARCHAR, sitings_wkb_len, 0,
  112. sitings_wkb_buf, sitings_wkb_len, &pcbvalue3);
  113. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  114. /* Execute the insert statement. */
  115. rc = SQLExecute (hstmt);
  116. returncode_check (NULL, hstmt, rc, "SQLExecute");
  117. /* INFORMIX_EXTEST_END mpointfromwkb */
  118. SQLFreeStmt (hstmt, SQL_CLOSE); /* Close the statement handle */
  119. SQLFreeHandle (SQL_HANDLE_STMT, hstmt); /* Free the statement handle */
  120. SQLDisconnect (hdbc); /* Close the connection */
  121. SQLFreeHandle (SQL_HANDLE_DBC, hdbc); /* Free the database handle */
  122. SQLFreeHandle (SQL_HANDLE_ENV, henv); /* Free the ODBC environment */
  123. printf( "\nTest Complete\n");
  124. }