/************************************************************************** * * shapetosql.c * * This program demonstrates how to insert spatial data into a database * in ESRI shapefile format using the Informix Spatial DataBlade's * SE_ShapeToSql function. * * SE_ShapeToSql is a special variation on SE_GeomFromShape which * assigns srid = 0 to the input shape. * * 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_shapes.c * * The following command will likely compile this source: * * cc -o shapetosql shapetosql.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 ESRI shapefile format data. */ #include "commfuncs.h" #include "commfuncs.c" #include "shapefuncs.c" void main (int argc, char **argv) { SQLHDBC hdbc; SQLHENV henv; SQLHSTMT hstmt; char sql_stmt[200]; int rc; SDWORD pcbvalue1, pcbvalue2; int max_alloced = 0; Geometry geom = {0}; Point pt[30]; int lot_id; char *lot_shape_buf; int lot_shape_len; /* 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); /* 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_Geometry)", SQL_NTS); returncode_check (NULL, hstmt, rc, "SQLExecDirect"); /* Initialize entries for non-spatial columns in the table */ lot_id = 1010; /* Populate the polygon point array */ pt[0].x = 2; pt[0].y = 57; pt[1].x = 21.5; pt[1].y = 57; pt[2].x = 21.5; pt[2].y = 38; pt[3].x = 2; pt[3].y = 38; pt[4].x = 2; pt[4].y = 57; geom.type = geomPolygon; geom.num_points = 5; geom.num_parts = 1; geom.pt = pt; /* Convert the polygon to an ESRI shape. */ geom_to_shape (&geom, &max_alloced, &lot_shape_len, &lot_shape_buf); /* INFORMIX_EXTEST_BEGIN shapetosql */ /* 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(?, SE_ShapeToSQL(?))"); /* 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 the second parameter. */ pcbvalue2 = lot_shape_len; rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_INFX_UDT_LVARCHAR, lot_shape_len, 0, lot_shape_buf, lot_shape_len, &pcbvalue2); returncode_check (NULL, hstmt, rc, "SQLBindParameter"); /* Execute the insert statement. */ rc = SQLExecute (hstmt); returncode_check (NULL, hstmt, rc, "SQLExecute"); /* INFORMIX_EXTEST_END shapetosql */ 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"); }