mlinefromshape.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**************************************************************************
  2. *
  3. * mlinefromshape.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_MlineFromShape 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 mlinefromshape mlinefromshape.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, pcbvalue3;
  45. Geometry geom = {0};
  46. Point pt[30];
  47. int offsets[5];
  48. int max_alloced = 0;
  49. int id;
  50. char *name;
  51. int name_len;
  52. char *water_shape_buf;
  53. int water_shape_len;
  54. int srid;
  55. /* Check for the correct number of arguments entered. */
  56. if (argc < 2)
  57. {
  58. printf ("Usage: %s <datasource> [<srid>]\n", argv[0]);
  59. exit (1);
  60. }
  61. /* Connect to the database. */
  62. server_connect ((UCHAR *) argv[1], &henv, &hdbc);
  63. /* Obtain srid to use for inserts */
  64. srid = (argc > 2) ? atoi(argv[2]) : 0;
  65. /* Allocate memory for the SQL statement handle and
  66. * associate the statement handle with the connection handle. */
  67. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt);
  68. returncode_check (hdbc, (SQLHSTMT)NULL, rc, "SQLAllocHandle");
  69. rc = SQLExecDirect (hstmt, (UCHAR *) "DROP TABLE waterways", SQL_NTS);
  70. rc = SQLExecDirect (hstmt, (UCHAR *) "CREATE TABLE waterways (id integer, name varchar(128), water ST_MultiLineString)", SQL_NTS);
  71. returncode_check (NULL, hstmt, rc, "SQLExecDirect");
  72. /* Initialize entries for non-spatial columns in the table */
  73. id = 12345;
  74. name = "Fedders Creek";
  75. name_len = strlen(name);
  76. /* Populate the first linestring. */
  77. offsets[0] = 0;
  78. pt[0].x = 10; pt[0].y = 10;
  79. pt[1].x = 10; pt[1].y = 20;
  80. /* Populate the second linestring. */
  81. offsets[1] = 2;
  82. pt[2].x = 5; pt[2].y = 15;
  83. pt[3].x = 15; pt[3].y = 15;
  84. geom.type = geomMultiLineString;
  85. geom.num_points = 4;
  86. geom.num_parts = 2;
  87. geom.offsets = offsets;
  88. geom.pt = pt;
  89. /* Convert the multilinestring to an ESRI shape. */
  90. geom_to_shape (&geom, &max_alloced, &water_shape_len, &water_shape_buf);
  91. /* INFORMIX_EXTEST_BEGIN mlinefromshape */
  92. /* Create the SQL insert statement to populate the waterways
  93. * table. The question marks are parameter markers that indicate
  94. * the column values that will be inserted at run time. */
  95. sprintf(sql_stmt,
  96. "INSERT INTO waterways (id,name,water) "
  97. "VALUES(?, ?, SE_MlineFromShape(?, %d))", srid);
  98. /* Prepare the SQL statement for execution. */
  99. rc = SQLPrepare (hstmt, (unsigned char *)sql_stmt, SQL_NTS);
  100. returncode_check (NULL, hstmt, rc, "SQLPrepare");
  101. /* Bind the id to the first parameter. */
  102. pcbvalue1 = 0;
  103. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG,
  104. SQL_INTEGER, 0, 0,
  105. &id, 0, &pcbvalue1);
  106. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  107. /* Bind the name to the second parameter. */
  108. pcbvalue2 = name_len;
  109. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR,
  110. SQL_CHAR, name_len, 0,
  111. name, name_len, &pcbvalue2);
  112. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  113. /* Bind the water geometry to the third parameter. */
  114. pcbvalue3 = water_shape_len;
  115. rc = SQLBindParameter (hstmt, 3, SQL_PARAM_INPUT, SQL_C_BINARY,
  116. SQL_INFX_UDT_LVARCHAR, water_shape_len, 0,
  117. water_shape_buf, water_shape_len, &pcbvalue3);
  118. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  119. /* Execute the insert statement. */
  120. rc = SQLExecute (hstmt);
  121. returncode_check (NULL, hstmt, rc, "SQLExecute");
  122. /* INFORMIX_EXTEST_END mlinefromshape */
  123. SQLFreeStmt (hstmt, SQL_CLOSE); /* Close the statement handle */
  124. SQLFreeHandle (SQL_HANDLE_STMT, hstmt); /* Free the statement handle */
  125. SQLDisconnect (hdbc); /* Close the connection */
  126. SQLFreeHandle (SQL_HANDLE_DBC, hdbc); /* Free the database handle */
  127. SQLFreeHandle (SQL_HANDLE_ENV, henv); /* Free the ODBC environment */
  128. printf( "\nTest Complete\n");
  129. }