/************************************************************************** * * mpointfromwkb.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_MpointFromWkb 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 mpointfromwkb mpointfromwkb.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, pcbvalue3; Geometry geom = {0}; Point pt[30]; int max_alloced = 0; char *genus, *species; int genus_len, species_len; char *sitings_wkb_buf; int sitings_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 species_sitings", SQL_NTS); rc = SQLExecDirect (hstmt, (UCHAR *) "CREATE TABLE species_sitings (id integer,genus varchar(128), species varchar(128), sitings ST_MultiPoint)", SQL_NTS); returncode_check (NULL, hstmt, rc, "SQLExecDirect"); /* Initialize entries for non-spatial columns in the table */ genus = "Falco"; genus_len = strlen(genus); species = "punctatus"; species_len = strlen(species); /* Populate the multipoint */ pt[0].x = 1; pt[0].y = 2; pt[1].x = 3; pt[1].y = 4; pt[2].x = 5; pt[2].y = 6; pt[3].x = 7; pt[3].y = 8; pt[4].x = 9; pt[4].y = 10; geom.type = geomMultiPoint; geom.num_points = 5; geom.num_parts = 5; geom.pt = pt; /* Convert the points to a Well Known Binary * representation of a multipoint. */ geom_to_wkb (&geom, &max_alloced, &sitings_wkb_len, &sitings_wkb_buf); /* INFORMIX_EXTEST_BEGIN mpointfromwkb */ /* Create the SQL insert statement to populate the species_sitings * table. The question marks are parameter markers that indicate * the column values that will be inserted at run time. */ sprintf(sql_stmt, "INSERT INTO species_sitings (species,genus,sitings) " "VALUES(?, ?, ST_MpointFromWKB(?, %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 species to the first parameter. */ pcbvalue1 = species_len; rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, species_len, 0, species, species_len, &pcbvalue1); returncode_check (NULL, hstmt, rc, "SQLBindParameter"); /* Bind the genus to the second parameter. */ pcbvalue2 = genus_len; rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, genus_len, 0, genus, genus_len, &pcbvalue2); returncode_check (NULL, hstmt, rc, "SQLBindParameter"); /* Bind the sitings geometry to the third parameter. */ pcbvalue3 = sitings_wkb_len; rc = SQLBindParameter (hstmt, 3, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_INFX_UDT_LVARCHAR, sitings_wkb_len, 0, sitings_wkb_buf, sitings_wkb_len, &pcbvalue3); returncode_check (NULL, hstmt, rc, "SQLBindParameter"); /* Execute the insert statement. */ rc = SQLExecute (hstmt); returncode_check (NULL, hstmt, rc, "SQLExecute"); /* INFORMIX_EXTEST_END mpointfromwkb */ 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"); }