123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- /**************************************************************************
- *
- * asbinary.ec
- *
- * 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:
- * esql -g -o asbinary asbinary.ec
- *
- **************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <float.h>
- EXEC SQL include sqltypes;
- EXEC SQL include exp_chk.ec;
- /*
- * 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);
- /*
- * Main program
- *
- */
- void main (int argc, char **argv)
- {
- /* Declare local host variables */
- EXEC SQL BEGIN DECLARE SECTION;
- int i, n, xid;
- short typ;
- char dbname[256];
- char sql_stmt[200];
- var binary query_wkb;
- var binary 'st_geometry' fetched_wkb;
- EXEC SQL END DECLARE SECTION;
- /* Declare other local variables */
- int rc;
- Geometry geom = {0};
- Point pt[30];
- int suboffsets[5];
- int max_alloced = 0;
- char *fetched_wkb_buf;
- 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 ("\nUsage: %s <database> [<srid>]\n", argv[0]);
- exit (1);
- }
- /* Define exception handling routine for EXEC SQL statements */
- EXEC SQL whenever sqlerror CALL ignore206;
- /* Connect to the database. */
- sprintf(dbname, "%s", argv[1]);
- EXEC SQL connect to :dbname;
- /* Obtain srid to use for inserts */
- srid = (argc > 2) ? atoi(argv[2]) : 0;
- EXEC SQL DROP TABLE sensitive_areas;
- EXEC SQL CREATE TABLE sensitive_areas (id integer, zone ST_Polygon);
- 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);
- EXEC SQL execute immediate :sql_stmt;
- sprintf(sql_stmt,
- "INSERT INTO sensitive_areas VALUES(2, "
- "'%d polygon((22 18,28 18,28 12,22 12,22 18))')", srid);
- EXEC SQL execute immediate :sql_stmt;
- sprintf(sql_stmt,
- "INSERT INTO sensitive_areas VALUES(3, "
- "'%d polygon((42 18,48 18,48 13,42 13,42 18))')", srid);
- EXEC SQL execute immediate :sql_stmt;
- /* 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 WKB representation of a polygon. */
- geom_to_wkb (&geom, &max_alloced, &query_wkb_len, &query_wkb_buf);
- /* Create host variable for WKB representation */
- if ((rc = ifx_var_alloc(&query_wkb,query_wkb_len)) < 0)
- {
- fprintf(stderr, "Error calling ifx_var_alloc.");
- exit(1);
- }
- if ((rc = ifx_var_setdata(&query_wkb,query_wkb_buf,query_wkb_len)) < 0)
- {
- fprintf(stderr, "Error calling ifx_var_setdata.");
- exit(1);
- }
- if ((rc = ifx_var_setlen(&query_wkb,query_wkb_len)) < 0)
- {
- fprintf(stderr, "Error calling ifx_var_setlen.");
- exit(1);
- }
- /* Create the SELECT statement. */
- sprintf(sql_stmt,
- "SELECT ST_AsBinary(zone) "
- "FROM sensitive_areas WHERE "
- "SE_EnvelopesIntersect(zone,ST_PolyFromWKB(?,%d))", srid);
- /* Prepare the SELECT statement. */
- EXEC SQL prepare sel_stmt from :sql_stmt;
- /* Declare cursor for the SELECT statement. */
- EXEC SQL declare sel_curs cursor for sel_stmt;
- /* Allocate a statement descriptor. */
- EXEC SQL allocate descriptor 'sel_desc';
- /* Set the number of input parameters */
- n = 1;
- EXEC SQL set descriptor 'sel_desc' COUNT = :n;
- /* Bind the query WKB to the input parameter */
- typ = SQLUDTVAR;
- xid = XID_LVARCHAR;
- EXEC SQL set descriptor 'sel_desc' VALUE :n
- TYPE = :typ,
- EXTYPEID = :xid,
- DATA = :query_wkb;
- /* Open cursor for the SELECT statement. */
- EXEC SQL open sel_curs using sql descriptor 'sel_desc';
- /* Fetch each polygon within the display window and display it. */
- while (1)
- {
- EXEC SQL fetch sel_curs into :fetched_wkb;
- if (sqlca.sqlcode == SQLNOTFOUND)
- break;
- printf("Fetched row.\n");
- if ((fetched_wkb_len = ifx_var_getlen(&fetched_wkb)) == (int) NULL)
- {
- fprintf(stderr, "Error calling ifx_var_getlen.");
- exit(1);
- }
- if ((fetched_wkb_buf = ifx_var_getdata(&fetched_wkb)) == NULL)
- {
- fprintf(stderr, "Error calling ifx_var_getdata.");
- exit(1);
- }
- draw_polygon(fetched_wkb_buf);
- if ((rc = ifx_var_dealloc(&fetched_wkb)) < 0)
- {
- fprintf(stderr, "Error calling ifx_var_dealloc.");
- exit(1);
- }
- }
- /* Close the result set cursor */
- EXEC SQL close sel_curs;
- /* Deallocate host variable for WKB representation */
- if ((rc = ifx_var_dealloc(&query_wkb)) < 0)
- {
- fprintf(stderr, "Error calling ifx_var_dealloc.");
- exit(1);
- }
- /* Free resources associated with SELECT statement */
- EXEC SQL deallocate descriptor 'sel_desc';
- EXEC SQL free sel_curs;
- EXEC SQL free sel_stmt;
- /* Disconnect from the database */
- EXEC SQL disconnect current;
- printf( "\nTest Complete\n");
- }
- /*
- * Local function draw_polygon
- *
- */
- 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 */
- }
|