123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- /**************************************************************************
- *
- * asbinary.c
- *
- * This program demonstrates how to retrieve spatial data from a database
- * in OGIS well-known-binary data format using the Informix Spatial
- * DataBlade's ST_AsBinary function.
- *
- * The following command will likely compile this source:
- *
- * cc -o asbinary asbinary.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 OGIS well-known-binary format data.
- */
- #include "commfuncs.h"
- #include "commfuncs.c"
- #include "wkbfuncs.c"
- /*
- * Local function prototypes
- */
- void draw_polygon (char *wkb_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 suboffsets[5];
- int max_alloced = 0;
- char fetched_wkb_buf[10000];
- int fetched_wkb_len;
- char *query_wkb_buf;
- int query_wkb_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");
- 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");
- /* Populate the query polygon point array */
- suboffsets[0] = 0;
- 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.num_subparts = 1;
- geom.suboffsets = suboffsets;
- geom.pt = pt;
- /* Convert the points to a Well Known Binary
- * representation of a polygon. */
- geom_to_wkb (&geom, &max_alloced, &query_wkb_len, &query_wkb_buf);
- /* INFORMIX_EXTEST_BEGIN asbinary */
- /* Create the SQL expression. */
- sprintf(sql_stmt,
- "SELECT ST_AsBinary(zone) "
- "FROM sensitive_areas WHERE "
- "SE_EnvelopesIntersect(zone,ST_PolyFromWKB(?,%d))", srid);
-
- /* Prepare the SQL statement. */
- SQLPrepare(hstmt, (UCHAR *) sql_stmt, SQL_NTS);
- returncode_check (NULL, hstmt, rc, "SQLPrepare");
- /* Bind the query shape parameter. */
- pcbvalue1 = query_wkb_len;
- SQLBindParameter (hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY,
- SQL_INFX_UDT_LVARCHAR, query_wkb_len, 0,
- query_wkb_buf, query_wkb_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 buildingfootprint polygons)
- * to the fetched_binary variable. */
- SQLBindCol (hstmt, 1, SQL_C_BINARY,
- fetched_wkb_buf, 10000, &fetched_wkb_len);
- /* Fetch each polygon within the display window and display it. */
- while (1)
- {
- rc = SQLFetch(hstmt);
- if (rc == SQL_NO_DATA_FOUND)
- break;
- else
- returncode_check(NULL, hstmt, rc, "SQLFetch");
-
- draw_polygon(fetched_wkb_buf);
- }
- /* Close the result set cursor */
- SQLCloseCursor(hstmt);
-
- /* INFORMIX_EXTEST_END asbinary */
- 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 *wkb_buf)
- {
- Geometry geom = {0};
-
- /* Convert the incoming well-known binary byte stream
- * into a Geometry data structure. */
- wkb_to_geom (wkb_buf, &geom);
- /* The remainder of this function is left as an exercise for the reader */
- }
|