12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280 |
- /***********************************************************************
- *
- * load_shapes.ec -- Inserts of all shape types from binary.
- *
- *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- *
- * Purpose:
- * Tests inserts of all shape types from binary.
- *
- * The following command will likely compile this source:
- * esql -g -o load_shapes load_shapes.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 ESRI shapefile format data.
- */
- #include "commfuncs.h"
- #include "commfuncs.c"
- #include "shapefuncs.c"
- /*
- * Local function prototypes
- *
- */
- static void insert_shape (SpatialColumn *spatial_column,
- int id,
- GeomType geom_type,
- int data_len,
- char *binary);
- static void create_table (SpatialColumn *spatial_column);
- /************************************************************************
- *
- * main -- Inserts of all shape types from binary shape format into a
- * test table.
- *
- *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- *
- * Purpose:
- * Load a table with all types of geometry from shapes.
- *
- *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- *
- * Parameters:
- * argc <Input> == (int) The parameter index.
- * argv <Input> == (char[] *) Pointer to an array of parameters.
- *
- *************************************************************************/
- void main (int argc, char **argv)
- {
- /* Declare local host variables */
- EXEC SQL BEGIN DECLARE SECTION;
- char dbname[256];
- EXEC SQL END DECLARE SECTION;
- /* Declare other local variables */
- char *binary;
- int rc;
- int num_points;
- int num_parts;
- SpatialColumn sc;
- Geometry geom = {0};
- int offsets[6];
- Point pt[30];
- double z[30];
- double m[30];
- int data_len;
- int max_alloced = 0;
- /* Check for the correct number of arguments entered. */
- if (argc < 2)
- {
- printf ("\nUsage: %s <database> [<srid>]\n", argv[0]);
- printf ("\n");
- printf ("\nThis program will create a table named 'shape_test' and\n");
- printf ("insert data using various SE_xxxFromShape functions.\n");
- 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 */
- sc.srid = (argc > 2) ? atoi(argv[2]) : 0;
- /* Load the spatial column structure. */
- strcpy (sc.table, "shape_test");
- strcpy (sc.column, "geom");
- /* Create the test table */
- create_table (&sc);
- /* The remainder of the main function prepares the various
- geometry data types with valid X, Y coordinates, Z ordinates and
- measures. It passes these geometries to geom_to_shape function
- which converts them into shapes. The shapes are then passed to
- the insert_shape function which inserts the shapes into the spatial
- column of the table. Insert_shape also inserts an integer key into
- the table.
- */
-
- geom.offsets = offsets;
- geom.pt = pt;
- geom.z = z;
- geom.m = m;
-
- /**********/
- /* */
- /* POINTS */
- /* */
- /**********/
- /*======================================================================*/
- /* */
- /* Insert a single point. */
- /* */
- /*======================================================================*/
- /* Populate the point. */
- pt[0].x = 10; pt[0].y = -10;
- geom.type = geomPoint;
- geom.num_points = 1;
- geom.num_parts = 1;
- /* Convert the point to an ESRI shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
- /* Insert the shape into the table */
- insert_shape (&sc, 1, geomPoint, data_len, binary);
- /*======================================================================*/
- /* */
- /* Insert a point with a Z coordinate. */
- /* */
- /*======================================================================*/
- /* Populate the point. */
- pt[0].x = -50.123; pt[0].y = -50.1234567; z[0] = 50;
- geom.type = geomPointZ;
- geom.num_points = 1;
- geom.num_parts = 1;
- /* Convert the point to an ESRI shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
-
- /* Insert the shape into the table. */
- insert_shape (&sc, 2, geomPoint, data_len, binary);
-
- /*======================================================================*/
- /* */
- /* Insert a point with a measure. */
- /* */
- /*======================================================================*/
-
- /* Populate the point. */
- pt[0].x = -.123; pt[0].y = -.1234567; m[0] = 10;
-
- geom.type = geomPointM;
- geom.num_points = 1;
- geom.num_parts = 1;
- /* Convert the point to an ESRI shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
- /* Insert the shape into the table. */
- insert_shape (&sc, 3, geomPoint, data_len, binary);
- /*======================================================================*/
- /* */
- /* Insert a point with a Z coordinate and a measure. */
- /* */
- /*======================================================================*/
- /* Populate the point. */
- pt[0].x = 1; pt[0].y = 2; z[0] = 3; m[0] = 4;
- geom.type = geomPointZM;
- geom.num_points = 1;
- geom.num_parts = 1;
- /* Convert the point to an ESRI shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
-
- /* Insert the shape into the table. */
- insert_shape (&sc, 4, geomPoint, data_len, binary);
- /***************/
- /* */
- /* MULTIPOINTS */
- /* */
- /***************/
-
- /*======================================================================*/
- /* */
- /* Insert a multipoint containing 5 points. */
- /* */
- /*======================================================================*/
- /* Populate the multipoint */
- pt[0].x = 1; pt[0].y = 2;
- pt[1].x = 3; pt[1].y = 4;
- pt[2].x = 5; pt[2].y = 6;
- pt[3].x = 7; pt[3].y = 8;
- pt[4].x = 9; pt[4].y = 10;
-
- geom.type = geomMultiPoint;
- geom.num_points = 5;
- geom.num_parts = 5;
- /* Convert the multipoint into an ESRI shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
- /* Insert the shape into a table. */
- insert_shape (&sc, 5, geomMultiPoint, data_len, binary);
-
- /*======================================================================*/
- /* */
- /* Insert a multipoint containing a single point. */
- /* */
- /*======================================================================*/
- /* Populate the multipoint. */
- pt[0].x = 10.0; pt[0].y = 3.1415927;
- geom.type = geomMultiPoint;
- geom.num_points = 1;
- geom.num_parts = 1;
- /* Convert the multipoint into an ESRI shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
-
- /* Insert the shape into a table. */
- insert_shape (&sc, 6, geomMultiPoint, data_len, binary);
-
- /*======================================================================*/
- /* */
- /* Insert a multipoint containing a three points sharing the same */
- /* location. This multipoint is non-simple since some of its points */
- /* share the same location. */
- /* */
- /*======================================================================*/
- /* Populate the multipoint. */
- pt[0].x = 1; pt[0].y = 1;
- pt[1].x = 1; pt[1].y = 1;
- pt[2].x = 1; pt[2].y = 1;
- geom.type = geomMultiPoint;
- geom.num_points = 3;
- geom.num_parts = 3;
- /* Convert the multipoint into an ESRI shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
- /* Insert the shape into a table. */
- insert_shape (&sc, 7, geomMultiPoint, data_len, binary);
-
- /*======================================================================*/
- /* */
- /* Insert a multipoint containing a single point with a measure. */
- /* */
- /*======================================================================*/
- /* Populate the multipoint. */
- pt[0].x = -0.123; pt[0].y = -0.1234567; m[0] = 10;
-
- geom.type = geomMultiPointM;
- geom.num_points = 1;
- geom.num_parts = 1;
- /* Convert the multipoint into an ESRI shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
- /* Insert the shape into a multipoint. */
- insert_shape (&sc, 8, geomMultiPoint, data_len, binary);
-
- /*======================================================================*/
- /* */
- /* Insert a multipoint containing three points with z coordinates and */
- /* measures. */
- /* */
- /*======================================================================*/
- /* Populate the multipoint. */
- pt[0].x = 1; pt[0].y = 2; z[0] = 3; m[0] = 4;
- pt[1].x = 5; pt[1].y = 6; z[1] = 7; m[1] = 8;
- pt[2].x = 9; pt[2].y = 10; z[2] = 11; m[2] = 12;
- geom.type = geomMultiPointZM;
- geom.num_points = 3;
- geom.num_parts = 3;
- /* Convert the multipoint into an ESRI shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
-
- /* Insert the shape into a multipoint. */
- insert_shape (&sc, 9, geomMultiPoint, data_len, binary);
-
- /***************/
- /* */
- /* LINESTRINGS */
- /* */
- /***************/
-
- /*======================================================================*/
- /* */
- /* Insert a linestring containing two vertices. */
- /* */
- /*======================================================================*/
- /* Populate the linestring. */
- pt[0].x = -50.123; pt[0].y = -50.1234567;
- pt[1].x = 50; pt[1].y = 50;
- geom.type = geomLineString;
- geom.num_points = 2;
- geom.num_parts = 1;
- /* Convert the linestring into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
- /* Insert the shape into a table. */
- insert_shape (&sc, 10, geomLineString, data_len, binary);
-
- /*======================================================================*/
- /* */
- /* Insert a linestring contain 6 vertices. */
- /* */
- /*======================================================================*/
- /* Populate the linestring. */
- pt[0].x = 0; pt[0].y = 0;
- pt[1].x = 1; pt[1].y = 1;
- pt[2].x = 2; pt[2].y = 2;
- pt[3].x = 3; pt[3].y = 3;
- pt[4].x = 4.5; pt[4].y = 5.4;
- pt[5].x = 7; pt[5].y = 7;
- geom.type = geomLineString;
- geom.num_points = 6;
- geom.num_parts = 1;
- /* Convert the linestring into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
- /* Insert the shape into the table. */
- insert_shape (&sc, 11, geomLineString, data_len, binary);
-
- /*======================================================================*/
- /* */
- /* Insert a linestring that is a ring containing 4 vertices. */
- /* */
- /*======================================================================*/
- /* Populate the linestring. */
- pt[0].x = 10; pt[0].y = 10;
- pt[1].x = 10; pt[1].y = 20;
- pt[2].x = 20; pt[2].y = 10;
- pt[3].x = 10; pt[3].y = 10;
-
- geom.type = geomLineString;
- geom.num_points = 4;
- geom.num_parts = 1;
- /* Convert the linestring into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
- /* Insert the shape into the table. */
- insert_shape (&sc, 12, geomLineString, data_len, binary);
-
- /*======================================================================*/
- /* */
- /* Insert a linestring that intersects its interior and therefore is */
- /* non-simple. */
- /* */
- /*======================================================================*/
- /* Populate the linestring. */
- pt[0].x = 10; pt[0].y = 10;
- pt[1].x = 20; pt[1].y = 20;
- pt[2].x = 25; pt[2].y = 15;
- pt[3].x = 0; pt[3].y = 15;
- geom.type = geomLineString;
- geom.num_points = 4;
- geom.num_parts = 1;
- /* Convert the linestring into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
- /* Insert the shape into the table. */
- insert_shape (&sc, 13, geomLineString, data_len, binary);
-
- /*======================================================================*/
- /* */
- /* Insert a linestring containing two vertices with Z coordinates. */
- /* */
- /*======================================================================*/
-
- /* Populate the linestring. */
- pt[0].x = -50.123; pt[0].y = -50.1234567; z[0] = 50;
- pt[1].x = 1; pt[1].y = 2; z[1] = 60;
-
- geom.type = geomLineStringZ;
- geom.num_points = 2;
- geom.num_parts = 1;
- /* Convert the linestring into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
- /* Insert the shape into the table. */
- insert_shape (&sc, 14, geomLineString, data_len, binary);
-
- /*======================================================================*/
- /* */
- /* Insert a linestring containing two vertices with measures. */
- /* */
- /*======================================================================*/
- /* Populate the linestring. */
- pt[0].x = 1; pt[0].y = 2; m[0] = 3;
- pt[1].x = 4; pt[1].y = 5; m[1] = 6;
- geom.type = geomLineStringM;
- geom.num_points = 2;
- geom.num_parts = 1;
- /* Convert the linestring into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
- /* Insert the shape into the table. */
- insert_shape (&sc, 15, geomLineString, data_len, binary);
-
- /*======================================================================*/
- /* */
- /* Insert a linestring containing two vertices with Z ordinate and */
- /* measures. */
- /* */
- /*======================================================================*/
- /* Populate the linestring. */
- pt[0].x = 7; pt[0].y = 8; z[0] = 9; m[0] = 10;
- pt[1].x = 11; pt[1].y = 12; z[1] = 13; m[1] = 14;
-
- geom.type = geomLineStringZM;
- geom.num_points = 2;
- geom.num_parts = 1;
- /* Convert the linestring into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
- /* Insert the shape into the table. */
- insert_shape (&sc, 16, geomLineString, data_len, binary);
-
- /********************/
- /* */
- /* MULTILINESTRINGS */
- /* */
- /********************/
-
- /*======================================================================*/
- /* */
- /* Insert a multilinestring containing two linestrings that intersect */
- /* each others interiors. Since the elements of the multilinestring */
- /* intersect at their interior it is not simple. */
- /* */
- /*======================================================================*/
- /* The first linestring. */
-
- pt[0].x = 10; pt[0].y = 10; offsets[0] = 0;
- pt[1].x = 10; pt[1].y = 20;
- /* The second linestring. */
- pt[2].x = 5; pt[2].y = 15; offsets[1] = 2;
- pt[3].x = 15; pt[3].y = 15;
-
- geom.type = geomLineString;
- geom.num_points = 4;
- geom.num_parts = 2;
- /* Convert the multilinestring into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
-
- /* Insert the shape into the table. */
- insert_shape (&sc, 17, geomMultiLineString, data_len, binary);
- /*======================================================================*/
- /* */
- /* Insert a multilinestring containing four linestrings that do not */
- /* intersect. The multilinestring is simple since its interior is not */
- /* intersected. */
- /* */
- /*======================================================================*/
- /* The first linestring. */
- pt[0].x = 1; pt[0].y = 1; offsets[0] = 0;
- pt[1].x = 11; pt[1].y = 1;
- pt[2].x = 11; pt[2].y = -9;
- /* The second linestring. */
- pt[3].x = 4; pt[3].y = 10; offsets[1] = 3;
- pt[4].x = 4; pt[4].y = 20;
-
- /* The third linestring. */
- pt[5].x = 10; pt[5].y = 10; offsets[2] = 5;
- pt[6].x = 20; pt[6].y = 10;
-
- /* The fourth linestring. */
- pt[7].x = -10; pt[7].y = -10; offsets[3] = 7;
- pt[8].x = -20; pt[8].y = -10;
- pt[9].x = -30; pt[9].y = -10;
- pt[10].x = -30; pt[10].y = 0;
-
- geom.type = geomLineString;
- geom.num_points = 11;
- geom.num_parts = 4;
- /* Convert the multilinestring into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
- /* Insert the shape into the table. */
- insert_shape (&sc, 18, geomMultiLineString, data_len, binary);
-
- /*======================================================================*/
- /* */
- /* Insert a multilinestring containing 3 linestrings with Z ordinates. */
- /* */
- /*======================================================================*/
- /* The first linestring. */
-
- pt[0].x = 1; pt[0].y = 1; z[0] = 10; offsets[0] = 0;
- pt[1].x = 11; pt[1].y = 1; z[1] = 20;
- pt[2].x = 11; pt[2].y = 11; z[2] = 30;
- /* The second linestring. */
- pt[3].x = 4; pt[3].y = 10; z[3] = -1; offsets[1] = 3;
- pt[4].x = 4; pt[4].y = 20; z[4] = 2;
-
- /* The third linestring. */
- pt[5].x = 10; pt[5].y = 10; z[5] = 10;
- pt[6].x = 20; pt[6].y = 10; z[6] = 20; offsets[2] = 5;
-
- geom.type = geomLineStringZ;
- geom.num_points = 7;
- geom.num_parts = 3;
- /* Convert the multilinestring into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
-
- /* Insert the shape into the table. */
- insert_shape (&sc, 19, geomMultiLineString, data_len, binary);
-
- /*======================================================================*/
- /* */
- /* Insert a multilinestring containing 2 linestrings with measures. */
- /* */
- /*======================================================================*/
-
- /* The first linestring. */
-
- pt[0].x = 1; pt[0].y = 1; m[0] = 10; offsets[0] = 0;
- pt[1].x = 1; pt[1].y = 10; m[1] = 20;
- /* The second linestring. */
- pt[2].x = 11; pt[2].y = 11; m[2] = 30; offsets[1] = 2;
- pt[3].x = 21; pt[3].y = 10; m[3] = -1;
-
- geom.type = geomLineStringM;
- geom.num_points = 4;
- geom.num_parts = 2;
- /* Convert the multilinestring into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
-
- /* Insert the shape into the table. */
- insert_shape (&sc, 20, geomMultiLineString, data_len, binary);
-
- /*======================================================================*/
- /* */
- /* Insert a multilinestring comtaining two linestrings with Z ordinates */
- /* and measures. */
- /* */
- /*======================================================================*/
- /* The first linestring. */
- pt[0].x = 1; pt[0].y = 1; z[0] = 10; m[0] = 10; offsets[0] = 0;
- pt[1].x = 11; pt[1].y = 1; z[1] = 15; m[1] = 20;
- pt[2].x = 11; pt[2].y = 11; z[2] = 25; m[2] = 30;
-
- /* The second linestring. */
- pt[3].x = 4; pt[3].y = 10; z[3] = 35; m[3] = 40; offsets[1] = 3;
- pt[4].x = 4; pt[4].y = 20; z[4] = 45; m[4] = 50;
-
- geom.type = geomLineStringZM;
- geom.num_points = 5;
- geom.num_parts = 2;
- /* Convert the multilinestring into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
- /* Insert the shape into the table. */
- insert_shape (&sc, 21, geomMultiLineString, data_len, binary);
-
-
- /************/
- /* */
- /* POLYGONS */
- /* */
- /************/
-
-
- /*======================================================================*/
- /* */
- /* Insert a polygon with no interior rings. */
- /* */
- /*======================================================================*/
- /* The exterior ring */
- pt[0].x = 10; pt[0].y = 10;
- pt[1].x = 10; pt[1].y = 20;
- pt[2].x = 20; pt[2].y = 20;
- pt[3].x = 20; pt[3].y = 10;
- pt[4].x = 10; pt[4].y = 10;
- geom.type = geomPolygon;
- geom.num_points = 5;
- geom.num_parts = 1;
- /* Convert the polygon into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
- /* Insert the shape into a table. */
- insert_shape (&sc, 22, geomPolygon, data_len, binary);
- /*======================================================================*/
- /* */
- /* Insert a polygon with one interior ring. */
- /* */
- /*======================================================================*/
- /* The exterior ring. */
- pt[0].x = 10; pt[0].y = 10; offsets[0] = 0;
- pt[1].x = 10; pt[1].y = 20;
- pt[2].x = 20; pt[2].y = 20;
- pt[3].x = 20; pt[3].y = 10;
- pt[4].x = 10; pt[4].y = 10;
-
- /* The interior ring. */
- pt[5].x = 12; pt[5].y = 12; offsets[1] = 5;
- pt[6].x = 12; pt[6].y = 13;
- pt[7].x = 13; pt[7].y = 13;
- pt[8].x = 13; pt[8].y = 12;
- pt[9].x = 12; pt[9].y = 12;
-
- geom.type = geomPolygon;
- geom.num_points = 10;
- geom.num_parts = 2;
- /* Convert the polygon into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
- /* Insert the shape into the table. */
- insert_shape (&sc, 23, geomPolygon, data_len, binary);
-
- /*======================================================================*/
- /* */
- /* Insert a polygon with 2 interior rings and Z ordinates. */
- /* */
- /*======================================================================*/
- /* The exterior ring. */
- pt[0].x = 10; pt[0].y = 10; z[0] = 1; offsets[0] = 0;
- pt[1].x = 10; pt[1].y = 20; z[1] = 2;
- pt[2].x = 20; pt[2].y = 20; z[2] = 3;
- pt[3].x = 20; pt[3].y = 10; z[3] = 4;
- pt[4].x = 10; pt[4].y = 10; z[4] = 1;
-
- /* The first interior ring. */
- pt[5].x = 12; pt[5].y = 12; z[5] = 6; offsets[1] = 5;
- pt[6].x = 12; pt[6].y = 13; z[6] = 7;
- pt[7].x = 13; pt[7].y = 13; z[7] = 8;
- pt[8].x = 13; pt[8].y = 12; z[8] = 9;
- pt[9].x = 12; pt[9].y = 12; z[9] = 6;
-
- /* The second interior ring. */
- pt[10].x = 16; pt[10].y = 16; z[10] = 11; offsets[2] = 10;
- pt[11].x = 16; pt[11].y = 18; z[11] = 12;
- pt[12].x = 18; pt[12].y = 18; z[12] = 13;
- pt[13].x = 18; pt[13].y = 16; z[13] = 14;
- pt[14].x = 16; pt[14].y = 16; z[14] = 11;
-
- geom.type = geomPolygonZ;
- geom.num_points = 15;
- geom.num_parts = 3;
- /* Convert the polygon into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
-
- /* Insert the shape into a table. */
- insert_shape (&sc, 24, geomPolygon, data_len, binary);
-
- /*======================================================================*/
- /* */
- /* Insert a polygon containing no interior rings with measures. */
- /* */
- /*======================================================================*/
-
- /* The exterior ring. */
- pt[0].x = 10; pt[0].y = 10; m[0] = 100;
- pt[1].x = 10; pt[1].y = 20; m[1] = -1;
- pt[2].x = 20; pt[2].y = 20; m[2] = 300;
- pt[3].x = 20; pt[3].y = 10; m[3] = -2;
- pt[4].x = 10; pt[4].y = 10; m[4] = 400;
- geom.type = geomPolygonM;
- geom.num_points = 5;
- geom.num_parts = 1;
- /* Convert the polygon into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
-
- /* Insert the shape into a table. */
- insert_shape (&sc, 25, geomPolygon, data_len, binary);
- /*======================================================================*/
- /* */
- /* Insert a polygon with Z ordinates measures but no interior rings. */
- /* */
- /*======================================================================*/
- /* The exterior ring. */
- pt[0].x = 20; pt[0].y = 20; z[0] = 100; m[0] = -1;
- pt[1].x = 20; pt[1].y = 30; z[1] = -1; m[1] = -2;
- pt[2].x = 30; pt[2].y = 30; z[2] = 300; m[2] = -3;
- pt[3].x = 30; pt[3].y = 20; z[3] = -2; m[3] = -5;
- pt[4].x = 20; pt[4].y = 20; z[4] = 100; m[4] = -1;
-
- geom.type = geomPolygonZM;
- geom.num_points = 5;
- geom.num_parts = 1;
- /* Convert the polygon into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
- /* Insert the shape into a table. */
- insert_shape (&sc, 26, geomPolygon, data_len, binary);
-
- /******************/
- /* */
- /* MULTIPOLYGONS */
- /* */
- /******************/
-
- /*======================================================================*/
- /* */
- /* Insert a multipolygon. The first polygon contains one interior ring, */
- /* the second polygon contains no interior rings, and the third polygon */
- /* contains two interior rings. */
- /* */
- /*======================================================================*/
- /* The first polygon's exterior ring. */
- pt[0].x = 10; pt[0].y = 10; offsets[0] = 0;
- pt[1].x = 10; pt[1].y = 20;
- pt[2].x = 20; pt[2].y = 20;
- pt[3].x = 20; pt[3].y = 10;
- pt[4].x = 10; pt[4].y = 10;
-
- /* The first polygon's interior ring. */
- pt[5].x = 14; pt[5].y = 14; offsets[1] = 5;
- pt[6].x = 14; pt[6].y = 16;
- pt[7].x = 16; pt[7].y = 16;
- pt[8].x = 16; pt[8].y = 14;
- pt[9].x = 14; pt[9].y = 14;
-
- /* The second polygon's exterior ring. */
- pt[10].x = 10; pt[10].y = 10; offsets[2] = 10;
- pt[11].x = 10; pt[11].y = 20;
- pt[12].x = 20; pt[12].y = 20;
- pt[13].x = 20; pt[13].y = 10;
- pt[14].x = 10; pt[14].y = 10;
-
- /* The third polygon's exterior ring. */
- pt[15].x = 50; pt[15].y = 50; offsets[3] = 15;
- pt[16].x = 50; pt[16].y = 60;
- pt[17].x = 60; pt[17].y = 60;
- pt[18].x = 60; pt[18].y = 50;
- pt[19].x = 50; pt[19].y = 50;
-
- /* The third polygon's interior ring. */
- pt[20].x = 52; pt[20].y = 52; offsets[4] = 20;
- pt[21].x = 52; pt[21].y = 54;
- pt[22].x = 54; pt[22].y = 54;
- pt[23].x = 54; pt[23].y = 52;
- pt[24].x = 52; pt[24].y = 52;
-
- /* The second polygon's interior ring. */
- pt[25].x = 56; pt[25].y = 56; offsets[5] = 25;
- pt[26].x = 56; pt[26].y = 58;
- pt[27].x = 58; pt[27].y = 58;
- pt[28].x = 58; pt[28].y = 56;
- pt[29].x = 56; pt[29].y = 56;
-
- geom.type = geomMultiPolygon;
- geom.num_points = 30;
- geom.num_parts = 6;
- /* Convert the multipolygon into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
-
- /* Insert the shape into the table. */
- insert_shape (&sc, 27, geomMultiPolygon, data_len, binary);
- /*======================================================================*/
- /* */
- /* Insert a multipolygon containing two polygons with Z ordinates */
- /* and no interior rings. */
- /* */
- /*======================================================================*/
-
- /* The first polygon's exterior ring. */
- pt[0].x = 10; pt[0].y = 10; z[0] = 1; offsets[0] = 0;
- pt[1].x = 10; pt[1].y = 20; z[1] = 2;
- pt[2].x = 20; pt[2].y = 20; z[2] = 3;
- pt[3].x = 20; pt[3].y = 10; z[3] = 4;
- pt[4].x = 10; pt[4].y = 10; z[4] = 1;
-
- /* The second polygon's exterior ring. */
- pt[5].x = 1; pt[5].y = 1; z[5] = 1; offsets[1] = 5;
- pt[6].x = 1; pt[6].y = 5; z[6] = 2;
- pt[7].x = 5; pt[7].y = 5; z[7] = 3;
- pt[8].x = 5; pt[8].y = 1; z[8] = 4;
- pt[9].x = 1; pt[9].y = 1; z[9] = 1;
- geom.type = geomMultiPolygonZ;
- geom.num_points = 10;
- geom.num_parts = 2;
- /* Convert the multipolygon into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
-
- /* Insert the shape into a table. */
- insert_shape (&sc, 28, geomMultiPolygon, data_len, binary);
-
- /*======================================================================*/
- /* */
- /* Insert a multipolygon containing two polygons with measures and no */
- /* interior rings. */
- /* */
- /*======================================================================*/
-
- /* The first polygon's exterior ring. */
- pt[0].x = 10; pt[0].y = 10; m[0] = 1; offsets[0] = 0;
- pt[1].x = 10; pt[1].y = 20; m[1] = 2;
- pt[2].x = 20; pt[2].y = 20; m[2] = 3;
- pt[3].x = 20; pt[3].y = 10; m[3] = 4;
- pt[4].x = 10; pt[4].y = 10; m[4] = 1;
-
- /* The second polygon's exterior ring. */
- pt[5].x = 1; pt[5].y = 1; m[5] = 5; offsets[1] = 5;
- pt[6].x = 1; pt[6].y = 5; m[6] = 6;
- pt[7].x = 5; pt[7].y = 5; m[7] = 7;
- pt[8].x = 5; pt[8].y = 1; m[8] = 8;
- pt[9].x = 1; pt[9].y = 1; m[9] = 9;
-
- geom.type = geomMultiPolygonM;
- geom.num_points = 10;
- geom.num_parts = 2;
- /* Convert the multipolygon into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
- /* Insert the shape into the table. */
- insert_shape (&sc, 29, geomMultiPolygon, data_len, binary);
- /*======================================================================*/
- /* */
- /* Insert a multipolygon containing a two polygons with Z ordinates and */
- /* measures. The first polygon contains an interior ring, but the */
- /* second one does not. */
- /* */
- /*======================================================================*/
- /* The first polygon's exterior ring. */
- pt[0].x = 10; pt[0].y = 10; z[0] = 1; m[0] = 6; offsets[0] = 0;
- pt[1].x = 10; pt[1].y = 20; z[1] = 2; m[1] = 7;
- pt[2].x = 20; pt[2].y = 20; z[2] = 3; m[2] = 8;
- pt[3].x = 20; pt[3].y = 10; z[3] = 4; m[3] = 9;
- pt[4].x = 10; pt[4].y = 10; z[4] = 1; m[4] = 10;
-
- /* The first polygon's interior ring. */
- pt[5].x = 14; pt[5].y = 14; z[5] = -1; m[5] = -2; offsets[1] = 5;
- pt[6].x = 14; pt[6].y = 16; z[6] = 3; m[6] = 4;
- pt[7].x = 16; pt[7].y = 16; z[7] = -5; m[7] = 6;
- pt[8].x = 16; pt[8].y = 14; z[8] = 7; m[8] = -8;
- pt[9].x = 14; pt[9].y = 14; z[9] = -1; m[9] = -10.01;
-
- /* The second polygon's exterior ring. */
- pt[10].x = 1; pt[10].y = 1; z[10] = 9; m[10] = 6; offsets[2] = 10;
- pt[11].x = 1; pt[11].y = 5; z[11] = 6; m[11] = 7;
- pt[12].x = 5; pt[12].y = 5; z[12] = 7; m[12] = 8;
- pt[13].x = 5; pt[13].y = 1; z[13] = 8; m[13] = 9;
- pt[14].x = 1; pt[14].y = 1; z[14] = 9; m[14] = 16;
-
- geom.type = geomMultiPolygonZM;
- geom.num_points = 15;
- geom.num_parts = 3;
- /* Convert the multipolygon into a shape. */
- geom_to_shape (&geom, &max_alloced, &data_len, &binary);
-
- /* Insert the shape into the table. */
- insert_shape (&sc, 30, geomMultiPolygon, data_len, binary);
- /*===============================*/
- /* */
- /* Disconnect from the database. */
- /* */
- /*===============================*/
-
- EXEC SQL disconnect current;
- }
- /***********************************************************************
- *
- * create_table - Creates a table for inserting spatial data
- *
- *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- *
- * Purpose:
- *
- *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- *
- * Parameters:
- * sc <Input> == (SpatialColumn *) Spatial column.
- *
- * RETURN <Output> == (void)
- *
- ***********************************************************************/
- static void create_table (
- SpatialColumn *sc
- )
- {
- EXEC SQL BEGIN DECLARE SECTION;
- char sql_stmt[256];
- EXEC SQL END DECLARE SECTION;
-
- sprintf (sql_stmt, "DROP TABLE %s", sc->table);
- EXEC SQL execute immediate :sql_stmt;
- sprintf (sql_stmt, "CREATE TABLE %s (id integer, %s ST_Geometry)",
- sc->table, sc->column);
- EXEC SQL execute immediate :sql_stmt;
- }
- /***********************************************************************
- *
- * insert_shape - Inserts the shape and integer id values
- * the spatial and id columns of the table
- * specified as arguments to the main program.
- *
- *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- *
- * Purpose:
- * The function inserts the shape into the
- * spatial column and an integer into the id column.
- *
- *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- *
- * Parameters:
- * sc <Input> == (SpatialColumn *) Spatial column.
- * id <Input> == (int) The integer id value.
- * type <Input> == (GeomType) The data type of the geometry.
- * data_len <Input> == (int) The number of bytes in the shape.
- * shape_buffer <Input> == (char *) The shape.
- *
- * RETURN <Output> == (void)
- *
- ***********************************************************************/
- static void insert_shape (
- SpatialColumn *sc,
- int id,
- GeomType type,
- int data_len,
- char *shape_buffer
- )
- {
- EXEC SQL BEGIN DECLARE SECTION;
- int i, n, xid;
- short typ;
- char shape_sql[256];
- var binary shape;
- EXEC SQL END DECLARE SECTION;
- int rc;
- char shapefunction[19];
- /* Set the shapefunction to the "FromShape" function that corresponds */
- /* to the data type of the geometry representation. */
- switch (type)
- {
- case geomPoint:
- strcpy (shapefunction,"SE_PointFromShape");
- break;
- case geomLineString:
- strcpy (shapefunction,"SE_LineFromShape");
- break;
- case geomPolygon:
- strcpy (shapefunction,"SE_PolyFromShape");
- break;
- case geomMultiPoint:
- strcpy (shapefunction,"SE_MPointFromShape");
- break;
- case geomMultiLineString:
- strcpy (shapefunction,"SE_MLineFromShape");
- break;
- case geomMultiPolygon:
- strcpy (shapefunction,"SE_MPolyFromShape");
- break;
- }
- /* Generate the SQL insert expression. The integer value and the */
- /* name of the spatial column are entered as bind parameters. */
- sprintf (shape_sql,
- "insert into %s (id, %s) values (?, %s (?, %d))",
- sc->table, sc->column, shapefunction, sc->srid);
- /* Prepare the SQL statement for execution. */
- fprintf (stdout, "SQL is %s \n", shape_sql);
- EXEC SQL prepare ins_stmt from :shape_sql;
- /* Allocate a statement descriptor. */
- EXEC SQL allocate descriptor 'ins_desc';
- /* Set the number of input parameters */
- n = 2;
- EXEC SQL set descriptor 'ins_desc' COUNT = :n;
- /* Bind the input id to the first input parameter */
- n = 1; typ = SQLINT; i = id;
- EXEC SQL set descriptor 'ins_desc' VALUE :n
- TYPE = :typ,
- DATA = :i;
- /* Create host variable for the shape. */
- if ((rc = ifx_var_alloc(&shape,data_len)) < 0)
- {
- fprintf(stderr, "Error calling ifx_var_alloc.");
- exit(1);
- }
- if ((rc = ifx_var_setdata(&shape,shape_buffer,data_len)) < 0)
- {
- fprintf(stderr, "Error calling ifx_var_setdata.");
- exit(1);
- }
- if ((rc = ifx_var_setlen(&shape,data_len)) < 0)
- {
- fprintf(stderr, "Error calling ifx_var_setlen.");
- exit(1);
- }
- /* Bind the shape to the second input parameter */
- n = 2; typ = SQLUDTVAR; xid = XID_LVARCHAR;
- EXEC SQL set descriptor 'ins_desc' VALUE :n
- TYPE = :typ,
- EXTYPEID = :xid,
- DATA = :shape;
- /* Execute the insert statement. */
- EXEC SQL execute ins_stmt using sql descriptor 'ins_desc';
- /* Free resources associated with the shape host variable */
- if ((rc = ifx_var_dealloc(&shape)) < 0)
- {
- fprintf(stderr, "Error calling ifx_var_dealloc.");
- exit(1);
- }
- /* Free resources associated with INSERT statement */
- EXEC SQL deallocate descriptor 'ins_desc';
- EXEC SQL free ins_stmt;
- }
|