/************************************************************************** * * mpolyfromwkb.c * * This program demonstrates how to insert spatial data into a database * in OGIS well-known binary format using the Informix Spatial DataBlade's * ST_MpolyFromWkb function. * * This program creates a table and inserts several polygons into it. * For an example program which shows how to insert all spatial types, * please see load_wkb.c * * The following command will likely compile this source: * * cc -o mpolyfromwkb mpolyfromwkb.c \ * -I$INFORMIXDIR/incl/cli -L$INFORMIXDIR/lib/cli -lifcli -lifdmr * **************************************************************************/ #include #include #include #include #ifdef WINNT #include #include #include #endif #include /* ODBC typedefs and data structures */ #include "odbcutils.c" /* ODBC utility functions */ /* * Constants, macros, typedefs, data structures, & functions for * processing OGIS well-known-binary format data. */ #include "commfuncs.h" #include "commfuncs.c" #include "wkbfuncs.c" void main (int argc, char **argv) { SQLHDBC hdbc; SQLHENV henv; SQLHSTMT hstmt; char sql_stmt[200]; int rc; SDWORD pcbvalue1, pcbvalue2; Geometry geom = {0}; Point pt[30]; int offsets[5]; int suboffsets[6]; int max_alloced = 0; int lot_id; char *lot_wkb_buf; int lot_wkb_len; int srid; /* Check for the correct number of arguments entered. */ if (argc < 2) { printf ("Usage: %s []\n", argv[0]); exit (1); } /* Connect to the database. */ server_connect ((UCHAR *) argv[1], &henv, &hdbc); /* Obtain srid to use for inserts */ srid = (argc > 2) ? atoi(argv[2]) : 0; /* Allocate memory for the SQL statement handle and * associate the statement handle with the connection handle. */ rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt); returncode_check (hdbc, (SQLHSTMT)NULL, rc, "SQLAllocHandle"); rc = SQLExecDirect (hstmt, (UCHAR *) "DROP TABLE lots", SQL_NTS); rc = SQLExecDirect (hstmt, (UCHAR *) "CREATE TABLE lots (lot_id integer, lot ST_MultiPolygon)", SQL_NTS); returncode_check (NULL, hstmt, rc, "SQLExecDirect"); /* Initialize entries for non-spatial columns in the table */ lot_id = 203; /* The first polygon's exterior ring. */ offsets[0] = 0; suboffsets[0] = 0; pt[0].x = 10; pt[0].y = 10; pt[1].x = 10; pt[1].y = 20; pt[2].x = 20; pt[2].y = 20; pt[3].x = 20; pt[3].y = 10; pt[4].x = 10; pt[4].y = 10; /* The first polygon's interior ring. */ suboffsets[1] = 5; pt[5].x = 14; pt[5].y = 14; pt[6].x = 14; pt[6].y = 16; pt[7].x = 16; pt[7].y = 16; pt[8].x = 16; pt[8].y = 14; pt[9].x = 14; pt[9].y = 14; /* The second polygon's exterior ring. */ offsets[1] = 2; suboffsets[2] = 10; pt[10].x = 100; pt[10].y = 100; pt[11].x = 100; pt[11].y = 200; pt[12].x = 200; pt[12].y = 200; pt[13].x = 200; pt[13].y = 100; pt[14].x = 100; pt[14].y = 100; /* The third polygons exterior ring. */ offsets[2] = 3; suboffsets[3] = 15; pt[15].x = 50; pt[15].y = 50; pt[16].x = 50; pt[16].y = 60; pt[17].x = 60; pt[17].y = 60; pt[18].x = 60; pt[18].y = 50; pt[19].x = 50; pt[19].y = 50; /* The third polygon's interior ring. */ suboffsets[4] = 20; pt[20].x = 52; pt[20].y = 52; pt[21].x = 52; pt[21].y = 54; pt[22].x = 54; pt[22].y = 54; pt[23].x = 54; pt[23].y = 52; pt[24].x = 52; pt[24].y = 52; /* The third polygon's interior ring. */ suboffsets[5] = 25; pt[25].x = 56; pt[25].y = 56; pt[26].x = 56; pt[26].y = 58; pt[27].x = 58; pt[27].y = 58; pt[28].x = 58; pt[28].y = 56; pt[29].x = 56; pt[29].y = 56; geom.type = geomMultiPolygon; geom.num_points = 30; geom.num_parts = 3; geom.num_subparts = 6; geom.offsets = offsets; geom.suboffsets = suboffsets; geom.pt = pt; /* Convert the points to a Well Known Binary * representation of a multipolygon. */ geom_to_wkb (&geom, &max_alloced, &lot_wkb_len, &lot_wkb_buf); /* INFORMIX_EXTEST_BEGIN mpolyfromwkb */ /* Create the SQL insert statement to populate the lots table. * The question marks are parameter markers that indicate the * column values that will be inserted at run time. */ sprintf(sql_stmt, "INSERT INTO lots (lot_id, lot) " "VALUES(?, ST_MpolyFromWKB(?, %d))", srid); /* Prepare the SQL statement for execution. */ rc = SQLPrepare (hstmt, (unsigned char *)sql_stmt, SQL_NTS); returncode_check (NULL, hstmt, rc, "SQLPrepare"); /* Bind the lot_id to the first parameter. */ pcbvalue1 = 0; rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &lot_id, 0, &pcbvalue1); returncode_check (NULL, hstmt, rc, "SQLBindParameter"); /* Bind the lot geometry to the second parameter. */ pcbvalue2 = lot_wkb_len; rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_INFX_UDT_LVARCHAR, lot_wkb_len, 0, lot_wkb_buf, lot_wkb_len, &pcbvalue2); returncode_check (NULL, hstmt, rc, "SQLBindParameter"); /* Execute the insert statement. */ rc = SQLExecute (hstmt); returncode_check (NULL, hstmt, rc, "SQLExecute"); /* INFORMIX_EXTEST_END mpolyfromwkb */ SQLFreeStmt (hstmt, SQL_CLOSE); /* Close the statement handle */ SQLFreeHandle (SQL_HANDLE_STMT, hstmt); /* Free the statement handle */ SQLDisconnect (hdbc); /* Close the connection */ SQLFreeHandle (SQL_HANDLE_DBC, hdbc); /* Free the database handle */ SQLFreeHandle (SQL_HANDLE_ENV, henv); /* Free the ODBC environment */ printf( "\nTest Complete\n"); }