123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- /**************************************************************************
- *
- * pointfromshape.c
- *
- * This program demonstrates how to insert spatial data into a database
- * in ESRI shapefile format using the Informix Spatial DataBlade's
- * SE_PointFromShape 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_shapes.c
- *
- * The following command will likely compile this source:
- *
- * cc -o pointfromshape pointfromshape.c \
- * -I$INFORMIXDIR/incl/cli -L$INFORMIXDIR/lib/cli -lifcli -lifdmr
- *
- **************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <float.h>
- #ifdef WINNT
- #include <io.h>
- #include <windows.h>
- #include <conio.h>
- #endif
- #include <infxcli.h> /* 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, pcbvalue3;
- Geometry geom = {0};
- Point pt[30];
- int max_alloced = 0;
- char *name;
- int name_len;
- int site_id;
- char *location_shape_buf;
- int location_shape_len;
- int srid;
-
- /* Check for the correct number of arguments entered. */
- if (argc < 2)
- {
- printf ("Usage: %s <datasource> [<srid>]\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 hazardous_sites", SQL_NTS);
- rc = SQLExecDirect (hstmt, (UCHAR *) "CREATE TABLE hazardous_sites (id integer, name varchar(128), location ST_Point)", SQL_NTS);
- returncode_check (NULL, hstmt, rc, "SQLExecDirect");
- /* Initialize entries for non-spatial columns in the table */
- site_id = 59;
- name = "Landmark Industrial";
- name_len = strlen(name);
-
- /* Populate the point */
- pt[0].x = 48235; pt[0].y = 29746;
- geom.type = geomPoint;
- geom.num_points = 1;
- geom.num_parts = 1;
- geom.pt = pt;
- /* Convert the point to an ESRI shape. */
- geom_to_shape (&geom, &max_alloced, &location_shape_len, &location_shape_buf);
-
- /* INFORMIX_EXTEST_BEGIN pointfromshape */
- /* Create the SQL insert statement to populate the hazardous_sites
- * table. The question marks are parameter markers that indicate
- * the column values that will be inserted at run time. */
- sprintf(sql_stmt,
- "INSERT INTO hazardous_sites (id, name, location) "
- "VALUES(?, ?, SE_PointFromShape(?, %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 site_id to the first parameter. */
- pcbvalue1 = 0;
- rc = SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG,
- SQL_INTEGER, 0, 0,
- &site_id, 0, &pcbvalue1);
- returncode_check (NULL, hstmt, rc, "SQLBindParameter");
- /* Bind the name to the second parameter. */
- pcbvalue2 = name_len;
- rc = SQLBindParameter (hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR,
- SQL_CHAR, name_len, 0,
- name, 0, &pcbvalue2);
- returncode_check (NULL, hstmt, rc, "SQLBindParameter");
- /* Bind the location geometry to the third parameter. */
- pcbvalue3 = location_shape_len;
- rc = SQLBindParameter (hstmt, 3, SQL_PARAM_INPUT, SQL_C_BINARY,
- SQL_INFX_UDT_LVARCHAR, location_shape_len, 0,
- location_shape_buf, location_shape_len, &pcbvalue3);
- returncode_check (NULL, hstmt, rc, "SQLBindParameter");
- /* Execute the insert statement. */
- rc = SQLExecute (hstmt);
- returncode_check (NULL, hstmt, rc, "SQLExecute");
- /* INFORMIX_EXTEST_END pointfromshape */
- 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");
- }
|