mpolyfromshape.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**************************************************************************
  2. *
  3. * mpolyfromshape.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_MpolyFromShape 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 mpolyfromshape mpolyfromshape.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. Geometry geom = {0};
  46. Point pt[30];
  47. int offsets[10];
  48. int max_alloced = 0;
  49. int lot_id;
  50. char *lot_shape_buf;
  51. int lot_shape_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 lots", SQL_NTS);
  68. rc = SQLExecDirect (hstmt, (UCHAR *) "CREATE TABLE lots (lot_id integer, lot ST_Geometry)", SQL_NTS);
  69. returncode_check (NULL, hstmt, rc, "SQLExecDirect");
  70. /* Initialize entries for non-spatial columns in the table */
  71. lot_id = 203;
  72. /* The first polygon's exterior ring. */
  73. offsets[0] = 0;
  74. pt[0].x = 10; pt[0].y = 10;
  75. pt[1].x = 10; pt[1].y = 20;
  76. pt[2].x = 20; pt[2].y = 20;
  77. pt[3].x = 20; pt[3].y = 10;
  78. pt[4].x = 10; pt[4].y = 10;
  79. /* The first polygon's interior ring. */
  80. offsets[1] = 5;
  81. pt[5].x = 14; pt[5].y = 14;
  82. pt[6].x = 14; pt[6].y = 16;
  83. pt[7].x = 16; pt[7].y = 16;
  84. pt[8].x = 16; pt[8].y = 14;
  85. pt[9].x = 14; pt[9].y = 14;
  86. /* The second polygon's exterior ring. */
  87. offsets[2] = 10;
  88. pt[10].x = 100; pt[10].y = 100;
  89. pt[11].x = 100; pt[11].y = 200;
  90. pt[12].x = 200; pt[12].y = 200;
  91. pt[13].x = 200; pt[13].y = 100;
  92. pt[14].x = 100; pt[14].y = 100;
  93. /* The third polygons exterior ring. */
  94. offsets[3] = 15;
  95. pt[15].x = 50; pt[15].y = 50;
  96. pt[16].x = 50; pt[16].y = 60;
  97. pt[17].x = 60; pt[17].y = 60;
  98. pt[18].x = 60; pt[18].y = 50;
  99. pt[19].x = 50; pt[19].y = 50;
  100. /* The third polygon's interior ring. */
  101. offsets[4] = 20;
  102. pt[20].x = 52; pt[20].y = 52;
  103. pt[21].x = 52; pt[21].y = 54;
  104. pt[22].x = 54; pt[22].y = 54;
  105. pt[23].x = 54; pt[23].y = 52;
  106. pt[24].x = 52; pt[24].y = 52;
  107. /* The third polygon's interior ring. */
  108. offsets[5] = 25;
  109. pt[25].x = 56; pt[25].y = 56;
  110. pt[26].x = 56; pt[26].y = 58;
  111. pt[27].x = 58; pt[27].y = 58;
  112. pt[28].x = 58; pt[28].y = 56;
  113. pt[29].x = 56; pt[29].y = 56;
  114. geom.type = geomMultiPolygon;
  115. geom.num_points = 30;
  116. geom.num_parts = 6;
  117. geom.offsets = offsets;
  118. geom.pt = pt;
  119. /* Convert the polygon to an ESRI shape. */
  120. geom_to_shape (&geom, &max_alloced, &lot_shape_len, &lot_shape_buf);
  121. /* INFORMIX_EXTEST_BEGIN mpolyfromshape */
  122. /* Create the SQL insert statement to populate the lots table.
  123. * The question marks are parameter markers that indicate the
  124. * column values that will be inserted at run time. */
  125. sprintf(sql_stmt,
  126. "INSERT INTO lots (lot_id,lot)"
  127. "VALUES(?, SE_MpolyFromShape(?, %d))", srid);
  128. /* Prepare the SQL statement for execution. */
  129. rc = SQLPrepare (hstmt, (unsigned char *)sql_stmt, SQL_NTS);
  130. returncode_check (NULL, hstmt, rc, "SQLPrepare");
  131. /* Bind the lot_id to the first parameter. */
  132. pcbvalue1 = 0;
  133. rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG,
  134. SQL_INTEGER, 0, 0,
  135. &lot_id, 0, &pcbvalue1);
  136. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  137. /* Bind the lot geometry to the second parameter. */
  138. pcbvalue2 = lot_shape_len;
  139. rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_BINARY,
  140. SQL_INFX_UDT_LVARCHAR, lot_shape_len, 0,
  141. lot_shape_buf, lot_shape_len, &pcbvalue2);
  142. returncode_check (NULL, hstmt, rc, "SQLBindParameter");
  143. /* Execute the insert statement. */
  144. rc = SQLExecute (hstmt);
  145. returncode_check (NULL, hstmt, rc, "SQLExecute");
  146. /* INFORMIX_EXTEST_END mpolyfromshape */
  147. SQLFreeStmt (hstmt, SQL_CLOSE); /* Close the statement handle */
  148. SQLFreeHandle (SQL_HANDLE_STMT, hstmt); /* Free the statement handle */
  149. SQLDisconnect (hdbc); /* Close the connection */
  150. SQLFreeHandle (SQL_HANDLE_DBC, hdbc); /* Free the database handle */
  151. SQLFreeHandle (SQL_HANDLE_ENV, henv); /* Free the ODBC environment */
  152. printf( "\nTest Complete\n");
  153. }