123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- /**************************************************************************
- *
- * asshape.c
- *
- * This program demonstrates how to retrieve spatial data from a database
- * in ESRI shapefile format using the Informix Spatial DataBlade's
- * SE_AsShape function.
- *
- * The following command will likely compile this source:
- *
- * cc -o asshape asshape.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"
- /*
- * Local function prototypes
- */
- void draw_polygon (char *shape_buf);
- void main (int argc, char **argv)
- {
- SQLHDBC hdbc;
- SQLHENV henv;
- SQLHSTMT hstmt;
- char sql_stmt[200];
- int rc;
- SDWORD pcbvalue1;
- Geometry geom = {0};
- Point pt[30];
- int max_alloced = 0;
- char fetched_shape_buf[10000];
- int fetched_shape_len;
- char *query_shape_buf;
- int query_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");
- /* Populate the query polygon point array */
- pt[0].x = 10; pt[0].y = 10;
- pt[1].x = 25; pt[1].y = 10;
- pt[2].x = 25; pt[2].y = 20;
- pt[3].x = 10; pt[3].y = 20;
- pt[4].x = 10; pt[4].y = 10;
- 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, &query_shape_len, &query_shape_buf);
- sprintf(sql_stmt,
- "DROP TABLE sensitive_areas");
- rc = SQLExecDirect (hstmt, (UCHAR *) sql_stmt, SQL_NTS);
-
- sprintf(sql_stmt,
- "CREATE TABLE sensitive_areas (id integer, zone ST_Polygon)");
- rc = SQLExecDirect (hstmt, (UCHAR *) sql_stmt, SQL_NTS);
- returncode_check (NULL, hstmt, rc, "SQLExecDirect");
- sprintf(sql_stmt,
- "INSERT INTO sensitive_areas VALUES(1, "
- "'%d polygon((22 45,22 58,28 58,28 42,25 42,25 45,22 45))')",srid);
- rc = SQLExecDirect (hstmt, (UCHAR *) sql_stmt, SQL_NTS);
- returncode_check (NULL, hstmt, rc, "SQLExecDirect");
- sprintf(sql_stmt,
- "INSERT INTO sensitive_areas VALUES(2, "
- "'%d polygon((22 18,28 18,28 12,22 12,22 18))')", srid);
- rc = SQLExecDirect (hstmt, (UCHAR *) sql_stmt, SQL_NTS);
- returncode_check (NULL, hstmt, rc, "SQLExecDirect");
- sprintf(sql_stmt,
- "INSERT INTO sensitive_areas VALUES(3, "
- "'%d polygon((42 18,48 18,48 13,42 13,42 18))')", srid);
- rc = SQLExecDirect (hstmt, (UCHAR *) sql_stmt, SQL_NTS);
- returncode_check (NULL, hstmt, rc, "SQLExecDirect");
- /* INFORMIX_EXTEST_BEGIN asshape */
- /* Create the SQL expression. */
- sprintf(sql_stmt,
- "SELECT SE_AsShape(zone) "
- "FROM sensitive_areas WHERE "
- "SE_EnvelopesIntersect(zone,SE_PolyFromShape(?,%d))", srid);
- /* Prepare the SQL statement. */
- SQLPrepare(hstmt, (UCHAR *)sql_stmt, SQL_NTS);
- returncode_check (NULL, hstmt, rc, "SQLPrepare");
- /* Bind the query geometry parameter. */
- pcbvalue1 = query_shape_len;
- SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
- SQL_INFX_UDT_LVARCHAR, query_shape_len, 0,
- query_shape_buf, query_shape_len, &pcbvalue1);
- returncode_check (NULL, hstmt, rc, "SQLBindParameter");
- /* Execute the query. */
- rc = SQLExecute(hstmt);
- returncode_check (NULL, hstmt, rc, "SQLExecute");
- /* Assign the results of the query (the Zone polygons) to the
- fetched_shape_buf variable. */
- SQLBindCol (hstmt, 1, SQL_C_BINARY,
- fetched_shape_buf, 10000, &fetched_shape_len);
- /* Fetch each polygon within the display window and display it. */
- while (SQL_SUCCESS == (rc = SQLFetch(hstmt))) {
- draw_polygon(fetched_shape_buf);
- }
- if (rc != SQL_NO_DATA_FOUND) {
- returncode_check(NULL, hstmt, rc, "SQLFetch");
- }
- /* INFORMIX_EXTEST_END asshape */
- 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");
- }
- void draw_polygon (char *shape_buf)
- {
- Geometry geom = {0};
-
- /* Convert the incoming shapefile format data stream
- * into a Geometry data structure. */
- shape_to_geom (shape_buf, &geom);
- /* The remainder of this function is left as an exercise for the reader */
- }
|