geomfromshape.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**************************************************************************
  2. *
  3. * geomfromshape.c
  4. *
  5. * This program demonstrates how to insert spatial data into a database
  6. * in ESRI shapefile format using the Informix Spatial DataBlade's
  7. * SE_GeomFromShape 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_shapes.c
  12. *
  13. * The following command will likely compile this source:
  14. *
  15. * cc -o geomfromshape geomfromshape.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 ESRI shapefile format data.
  33. */
  34. #include "commfuncs.h"
  35. #include "commfuncs.c"
  36. #include "shapefuncs.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;
  45. int max_alloced = 0;
  46. Geometry geom = {0};
  47. Point pt[30];
  48. int lot_id;
  49. char *lot_shape_buf;
  50. int lot_shape_len;
  51. int srid;
  52. /* Check for the correct number of arguments entered. */
  53. if (argc < 2)
  54. {
  55. printf ("Usage: %s <datasource> [<srid>]\n", argv[0]);
  56. exit (1);
  57. }
  58. /* Connect to the database. */
  59. server_connect ((SQLCHAR*) argv[1], &henv, &hdbc);
  60. /* Obtain srid to use for inserts */
  61. srid = (argc > 2) ? atoi(argv[2]) : 0;
  62. /* Allocate memory for the SQL statement handle and
  63. * associate the statement handle with the connection handle. */
  64. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt);
  65. returncode_check (hdbc, (SQLHSTMT)NULL, rc, "SQLAllocHandle");
  66. rc = SQLExecDirect (hstmt, (SQLCHAR*) "DROP TABLE lots", SQL_NTS);
  67. rc = SQLExecDirect (hstmt, (SQLCHAR*) "CREATE TABLE lots (lot_id integer, lot ST_Geometry)", SQL_NTS);
  68. returncode_check (NULL, hstmt, rc, "SQLExecDirect");
  69. /* Initialize entries for non-spatial columns in the table */
  70. lot_id = 1010;
  71. /* Populate the polygon point array */
  72. pt[0].x = 2; pt[0].y = 57;
  73. pt[1].x = 21.5; pt[1].y = 57;
  74. pt[2].x = 21.5; pt[2].y = 38;
  75. pt[3].x = 2; pt[3].y = 38;
  76. pt[4].x = 2; pt[4].y = 57;
  77. geom.type = geomPolygon;
  78. geom.num_points = 5;
  79. geom.num_parts = 1;
  80. geom.pt = pt;
  81. /* Convert the polygon to an ESRI shape. */
  82. geom_to_shape (&geom, &max_alloced, &lot_shape_len, &lot_shape_buf);
  83. /* INFORMIX_EXTEST_BEGIN geomfromshape */
  84. /* Create the SQL insert statement to populate the lots
  85. * table. The question marks are parameter markers that indicate
  86. * the column values that will be inserted at run time. */
  87. sprintf(sql_stmt,
  88. "INSERT INTO lots (lot_id, lot) "
  89. "VALUES(?, SE_GeomFromShape(?, %d))", srid);
  90. /* Prepare the SQL statement for execution. */
  91. rc = SQLPrepare (hstmt, (unsigned char *)sql_stmt, SQL_NTS);
  92. returncode_check (NULL, hstmt, rc, "SQLPrepare");
  93. /* Bind the lot_id to the first parameter. */
  94. pcbvalue1 = 0;
  95. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG,
  96. SQL_INTEGER, 0, 0,
  97. &lot_id, 0, &pcbvalue1);
  98. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  99. /* Bind the lot geometry to the second parameter. */
  100. pcbvalue2 = lot_shape_len;
  101. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_BINARY,
  102. SQL_INFX_UDT_LVARCHAR, lot_shape_len, 0,
  103. lot_shape_buf, lot_shape_len, &pcbvalue2);
  104. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  105. /* Execute the insert statement. */
  106. rc = SQLExecute (hstmt);
  107. returncode_check (NULL, hstmt, rc, "SQLExecute");
  108. /* INFORMIX_EXTEST_END geomfromshape */
  109. SQLFreeStmt (hstmt, SQL_CLOSE); /* Close the statement handle */
  110. SQLFreeHandle (SQL_HANDLE_STMT, hstmt); /* Free the statement handle */
  111. SQLDisconnect (hdbc); /* Close the connection */
  112. SQLFreeHandle (SQL_HANDLE_DBC, hdbc); /* Free the database handle */
  113. SQLFreeHandle (SQL_HANDLE_ENV, henv); /* Free the ODBC environment */
  114. printf( "\nTest Complete\n");
  115. }