read_pg.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  1. /*!
  2. \file lib/vector/Vlib/read_pg.c
  3. \brief Vector library - reading features (PostGIS format)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. \todo Currently only points, linestrings and polygons are supported,
  6. implement also other types
  7. \todo Support multigeometries
  8. \todo PostGIS Topology - fix category handling (read categories
  9. from feature table)
  10. (C) 2011-2013 by the GRASS Development Team
  11. This program is free software under the GNU General Public License
  12. (>=v2). Read the file COPYING that comes with GRASS for details.
  13. \author Martin Landa <landa.martin gmail.com>
  14. */
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <limits.h>
  18. #include <grass/vector.h>
  19. #include <grass/dbmi.h>
  20. #include <grass/glocale.h>
  21. #include "local_proto.h"
  22. #ifdef HAVE_POSTGRES
  23. #include "pg_local_proto.h"
  24. /* #define USE_CURSOR_RND */
  25. static unsigned char *wkb_data;
  26. static unsigned int wkb_data_length;
  27. static int read_next_line_pg(struct Map_info *,
  28. struct line_pnts *, struct line_cats *, int);
  29. SF_FeatureType get_feature(struct Map_info *, int, int);
  30. static unsigned char *hex_to_wkb(const char *, int *);
  31. static int point_from_wkb(const unsigned char *, int, int, int,
  32. struct line_pnts *);
  33. static int linestring_from_wkb(const unsigned char *, int, int, int,
  34. struct line_pnts *, int);
  35. static int polygon_from_wkb(const unsigned char *, int, int, int,
  36. struct Format_info_cache *, int *);
  37. static int geometry_collection_from_wkb(const unsigned char *, int, int, int,
  38. struct Format_info_cache *,
  39. struct feat_parts *);
  40. static int error_corrupted_data(const char *);
  41. static void add_fpart(struct feat_parts *, SF_FeatureType, int, int);
  42. static int get_centroid(struct Map_info *, int, struct line_pnts *, struct line_cats *);
  43. static void error_tuples(struct Format_info_pg *);
  44. #endif
  45. /*!
  46. \brief Read next feature from PostGIS layer. Skip
  47. empty features (level 1 without topology).
  48. t
  49. This function implements sequential access.
  50. The action of this routine can be modified by:
  51. - Vect_read_constraint_region()
  52. - Vect_read_constraint_type()
  53. - Vect_remove_constraints()
  54. \param Map pointer to Map_info structure
  55. \param[out] line_p container used to store line points within
  56. (pointer to line_pnts struct)
  57. \param[out] line_c container used to store line categories within
  58. (pointer line_cats struct)
  59. \return feature type
  60. \return -2 no more features (EOF)
  61. \return -1 out of memory
  62. */
  63. int V1_read_next_line_pg(struct Map_info *Map,
  64. struct line_pnts *line_p, struct line_cats *line_c)
  65. {
  66. #ifdef HAVE_POSTGRES
  67. G_debug(3, "V1_read_next_line_pg()");
  68. /* constraints not ignored */
  69. return read_next_line_pg(Map, line_p, line_c, FALSE);
  70. #else
  71. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  72. return -1;
  73. #endif
  74. }
  75. /*!
  76. \brief Read next feature from PostGIS layer on topological level
  77. (simple feature access).
  78. This function implements sequential access.
  79. \param Map pointer to Map_info structure
  80. \param[out] line_p container used to store line points within
  81. (pointer to line_pnts struct)
  82. \param[out] line_c container used to store line categories within
  83. (pointer to line_cats struct)
  84. \return feature type
  85. \return -2 no more features (EOF)
  86. \return -1 on failure
  87. */
  88. int V2_read_next_line_pg(struct Map_info *Map, struct line_pnts *line_p,
  89. struct line_cats *line_c)
  90. {
  91. #ifdef HAVE_POSTGRES
  92. int line, ret;
  93. struct P_line *Line;
  94. struct bound_box lbox, mbox;
  95. struct Format_info_pg *pg_info;
  96. G_debug(3, "V2_read_next_line_pg()");
  97. pg_info = &(Map->fInfo.pg);
  98. if (Map->constraint.region_flag)
  99. Vect_get_constraint_box(Map, &mbox);
  100. ret = -1;
  101. while (TRUE) {
  102. line = Map->next_line;
  103. if (Map->next_line > Map->plus.n_lines)
  104. return -2;
  105. Line = Map->plus.Line[line];
  106. if (Line == NULL) { /* skip dead features */
  107. Map->next_line++;
  108. continue;
  109. }
  110. if (Map->constraint.type_flag) {
  111. /* skip by type */
  112. if (!(Line->type & Map->constraint.type)) {
  113. Map->next_line++;
  114. continue;
  115. }
  116. }
  117. if (!pg_info->toposchema_name &&
  118. Line->type == GV_CENTROID) {
  119. G_debug(4, "Determine centroid for simple features");
  120. if (line_p != NULL) {
  121. int i, found;
  122. struct bound_box box;
  123. struct boxlist list;
  124. struct P_topo_c *topo = (struct P_topo_c *)Line->topo;
  125. /* get area bbox */
  126. Vect_get_area_box(Map, topo->area, &box);
  127. /* search in spatial index for centroid with area bbox */
  128. dig_init_boxlist(&list, TRUE);
  129. Vect_select_lines_by_box(Map, &box, Line->type, &list);
  130. found = -1;
  131. for (i = 0; i < list.n_values; i++) {
  132. if (list.id[i] == line) {
  133. found = i;
  134. break;
  135. }
  136. }
  137. if (found > -1) {
  138. Vect_reset_line(line_p);
  139. Vect_append_point(line_p, list.box[found].E,
  140. list.box[found].N, 0.0);
  141. }
  142. }
  143. if (line_c != NULL) {
  144. /* cat = FID and offset = FID for centroid */
  145. Vect_reset_cats(line_c);
  146. Vect_cat_set(line_c, 1, (int)Line->offset);
  147. }
  148. ret = GV_CENTROID;
  149. }
  150. else {
  151. /* ignore constraints */
  152. ret = read_next_line_pg(Map, line_p, line_c, TRUE);
  153. if (ret != Line->type) {
  154. G_warning(_("Unexpected feature type (%d) - should be (%d)"),
  155. ret, Line->type);
  156. return -1;
  157. }
  158. }
  159. if (Map->constraint.region_flag) {
  160. /* skip by region */
  161. Vect_line_box(line_p, &lbox);
  162. if (!Vect_box_overlap(&lbox, &mbox)) {
  163. Map->next_line++;
  164. continue;
  165. }
  166. }
  167. /* skip by field ignored */
  168. Map->next_line++; /* read next */
  169. return ret;
  170. }
  171. #else
  172. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  173. #endif
  174. return -1; /* not reached */
  175. }
  176. /*!
  177. \brief Read feature from PostGIS layer at given offset (level 1 without topology)
  178. This function implements random access on level 1.
  179. \param Map pointer to Map_info structure
  180. \param[out] line_p container used to store line points within
  181. (pointer line_pnts struct)
  182. \param[out] line_c container used to store line categories within
  183. (pointer line_cats struct)
  184. \param offset given offset
  185. \return line type
  186. \return 0 dead line
  187. \return -2 no more features
  188. \return -1 out of memory
  189. */
  190. int V1_read_line_pg(struct Map_info *Map,
  191. struct line_pnts *line_p, struct line_cats *line_c,
  192. off_t offset)
  193. {
  194. #ifdef HAVE_POSTGRES
  195. long fid;
  196. int ipart, type;
  197. struct Format_info_pg *pg_info;
  198. pg_info = &(Map->fInfo.pg);
  199. G_debug(3, "V1_read_line_pg(): offset = %lu offset_num = %lu",
  200. (long)offset, (long)pg_info->offset.array_num);
  201. if (offset >= pg_info->offset.array_num)
  202. return -2; /* nothing to read */
  203. if (line_p != NULL)
  204. Vect_reset_line(line_p);
  205. if (line_c != NULL)
  206. Vect_reset_cats(line_c);
  207. fid = pg_info->offset.array[offset];
  208. G_debug(4, " fid = %ld", fid);
  209. /* read feature to cache if necessary */
  210. if (pg_info->cache.fid != fid) {
  211. int type;
  212. G_debug(3, "read (%s) feature (fid = %ld) to cache",
  213. pg_info->table_name, fid);
  214. get_feature(Map, fid, -1);
  215. if (pg_info->cache.sf_type == SF_NONE) {
  216. G_warning(_("Feature %ld without geometry skipped"), fid);
  217. return -1;
  218. }
  219. type = (int)pg_info->cache.sf_type;
  220. if (type < 0) /* -1 || - 2 */
  221. return type;
  222. }
  223. /* get data from cache */
  224. if (pg_info->cache.sf_type == SF_POINT ||
  225. pg_info->cache.sf_type == SF_LINESTRING)
  226. ipart = 0;
  227. else
  228. ipart = pg_info->offset.array[offset + 1];
  229. type = pg_info->cache.lines_types[ipart];
  230. G_debug(3, "read feature part: %d -> type = %d", ipart, type);
  231. if (line_p)
  232. Vect_append_points(line_p, pg_info->cache.lines[ipart], GV_FORWARD);
  233. if (line_c)
  234. Vect_cat_set(line_c, 1, (int)fid);
  235. return type;
  236. #else
  237. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  238. return -1;
  239. #endif
  240. }
  241. /*!
  242. \brief Read feature from PostGIS layer on topological level
  243. This function implements random access on level 2.
  244. Note: Topology must be built at level >= GV_BUILD_BASE
  245. \param Map pointer to Map_info structure
  246. \param[out] line_p container used to store line points within (pointer line_pnts struct)
  247. \param[out] line_c container used to store line categories within (pointer line_cats struct)
  248. \param line feature id to read
  249. \return feature type
  250. \return 0 dead feature
  251. \return -1 on error
  252. */
  253. int V2_read_line_pg(struct Map_info *Map, struct line_pnts *line_p,
  254. struct line_cats *line_c, int line)
  255. {
  256. #ifdef HAVE_POSTGRES
  257. int fid, cache_idx;
  258. struct Format_info_pg *pg_info;
  259. struct P_line *Line;
  260. pg_info = &(Map->fInfo.pg);
  261. if (line < 1 || line > Map->plus.n_lines) {
  262. G_warning(_("Attempt to access feature with invalid id (%d)"), line);
  263. return -1;
  264. }
  265. Line = Map->plus.Line[line];
  266. if (Line == NULL) {
  267. G_warning(_("Attempt to access dead feature %d"), line);
  268. return 0;
  269. }
  270. G_debug(4, "V2_read_line_pg() line = %d type = %d offset = %"PRI_OFF_T,
  271. line, Line->type, Line->offset);
  272. if (!line_p && !line_c)
  273. return Line->type;
  274. if (line_p)
  275. Vect_reset_line(line_p);
  276. if (Line->type == GV_CENTROID && !pg_info->toposchema_name) {
  277. /* simple features access: get centroid from sidx */
  278. return get_centroid(Map, line, line_p, line_c);
  279. }
  280. /* get feature id */
  281. if (pg_info->toposchema_name)
  282. fid = Line->offset;
  283. else
  284. fid = pg_info->offset.array[Line->offset];
  285. /* read feature */
  286. if (pg_info->cache.ctype == CACHE_MAP) {
  287. cache_idx = line - 1;
  288. if (cache_idx >= pg_info->cache.lines_num)
  289. G_fatal_error(_("Requesting invalid feature from cache (%d). Number of features in cache: %d"),
  290. cache_idx, pg_info->cache.lines_num);
  291. if (pg_info->cache.lines_types[cache_idx] != Line->type)
  292. G_warning(_("Feature %d: unexpected type (%d) - should be %d"),
  293. line, pg_info->cache.lines_types[cache_idx], Line->type);
  294. }
  295. else {
  296. get_feature(Map, fid, Line->type);
  297. cache_idx = 0;
  298. }
  299. /* check sf type */
  300. if (pg_info->cache.sf_type == SF_NONE) {
  301. G_warning(_("Feature %d without geometry skipped"), line);
  302. return -1;
  303. }
  304. if (0 > (int)pg_info->cache.sf_type) /* -1 || - 2 */
  305. return -1;
  306. if (line_c) {
  307. int cat;
  308. Vect_reset_cats(line_c);
  309. if (!pg_info->toposchema_name) { /* simple features access */
  310. cat = fid;
  311. }
  312. else { /* PostGIS Topology (cats are cached) */
  313. cat = pg_info->cache.lines_cats[cache_idx];
  314. if (cat == 0) { /* not cached yet */
  315. int col_idx;
  316. Vect__select_line_pg(pg_info, fid, Line->type);
  317. col_idx = Line->type & GV_POINTS ? 2 : 3;
  318. if (!PQgetisnull(pg_info->res, 0, col_idx))
  319. cat = pg_info->cache.lines_cats[cache_idx] =
  320. atoi(PQgetvalue(pg_info->res, 0, col_idx));
  321. else
  322. pg_info->cache.lines_cats[cache_idx] = -1; /* no cat */
  323. }
  324. }
  325. if (cat > 0)
  326. Vect_cat_set(line_c, 1, cat);
  327. }
  328. if (line_p)
  329. Vect_append_points(line_p, pg_info->cache.lines[cache_idx], GV_FORWARD);
  330. return Line->type;
  331. #else
  332. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  333. return -1;
  334. #endif
  335. }
  336. #ifdef HAVE_POSTGRES
  337. /*!
  338. \brief Read next feature from PostGIS layer.
  339. \param Map pointer to Map_info structure
  340. \param[out] line_p container used to store line points within
  341. (pointer to line_pnts struct)
  342. \param[out] line_c container used to store line categories within
  343. (pointer line_cats struct)
  344. \param ignore_constraints TRUE to ignore constraints (type, region)
  345. \return feature type
  346. \return -2 no more features (EOF)
  347. \return -1 out of memory
  348. */
  349. int read_next_line_pg(struct Map_info *Map,
  350. struct line_pnts *line_p, struct line_cats *line_c,
  351. int ignore_constraints)
  352. {
  353. int itype;
  354. SF_FeatureType sf_type;
  355. struct Format_info_pg *pg_info;
  356. struct bound_box mbox, lbox;
  357. struct line_pnts *iline;
  358. pg_info = &(Map->fInfo.pg);
  359. if (Map->constraint.region_flag && !ignore_constraints)
  360. Vect_get_constraint_box(Map, &mbox);
  361. while (TRUE) {
  362. /* reset data structures */
  363. if (line_p != NULL)
  364. Vect_reset_line(line_p);
  365. if (line_c != NULL)
  366. Vect_reset_cats(line_c);
  367. /* read feature to cache if necessary */
  368. while (pg_info->cache.lines_next == pg_info->cache.lines_num) {
  369. if (pg_info->cache.ctype == CACHE_MAP &&
  370. pg_info->cache.fid == -2) {
  371. /* stop reading - last cached line */
  372. return -2;
  373. }
  374. /* cache feature -> line_p & line_c */
  375. sf_type = get_feature(Map, -1, -1);
  376. if (sf_type == SF_NONE) {
  377. G_warning(_("Feature %ld without geometry skipped"), pg_info->cache.fid);
  378. return -1;
  379. }
  380. if ((int)sf_type < 0) { /* -1 || - 2 */
  381. if (pg_info->cache.ctype == CACHE_MAP)
  382. pg_info->cache.fid = -2; /* last line cached */
  383. return (int)sf_type;
  384. }
  385. if (sf_type == SF_GEOMETRY || sf_type == SF_NONE) {
  386. G_warning(_("Feature without geometry. Skipped."));
  387. pg_info->cache.lines_next = pg_info->cache.lines_num = 0;
  388. continue;
  389. }
  390. G_debug(4, "%d lines read to cache", pg_info->cache.lines_num);
  391. /* store fid as offset to be used (used for topo access only */
  392. Map->head.last_offset = pg_info->cache.fid;
  393. }
  394. /* get data from cache, skip dead lines (NULL) */
  395. do {
  396. itype = pg_info->cache.lines_types[pg_info->cache.lines_next];
  397. iline = pg_info->cache.lines[pg_info->cache.lines_next];
  398. pg_info->cache.lines_next++; /* read next line from cache */
  399. } while (iline == NULL);
  400. G_debug(4, "read next cached line %d (type = %d)",
  401. pg_info->cache.lines_next, itype);
  402. /* apply constraints */
  403. if (Map->constraint.type_flag && !ignore_constraints) {
  404. /* skip feature by type */
  405. if (!(itype & Map->constraint.type))
  406. continue;
  407. }
  408. if (line_p && Map->constraint.region_flag && !ignore_constraints) {
  409. /* skip feature by region */
  410. Vect_line_box(iline, &lbox);
  411. if (!Vect_box_overlap(&lbox, &mbox))
  412. continue;
  413. }
  414. /* skip feature by field ignored */
  415. if (line_p)
  416. Vect_append_points(line_p, iline, GV_FORWARD);
  417. if (line_c) {
  418. int cat;
  419. if (!pg_info->toposchema_name) { /* simple features access */
  420. cat = (int)pg_info->cache.fid;
  421. }
  422. else { /* PostGIS Topology (cats are cached) */
  423. cat = pg_info->cache.lines_cats[pg_info->cache.lines_next-1];
  424. if (cat == 0) { /* not cached yet */
  425. int col_idx;
  426. col_idx = itype & GV_POINTS ? 2 : 3;
  427. if (!PQgetisnull(pg_info->res, pg_info->cache.lines_next-1, col_idx))
  428. cat = pg_info->cache.lines_cats[Map->next_line-1] =
  429. atoi(PQgetvalue(pg_info->res, pg_info->cache.lines_next-1, col_idx));
  430. else
  431. pg_info->cache.lines_cats[Map->next_line-1] = -1; /* no cat */
  432. }
  433. }
  434. if (cat > 0)
  435. Vect_cat_set(line_c, 1, cat);
  436. }
  437. return itype;
  438. }
  439. return -1; /* not reached */
  440. }
  441. /*!
  442. \brief Read feature geometry
  443. Geometry is stored in lines cache.
  444. \param[in,out] Map pointer to Map_info struct
  445. \param fid feature id to be read (-1 for next)
  446. \param type feature type (GV_POINT, GV_LINE, ...) - use only for topological access
  447. \return simple feature type (SF_POINT, SF_LINESTRING, ...)
  448. \return -1 on error
  449. */
  450. SF_FeatureType get_feature(struct Map_info *Map, int fid, int type)
  451. {
  452. int seq_type;
  453. int force_type; /* force type (GV_BOUNDARY or GV_CENTROID) for topo access only */
  454. char *data;
  455. struct Format_info_pg *pg_info;
  456. pg_info = &(Map->fInfo.pg);
  457. if (!pg_info->geom_column && !pg_info->topogeom_column) {
  458. G_warning(_("No geometry or topo geometry column defined"));
  459. return -1;
  460. }
  461. if (fid < 1) { /* sequantial access */
  462. if (pg_info->cursor_name == NULL &&
  463. Vect__open_cursor_next_line_pg(pg_info, FALSE, Map->plus.built) != 0)
  464. return -1;
  465. }
  466. else { /* random access */
  467. if (!pg_info->fid_column && !pg_info->toposchema_name) {
  468. G_warning(_("Random access not supported. "
  469. "Primary key not defined."));
  470. return -1;
  471. }
  472. #ifdef USE_CURSOR_RND
  473. if (pg_info->cursor_fid > 0)
  474. pg_info->next_line = fid - pg_info->cursor_fid;
  475. else
  476. pg_info->next_line = 0;
  477. if (pg_info->next_line < 0 || pg_info->next_line > CURSOR_PAGE)
  478. Vect__close_cursor_pg(pg_info);
  479. if (pg_info->cursor_name == NULL &&
  480. Vect__open_cursor_line_pg(pg_info, fid, type) != 0)
  481. return -1;
  482. #else
  483. pg_info->next_line = 0;
  484. if (Vect__select_line_pg(pg_info, fid, type) != 0)
  485. return -1;
  486. #endif
  487. }
  488. /* do we need to fetch more records ? */
  489. if (PQntuples(pg_info->res) == CURSOR_PAGE &&
  490. PQntuples(pg_info->res) == pg_info->next_line) {
  491. char stmt[DB_SQL_MAX];
  492. PQclear(pg_info->res);
  493. sprintf(stmt, "FETCH %d in %s", CURSOR_PAGE, pg_info->cursor_name);
  494. G_debug(3, "SQL: %s", stmt);
  495. pg_info->res = PQexec(pg_info->conn, stmt);
  496. if (!pg_info->res || PQresultStatus(pg_info->res) != PGRES_TUPLES_OK) {
  497. error_tuples(pg_info);
  498. return -1;
  499. }
  500. pg_info->next_line = 0;
  501. }
  502. G_debug(3, "get_feature(): next_line = %d", pg_info->next_line);
  503. /* out of results ? */
  504. if (PQntuples(pg_info->res) == pg_info->next_line) {
  505. if (Vect__close_cursor_pg(pg_info) != 0)
  506. return -1; /* failure */
  507. else
  508. return -2; /* nothing to read */
  509. }
  510. force_type = -1;
  511. if (pg_info->toposchema_name) {
  512. if (fid < 0) {
  513. /* sequatial access */
  514. seq_type = atoi(PQgetvalue(pg_info->res, pg_info->next_line, 2));
  515. if (seq_type == GV_BOUNDARY ||
  516. (seq_type == GV_LINE && pg_info->feature_type == SF_POLYGON))
  517. force_type = GV_BOUNDARY;
  518. else if (seq_type == GV_CENTROID)
  519. force_type = GV_CENTROID;
  520. }
  521. else {
  522. /* random access: check topological elemenent type consistency */
  523. if (type & GV_POINTS) {
  524. if (type == GV_POINT &&
  525. strlen(PQgetvalue(pg_info->res, pg_info->next_line, 1)) != 0)
  526. G_warning(_("Inconsistency in topology: detected centroid (should be point)"));
  527. }
  528. else {
  529. int left_face, right_face;
  530. left_face = atoi(PQgetvalue(pg_info->res, pg_info->next_line, 1));
  531. right_face = atoi(PQgetvalue(pg_info->res, pg_info->next_line, 2));
  532. if (type == GV_LINE &&
  533. (left_face != 0 || right_face != 0))
  534. G_warning(_("Inconsistency in topology: detected boundary (should be line)"));
  535. }
  536. }
  537. }
  538. /* get geometry data */
  539. data = (char *)PQgetvalue(pg_info->res, pg_info->next_line, 0);
  540. /* load feature to the cache */
  541. pg_info->cache.sf_type = Vect__cache_feature_pg(data,
  542. FALSE, force_type,
  543. &(pg_info->cache), NULL);
  544. /* cache also categories (only for PostGIS Topology) */
  545. if (pg_info->toposchema_name) {
  546. int cat, col_idx;
  547. col_idx = fid < 0 ? 3 : 2; /* TODO: dermine col_idx for random access */
  548. if (!PQgetisnull(pg_info->res, pg_info->next_line, col_idx))
  549. cat = atoi(PQgetvalue(pg_info->res, pg_info->next_line, col_idx));
  550. else
  551. cat = -1; /* no cat */
  552. pg_info->cache.lines_cats[pg_info->cache.lines_next] = cat;
  553. G_debug(3, "line=%d, type=%d -> cat=%d", pg_info->cache.lines_next+1,
  554. pg_info->cache.lines_types[pg_info->cache.lines_next], cat);
  555. }
  556. /* set feature id */
  557. if (fid < 0) {
  558. pg_info->cache.fid =
  559. atoi(PQgetvalue(pg_info->res, pg_info->next_line, 1));
  560. pg_info->next_line++;
  561. }
  562. else {
  563. pg_info->cache.fid = fid;
  564. }
  565. return pg_info->cache.sf_type;
  566. }
  567. /*!
  568. \brief Convert HEX to WKB data
  569. \param hex_data HEX data
  570. \param[out] nbytes number of bytes in output buffer
  571. \return pointer to WKB data buffer
  572. */
  573. unsigned char *hex_to_wkb(const char *hex_data, int *nbytes)
  574. {
  575. unsigned int length;
  576. int i;
  577. length = strlen(hex_data) / 2 + 1;
  578. if (length > wkb_data_length) {
  579. wkb_data_length = length;
  580. wkb_data = G_realloc(wkb_data, wkb_data_length);
  581. }
  582. *nbytes = length - 1;
  583. for (i = 0; i < (*nbytes); i++) {
  584. wkb_data[i] =
  585. (unsigned char)((hex_data[2 * i] >
  586. 'F' ? hex_data[2 * i] - 0x57 : hex_data[2 * i] >
  587. '9' ? hex_data[2 * i] - 0x37 : hex_data[2 * i] -
  588. 0x30) << 4);
  589. wkb_data[i] |=
  590. (unsigned char)(hex_data[2 * i + 1] >
  591. 'F' ? hex_data[2 * i + 1] -
  592. 0x57 : hex_data[2 * i + 1] >
  593. '9' ? hex_data[2 * i + 1] -
  594. 0x37 : hex_data[2 * i + 1] - 0x30);
  595. }
  596. wkb_data[(*nbytes)] = 0;
  597. return wkb_data;
  598. }
  599. /*!
  600. \brief Read geometry from HEX data
  601. This code is inspired by OGRGeometryFactory::createFromWkb() from
  602. GDAL/OGR library.
  603. \param data HEX data
  604. \param skip_polygon skip polygons (level 1)
  605. \param force_type force GV_BOUNDARY or GV_CENTROID (used for PostGIS topology only)
  606. \param[out] cache lines cache
  607. \param[out] fparts used for building pseudo-topology (or NULL)
  608. \return simple feature type
  609. \return SF_GEOMETRY on error
  610. */
  611. SF_FeatureType Vect__cache_feature_pg(const char *data, int skip_polygon,
  612. int force_type,
  613. struct Format_info_cache *cache,
  614. struct feat_parts * fparts)
  615. {
  616. int ret, byte_order, nbytes, is3D;
  617. unsigned char *wkb_data;
  618. unsigned int wkb_flags;
  619. SF_FeatureType ftype;
  620. /* reset cache */
  621. if (cache->ctype == CACHE_MAP)
  622. cache->lines_num++;
  623. else {
  624. /* next to be read from cache */
  625. cache->lines_next = 0;
  626. cache->lines_num = 1;
  627. }
  628. cache->fid = -1;
  629. if (fparts)
  630. fparts->n_parts = 0;
  631. wkb_flags = 0;
  632. wkb_data = hex_to_wkb(data, &nbytes);
  633. if (nbytes < 5) {
  634. /* G_free(wkb_data); */
  635. if (nbytes > 0) {
  636. G_debug(3, "Vect__cache_feature_pg(): invalid geometry");
  637. G_warning(_("Invalid WKB content: %d bytes"), nbytes);
  638. return SF_GEOMETRY;
  639. }
  640. else {
  641. G_debug(3, "Vect__cache_feature_pg(): no geometry");
  642. return SF_NONE;
  643. }
  644. }
  645. /* parsing M coordinate not supported */
  646. memcpy(&wkb_flags, wkb_data + 1, 4);
  647. byte_order = (wkb_data[0] == 0 ? ENDIAN_BIG : ENDIAN_LITTLE);
  648. if (byte_order == ENDIAN_BIG)
  649. wkb_flags = SWAP32(wkb_flags);
  650. if (wkb_flags & 0x40000000) {
  651. G_warning(_("Reading EWKB with 4-dimensional coordinates (XYZM) "
  652. "is not supported"));
  653. /* G_free(wkb_data); */
  654. return SF_GEOMETRY;
  655. }
  656. /* PostGIS EWKB format includes an SRID, but this won't be
  657. understood by OGR, so if the SRID flag is set, we remove the
  658. SRID (bytes at offset 5 to 8).
  659. */
  660. if (nbytes > 9 &&
  661. ((byte_order == ENDIAN_BIG && (wkb_data[1] & 0x20)) ||
  662. (byte_order == ENDIAN_LITTLE && (wkb_data[4] & 0x20)))) {
  663. memmove(wkb_data + 5, wkb_data + 9, nbytes - 9);
  664. nbytes -= 4;
  665. if (byte_order == ENDIAN_BIG)
  666. wkb_data[1] &= (~0x20);
  667. else
  668. wkb_data[4] &= (~0x20);
  669. }
  670. if (nbytes < 9 && nbytes != -1) {
  671. /* G_free(wkb_data); */
  672. return SF_GEOMETRY;
  673. }
  674. /* Get the geometry feature type. For now we assume that geometry
  675. type is between 0 and 255 so we only have to fetch one byte.
  676. */
  677. if (byte_order == ENDIAN_LITTLE) {
  678. ftype = (SF_FeatureType) wkb_data[1];
  679. is3D = wkb_data[4] & 0x80 || wkb_data[2] & 0x80;
  680. }
  681. else {
  682. ftype = (SF_FeatureType) wkb_data[4];
  683. is3D = wkb_data[1] & 0x80 || wkb_data[3] & 0x80;
  684. }
  685. G_debug(3, "Vect__cache_feature_pg(): sf_type = %d", ftype);
  686. /* allocate space in lines cache - be minimalistic
  687. more lines require eg. polygon with more rings, multi-features
  688. or geometry collections
  689. */
  690. if (cache->ctype == CACHE_MAP) {
  691. Vect__reallocate_cache(cache, 1, TRUE);
  692. }
  693. else {
  694. if (!cache->lines) {
  695. Vect__reallocate_cache(cache, 1, FALSE);
  696. }
  697. }
  698. ret = -1;
  699. if (ftype == SF_POINT) {
  700. cache->lines_types[cache->lines_num-1] = force_type == GV_CENTROID ? force_type : GV_POINT;
  701. ret = point_from_wkb(wkb_data, nbytes, byte_order,
  702. is3D, cache->lines[cache->lines_num-1]);
  703. add_fpart(fparts, ftype, 0, 1);
  704. }
  705. else if (ftype == SF_LINESTRING) {
  706. cache->lines_types[cache->lines_num-1] = force_type == GV_BOUNDARY ? force_type : GV_LINE;
  707. ret = linestring_from_wkb(wkb_data, nbytes, byte_order,
  708. is3D, cache->lines[cache->lines_num-1], FALSE);
  709. add_fpart(fparts, ftype, 0, 1);
  710. }
  711. else if (ftype == SF_POLYGON && !skip_polygon) {
  712. int nrings;
  713. cache->lines_num = 0; /* reset before reading rings */
  714. ret = polygon_from_wkb(wkb_data, nbytes, byte_order,
  715. is3D, cache, &nrings);
  716. add_fpart(fparts, ftype, 0, nrings);
  717. }
  718. else if (ftype == SF_MULTIPOINT ||
  719. ftype == SF_MULTILINESTRING ||
  720. ftype == SF_MULTIPOLYGON || ftype == SF_GEOMETRYCOLLECTION) {
  721. ret = geometry_collection_from_wkb(wkb_data, nbytes, byte_order,
  722. is3D, cache, fparts);
  723. }
  724. else {
  725. G_warning(_("Unsupported feature type %d"), ftype);
  726. }
  727. if (cache->ctype != CACHE_MAP) {
  728. /* read next feature from cache */
  729. cache->lines_next = 0;
  730. }
  731. /* G_free(wkb_data); */
  732. return ret > 0 ? ftype : SF_GEOMETRY;
  733. }
  734. /*!
  735. \brief Read point for WKB data
  736. See OGRPoint::importFromWkb() from GDAL/OGR library
  737. \param wkb_data WKB data
  738. \param nbytes number of bytes (WKB data buffer)
  739. \param byte_order byte order (ENDIAN_LITTLE, ENDIAN_BIG)
  740. \param with_z WITH_Z for 3D data
  741. \param[out] line_p point geometry (pointer to line_pnts struct)
  742. \return wkb size
  743. \return -1 on error
  744. */
  745. int point_from_wkb(const unsigned char *wkb_data, int nbytes, int byte_order,
  746. int with_z, struct line_pnts *line_p)
  747. {
  748. double x, y, z;
  749. if (nbytes < 21 && nbytes != -1)
  750. return -1;
  751. /* get vertex */
  752. memcpy(&x, wkb_data + 5, 8);
  753. memcpy(&y, wkb_data + 5 + 8, 8);
  754. if (byte_order == ENDIAN_BIG) {
  755. SWAPDOUBLE(&x);
  756. SWAPDOUBLE(&y);
  757. }
  758. if (with_z) {
  759. if (nbytes < 29 && nbytes != -1)
  760. return -1;
  761. memcpy(&z, wkb_data + 5 + 16, 8);
  762. if (byte_order == ENDIAN_BIG) {
  763. SWAPDOUBLE(&z);
  764. }
  765. }
  766. else {
  767. z = 0.0;
  768. }
  769. if (line_p) {
  770. Vect_reset_line(line_p);
  771. Vect_append_point(line_p, x, y, z);
  772. }
  773. return 5 + 8 * (with_z == WITH_Z ? 3 : 2);
  774. }
  775. /*!
  776. \brief Read line for WKB data
  777. See OGRLineString::importFromWkb() from GDAL/OGR library
  778. \param wkb_data WKB data
  779. \param nbytes number of bytes (WKB data buffer)
  780. \param byte_order byte order (ENDIAN_LITTLE, ENDIAN_BIG)
  781. \param with_z WITH_Z for 3D data
  782. \param[out] line_p line geometry (pointer to line_pnts struct)
  783. \return wkb size
  784. \return -1 on error
  785. */
  786. int linestring_from_wkb(const unsigned char *wkb_data, int nbytes,
  787. int byte_order, int with_z, struct line_pnts *line_p,
  788. int is_ring)
  789. {
  790. int npoints, point_size, buff_min_size, offset;
  791. int i;
  792. double x, y, z;
  793. if (is_ring)
  794. offset = 5;
  795. else
  796. offset = 0;
  797. if (is_ring && nbytes < 4 && nbytes != -1)
  798. return error_corrupted_data(NULL);
  799. /* get the vertex count */
  800. memcpy(&npoints, wkb_data + (5 - offset), 4);
  801. if (byte_order == ENDIAN_BIG) {
  802. npoints = SWAP32(npoints);
  803. }
  804. /* check if the wkb stream buffer is big enough to store fetched
  805. number of points. 16 or 24 - size of point structure
  806. */
  807. point_size = with_z ? 24 : 16;
  808. if (npoints < 0 || npoints > INT_MAX / point_size)
  809. return error_corrupted_data(NULL);
  810. buff_min_size = point_size * npoints;
  811. if (nbytes != -1 && buff_min_size > nbytes - (9 - offset))
  812. return error_corrupted_data(_("Length of input WKB is too small"));
  813. if (line_p)
  814. Vect_reset_line(line_p);
  815. /* get the vertex */
  816. for (i = 0; i < npoints; i++) {
  817. memcpy(&x, wkb_data + (9 - offset) + i * point_size, 8);
  818. memcpy(&y, wkb_data + (9 - offset) + 8 + i * point_size, 8);
  819. if (with_z)
  820. memcpy(&z, wkb_data + (9 - offset) + 16 + i * point_size, 8);
  821. else
  822. z = 0.0;
  823. if (byte_order == ENDIAN_BIG) {
  824. SWAPDOUBLE(&x);
  825. SWAPDOUBLE(&y);
  826. if (with_z)
  827. SWAPDOUBLE(&z);
  828. }
  829. if (line_p)
  830. Vect_append_point(line_p, x, y, z);
  831. }
  832. return (9 - offset) + (with_z == WITH_Z ? 3 : 2) * 8 * line_p->n_points;
  833. }
  834. /*!
  835. \brief Read polygon for WKB data
  836. See OGRPolygon::importFromWkb() from GDAL/OGR library
  837. \param wkb_data WKB data
  838. \param nbytes number of bytes (WKB data buffer)
  839. \param byte_order byte order (ENDIAN_LITTLE, ENDIAN_BIG)
  840. \param with_z WITH_Z for 3D data
  841. \param[out] line_p array of rings (pointer to line_pnts struct)
  842. \param[out] nrings number of rings
  843. \return wkb size
  844. \return -1 on error
  845. */
  846. int polygon_from_wkb(const unsigned char *wkb_data, int nbytes,
  847. int byte_order, int with_z,
  848. struct Format_info_cache *cache, int *nrings)
  849. {
  850. int data_offset, i, nsize, isize;
  851. int num_of_rings;
  852. struct line_pnts *line_i;
  853. if (nbytes < 9 && nbytes != -1)
  854. return -1;
  855. /* get the ring count */
  856. memcpy(nrings, wkb_data + 5, 4);
  857. if (byte_order == ENDIAN_BIG) {
  858. *nrings = SWAP32(*nrings);
  859. }
  860. if (*nrings < 0) {
  861. return -1;
  862. }
  863. num_of_rings = *nrings;
  864. /* reallocate space for islands if needed */
  865. Vect__reallocate_cache(cache, num_of_rings, FALSE);
  866. cache->lines_num += num_of_rings;
  867. /* each ring has a minimum of 4 bytes (point count) */
  868. if (nbytes != -1 && nbytes - 9 < num_of_rings * 4) {
  869. return error_corrupted_data(_("Length of input WKB is too small"));
  870. }
  871. data_offset = 9;
  872. if (nbytes != -1)
  873. nbytes -= data_offset;
  874. /* get the rings */
  875. nsize = 9;
  876. for (i = 0; i < num_of_rings; i++) {
  877. if (cache->lines_next >= cache->lines_num)
  878. G_fatal_error(_("Invalid cache index %d (max: %d)"),
  879. cache->lines_next, cache->lines_num);
  880. line_i = cache->lines[cache->lines_next];
  881. cache->lines_types[cache->lines_next++] = GV_BOUNDARY;
  882. linestring_from_wkb(wkb_data + data_offset, nbytes, byte_order,
  883. with_z, line_i, TRUE);
  884. if (nbytes != -1) {
  885. isize = 4 + 8 * (with_z == WITH_Z ? 3 : 2) * line_i->n_points;
  886. nbytes -= isize;
  887. }
  888. nsize += isize;
  889. data_offset += isize;
  890. }
  891. return nsize;
  892. }
  893. /*!
  894. \brief Read geometry collection for WKB data
  895. See OGRGeometryCollection::importFromWkbInternal() from GDAL/OGR library
  896. \param wkb_data WKB data
  897. \param nbytes number of bytes (WKB data buffer)
  898. \param byte_order byte order (ENDIAN_LITTLE, ENDIAN_BIG)
  899. \param with_z WITH_Z for 3D data
  900. \param ipart part to cache (starts at 0)
  901. \param[out] cache lines cache
  902. \param[in,out] fparts feature parts (required for building pseudo-topology)
  903. \return number of parts
  904. \return -1 on error
  905. */
  906. int geometry_collection_from_wkb(const unsigned char *wkb_data, int nbytes,
  907. int byte_order, int with_z,
  908. struct Format_info_cache *cache,
  909. struct feat_parts *fparts)
  910. {
  911. int ipart, nparts, data_offset, nsize;
  912. unsigned char *wkb_subdata;
  913. SF_FeatureType ftype;
  914. if (nbytes < 9 && nbytes != -1)
  915. return error_corrupted_data(NULL);
  916. /* get the geometry count */
  917. memcpy(&nparts, wkb_data + 5, 4);
  918. if (byte_order == ENDIAN_BIG) {
  919. nparts = SWAP32(nparts);
  920. }
  921. if (nparts < 0 || nparts > INT_MAX / 9) {
  922. return error_corrupted_data(NULL);
  923. }
  924. G_debug(5, "\t(geometry collections) parts: %d", nparts);
  925. /* each geometry has a minimum of 9 bytes */
  926. if (nbytes != -1 && nbytes - 9 < nparts * 9) {
  927. return error_corrupted_data(_("Length of input WKB is too small"));
  928. }
  929. data_offset = 9;
  930. if (nbytes != -1)
  931. nbytes -= data_offset;
  932. /* reallocate space for parts if needed */
  933. Vect__reallocate_cache(cache, nparts, FALSE);
  934. /* get parts */
  935. for (ipart = 0; ipart < nparts; ipart++) {
  936. wkb_subdata = (unsigned char *)wkb_data + data_offset;
  937. if (nbytes < 9 && nbytes != -1)
  938. return error_corrupted_data(NULL);
  939. if (byte_order == ENDIAN_LITTLE) {
  940. ftype = (SF_FeatureType) wkb_subdata[1];
  941. }
  942. else {
  943. ftype = (SF_FeatureType) wkb_subdata[4];
  944. }
  945. if (ftype == SF_POINT) {
  946. cache->lines_types[cache->lines_next] = GV_POINT;
  947. nsize = point_from_wkb(wkb_subdata, nbytes, byte_order, with_z,
  948. cache->lines[cache->lines_next]);
  949. cache->lines_num++;
  950. add_fpart(fparts, ftype, cache->lines_next, 1);
  951. cache->lines_next++;
  952. }
  953. else if (ftype == SF_LINESTRING) {
  954. cache->lines_types[cache->lines_next] = GV_LINE;
  955. nsize =
  956. linestring_from_wkb(wkb_subdata, nbytes, byte_order, with_z,
  957. cache->lines[cache->lines_next], FALSE);
  958. cache->lines_num++;
  959. add_fpart(fparts, ftype, cache->lines_next, 1);
  960. cache->lines_next++;
  961. }
  962. else if (ftype == SF_POLYGON) {
  963. int idx, nrings;
  964. idx = cache->lines_next;
  965. nsize = polygon_from_wkb(wkb_subdata, nbytes, byte_order,
  966. with_z, cache, &nrings);
  967. add_fpart(fparts, ftype, idx, nrings);
  968. }
  969. else if (ftype == SF_GEOMETRYCOLLECTION ||
  970. ftype == SF_MULTIPOLYGON ||
  971. ftype == SF_MULTILINESTRING || ftype == SF_MULTIPOLYGON) {
  972. geometry_collection_from_wkb(wkb_subdata, nbytes, byte_order,
  973. with_z, cache, fparts);
  974. }
  975. else {
  976. G_warning(_("Unsupported feature type %d"), ftype);
  977. }
  978. if (nbytes != -1) {
  979. nbytes -= nsize;
  980. }
  981. data_offset += nsize;
  982. }
  983. return nparts;
  984. }
  985. /*!
  986. \brief Report error message
  987. \param msg message (NULL)
  988. \return -1
  989. */
  990. int error_corrupted_data(const char *msg)
  991. {
  992. if (msg)
  993. G_warning(_("Corrupted data. %s."), msg);
  994. else
  995. G_warning(_("Corrupted data"));
  996. return -1;
  997. }
  998. /*!
  999. \brief Create select cursor for sequential access (internal use only)
  1000. Allocated cursor name should be freed by G_free().
  1001. \param pg_info pointer to Format_info_pg struct
  1002. \param fetch_all TRUE to fetch all records
  1003. \param[out] cursor name
  1004. \return 0 on success
  1005. \return -1 on failure
  1006. */
  1007. int Vect__open_cursor_next_line_pg(struct Format_info_pg *pg_info, int fetch_all, int built_level)
  1008. {
  1009. char stmt[DB_SQL_MAX];
  1010. if (Vect__execute_pg(pg_info->conn, "BEGIN") == -1)
  1011. return -1;
  1012. /* set cursor name */
  1013. G_asprintf(&(pg_info->cursor_name),
  1014. "%s_%s_%p", pg_info->schema_name, pg_info->table_name, pg_info->conn);
  1015. if (!pg_info->toposchema_name) {
  1016. /* simple feature access (geom, fid) */
  1017. /* TODO: start_fid */
  1018. if (pg_info->where) {
  1019. /* set attribute filter if where sql statement defined */
  1020. char **tokens = G_tokenize(pg_info->where, "=");
  1021. if (G_number_of_tokens(tokens) != 2) {
  1022. G_warning(_("Unable to parse '%s'"), pg_info->where);
  1023. return -1;
  1024. }
  1025. sprintf(stmt,
  1026. "DECLARE %s CURSOR FOR SELECT \"%s\",\"%s\" FROM \"%s\".\"%s\" WHERE \"%s\"=%s ORDER BY \"%s\"",
  1027. pg_info->cursor_name, pg_info->geom_column, pg_info->fid_column, pg_info->schema_name,
  1028. pg_info->table_name, tokens[0], tokens[1], pg_info->fid_column);
  1029. G_free_tokens(tokens);
  1030. }
  1031. else {
  1032. sprintf(stmt,
  1033. "DECLARE %s CURSOR FOR SELECT \"%s\",\"%s\" FROM \"%s\".\"%s\" ORDER BY \"%s\"",
  1034. pg_info->cursor_name, pg_info->geom_column, pg_info->fid_column, pg_info->schema_name,
  1035. pg_info->table_name, pg_info->fid_column);
  1036. }
  1037. }
  1038. else {
  1039. /* topology access (geom,id,fid,type) */
  1040. /* TODO: optimize SQL statement (for points/centroids) */
  1041. sprintf(stmt,
  1042. "DECLARE %s CURSOR FOR "
  1043. "SELECT geom,id,type,fid FROM ("
  1044. "SELECT tt.node_id AS id,tt.geom, %d AS type, ft.%s AS fid FROM \"%s\".node AS tt "
  1045. "LEFT JOIN \"%s\".\"%s\" AS ft ON (%s).type = 1 AND (%s).id = node_id "
  1046. "WHERE containing_face IS NULL AND node_id NOT IN "
  1047. "(SELECT node FROM (SELECT start_node AS node FROM \"%s\".edge GROUP BY start_node UNION ALL "
  1048. "SELECT end_node AS node FROM \"%s\".edge GROUP BY end_node) AS foo) UNION ALL "
  1049. "SELECT tt.node_id AS id,tt.geom, %d AS type, ft.%s AS fid FROM \"%s\".node AS tt "
  1050. "LEFT JOIN \"%s\".\"%s\" AS ft ON (%s).type = 3 AND (%s).id = %s "
  1051. "WHERE containing_face IS NOT NULL AND node_id NOT IN "
  1052. "(SELECT node FROM (SELECT start_node AS node FROM \"%s\".edge GROUP BY start_node UNION ALL "
  1053. "SELECT end_node AS node FROM \"%s\".edge GROUP BY end_node) AS foo) UNION ALL "
  1054. "SELECT tt.edge_id AS id, tt.geom, %d AS type, ft.%s AS fid FROM \"%s\".edge AS tt "
  1055. "LEFT JOIN \"%s\".\"%s\" AS ft ON (%s).type = 2 AND (%s).id = edge_id "
  1056. "WHERE left_face = 0 AND right_face = 0 UNION ALL "
  1057. "SELECT tt.edge_id AS id, tt.geom, %d AS type, ft.%s AS fid FROM \"%s\".edge AS tt "
  1058. "LEFT JOIN \"%s\".\"%s\" AS ft ON (%s).type = 2 AND (%s).id = edge_id "
  1059. "WHERE left_face != 0 OR right_face != 0 ) AS foo ORDER BY type,id",
  1060. pg_info->cursor_name,
  1061. GV_POINT, pg_info->fid_column, pg_info->toposchema_name, pg_info->schema_name, pg_info->table_name,
  1062. pg_info->topogeom_column, pg_info->topogeom_column, pg_info->toposchema_name, pg_info->toposchema_name,
  1063. GV_CENTROID, pg_info->fid_column, pg_info->toposchema_name, pg_info->schema_name, pg_info->table_name,
  1064. pg_info->topogeom_column, pg_info->topogeom_column,
  1065. built_level >= GV_BUILD_CENTROIDS ? "containing_face" : "node_id",
  1066. pg_info->toposchema_name, pg_info->toposchema_name,
  1067. GV_LINE, pg_info->fid_column, pg_info->toposchema_name, pg_info->schema_name, pg_info->table_name,
  1068. pg_info->topogeom_column, pg_info->topogeom_column,
  1069. GV_BOUNDARY, pg_info->fid_column, pg_info->toposchema_name, pg_info->schema_name, pg_info->table_name,
  1070. pg_info->topogeom_column, pg_info->topogeom_column);
  1071. }
  1072. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  1073. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  1074. return -1;
  1075. }
  1076. if (fetch_all)
  1077. sprintf(stmt, "FETCH ALL in %s", pg_info->cursor_name);
  1078. else
  1079. sprintf(stmt, "FETCH %d in %s", CURSOR_PAGE, pg_info->cursor_name);
  1080. G_debug(3, "SQL: %s", stmt);
  1081. pg_info->res = PQexec(pg_info->conn, stmt); /* fetch records from select cursor */
  1082. if (!pg_info->res || PQresultStatus(pg_info->res) != PGRES_TUPLES_OK) {
  1083. error_tuples(pg_info);
  1084. return -1;
  1085. }
  1086. pg_info->next_line = 0;
  1087. return 0;
  1088. }
  1089. /*!
  1090. \brief Open select cursor for random access (internal use only)
  1091. Fetch number of feature (given by CURSOR_PAGE) starting with
  1092. <em>fid</em>.
  1093. Allocated cursor name should be freed by G_free().
  1094. \param pg_info pointer to Format_info_pg struct
  1095. \param fid feature id to get
  1096. \param type feature type
  1097. \return 0 on success
  1098. \return -1 on failure
  1099. */
  1100. int Vect__open_cursor_line_pg(struct Format_info_pg *pg_info, int fid, int type)
  1101. {
  1102. char stmt[DB_SQL_MAX];
  1103. G_debug(3, "Vect__open_cursor_line_pg(): fid range = %d-%d, type = %d",
  1104. fid, fid + CURSOR_PAGE, type);
  1105. if (Vect__execute_pg(pg_info->conn, "BEGIN") == -1)
  1106. return -1;
  1107. pg_info->cursor_fid = fid;
  1108. G_asprintf(&(pg_info->cursor_name),
  1109. "%s_%s_%d_%p", pg_info->schema_name, pg_info->table_name, fid, pg_info->conn);
  1110. if (!pg_info->toposchema_name) {
  1111. /* simple feature access (geom) */
  1112. sprintf(stmt,
  1113. "DECLARE %s CURSOR FOR SELECT %s FROM \"%s\".\"%s\" "
  1114. "WHERE %s BETWEEN %d AND %d ORDER BY %s", pg_info->cursor_name,
  1115. pg_info->geom_column, pg_info->schema_name, pg_info->table_name,
  1116. pg_info->fid_column, fid, fid + CURSOR_PAGE, pg_info->fid_column);
  1117. }
  1118. else {
  1119. /* topological access */
  1120. if (!(type & (GV_POINTS | GV_LINES))) {
  1121. G_warning(_("Unsupported feature type %d"), type);
  1122. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  1123. return -1;
  1124. }
  1125. if (type & GV_POINTS) {
  1126. /* points (geom,containing_face) */
  1127. sprintf(stmt,
  1128. "DECLARE %s CURSOR FOR SELECT geom,containing_face "
  1129. " FROM \"%s\".node WHERE node_id BETWEEN %d AND %d ORDER BY node_id",
  1130. pg_info->cursor_name,
  1131. pg_info->toposchema_name, fid, fid + CURSOR_PAGE);
  1132. }
  1133. else {
  1134. /* edges (geom,left_face,right_face) */
  1135. sprintf(stmt,
  1136. "DECLARE %s CURSOR FOR SELECT geom,left_face,right_face "
  1137. " FROM \"%s\".edge WHERE edge_id BETWEEN %d AND %d ORDER BY edge_id",
  1138. pg_info->cursor_name,
  1139. pg_info->toposchema_name, fid, fid + CURSOR_PAGE);
  1140. }
  1141. }
  1142. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  1143. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  1144. return -1;
  1145. }
  1146. pg_info->next_line = 0;
  1147. sprintf(stmt, "FETCH ALL in %s", pg_info->cursor_name);
  1148. pg_info->res = PQexec(pg_info->conn, stmt);
  1149. if (!pg_info->res || PQresultStatus(pg_info->res) != PGRES_TUPLES_OK) {
  1150. error_tuples(pg_info);
  1151. return -1;
  1152. }
  1153. return 0;
  1154. }
  1155. /*!
  1156. \brief Close select cursor
  1157. \param pg_info pointer to Format_info_pg struct
  1158. \return 0 on success
  1159. \return -1 on failure
  1160. */
  1161. int Vect__close_cursor_pg(struct Format_info_pg *pg_info)
  1162. {
  1163. if (pg_info->res) {
  1164. PQclear(pg_info->res);
  1165. pg_info->res = NULL;
  1166. }
  1167. if (pg_info->cursor_name) {
  1168. char stmt[DB_SQL_MAX];
  1169. sprintf(stmt, "CLOSE %s", pg_info->cursor_name);
  1170. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  1171. G_warning(_("Unable to close cursor %s"), pg_info->cursor_name);
  1172. return -1;
  1173. }
  1174. Vect__execute_pg(pg_info->conn, "COMMIT");
  1175. G_free(pg_info->cursor_name);
  1176. pg_info->cursor_name = NULL;
  1177. }
  1178. return 0;
  1179. }
  1180. /*!
  1181. \brief Select feature (internal use only)
  1182. \param pg_info pointer to Format_info_pg struct
  1183. \param fid feature id to get
  1184. \param type feature type
  1185. \return 0 on success
  1186. \return -1 on failure
  1187. */
  1188. int Vect__select_line_pg(struct Format_info_pg *pg_info, int fid, int type)
  1189. {
  1190. char stmt[DB_SQL_MAX];
  1191. if (!pg_info->toposchema_name) {
  1192. /* simple feature access */
  1193. sprintf(stmt,
  1194. "SELECT %s FROM \"%s\".\"%s\" WHERE %s = %d",
  1195. pg_info->geom_column, pg_info->schema_name, pg_info->table_name,
  1196. pg_info->fid_column, fid);
  1197. }
  1198. else {
  1199. /* topological access */
  1200. if (!(type & (GV_POINTS | GV_LINES))) {
  1201. G_warning(_("Unsupported feature type %d"), type);
  1202. return -1;
  1203. }
  1204. if (type & GV_POINTS) {
  1205. int topotype;
  1206. char *nodeid;
  1207. if (type == GV_POINT) {
  1208. topotype = 1;
  1209. nodeid = pg_info->fid_column;
  1210. }
  1211. else { /* assuming GV_CENTROID */
  1212. topotype = 3;
  1213. nodeid = "containing_face";
  1214. }
  1215. sprintf(stmt,
  1216. "SELECT tt.geom,tt.containing_face,ft.%s FROM \"%s\".node AS tt "
  1217. "LEFT JOIN \"%s\".\"%s\" AS ft ON (%s).type = %d and (%s).id = %s "
  1218. "WHERE node_id = %d",
  1219. pg_info->fid_column, pg_info->toposchema_name,
  1220. pg_info->schema_name, pg_info->table_name, pg_info->topogeom_column,
  1221. topotype, pg_info->topogeom_column, nodeid, fid);
  1222. }
  1223. else {
  1224. sprintf(stmt,
  1225. "SELECT tt.geom,tt.left_face,tt.right_face,ft.%s FROM \"%s\".edge AS tt "
  1226. "LEFT JOIN \"%s\".\"%s\" AS ft ON (%s).type = 2 and (%s).id = edge_id "
  1227. "WHERE edge_id = %d",
  1228. pg_info->fid_column, pg_info->toposchema_name,
  1229. pg_info->schema_name, pg_info->table_name, pg_info->topogeom_column,
  1230. pg_info->topogeom_column, fid);
  1231. }
  1232. }
  1233. G_debug(3, "SQL: %s", stmt);
  1234. pg_info->next_line = 0;
  1235. pg_info->res = PQexec(pg_info->conn, stmt);
  1236. if (!pg_info->res || PQresultStatus(pg_info->res) != PGRES_TUPLES_OK) {
  1237. error_tuples(pg_info);
  1238. return -1;
  1239. }
  1240. return 0;
  1241. }
  1242. /*!
  1243. \brief Execute SQL statement
  1244. See pg_local_proto.h
  1245. \param conn pointer to PGconn
  1246. \param stmt query
  1247. \return 0 on success
  1248. \return -1 on error
  1249. */
  1250. int Vect__execute_pg(PGconn * conn, const char *stmt)
  1251. {
  1252. PGresult *result;
  1253. result = NULL;
  1254. G_debug(3, "Vect__execute_pg(): %s", stmt);
  1255. result = PQexec(conn, stmt);
  1256. if (!result || PQresultStatus(result) != PGRES_COMMAND_OK) {
  1257. size_t stmt_len;
  1258. char stmt_prt[512];
  1259. PQclear(result);
  1260. stmt_len = strlen(stmt);
  1261. strncpy(stmt_prt, stmt, stmt_len > 511 ? 511 : stmt_len);
  1262. stmt_prt[stmt_len > 511 ? 511 : stmt_len] = '\0';
  1263. G_warning(_("Execution failed: %s (...)\nReason: %s"), stmt_prt,
  1264. PQerrorMessage(conn));
  1265. return -1;
  1266. }
  1267. PQclear(result);
  1268. return 0;
  1269. }
  1270. /*!
  1271. \brief Execute SQL statement and get value.
  1272. \param conn pointer to PGconn
  1273. \param stmt query
  1274. \return value on success
  1275. \return -1 on error
  1276. */
  1277. int Vect__execute_get_value_pg(PGconn *conn, const char *stmt)
  1278. {
  1279. int ret;
  1280. PGresult *result;
  1281. result = NULL;
  1282. G_debug(3, "Vect__execute_get_value_pg(): %s", stmt);
  1283. result = PQexec(conn, stmt);
  1284. if (!result || PQresultStatus(result) != PGRES_TUPLES_OK ||
  1285. PQntuples(result) != 1) {
  1286. PQclear(result);
  1287. G_warning(_("Execution failed: %s\nReason: %s"), stmt,
  1288. PQerrorMessage(conn));
  1289. return -1;
  1290. }
  1291. ret = atoi(PQgetvalue(result, 0, 0));
  1292. PQclear(result);
  1293. return ret;
  1294. }
  1295. /*!
  1296. \brief Reallocate lines cache
  1297. */
  1298. void Vect__reallocate_cache(struct Format_info_cache *cache, int num, int incr)
  1299. {
  1300. int i;
  1301. if (!incr && cache->lines_alloc >= num)
  1302. return;
  1303. if (!incr && !cache->lines) {
  1304. /* most of features requires only one line cache */
  1305. cache->lines_alloc = 1;
  1306. }
  1307. else {
  1308. cache->lines_alloc += num;
  1309. }
  1310. cache->lines = (struct line_pnts **)G_realloc(cache->lines,
  1311. cache->lines_alloc *
  1312. sizeof(struct line_pnts *));
  1313. cache->lines_types = (int *)G_realloc(cache->lines_types,
  1314. cache->lines_alloc * sizeof(int));
  1315. cache->lines_cats = (int *)G_realloc(cache->lines_cats,
  1316. cache->lines_alloc * sizeof(int));
  1317. if (cache->lines_alloc > 1) {
  1318. for (i = cache->lines_alloc - num; i < cache->lines_alloc; i++) {
  1319. cache->lines[i] = Vect_new_line_struct();
  1320. cache->lines_types[i] = -1;
  1321. cache->lines_cats[i] = -1;
  1322. }
  1323. }
  1324. else {
  1325. cache->lines[0] = Vect_new_line_struct();
  1326. cache->lines_types[0] = -1;
  1327. cache->lines_cats[0] = -1;
  1328. }
  1329. }
  1330. void add_fpart(struct feat_parts *fparts, SF_FeatureType ftype,
  1331. int idx, int nlines)
  1332. {
  1333. if (!fparts)
  1334. return;
  1335. if (fparts->a_parts == 0 || fparts->n_parts >= fparts->a_parts) {
  1336. if (fparts->a_parts == 0)
  1337. fparts->a_parts = 1;
  1338. else
  1339. fparts->a_parts += fparts->n_parts;
  1340. fparts->ftype = (SF_FeatureType *) G_realloc(fparts->ftype,
  1341. fparts->a_parts *
  1342. sizeof(SF_FeatureType));
  1343. fparts->nlines =
  1344. (int *)G_realloc(fparts->nlines, fparts->a_parts * sizeof(int));
  1345. fparts->idx =
  1346. (int *)G_realloc(fparts->idx, fparts->a_parts * sizeof(int));
  1347. }
  1348. fparts->ftype[fparts->n_parts] = ftype;
  1349. fparts->idx[fparts->n_parts] = idx;
  1350. fparts->nlines[fparts->n_parts] = nlines;
  1351. fparts->n_parts++;
  1352. }
  1353. /*
  1354. \brief Get centroid
  1355. \param pg_info pointer to Format_info_pg
  1356. \param centroid centroid id
  1357. \param[out] line_p output geometry
  1358. \return GV_CENTROID on success
  1359. \return -1 on error
  1360. */
  1361. int get_centroid(struct Map_info *Map, int centroid,
  1362. struct line_pnts *line_p, struct line_cats *line_c)
  1363. {
  1364. int i, found;
  1365. struct bound_box box;
  1366. struct boxlist list;
  1367. struct P_line *Line;
  1368. struct P_topo_c *topo;
  1369. Line = Map->plus.Line[centroid];
  1370. topo = (struct P_topo_c *)Line->topo;
  1371. /* get area bbox */
  1372. Vect_get_area_box(Map, topo->area, &box);
  1373. /* search in spatial index for centroid with area bbox */
  1374. dig_init_boxlist(&list, TRUE);
  1375. Vect_select_lines_by_box(Map, &box, Line->type, &list);
  1376. found = -1;
  1377. for (i = 0; i < list.n_values; i++) {
  1378. if (list.id[i] == centroid) {
  1379. found = i;
  1380. break;
  1381. }
  1382. }
  1383. if (found == -1)
  1384. return -1;
  1385. if (line_p) {
  1386. Vect_reset_line(line_p);
  1387. Vect_append_point(line_p, list.box[found].E, list.box[found].N, 0.0);
  1388. }
  1389. if (line_c) {
  1390. Vect_cat_set(line_c, 1, Line->offset);
  1391. }
  1392. return GV_CENTROID;
  1393. }
  1394. void error_tuples(struct Format_info_pg *pg_info)
  1395. {
  1396. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  1397. G_warning(_("Unable to read features. Reason:\n%s"),
  1398. PQresultErrorMessage(pg_info->res));
  1399. if (pg_info->res) {
  1400. PQclear(pg_info->res);
  1401. pg_info->res = NULL;
  1402. }
  1403. }
  1404. #endif