load_wkb.ec 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. /***********************************************************************
  2. *
  3. * load_wkb.ec -- Loads data in Well Known Binary data format.
  4. *
  5. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  6. *
  7. * Purpose:
  8. * Creates a sample dataset using OGIS Well Known Binary functions
  9. *
  10. * The following command will likely compile this source:
  11. * esql -g -o load_wkb load_wkb.ec
  12. *
  13. ***********************************************************************/
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <float.h>
  18. EXEC SQL include sqltypes;
  19. EXEC SQL include exp_chk.ec;
  20. /*
  21. * Constants, macros, typedefs, data structures, & functions for
  22. * processing OGIS well-known-binary format data.
  23. */
  24. #include "commfuncs.h"
  25. #include "commfuncs.c"
  26. #include "wkbfuncs.c"
  27. /*
  28. * Local function prototypes
  29. *
  30. */
  31. static void insert_wkb (SpatialColumn *spatial_column,
  32. int id,
  33. WkbType type,
  34. int data_len,
  35. char *binary);
  36. static void create_table (SpatialColumn *spatial_column);
  37. /************************************************************************
  38. *
  39. * main -- Inserts of all wkb types from binary data into a test table.
  40. *
  41. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  42. *
  43. * Purpose:
  44. * Load a table with all types of geometry from wkb format.
  45. *
  46. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  47. *
  48. * Parameters:
  49. * argc <Input> == (int) The parameter index.
  50. * argv <Input> == (char[] *) Pointer to an array of parameters.
  51. *
  52. *************************************************************************/
  53. void main (int argc, char **argv)
  54. {
  55. /* Declare local host variables */
  56. EXEC SQL BEGIN DECLARE SECTION;
  57. char dbname[256];
  58. EXEC SQL END DECLARE SECTION;
  59. /* Declare other local variables */
  60. char *binary;
  61. int rc;
  62. SpatialColumn sc;
  63. Geometry geom = {0};
  64. int offsets[10];
  65. int suboffsets[10];
  66. Point pt[30];
  67. int data_len;
  68. int max_alloced = 0;
  69. /* Check for the correct number of arguments entered. */
  70. if (argc < 2)
  71. {
  72. printf ("\nUsage: %s <database> [<srid>]\n", argv[0]);
  73. printf ("\n");
  74. printf ("\nThis program will create a table named 'wkb_test' and\n");
  75. printf ("insert data using various ST_xxxFromWkb functions.\n");
  76. exit (1);
  77. }
  78. /* Define exception handling routine for EXEC SQL statements */
  79. EXEC SQL whenever sqlerror CALL ignore206;
  80. /* Connect to the database. */
  81. sprintf(dbname, "%s", argv[1]);
  82. EXEC SQL connect to :dbname;
  83. /* Obtain srid to use for inserts */
  84. sc.srid = (argc > 2) ? atoi(argv[2]) : 0;
  85. /* Load the spatial column structure. */
  86. strcpy (sc.table, "wkb_test");
  87. strcpy (sc.column, "geom");
  88. /* Create the test table */
  89. create_table (&sc);
  90. /* The remainder of the main function prepares the various WKB
  91. geometry structures with valid X,Y coordinate data and
  92. passes these structures to the insert_wkb function. insert_wkb
  93. inserts the geometry into the spatial column of the
  94. table, along with an integer id value.
  95. */
  96. geom.offsets = offsets;
  97. geom.suboffsets = suboffsets;
  98. geom.pt = pt;
  99. /*********************************************************/
  100. /* */
  101. /* POINTS */
  102. /* */
  103. /* Points are written into the wkbPoint structure which */
  104. /* includes the byte order (little endian or big */
  105. /* endian), the data type, (in this case point), and */
  106. /* The X,Y coordinate pair. */
  107. /* */
  108. /*********************************************************/
  109. /*======================================================================*/
  110. /* */
  111. /* Insert a single point. */
  112. /* */
  113. /*======================================================================*/
  114. /* Populate the point */
  115. pt[0].x = 10; pt[0].y = -10;
  116. geom.type = geomPoint;
  117. geom.num_points = 1;
  118. geom.num_parts = 1;
  119. /* Convert the point to a Well Known Binary representation. */
  120. geom_to_wkb (&geom, &max_alloced, &data_len, &binary);
  121. /* Store the point in the table. */
  122. insert_wkb (&sc, 1, wkbPoint, data_len, binary);
  123. /*********************************************************/
  124. /* */
  125. /* MULTIPOINTS */
  126. /* */
  127. /* Multipoints are collections of points and as such */
  128. /* the wkbMultiPoint structure stores an array of */
  129. /* WKBPoint structures. Therefore the thePoint WKBPoint */
  130. /* variable is populated successively and each one */
  131. /* is stored as an element of the wkbMultiPoint.point[] */
  132. /* array. */
  133. /* */
  134. /*********************************************************/
  135. /*======================================================================*/
  136. /* */
  137. /* Insert a multipoint containing 5 points. This will be a simple */
  138. /* multipoint since none of the points share the same location. */
  139. /* */
  140. /*======================================================================*/
  141. /* Populate the multipoint */
  142. pt[0].x = 1; pt[0].y = 2;
  143. pt[1].x = 3; pt[1].y = 4;
  144. pt[2].x = 5; pt[2].y = 6;
  145. pt[3].x = 7; pt[3].y = 8;
  146. pt[4].x = 9; pt[4].y = 10;
  147. geom.type = geomMultiPoint;
  148. geom.num_points = 5;
  149. geom.num_parts = 5;
  150. /* Convert the points to a Well Known Binary representation of a */
  151. /* multipoint. */
  152. geom_to_wkb (&geom, &max_alloced, &data_len, &binary);
  153. /* Insert the record into the table */
  154. insert_wkb (&sc, 2, wkbMultiPoint, data_len, binary);
  155. /*======================================================================*/
  156. /* */
  157. /* Insert a multipoint containing a single point. */
  158. /* */
  159. /*======================================================================*/
  160. /* Populate the multipoint. */
  161. pt[0].x = 10.0; pt[0].y = 3.1415927;
  162. geom.type = geomMultiPoint;
  163. geom.num_points = 1;
  164. geom.num_parts = 1;
  165. /* Convert the points to a Well Known Binary representation of a */
  166. /* multipoint. */
  167. geom_to_wkb (&geom, &max_alloced, &data_len, &binary);
  168. /* Insert the record into the table */
  169. insert_wkb (&sc, 3, wkbMultiPoint, data_len, binary);
  170. /*======================================================================*/
  171. /* */
  172. /* Insert a multipoint containing three points at the same location. */
  173. /* This is a non-simple multipoint since it contains points that */
  174. /* share the same location. */
  175. /* */
  176. /*======================================================================*/
  177. /* Populate the multipoint. */
  178. pt[0].x = 1; pt[0].y = 1;
  179. pt[0].x = 1; pt[0].y = 1;
  180. pt[0].x = 1; pt[0].y = 1;
  181. geom.type = geomMultiPoint;
  182. geom.num_points = 3;
  183. geom.num_parts = 3;
  184. /* Convert the points to a Well Known Binary representation of a */
  185. /* multipoint. */
  186. geom_to_wkb (&geom, &max_alloced, &data_len, &binary);
  187. /* Insert the record into the table. */
  188. insert_wkb (&sc, 4, wkbMultiPoint, data_len, binary);
  189. /*********************************************************/
  190. /* */
  191. /* LINESTRINGS */
  192. /* */
  193. /* Linestrings are linear paths of X,Y coordinates */
  194. /* assembled in the wkbLineString structure which */
  195. /* includes the byte order, data type (LINESTRING), */
  196. /* number of points in the linestring, and the array */
  197. /* of X,Y coordinates that form the linestring. */
  198. /* */
  199. /*********************************************************/
  200. /*======================================================================*/
  201. /* */
  202. /* Insert a two point linestring. This linestring is simple since does */
  203. /* not intersect its interior. */
  204. /* */
  205. /*======================================================================*/
  206. /* Populate the linestring. */
  207. pt[0].x = -50.123; pt[0].y = -50.1234567;
  208. pt[1].x = 50; pt[1].y = 50;
  209. geom.type = geomLineString;
  210. geom.num_points = 2;
  211. geom.num_parts = 1;
  212. /* Convert the points to a Well Known Binary representation of a */
  213. /* multipoint. */
  214. geom_to_wkb (&geom, &max_alloced, &data_len, &binary);
  215. /* Insert the record into the table */
  216. insert_wkb (&sc, 5, wkbLineString, data_len, binary);
  217. /*======================================================================*/
  218. /* */
  219. /* Insert a six point linestring. The linestring is simple because it */
  220. /* does not intersect its interior. */
  221. /* */
  222. /*======================================================================*/
  223. /* Populate the linestring. */
  224. pt[0].x = 0; pt[0].y = 0;
  225. pt[1].x = 1; pt[1].y = 1;
  226. pt[2].x = 2; pt[2].y = 2;
  227. pt[3].x = 3; pt[3].y = 3;
  228. pt[4].x = 4.5; pt[4].y = 5.4;
  229. pt[5].x = 7; pt[5].y = 7;
  230. geom.type = geomLineString;
  231. geom.num_points = 6;
  232. geom.num_parts = 1;
  233. /* Convert the points to a Well Known Binary representation of a */
  234. /* multipoint. */
  235. geom_to_wkb (&geom, &max_alloced, &data_len, &binary);
  236. /* Insert the record into the table. */
  237. insert_wkb (&sc, 6, wkbLineString, data_len, binary);
  238. /*======================================================================*/
  239. /* */
  240. /* Insert a closed linestring (ring). This linestring is simple because */
  241. /* the interior is not intersected. */
  242. /* */
  243. /*======================================================================*/
  244. /* Populate the linestring (ring). */
  245. pt[0].x = 10; pt[0].y = 10;
  246. pt[1].x = 10; pt[1].y = 20;
  247. pt[2].x = 20; pt[2].y = 10;
  248. pt[3].x = 10; pt[3].y = 10;
  249. geom.type = geomLineString;
  250. geom.num_points = 4;
  251. geom.num_parts = 1;
  252. /* Convert the points to a Well Known Binary representation of a */
  253. /* multipoint. */
  254. geom_to_wkb (&geom, &max_alloced, &data_len, &binary);
  255. /* Insert the record into the table. */
  256. insert_wkb (&sc, 7, wkbLineString, data_len, binary);
  257. /*======================================================================*/
  258. /* */
  259. /* Insert a four point self intersecting linestring. This linestring */
  260. /* is considered non-simple because it intersects its interior. */
  261. /* */
  262. /*======================================================================*/
  263. /* Populate the linestring. */
  264. pt[0].x = 10; pt[0].y = 10;
  265. pt[1].x = 20; pt[1].y = 20;
  266. pt[2].x = 25; pt[2].y = 15;
  267. pt[3].x = 0; pt[3].y = 15;
  268. geom.type = geomLineString;
  269. geom.num_points = 4;
  270. geom.num_parts = 1;
  271. /* Convert the points to a Well Known Binary representation of a */
  272. /* multipoint. */
  273. geom_to_wkb (&geom, &max_alloced, &data_len, &binary);
  274. /* Insert the record into the table. */
  275. insert_wkb (&sc, 8, wkbLineString, data_len, binary);
  276. /***********************************************************/
  277. /* */
  278. /* MULTILINESTRINGS */
  279. /* */
  280. /* Multilinestrings are collections of linestrings. The */
  281. /* wkbMultiLineString structure stores an array of */
  282. /* wkbLineString structures. Therefore the theLinestring */
  283. /* variable is populated successively and each one is */
  284. /* stored as an element of the */
  285. /* wkbMultiLineString.linestrings[] array. */
  286. /* */
  287. /***********************************************************/
  288. /*======================================================================*/
  289. /* */
  290. /* Insert a multilinestring containing two intersecting linestrings. */
  291. /* Since the linestrings intersect at their interiors they are */
  292. /* considered non-simple. */
  293. /* */
  294. /*======================================================================*/
  295. /* Populate the first linestring. */
  296. pt[0].x = 10; pt[0].y = 10; offsets[0] = 0;
  297. pt[1].x = 10; pt[1].y = 20;
  298. /* Populate the second linestring. */
  299. pt[2].x = 5; pt[2].y = 15; offsets[1] = 2;
  300. pt[3].x = 15; pt[3].y = 15;
  301. geom.type = geomMultiLineString;
  302. geom.num_points = 4;
  303. geom.num_parts = 2;
  304. /* Convert the points to a Well Known Binary representation of a */
  305. /* multilinestring. */
  306. geom_to_wkb (&geom, &max_alloced, &data_len, &binary);
  307. /* Insert the record into the table. */
  308. insert_wkb (&sc, 9, wkbMultiLineString, data_len, binary);
  309. /*======================================================================*/
  310. /* */
  311. /* Insert a multilinestring containing 4 non-intersecting linestrings. */
  312. /* The multilinesting is simple because the linestrings do not */
  313. /* intersect. */
  314. /* */
  315. /*======================================================================*/
  316. /* Populate the first linestring. */
  317. pt[0].x = 1; pt[0].y = 1; offsets[0] = 0;
  318. pt[1].x = 11; pt[1].y = 1;
  319. pt[2].x = 11; pt[2].y = -9;
  320. /* Populate the second linesting. */
  321. pt[3].x = 4; pt[3].y = 10; offsets[1] = 3;
  322. pt[4].x = 4; pt[4].y = 20;
  323. /* Populate the third linestring. */
  324. pt[5].x = 10; pt[5].y = 10; offsets[2] = 5;
  325. pt[6].x = 20; pt[6].y = 10;
  326. /* Populate the fourth linestring. */
  327. pt[7].x = -10; pt[7].y = -10; offsets[3] = 7;
  328. pt[8].x = -20; pt[8].y = -10;
  329. pt[9].x = -30; pt[9].y = -10;
  330. pt[10].x = -30; pt[10].y = 0;
  331. geom.type = geomMultiLineString;
  332. geom.num_points = 11;
  333. geom.num_parts = 4;
  334. /* Convert the points to a Well Known Binary representation of a */
  335. /* multilinestring. */
  336. geom_to_wkb (&geom, &max_alloced, &data_len, &binary);
  337. /* Insert the record into the table. */
  338. insert_wkb (&sc, 10, wkbMultiLineString, data_len, binary);
  339. /*********************************************************/
  340. /* */
  341. /* POLYGONS */
  342. /* */
  343. /* Polygons are areas formed by the enclosure of an */
  344. /* exterior ring and the exclusion of the area within */
  345. /* any number of non-overlapping interior rings. */
  346. /* The WKBPolygon structure includes the byte-order, */
  347. /* data-type (POLYGON), number of rings, and an array */
  348. /* of ring structures. The ring structures include the */
  349. /* number of points in the ring and the array of X,Y */
  350. /* coordinate points. */
  351. /* */
  352. /*********************************************************/
  353. /*======================================================================*/
  354. /* */
  355. /* Insert a polygon with no interior rings. Notice that to be a polygon */
  356. /* the first and last coordinate must be the same. */
  357. /* */
  358. /*======================================================================*/
  359. /* The exterior ring. */
  360. pt[0].x = 10; pt[0].y = 10;
  361. pt[1].x = 10; pt[1].y = 20;
  362. pt[2].x = 20; pt[2].y = 20;
  363. pt[3].x = 20; pt[3].y = 10;
  364. pt[4].x = 10; pt[4].y = 10;
  365. suboffsets[0]=0;
  366. geom.type = geomPolygon;
  367. geom.num_points = 5;
  368. geom.num_parts = 1;
  369. geom.num_subparts = 1;
  370. /* Convert the points to a Well Known Binary representation of a */
  371. /* multipoint. */
  372. geom_to_wkb (&geom, &max_alloced, &data_len, &binary);
  373. /* Insert the record into the table. */
  374. insert_wkb (&sc, 11, wkbPolygon, data_len, binary);
  375. /*======================================================================*/
  376. /* */
  377. /* Insert a polygon containing a single interior ring. */
  378. /* */
  379. /*======================================================================*/
  380. /* The exterior ring. */
  381. pt[0].x = 10; pt[0].y = 10; suboffsets[0] = 0; offsets[0] = 0;
  382. pt[1].x = 10; pt[1].y = 20;
  383. pt[2].x = 20; pt[2].y = 20;
  384. pt[3].x = 20; pt[3].y = 10;
  385. pt[4].x = 10; pt[4].y = 10;
  386. /* The interior ring */
  387. pt[5].x = 12; pt[5].y = 12; suboffsets[1] = 5;
  388. pt[6].x = 12; pt[6].y = 13;
  389. pt[7].x = 13; pt[7].y = 13;
  390. pt[8].x = 13; pt[8].y = 12;
  391. pt[9].x = 12; pt[9].y = 12;
  392. geom.type = geomPolygon;
  393. geom.num_points = 10;
  394. geom.num_parts = 1;
  395. geom.num_subparts = 2;
  396. /* Convert the points to a Well Known Binary representation of a */
  397. /* multipoint. */
  398. geom_to_wkb (&geom, &max_alloced, &data_len, &binary);
  399. /* Insert the record into the table. */
  400. insert_wkb (&sc, 12, wkbPolygon, data_len, binary);
  401. /*======================================================================*/
  402. /* */
  403. /* Insert a Polygon containing two interior rings. */
  404. /* */
  405. /*======================================================================*/
  406. /* The exterior ring. */
  407. pt[0].x = 10; pt[0].y = 10; suboffsets[0] = 0; offsets[0] = 0;
  408. pt[1].x = 10; pt[1].y = 20;
  409. pt[2].x = 20; pt[2].y = 20;
  410. pt[3].x = 20; pt[3].y = 10;
  411. pt[4].x = 10; pt[4].y = 10;
  412. /* The first interior ring. */
  413. pt[5].x = 12; pt[5].y = 12; suboffsets[1] = 5;
  414. pt[6].x = 12; pt[6].y = 13;
  415. pt[7].x = 13; pt[7].y = 13;
  416. pt[8].x = 13; pt[8].y = 12;
  417. pt[9].x = 12; pt[9].y = 12;
  418. /* The second interior ring. */
  419. pt[10].x = 16; pt[10].y = 16; suboffsets[2] = 10;
  420. pt[11].x = 16; pt[11].y = 18;
  421. pt[12].x = 18; pt[12].y = 18;
  422. pt[13].x = 18; pt[13].y = 16;
  423. pt[14].x = 16; pt[14].y = 16;
  424. geom.type = geomPolygon;
  425. geom.num_points = 15;
  426. geom.num_parts = 1;
  427. geom.num_subparts = 3;
  428. /* Convert the points to a Well Known Binary representation of a */
  429. /* polygon. */
  430. geom_to_wkb (&geom, &max_alloced, &data_len, &binary);
  431. /* Insert the record into the table. */
  432. insert_wkb (&sc, 13, wkbPolygon, data_len, binary);
  433. /*********************************************************/
  434. /* */
  435. /* MULTIPOLYGONS */
  436. /* */
  437. /* MultiPolygons are collections of polygons. The */
  438. /* WKBMultipoly structure stores an array of WKBPolygon */
  439. /* structures. The WKBPolygon structure is populated */
  440. /* successively and each one is stored as an element of */
  441. /* the WKBMultiPolygon.polygons[] array. */
  442. /* */
  443. /*********************************************************/
  444. /*======================================================================*/
  445. /* */
  446. /* Insert a multipolygon containing three polygons. The first polygon */
  447. /* contains a single interior ring, the second polygon contains no */
  448. /* interior rings and the third polygon contains 2 interior rings. */
  449. /* */
  450. /*======================================================================*/
  451. /* The first polygon's exterior ring. */
  452. pt[0].x = 10; pt[0].y = 10; suboffsets[0] = 0; offsets[0] = 0;
  453. pt[1].x = 10; pt[1].y = 20;
  454. pt[2].x = 20; pt[2].y = 20;
  455. pt[3].x = 20; pt[3].y = 10;
  456. pt[4].x = 10; pt[4].y = 10;
  457. /* The first polygon's interior ring. */
  458. pt[5].x = 14; pt[5].y = 14; suboffsets[1] = 5;
  459. pt[6].x = 14; pt[6].y = 16;
  460. pt[7].x = 16; pt[7].y = 16;
  461. pt[8].x = 16; pt[8].y = 14;
  462. pt[9].x = 14; pt[9].y = 14;
  463. /* The second polygon's exterior ring. */
  464. pt[10].x = 70; pt[10].y = 70; suboffsets[2] = 10; offsets[1] = 2;
  465. pt[11].x = 70; pt[11].y = 80;
  466. pt[12].x = 80; pt[12].y = 80;
  467. pt[13].x = 80; pt[13].y = 70;
  468. pt[14].x = 70; pt[14].y = 70;
  469. /* The third polygons exterior ring. */
  470. pt[15].x = 50; pt[15].y = 50; suboffsets[3] = 15; offsets[2] = 3;
  471. pt[16].x = 50; pt[16].y = 60;
  472. pt[17].x = 60; pt[17].y = 60;
  473. pt[18].x = 60; pt[18].y = 50;
  474. pt[19].x = 50; pt[19].y = 50;
  475. /* The third polygon's interior ring. */
  476. pt[20].x = 52; pt[20].y = 52; suboffsets[4] = 20;
  477. pt[21].x = 52; pt[21].y = 54;
  478. pt[22].x = 54; pt[22].y = 54;
  479. pt[23].x = 54; pt[23].y = 52;
  480. pt[24].x = 52; pt[24].y = 52;
  481. /* The third polygon's interior ring. */
  482. pt[25].x = 56; pt[25].y = 56; suboffsets[5] = 25;
  483. pt[26].x = 56; pt[26].y = 58;
  484. pt[27].x = 58; pt[27].y = 58;
  485. pt[28].x = 58; pt[28].y = 56;
  486. pt[29].x = 56; pt[29].y = 56;
  487. geom.type = geomMultiPolygon;
  488. geom.num_points = 30;
  489. geom.num_parts = 3;
  490. geom.num_subparts = 6;
  491. /* Convert the points to a Well Known Binary representation of a */
  492. /* multipolygon. */
  493. geom_to_wkb (&geom, &max_alloced, &data_len, &binary);
  494. /* Insert the multipolygon into the table. */
  495. insert_wkb (&sc, 14, wkbMultiPolygon, data_len, binary);
  496. /*======================================================================*/
  497. /* */
  498. /* Insert a multipolygon containing two polygons that do not have */
  499. /* interior rings. */
  500. /* */
  501. /*======================================================================*/
  502. /* The first polygon's exterior ring. */
  503. pt[0].x = 10; pt[0].y = 10; suboffsets[0] = 0; offsets[0] = 0;
  504. pt[1].x = 10; pt[1].y = 20;
  505. pt[2].x = 20; pt[2].y = 20;
  506. pt[3].x = 20; pt[3].y = 10;
  507. pt[4].x = 10; pt[4].y = 10;
  508. /* The second polygon's exterior ring. */
  509. pt[5].x = 1; pt[5].y = 1; suboffsets[1] = 5; offsets[1] = 1;
  510. pt[6].x = 1; pt[6].y = 5;
  511. pt[7].x = 5; pt[7].y = 5;
  512. pt[8].x = 5; pt[8].y = 1;
  513. pt[9].x = 1; pt[9].y = 1;
  514. geom.type = geomMultiPolygon;
  515. geom.num_points = 10;
  516. geom.num_parts = 2;
  517. geom.num_subparts = 2;
  518. /* Convert the points to a Well Known Binary representation of a */
  519. /* multipolygon. */
  520. geom_to_wkb (&geom, &max_alloced, &data_len, &binary);
  521. /* Insert the multipolygon into the table. */
  522. insert_wkb (&sc, 15, wkbMultiPolygon, data_len, binary);
  523. /*======================================================================*/
  524. /* */
  525. /* Insert a multipolygon containing two polygons. The first polygon */
  526. /* contains an interior ring and the second polygon does not. */
  527. /* */
  528. /*======================================================================*/
  529. /* Populate the first polygons exterior ring. */
  530. pt[0].x = 10; pt[0].y = 10; suboffsets[0] = 0; offsets[0] = 0;
  531. pt[1].x = 10; pt[1].y = 20;
  532. pt[2].x = 20; pt[2].y = 20;
  533. pt[3].x = 20; pt[3].y = 10;
  534. pt[4].x = 10; pt[4].y = 10;
  535. /* Populate the first polygon's interior ring. */
  536. pt[5].x = 14; pt[5].y = 14; suboffsets[1] = 5;
  537. pt[6].x = 14; pt[6].y = 16;
  538. pt[7].x = 16; pt[7].y = 16;
  539. pt[8].x = 16; pt[8].y = 14;
  540. pt[9].x = 14; pt[9].y = 14;
  541. /* Populate the exterior ring of the second polygon. */
  542. pt[10].x = 1; pt[10].y = 1; suboffsets[2] = 10; offsets[1] = 2;
  543. pt[11].x = 1; pt[11].y = 5;
  544. pt[12].x = 5; pt[12].y = 5;
  545. pt[13].x = 5; pt[13].y = 1;
  546. pt[14].x = 1; pt[14].y = 1;
  547. geom.type = geomMultiPolygon;
  548. geom.num_points = 15;
  549. geom.num_parts = 2;
  550. geom.num_subparts = 3;
  551. /* Convert the points to a Well Known Binary representation of a */
  552. /* multipolygon. */
  553. geom_to_wkb (&geom, &max_alloced, &data_len, &binary);
  554. /* Insert the multipolygon into the table. */
  555. insert_wkb (&sc, 16, wkbMultiPolygon, data_len, binary);
  556. /*===============================*/
  557. /* */
  558. /* Disconnect from the database. */
  559. /* */
  560. /*===============================*/
  561. EXEC SQL disconnect current;
  562. }
  563. /***********************************************************************
  564. *
  565. * create_table - Creates a table for inserting spatial data
  566. *
  567. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  568. *
  569. * Purpose:
  570. *
  571. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  572. *
  573. * Parameters:
  574. * sc <Input> == (SpatialColumn *) Spatial column.
  575. *
  576. * RETURN <Output> == (void)
  577. *
  578. ***********************************************************************/
  579. static void create_table (
  580. SpatialColumn *sc
  581. )
  582. {
  583. EXEC SQL BEGIN DECLARE SECTION;
  584. char sql_stmt[256];
  585. EXEC SQL END DECLARE SECTION;
  586. sprintf (sql_stmt, "DROP TABLE %s", sc->table);
  587. EXEC SQL execute immediate :sql_stmt;
  588. sprintf (sql_stmt, "CREATE TABLE %s (id integer, %s ST_Geometry)",
  589. sc->table, sc->column);
  590. EXEC SQL execute immediate :sql_stmt;
  591. }
  592. /***********************************************************************
  593. *
  594. * insert_wkb - Inserts the geometry and integer id values into
  595. * the spatial and id columns of the table
  596. * specified as arguments to the main program.
  597. *
  598. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  599. *
  600. * Purpose:
  601. * The function inserts the geometry WKB binary structure into the
  602. * spatial column and an integer into the id column.
  603. *
  604. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  605. *
  606. * Parameters:
  607. * sc <Input> == (SpatialColumn *) Spatial column.
  608. * id <Input> == (int) The integer id value.
  609. * type <Input> == (WkbType) The data type of the geometry.
  610. * data_len <Input> == (int) The number of bytes in wkb_buffer.
  611. * wkb_buffer <Input> == (char *) The WKB geometry structure.
  612. *
  613. * RETURN <Output> == (void)
  614. *
  615. ***********************************************************************/
  616. static void insert_wkb (
  617. SpatialColumn *sc,
  618. int id,
  619. WkbType type,
  620. int data_len,
  621. char *wkb_buffer
  622. )
  623. {
  624. EXEC SQL BEGIN DECLARE SECTION;
  625. int i, n, xid;
  626. short typ;
  627. char wkb_sql[256];
  628. var binary wkb;
  629. EXEC SQL END DECLARE SECTION;
  630. int rc;
  631. char wkbfunction[19];
  632. /* Set the wkbfunction to the "FromWkb" function that corresponds */
  633. /* to the data type of the WKB geometry representation. */
  634. switch (type)
  635. {
  636. case wkbPoint:
  637. strcpy (wkbfunction,"ST_PointFromWkb");
  638. break;
  639. case wkbLineString:
  640. strcpy (wkbfunction,"ST_LineFromWkb");
  641. break;
  642. case wkbPolygon:
  643. strcpy (wkbfunction,"ST_PolyFromWkb");
  644. break;
  645. case wkbMultiPoint:
  646. strcpy (wkbfunction,"ST_MPointFromWkb");
  647. break;
  648. case wkbMultiLineString:
  649. strcpy (wkbfunction,"ST_MLineFromWkb");
  650. break;
  651. case wkbMultiPolygon:
  652. strcpy (wkbfunction,"ST_MPolyFromWkb");
  653. break;
  654. }
  655. /* Generate the SQL insert expression. The integer value and the */
  656. /* name of the spatial column are entered as bind parameters. */
  657. sprintf (wkb_sql,
  658. "insert into %s (id, %s) values (?, %s (?, %d))",
  659. sc->table, sc->column, wkbfunction, sc->srid);
  660. /* Prepare the SQL statement for execution. */
  661. fprintf (stdout, "SQL is %s \n", wkb_sql);
  662. EXEC SQL prepare ins_stmt from :wkb_sql;
  663. /* Allocate a statement descriptor. */
  664. EXEC SQL allocate descriptor 'ins_desc';
  665. /* Set the number of input parameters */
  666. n = 2;
  667. EXEC SQL set descriptor 'ins_desc' COUNT = :n;
  668. /* Bind the input id to the first input parameter */
  669. n = 1; typ = SQLINT; i = id;
  670. EXEC SQL set descriptor 'ins_desc' VALUE :n
  671. TYPE = :typ,
  672. DATA = :i;
  673. /* Create host variable for WKB representation */
  674. if ((rc = ifx_var_alloc(&wkb,data_len)) < 0)
  675. {
  676. fprintf(stderr, "Error calling ifx_var_alloc.");
  677. exit(1);
  678. }
  679. if ((rc = ifx_var_setdata(&wkb,wkb_buffer,data_len)) < 0)
  680. {
  681. fprintf(stderr, "Error calling ifx_var_setdata.");
  682. exit(1);
  683. }
  684. if ((rc = ifx_var_setlen(&wkb,data_len)) < 0)
  685. {
  686. fprintf(stderr, "Error calling ifx_var_setlen.");
  687. exit(1);
  688. }
  689. /* Bind the WKB to the second input parameter */
  690. n = 2; typ = SQLUDTVAR; xid = XID_LVARCHAR;
  691. EXEC SQL set descriptor 'ins_desc' VALUE :n
  692. TYPE = :typ,
  693. EXTYPEID = :xid,
  694. DATA = :wkb;
  695. /* Execute the insert statement. */
  696. EXEC SQL execute ins_stmt using sql descriptor 'ins_desc';
  697. /* Free resources associated with the WKB host variable */
  698. if ((rc = ifx_var_dealloc(&wkb)) < 0)
  699. {
  700. fprintf(stderr, "Error calling ifx_var_dealloc.");
  701. exit(1);
  702. }
  703. /* Free resources associated with INSERT statement */
  704. EXEC SQL deallocate descriptor 'ins_desc';
  705. EXEC SQL free ins_stmt;
  706. }