load_shapes.ec 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. /***********************************************************************
  2. *
  3. * load_shapes.ec -- Inserts of all shape types from binary.
  4. *
  5. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  6. *
  7. * Purpose:
  8. * Tests inserts of all shape types from binary.
  9. *
  10. * The following command will likely compile this source:
  11. * esql -g -o load_shapes load_shapes.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 ESRI shapefile format data.
  23. */
  24. #include "commfuncs.h"
  25. #include "commfuncs.c"
  26. #include "shapefuncs.c"
  27. /*
  28. * Local function prototypes
  29. *
  30. */
  31. static void insert_shape (SpatialColumn *spatial_column,
  32. int id,
  33. GeomType geom_type,
  34. int data_len,
  35. char *binary);
  36. static void create_table (SpatialColumn *spatial_column);
  37. /************************************************************************
  38. *
  39. * main -- Inserts of all shape types from binary shape format into a
  40. * test table.
  41. *
  42. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  43. *
  44. * Purpose:
  45. * Load a table with all types of geometry from shapes.
  46. *
  47. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  48. *
  49. * Parameters:
  50. * argc <Input> == (int) The parameter index.
  51. * argv <Input> == (char[] *) Pointer to an array of parameters.
  52. *
  53. *************************************************************************/
  54. void main (int argc, char **argv)
  55. {
  56. /* Declare local host variables */
  57. EXEC SQL BEGIN DECLARE SECTION;
  58. char dbname[256];
  59. EXEC SQL END DECLARE SECTION;
  60. /* Declare other local variables */
  61. char *binary;
  62. int rc;
  63. int num_points;
  64. int num_parts;
  65. SpatialColumn sc;
  66. Geometry geom = {0};
  67. int offsets[6];
  68. Point pt[30];
  69. double z[30];
  70. double m[30];
  71. int data_len;
  72. int max_alloced = 0;
  73. /* Check for the correct number of arguments entered. */
  74. if (argc < 2)
  75. {
  76. printf ("\nUsage: %s <database> [<srid>]\n", argv[0]);
  77. printf ("\n");
  78. printf ("\nThis program will create a table named 'shape_test' and\n");
  79. printf ("insert data using various SE_xxxFromShape functions.\n");
  80. exit (1);
  81. }
  82. /* Define exception handling routine for EXEC SQL statements */
  83. EXEC SQL whenever sqlerror CALL ignore206;
  84. /* Connect to the database. */
  85. sprintf(dbname, "%s", argv[1]);
  86. EXEC SQL connect to :dbname;
  87. /* Obtain srid to use for inserts */
  88. sc.srid = (argc > 2) ? atoi(argv[2]) : 0;
  89. /* Load the spatial column structure. */
  90. strcpy (sc.table, "shape_test");
  91. strcpy (sc.column, "geom");
  92. /* Create the test table */
  93. create_table (&sc);
  94. /* The remainder of the main function prepares the various
  95. geometry data types with valid X, Y coordinates, Z ordinates and
  96. measures. It passes these geometries to geom_to_shape function
  97. which converts them into shapes. The shapes are then passed to
  98. the insert_shape function which inserts the shapes into the spatial
  99. column of the table. Insert_shape also inserts an integer key into
  100. the table.
  101. */
  102. geom.offsets = offsets;
  103. geom.pt = pt;
  104. geom.z = z;
  105. geom.m = m;
  106. /**********/
  107. /* */
  108. /* POINTS */
  109. /* */
  110. /**********/
  111. /*======================================================================*/
  112. /* */
  113. /* Insert a single point. */
  114. /* */
  115. /*======================================================================*/
  116. /* Populate the point. */
  117. pt[0].x = 10; pt[0].y = -10;
  118. geom.type = geomPoint;
  119. geom.num_points = 1;
  120. geom.num_parts = 1;
  121. /* Convert the point to an ESRI shape. */
  122. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  123. /* Insert the shape into the table */
  124. insert_shape (&sc, 1, geomPoint, data_len, binary);
  125. /*======================================================================*/
  126. /* */
  127. /* Insert a point with a Z coordinate. */
  128. /* */
  129. /*======================================================================*/
  130. /* Populate the point. */
  131. pt[0].x = -50.123; pt[0].y = -50.1234567; z[0] = 50;
  132. geom.type = geomPointZ;
  133. geom.num_points = 1;
  134. geom.num_parts = 1;
  135. /* Convert the point to an ESRI shape. */
  136. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  137. /* Insert the shape into the table. */
  138. insert_shape (&sc, 2, geomPoint, data_len, binary);
  139. /*======================================================================*/
  140. /* */
  141. /* Insert a point with a measure. */
  142. /* */
  143. /*======================================================================*/
  144. /* Populate the point. */
  145. pt[0].x = -.123; pt[0].y = -.1234567; m[0] = 10;
  146. geom.type = geomPointM;
  147. geom.num_points = 1;
  148. geom.num_parts = 1;
  149. /* Convert the point to an ESRI shape. */
  150. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  151. /* Insert the shape into the table. */
  152. insert_shape (&sc, 3, geomPoint, data_len, binary);
  153. /*======================================================================*/
  154. /* */
  155. /* Insert a point with a Z coordinate and a measure. */
  156. /* */
  157. /*======================================================================*/
  158. /* Populate the point. */
  159. pt[0].x = 1; pt[0].y = 2; z[0] = 3; m[0] = 4;
  160. geom.type = geomPointZM;
  161. geom.num_points = 1;
  162. geom.num_parts = 1;
  163. /* Convert the point to an ESRI shape. */
  164. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  165. /* Insert the shape into the table. */
  166. insert_shape (&sc, 4, geomPoint, data_len, binary);
  167. /***************/
  168. /* */
  169. /* MULTIPOINTS */
  170. /* */
  171. /***************/
  172. /*======================================================================*/
  173. /* */
  174. /* Insert a multipoint containing 5 points. */
  175. /* */
  176. /*======================================================================*/
  177. /* Populate the multipoint */
  178. pt[0].x = 1; pt[0].y = 2;
  179. pt[1].x = 3; pt[1].y = 4;
  180. pt[2].x = 5; pt[2].y = 6;
  181. pt[3].x = 7; pt[3].y = 8;
  182. pt[4].x = 9; pt[4].y = 10;
  183. geom.type = geomMultiPoint;
  184. geom.num_points = 5;
  185. geom.num_parts = 5;
  186. /* Convert the multipoint into an ESRI shape. */
  187. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  188. /* Insert the shape into a table. */
  189. insert_shape (&sc, 5, geomMultiPoint, data_len, binary);
  190. /*======================================================================*/
  191. /* */
  192. /* Insert a multipoint containing a single point. */
  193. /* */
  194. /*======================================================================*/
  195. /* Populate the multipoint. */
  196. pt[0].x = 10.0; pt[0].y = 3.1415927;
  197. geom.type = geomMultiPoint;
  198. geom.num_points = 1;
  199. geom.num_parts = 1;
  200. /* Convert the multipoint into an ESRI shape. */
  201. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  202. /* Insert the shape into a table. */
  203. insert_shape (&sc, 6, geomMultiPoint, data_len, binary);
  204. /*======================================================================*/
  205. /* */
  206. /* Insert a multipoint containing a three points sharing the same */
  207. /* location. This multipoint is non-simple since some of its points */
  208. /* share the same location. */
  209. /* */
  210. /*======================================================================*/
  211. /* Populate the multipoint. */
  212. pt[0].x = 1; pt[0].y = 1;
  213. pt[1].x = 1; pt[1].y = 1;
  214. pt[2].x = 1; pt[2].y = 1;
  215. geom.type = geomMultiPoint;
  216. geom.num_points = 3;
  217. geom.num_parts = 3;
  218. /* Convert the multipoint into an ESRI shape. */
  219. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  220. /* Insert the shape into a table. */
  221. insert_shape (&sc, 7, geomMultiPoint, data_len, binary);
  222. /*======================================================================*/
  223. /* */
  224. /* Insert a multipoint containing a single point with a measure. */
  225. /* */
  226. /*======================================================================*/
  227. /* Populate the multipoint. */
  228. pt[0].x = -0.123; pt[0].y = -0.1234567; m[0] = 10;
  229. geom.type = geomMultiPointM;
  230. geom.num_points = 1;
  231. geom.num_parts = 1;
  232. /* Convert the multipoint into an ESRI shape. */
  233. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  234. /* Insert the shape into a multipoint. */
  235. insert_shape (&sc, 8, geomMultiPoint, data_len, binary);
  236. /*======================================================================*/
  237. /* */
  238. /* Insert a multipoint containing three points with z coordinates and */
  239. /* measures. */
  240. /* */
  241. /*======================================================================*/
  242. /* Populate the multipoint. */
  243. pt[0].x = 1; pt[0].y = 2; z[0] = 3; m[0] = 4;
  244. pt[1].x = 5; pt[1].y = 6; z[1] = 7; m[1] = 8;
  245. pt[2].x = 9; pt[2].y = 10; z[2] = 11; m[2] = 12;
  246. geom.type = geomMultiPointZM;
  247. geom.num_points = 3;
  248. geom.num_parts = 3;
  249. /* Convert the multipoint into an ESRI shape. */
  250. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  251. /* Insert the shape into a multipoint. */
  252. insert_shape (&sc, 9, geomMultiPoint, data_len, binary);
  253. /***************/
  254. /* */
  255. /* LINESTRINGS */
  256. /* */
  257. /***************/
  258. /*======================================================================*/
  259. /* */
  260. /* Insert a linestring containing two vertices. */
  261. /* */
  262. /*======================================================================*/
  263. /* Populate the linestring. */
  264. pt[0].x = -50.123; pt[0].y = -50.1234567;
  265. pt[1].x = 50; pt[1].y = 50;
  266. geom.type = geomLineString;
  267. geom.num_points = 2;
  268. geom.num_parts = 1;
  269. /* Convert the linestring into a shape. */
  270. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  271. /* Insert the shape into a table. */
  272. insert_shape (&sc, 10, geomLineString, data_len, binary);
  273. /*======================================================================*/
  274. /* */
  275. /* Insert a linestring contain 6 vertices. */
  276. /* */
  277. /*======================================================================*/
  278. /* Populate the linestring. */
  279. pt[0].x = 0; pt[0].y = 0;
  280. pt[1].x = 1; pt[1].y = 1;
  281. pt[2].x = 2; pt[2].y = 2;
  282. pt[3].x = 3; pt[3].y = 3;
  283. pt[4].x = 4.5; pt[4].y = 5.4;
  284. pt[5].x = 7; pt[5].y = 7;
  285. geom.type = geomLineString;
  286. geom.num_points = 6;
  287. geom.num_parts = 1;
  288. /* Convert the linestring into a shape. */
  289. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  290. /* Insert the shape into the table. */
  291. insert_shape (&sc, 11, geomLineString, data_len, binary);
  292. /*======================================================================*/
  293. /* */
  294. /* Insert a linestring that is a ring containing 4 vertices. */
  295. /* */
  296. /*======================================================================*/
  297. /* Populate the linestring. */
  298. pt[0].x = 10; pt[0].y = 10;
  299. pt[1].x = 10; pt[1].y = 20;
  300. pt[2].x = 20; pt[2].y = 10;
  301. pt[3].x = 10; pt[3].y = 10;
  302. geom.type = geomLineString;
  303. geom.num_points = 4;
  304. geom.num_parts = 1;
  305. /* Convert the linestring into a shape. */
  306. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  307. /* Insert the shape into the table. */
  308. insert_shape (&sc, 12, geomLineString, data_len, binary);
  309. /*======================================================================*/
  310. /* */
  311. /* Insert a linestring that intersects its interior and therefore is */
  312. /* non-simple. */
  313. /* */
  314. /*======================================================================*/
  315. /* Populate the linestring. */
  316. pt[0].x = 10; pt[0].y = 10;
  317. pt[1].x = 20; pt[1].y = 20;
  318. pt[2].x = 25; pt[2].y = 15;
  319. pt[3].x = 0; pt[3].y = 15;
  320. geom.type = geomLineString;
  321. geom.num_points = 4;
  322. geom.num_parts = 1;
  323. /* Convert the linestring into a shape. */
  324. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  325. /* Insert the shape into the table. */
  326. insert_shape (&sc, 13, geomLineString, data_len, binary);
  327. /*======================================================================*/
  328. /* */
  329. /* Insert a linestring containing two vertices with Z coordinates. */
  330. /* */
  331. /*======================================================================*/
  332. /* Populate the linestring. */
  333. pt[0].x = -50.123; pt[0].y = -50.1234567; z[0] = 50;
  334. pt[1].x = 1; pt[1].y = 2; z[1] = 60;
  335. geom.type = geomLineStringZ;
  336. geom.num_points = 2;
  337. geom.num_parts = 1;
  338. /* Convert the linestring into a shape. */
  339. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  340. /* Insert the shape into the table. */
  341. insert_shape (&sc, 14, geomLineString, data_len, binary);
  342. /*======================================================================*/
  343. /* */
  344. /* Insert a linestring containing two vertices with measures. */
  345. /* */
  346. /*======================================================================*/
  347. /* Populate the linestring. */
  348. pt[0].x = 1; pt[0].y = 2; m[0] = 3;
  349. pt[1].x = 4; pt[1].y = 5; m[1] = 6;
  350. geom.type = geomLineStringM;
  351. geom.num_points = 2;
  352. geom.num_parts = 1;
  353. /* Convert the linestring into a shape. */
  354. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  355. /* Insert the shape into the table. */
  356. insert_shape (&sc, 15, geomLineString, data_len, binary);
  357. /*======================================================================*/
  358. /* */
  359. /* Insert a linestring containing two vertices with Z ordinate and */
  360. /* measures. */
  361. /* */
  362. /*======================================================================*/
  363. /* Populate the linestring. */
  364. pt[0].x = 7; pt[0].y = 8; z[0] = 9; m[0] = 10;
  365. pt[1].x = 11; pt[1].y = 12; z[1] = 13; m[1] = 14;
  366. geom.type = geomLineStringZM;
  367. geom.num_points = 2;
  368. geom.num_parts = 1;
  369. /* Convert the linestring into a shape. */
  370. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  371. /* Insert the shape into the table. */
  372. insert_shape (&sc, 16, geomLineString, data_len, binary);
  373. /********************/
  374. /* */
  375. /* MULTILINESTRINGS */
  376. /* */
  377. /********************/
  378. /*======================================================================*/
  379. /* */
  380. /* Insert a multilinestring containing two linestrings that intersect */
  381. /* each others interiors. Since the elements of the multilinestring */
  382. /* intersect at their interior it is not simple. */
  383. /* */
  384. /*======================================================================*/
  385. /* The first linestring. */
  386. pt[0].x = 10; pt[0].y = 10; offsets[0] = 0;
  387. pt[1].x = 10; pt[1].y = 20;
  388. /* The second linestring. */
  389. pt[2].x = 5; pt[2].y = 15; offsets[1] = 2;
  390. pt[3].x = 15; pt[3].y = 15;
  391. geom.type = geomLineString;
  392. geom.num_points = 4;
  393. geom.num_parts = 2;
  394. /* Convert the multilinestring into a shape. */
  395. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  396. /* Insert the shape into the table. */
  397. insert_shape (&sc, 17, geomMultiLineString, data_len, binary);
  398. /*======================================================================*/
  399. /* */
  400. /* Insert a multilinestring containing four linestrings that do not */
  401. /* intersect. The multilinestring is simple since its interior is not */
  402. /* intersected. */
  403. /* */
  404. /*======================================================================*/
  405. /* The first linestring. */
  406. pt[0].x = 1; pt[0].y = 1; offsets[0] = 0;
  407. pt[1].x = 11; pt[1].y = 1;
  408. pt[2].x = 11; pt[2].y = -9;
  409. /* The second linestring. */
  410. pt[3].x = 4; pt[3].y = 10; offsets[1] = 3;
  411. pt[4].x = 4; pt[4].y = 20;
  412. /* The third linestring. */
  413. pt[5].x = 10; pt[5].y = 10; offsets[2] = 5;
  414. pt[6].x = 20; pt[6].y = 10;
  415. /* The fourth linestring. */
  416. pt[7].x = -10; pt[7].y = -10; offsets[3] = 7;
  417. pt[8].x = -20; pt[8].y = -10;
  418. pt[9].x = -30; pt[9].y = -10;
  419. pt[10].x = -30; pt[10].y = 0;
  420. geom.type = geomLineString;
  421. geom.num_points = 11;
  422. geom.num_parts = 4;
  423. /* Convert the multilinestring into a shape. */
  424. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  425. /* Insert the shape into the table. */
  426. insert_shape (&sc, 18, geomMultiLineString, data_len, binary);
  427. /*======================================================================*/
  428. /* */
  429. /* Insert a multilinestring containing 3 linestrings with Z ordinates. */
  430. /* */
  431. /*======================================================================*/
  432. /* The first linestring. */
  433. pt[0].x = 1; pt[0].y = 1; z[0] = 10; offsets[0] = 0;
  434. pt[1].x = 11; pt[1].y = 1; z[1] = 20;
  435. pt[2].x = 11; pt[2].y = 11; z[2] = 30;
  436. /* The second linestring. */
  437. pt[3].x = 4; pt[3].y = 10; z[3] = -1; offsets[1] = 3;
  438. pt[4].x = 4; pt[4].y = 20; z[4] = 2;
  439. /* The third linestring. */
  440. pt[5].x = 10; pt[5].y = 10; z[5] = 10;
  441. pt[6].x = 20; pt[6].y = 10; z[6] = 20; offsets[2] = 5;
  442. geom.type = geomLineStringZ;
  443. geom.num_points = 7;
  444. geom.num_parts = 3;
  445. /* Convert the multilinestring into a shape. */
  446. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  447. /* Insert the shape into the table. */
  448. insert_shape (&sc, 19, geomMultiLineString, data_len, binary);
  449. /*======================================================================*/
  450. /* */
  451. /* Insert a multilinestring containing 2 linestrings with measures. */
  452. /* */
  453. /*======================================================================*/
  454. /* The first linestring. */
  455. pt[0].x = 1; pt[0].y = 1; m[0] = 10; offsets[0] = 0;
  456. pt[1].x = 1; pt[1].y = 10; m[1] = 20;
  457. /* The second linestring. */
  458. pt[2].x = 11; pt[2].y = 11; m[2] = 30; offsets[1] = 2;
  459. pt[3].x = 21; pt[3].y = 10; m[3] = -1;
  460. geom.type = geomLineStringM;
  461. geom.num_points = 4;
  462. geom.num_parts = 2;
  463. /* Convert the multilinestring into a shape. */
  464. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  465. /* Insert the shape into the table. */
  466. insert_shape (&sc, 20, geomMultiLineString, data_len, binary);
  467. /*======================================================================*/
  468. /* */
  469. /* Insert a multilinestring comtaining two linestrings with Z ordinates */
  470. /* and measures. */
  471. /* */
  472. /*======================================================================*/
  473. /* The first linestring. */
  474. pt[0].x = 1; pt[0].y = 1; z[0] = 10; m[0] = 10; offsets[0] = 0;
  475. pt[1].x = 11; pt[1].y = 1; z[1] = 15; m[1] = 20;
  476. pt[2].x = 11; pt[2].y = 11; z[2] = 25; m[2] = 30;
  477. /* The second linestring. */
  478. pt[3].x = 4; pt[3].y = 10; z[3] = 35; m[3] = 40; offsets[1] = 3;
  479. pt[4].x = 4; pt[4].y = 20; z[4] = 45; m[4] = 50;
  480. geom.type = geomLineStringZM;
  481. geom.num_points = 5;
  482. geom.num_parts = 2;
  483. /* Convert the multilinestring into a shape. */
  484. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  485. /* Insert the shape into the table. */
  486. insert_shape (&sc, 21, geomMultiLineString, data_len, binary);
  487. /************/
  488. /* */
  489. /* POLYGONS */
  490. /* */
  491. /************/
  492. /*======================================================================*/
  493. /* */
  494. /* Insert a polygon with no interior rings. */
  495. /* */
  496. /*======================================================================*/
  497. /* The exterior ring */
  498. pt[0].x = 10; pt[0].y = 10;
  499. pt[1].x = 10; pt[1].y = 20;
  500. pt[2].x = 20; pt[2].y = 20;
  501. pt[3].x = 20; pt[3].y = 10;
  502. pt[4].x = 10; pt[4].y = 10;
  503. geom.type = geomPolygon;
  504. geom.num_points = 5;
  505. geom.num_parts = 1;
  506. /* Convert the polygon into a shape. */
  507. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  508. /* Insert the shape into a table. */
  509. insert_shape (&sc, 22, geomPolygon, data_len, binary);
  510. /*======================================================================*/
  511. /* */
  512. /* Insert a polygon with one interior ring. */
  513. /* */
  514. /*======================================================================*/
  515. /* The exterior ring. */
  516. pt[0].x = 10; pt[0].y = 10; offsets[0] = 0;
  517. pt[1].x = 10; pt[1].y = 20;
  518. pt[2].x = 20; pt[2].y = 20;
  519. pt[3].x = 20; pt[3].y = 10;
  520. pt[4].x = 10; pt[4].y = 10;
  521. /* The interior ring. */
  522. pt[5].x = 12; pt[5].y = 12; offsets[1] = 5;
  523. pt[6].x = 12; pt[6].y = 13;
  524. pt[7].x = 13; pt[7].y = 13;
  525. pt[8].x = 13; pt[8].y = 12;
  526. pt[9].x = 12; pt[9].y = 12;
  527. geom.type = geomPolygon;
  528. geom.num_points = 10;
  529. geom.num_parts = 2;
  530. /* Convert the polygon into a shape. */
  531. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  532. /* Insert the shape into the table. */
  533. insert_shape (&sc, 23, geomPolygon, data_len, binary);
  534. /*======================================================================*/
  535. /* */
  536. /* Insert a polygon with 2 interior rings and Z ordinates. */
  537. /* */
  538. /*======================================================================*/
  539. /* The exterior ring. */
  540. pt[0].x = 10; pt[0].y = 10; z[0] = 1; offsets[0] = 0;
  541. pt[1].x = 10; pt[1].y = 20; z[1] = 2;
  542. pt[2].x = 20; pt[2].y = 20; z[2] = 3;
  543. pt[3].x = 20; pt[3].y = 10; z[3] = 4;
  544. pt[4].x = 10; pt[4].y = 10; z[4] = 1;
  545. /* The first interior ring. */
  546. pt[5].x = 12; pt[5].y = 12; z[5] = 6; offsets[1] = 5;
  547. pt[6].x = 12; pt[6].y = 13; z[6] = 7;
  548. pt[7].x = 13; pt[7].y = 13; z[7] = 8;
  549. pt[8].x = 13; pt[8].y = 12; z[8] = 9;
  550. pt[9].x = 12; pt[9].y = 12; z[9] = 6;
  551. /* The second interior ring. */
  552. pt[10].x = 16; pt[10].y = 16; z[10] = 11; offsets[2] = 10;
  553. pt[11].x = 16; pt[11].y = 18; z[11] = 12;
  554. pt[12].x = 18; pt[12].y = 18; z[12] = 13;
  555. pt[13].x = 18; pt[13].y = 16; z[13] = 14;
  556. pt[14].x = 16; pt[14].y = 16; z[14] = 11;
  557. geom.type = geomPolygonZ;
  558. geom.num_points = 15;
  559. geom.num_parts = 3;
  560. /* Convert the polygon into a shape. */
  561. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  562. /* Insert the shape into a table. */
  563. insert_shape (&sc, 24, geomPolygon, data_len, binary);
  564. /*======================================================================*/
  565. /* */
  566. /* Insert a polygon containing no interior rings with measures. */
  567. /* */
  568. /*======================================================================*/
  569. /* The exterior ring. */
  570. pt[0].x = 10; pt[0].y = 10; m[0] = 100;
  571. pt[1].x = 10; pt[1].y = 20; m[1] = -1;
  572. pt[2].x = 20; pt[2].y = 20; m[2] = 300;
  573. pt[3].x = 20; pt[3].y = 10; m[3] = -2;
  574. pt[4].x = 10; pt[4].y = 10; m[4] = 400;
  575. geom.type = geomPolygonM;
  576. geom.num_points = 5;
  577. geom.num_parts = 1;
  578. /* Convert the polygon into a shape. */
  579. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  580. /* Insert the shape into a table. */
  581. insert_shape (&sc, 25, geomPolygon, data_len, binary);
  582. /*======================================================================*/
  583. /* */
  584. /* Insert a polygon with Z ordinates measures but no interior rings. */
  585. /* */
  586. /*======================================================================*/
  587. /* The exterior ring. */
  588. pt[0].x = 20; pt[0].y = 20; z[0] = 100; m[0] = -1;
  589. pt[1].x = 20; pt[1].y = 30; z[1] = -1; m[1] = -2;
  590. pt[2].x = 30; pt[2].y = 30; z[2] = 300; m[2] = -3;
  591. pt[3].x = 30; pt[3].y = 20; z[3] = -2; m[3] = -5;
  592. pt[4].x = 20; pt[4].y = 20; z[4] = 100; m[4] = -1;
  593. geom.type = geomPolygonZM;
  594. geom.num_points = 5;
  595. geom.num_parts = 1;
  596. /* Convert the polygon into a shape. */
  597. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  598. /* Insert the shape into a table. */
  599. insert_shape (&sc, 26, geomPolygon, data_len, binary);
  600. /******************/
  601. /* */
  602. /* MULTIPOLYGONS */
  603. /* */
  604. /******************/
  605. /*======================================================================*/
  606. /* */
  607. /* Insert a multipolygon. The first polygon contains one interior ring, */
  608. /* the second polygon contains no interior rings, and the third polygon */
  609. /* contains two interior rings. */
  610. /* */
  611. /*======================================================================*/
  612. /* The first polygon's exterior ring. */
  613. pt[0].x = 10; pt[0].y = 10; offsets[0] = 0;
  614. pt[1].x = 10; pt[1].y = 20;
  615. pt[2].x = 20; pt[2].y = 20;
  616. pt[3].x = 20; pt[3].y = 10;
  617. pt[4].x = 10; pt[4].y = 10;
  618. /* The first polygon's interior ring. */
  619. pt[5].x = 14; pt[5].y = 14; offsets[1] = 5;
  620. pt[6].x = 14; pt[6].y = 16;
  621. pt[7].x = 16; pt[7].y = 16;
  622. pt[8].x = 16; pt[8].y = 14;
  623. pt[9].x = 14; pt[9].y = 14;
  624. /* The second polygon's exterior ring. */
  625. pt[10].x = 10; pt[10].y = 10; offsets[2] = 10;
  626. pt[11].x = 10; pt[11].y = 20;
  627. pt[12].x = 20; pt[12].y = 20;
  628. pt[13].x = 20; pt[13].y = 10;
  629. pt[14].x = 10; pt[14].y = 10;
  630. /* The third polygon's exterior ring. */
  631. pt[15].x = 50; pt[15].y = 50; offsets[3] = 15;
  632. pt[16].x = 50; pt[16].y = 60;
  633. pt[17].x = 60; pt[17].y = 60;
  634. pt[18].x = 60; pt[18].y = 50;
  635. pt[19].x = 50; pt[19].y = 50;
  636. /* The third polygon's interior ring. */
  637. pt[20].x = 52; pt[20].y = 52; offsets[4] = 20;
  638. pt[21].x = 52; pt[21].y = 54;
  639. pt[22].x = 54; pt[22].y = 54;
  640. pt[23].x = 54; pt[23].y = 52;
  641. pt[24].x = 52; pt[24].y = 52;
  642. /* The second polygon's interior ring. */
  643. pt[25].x = 56; pt[25].y = 56; offsets[5] = 25;
  644. pt[26].x = 56; pt[26].y = 58;
  645. pt[27].x = 58; pt[27].y = 58;
  646. pt[28].x = 58; pt[28].y = 56;
  647. pt[29].x = 56; pt[29].y = 56;
  648. geom.type = geomMultiPolygon;
  649. geom.num_points = 30;
  650. geom.num_parts = 6;
  651. /* Convert the multipolygon into a shape. */
  652. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  653. /* Insert the shape into the table. */
  654. insert_shape (&sc, 27, geomMultiPolygon, data_len, binary);
  655. /*======================================================================*/
  656. /* */
  657. /* Insert a multipolygon containing two polygons with Z ordinates */
  658. /* and no interior rings. */
  659. /* */
  660. /*======================================================================*/
  661. /* The first polygon's exterior ring. */
  662. pt[0].x = 10; pt[0].y = 10; z[0] = 1; offsets[0] = 0;
  663. pt[1].x = 10; pt[1].y = 20; z[1] = 2;
  664. pt[2].x = 20; pt[2].y = 20; z[2] = 3;
  665. pt[3].x = 20; pt[3].y = 10; z[3] = 4;
  666. pt[4].x = 10; pt[4].y = 10; z[4] = 1;
  667. /* The second polygon's exterior ring. */
  668. pt[5].x = 1; pt[5].y = 1; z[5] = 1; offsets[1] = 5;
  669. pt[6].x = 1; pt[6].y = 5; z[6] = 2;
  670. pt[7].x = 5; pt[7].y = 5; z[7] = 3;
  671. pt[8].x = 5; pt[8].y = 1; z[8] = 4;
  672. pt[9].x = 1; pt[9].y = 1; z[9] = 1;
  673. geom.type = geomMultiPolygonZ;
  674. geom.num_points = 10;
  675. geom.num_parts = 2;
  676. /* Convert the multipolygon into a shape. */
  677. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  678. /* Insert the shape into a table. */
  679. insert_shape (&sc, 28, geomMultiPolygon, data_len, binary);
  680. /*======================================================================*/
  681. /* */
  682. /* Insert a multipolygon containing two polygons with measures and no */
  683. /* interior rings. */
  684. /* */
  685. /*======================================================================*/
  686. /* The first polygon's exterior ring. */
  687. pt[0].x = 10; pt[0].y = 10; m[0] = 1; offsets[0] = 0;
  688. pt[1].x = 10; pt[1].y = 20; m[1] = 2;
  689. pt[2].x = 20; pt[2].y = 20; m[2] = 3;
  690. pt[3].x = 20; pt[3].y = 10; m[3] = 4;
  691. pt[4].x = 10; pt[4].y = 10; m[4] = 1;
  692. /* The second polygon's exterior ring. */
  693. pt[5].x = 1; pt[5].y = 1; m[5] = 5; offsets[1] = 5;
  694. pt[6].x = 1; pt[6].y = 5; m[6] = 6;
  695. pt[7].x = 5; pt[7].y = 5; m[7] = 7;
  696. pt[8].x = 5; pt[8].y = 1; m[8] = 8;
  697. pt[9].x = 1; pt[9].y = 1; m[9] = 9;
  698. geom.type = geomMultiPolygonM;
  699. geom.num_points = 10;
  700. geom.num_parts = 2;
  701. /* Convert the multipolygon into a shape. */
  702. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  703. /* Insert the shape into the table. */
  704. insert_shape (&sc, 29, geomMultiPolygon, data_len, binary);
  705. /*======================================================================*/
  706. /* */
  707. /* Insert a multipolygon containing a two polygons with Z ordinates and */
  708. /* measures. The first polygon contains an interior ring, but the */
  709. /* second one does not. */
  710. /* */
  711. /*======================================================================*/
  712. /* The first polygon's exterior ring. */
  713. pt[0].x = 10; pt[0].y = 10; z[0] = 1; m[0] = 6; offsets[0] = 0;
  714. pt[1].x = 10; pt[1].y = 20; z[1] = 2; m[1] = 7;
  715. pt[2].x = 20; pt[2].y = 20; z[2] = 3; m[2] = 8;
  716. pt[3].x = 20; pt[3].y = 10; z[3] = 4; m[3] = 9;
  717. pt[4].x = 10; pt[4].y = 10; z[4] = 1; m[4] = 10;
  718. /* The first polygon's interior ring. */
  719. pt[5].x = 14; pt[5].y = 14; z[5] = -1; m[5] = -2; offsets[1] = 5;
  720. pt[6].x = 14; pt[6].y = 16; z[6] = 3; m[6] = 4;
  721. pt[7].x = 16; pt[7].y = 16; z[7] = -5; m[7] = 6;
  722. pt[8].x = 16; pt[8].y = 14; z[8] = 7; m[8] = -8;
  723. pt[9].x = 14; pt[9].y = 14; z[9] = -1; m[9] = -10.01;
  724. /* The second polygon's exterior ring. */
  725. pt[10].x = 1; pt[10].y = 1; z[10] = 9; m[10] = 6; offsets[2] = 10;
  726. pt[11].x = 1; pt[11].y = 5; z[11] = 6; m[11] = 7;
  727. pt[12].x = 5; pt[12].y = 5; z[12] = 7; m[12] = 8;
  728. pt[13].x = 5; pt[13].y = 1; z[13] = 8; m[13] = 9;
  729. pt[14].x = 1; pt[14].y = 1; z[14] = 9; m[14] = 16;
  730. geom.type = geomMultiPolygonZM;
  731. geom.num_points = 15;
  732. geom.num_parts = 3;
  733. /* Convert the multipolygon into a shape. */
  734. geom_to_shape (&geom, &max_alloced, &data_len, &binary);
  735. /* Insert the shape into the table. */
  736. insert_shape (&sc, 30, geomMultiPolygon, data_len, binary);
  737. /*===============================*/
  738. /* */
  739. /* Disconnect from the database. */
  740. /* */
  741. /*===============================*/
  742. EXEC SQL disconnect current;
  743. }
  744. /***********************************************************************
  745. *
  746. * create_table - Creates a table for inserting spatial data
  747. *
  748. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  749. *
  750. * Purpose:
  751. *
  752. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  753. *
  754. * Parameters:
  755. * sc <Input> == (SpatialColumn *) Spatial column.
  756. *
  757. * RETURN <Output> == (void)
  758. *
  759. ***********************************************************************/
  760. static void create_table (
  761. SpatialColumn *sc
  762. )
  763. {
  764. EXEC SQL BEGIN DECLARE SECTION;
  765. char sql_stmt[256];
  766. EXEC SQL END DECLARE SECTION;
  767. sprintf (sql_stmt, "DROP TABLE %s", sc->table);
  768. EXEC SQL execute immediate :sql_stmt;
  769. sprintf (sql_stmt, "CREATE TABLE %s (id integer, %s ST_Geometry)",
  770. sc->table, sc->column);
  771. EXEC SQL execute immediate :sql_stmt;
  772. }
  773. /***********************************************************************
  774. *
  775. * insert_shape - Inserts the shape and integer id values
  776. * the spatial and id columns of the table
  777. * specified as arguments to the main program.
  778. *
  779. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  780. *
  781. * Purpose:
  782. * The function inserts the shape into the
  783. * spatial column and an integer into the id column.
  784. *
  785. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  786. *
  787. * Parameters:
  788. * sc <Input> == (SpatialColumn *) Spatial column.
  789. * id <Input> == (int) The integer id value.
  790. * type <Input> == (GeomType) The data type of the geometry.
  791. * data_len <Input> == (int) The number of bytes in the shape.
  792. * shape_buffer <Input> == (char *) The shape.
  793. *
  794. * RETURN <Output> == (void)
  795. *
  796. ***********************************************************************/
  797. static void insert_shape (
  798. SpatialColumn *sc,
  799. int id,
  800. GeomType type,
  801. int data_len,
  802. char *shape_buffer
  803. )
  804. {
  805. EXEC SQL BEGIN DECLARE SECTION;
  806. int i, n, xid;
  807. short typ;
  808. char shape_sql[256];
  809. var binary shape;
  810. EXEC SQL END DECLARE SECTION;
  811. int rc;
  812. char shapefunction[19];
  813. /* Set the shapefunction to the "FromShape" function that corresponds */
  814. /* to the data type of the geometry representation. */
  815. switch (type)
  816. {
  817. case geomPoint:
  818. strcpy (shapefunction,"SE_PointFromShape");
  819. break;
  820. case geomLineString:
  821. strcpy (shapefunction,"SE_LineFromShape");
  822. break;
  823. case geomPolygon:
  824. strcpy (shapefunction,"SE_PolyFromShape");
  825. break;
  826. case geomMultiPoint:
  827. strcpy (shapefunction,"SE_MPointFromShape");
  828. break;
  829. case geomMultiLineString:
  830. strcpy (shapefunction,"SE_MLineFromShape");
  831. break;
  832. case geomMultiPolygon:
  833. strcpy (shapefunction,"SE_MPolyFromShape");
  834. break;
  835. }
  836. /* Generate the SQL insert expression. The integer value and the */
  837. /* name of the spatial column are entered as bind parameters. */
  838. sprintf (shape_sql,
  839. "insert into %s (id, %s) values (?, %s (?, %d))",
  840. sc->table, sc->column, shapefunction, sc->srid);
  841. /* Prepare the SQL statement for execution. */
  842. fprintf (stdout, "SQL is %s \n", shape_sql);
  843. EXEC SQL prepare ins_stmt from :shape_sql;
  844. /* Allocate a statement descriptor. */
  845. EXEC SQL allocate descriptor 'ins_desc';
  846. /* Set the number of input parameters */
  847. n = 2;
  848. EXEC SQL set descriptor 'ins_desc' COUNT = :n;
  849. /* Bind the input id to the first input parameter */
  850. n = 1; typ = SQLINT; i = id;
  851. EXEC SQL set descriptor 'ins_desc' VALUE :n
  852. TYPE = :typ,
  853. DATA = :i;
  854. /* Create host variable for the shape. */
  855. if ((rc = ifx_var_alloc(&shape,data_len)) < 0)
  856. {
  857. fprintf(stderr, "Error calling ifx_var_alloc.");
  858. exit(1);
  859. }
  860. if ((rc = ifx_var_setdata(&shape,shape_buffer,data_len)) < 0)
  861. {
  862. fprintf(stderr, "Error calling ifx_var_setdata.");
  863. exit(1);
  864. }
  865. if ((rc = ifx_var_setlen(&shape,data_len)) < 0)
  866. {
  867. fprintf(stderr, "Error calling ifx_var_setlen.");
  868. exit(1);
  869. }
  870. /* Bind the shape to the second input parameter */
  871. n = 2; typ = SQLUDTVAR; xid = XID_LVARCHAR;
  872. EXEC SQL set descriptor 'ins_desc' VALUE :n
  873. TYPE = :typ,
  874. EXTYPEID = :xid,
  875. DATA = :shape;
  876. /* Execute the insert statement. */
  877. EXEC SQL execute ins_stmt using sql descriptor 'ins_desc';
  878. /* Free resources associated with the shape host variable */
  879. if ((rc = ifx_var_dealloc(&shape)) < 0)
  880. {
  881. fprintf(stderr, "Error calling ifx_var_dealloc.");
  882. exit(1);
  883. }
  884. /* Free resources associated with INSERT statement */
  885. EXEC SQL deallocate descriptor 'ins_desc';
  886. EXEC SQL free ins_stmt;
  887. }