read_pg.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  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. #ifdef HAVE_POSTGRES
  22. #include "pg_local_proto.h"
  23. /* #define USE_CURSOR_RND */
  24. static unsigned char *wkb_data;
  25. static unsigned int wkb_data_length;
  26. static int read_next_line_pg(struct Map_info *,
  27. struct line_pnts *, struct line_cats *, int);
  28. SF_FeatureType get_feature(struct Format_info_pg *, int, int);
  29. static unsigned char *hex_to_wkb(const char *, int *);
  30. static int point_from_wkb(const unsigned char *, int, int, int,
  31. struct line_pnts *);
  32. static int linestring_from_wkb(const unsigned char *, int, int, int,
  33. struct line_pnts *, int);
  34. static int polygon_from_wkb(const unsigned char *, int, int, int,
  35. struct Format_info_cache *, int *);
  36. static int geometry_collection_from_wkb(const unsigned char *, int, int, int,
  37. struct Format_info_cache *,
  38. struct feat_parts *);
  39. static int error_corrupted_data(const char *);
  40. static void reallocate_cache(struct Format_info_cache *, int);
  41. static void add_fpart(struct feat_parts *, SF_FeatureType, int, int);
  42. static int get_centroid(struct Map_info *, int, struct line_pnts *);
  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(pg_info, 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;
  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);
  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. get_feature(pg_info, fid, Line->type);
  287. /* check sf type */
  288. if (pg_info->cache.sf_type == SF_NONE) {
  289. G_warning(_("Feature %d without geometry skipped"), line);
  290. return -1;
  291. }
  292. if (0 > (int)pg_info->cache.sf_type) /* -1 || - 2 */
  293. return -1;
  294. if (line_c) {
  295. int cat;
  296. Vect_reset_cats(line_c);
  297. if (!pg_info->toposchema_name) /* simple features access */
  298. cat = (int) Line->offset;
  299. else /* PostGIS Topology (cats are cached) */
  300. cat = pg_info->cache.lines_cats[0];
  301. if (cat != -1)
  302. Vect_cat_set(line_c, 1, cat);
  303. }
  304. if (line_p)
  305. Vect_append_points(line_p, pg_info->cache.lines[0], GV_FORWARD);
  306. return Line->type;
  307. #else
  308. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  309. return -1;
  310. #endif
  311. }
  312. #ifdef HAVE_POSTGRES
  313. /*!
  314. \brief Read next feature from PostGIS layer.
  315. \param Map pointer to Map_info structure
  316. \param[out] line_p container used to store line points within
  317. (pointer to line_pnts struct)
  318. \param[out] line_c container used to store line categories within
  319. (pointer line_cats struct)
  320. \param ignore_constraints TRUE to ignore constraints (type, region)
  321. \return feature type
  322. \return -2 no more features (EOF)
  323. \return -1 out of memory
  324. */
  325. int read_next_line_pg(struct Map_info *Map,
  326. struct line_pnts *line_p, struct line_cats *line_c,
  327. int ignore_constraints)
  328. {
  329. int itype;
  330. SF_FeatureType sf_type;
  331. struct Format_info_pg *pg_info;
  332. struct bound_box mbox, lbox;
  333. struct line_pnts *iline;
  334. pg_info = &(Map->fInfo.pg);
  335. if (Map->constraint.region_flag && !ignore_constraints)
  336. Vect_get_constraint_box(Map, &mbox);
  337. while (TRUE) {
  338. /* reset data structures */
  339. if (line_p != NULL)
  340. Vect_reset_line(line_p);
  341. if (line_c != NULL)
  342. Vect_reset_cats(line_c);
  343. /* read feature to cache if necessary */
  344. while (pg_info->cache.lines_next == pg_info->cache.lines_num) {
  345. /* cache feature -> line_p & line_c */
  346. sf_type = get_feature(pg_info, -1, -1);
  347. if (sf_type == SF_NONE) {
  348. G_warning(_("Feature %ld without geometry skipped"), pg_info->cache.fid);
  349. return -1;
  350. }
  351. if ((int)sf_type < 0) /* -1 || - 2 */
  352. return (int)sf_type;
  353. if (sf_type == SF_UNKNOWN || sf_type == SF_NONE) {
  354. G_warning(_("Feature without geometry. Skipped."));
  355. pg_info->cache.lines_next = pg_info->cache.lines_num = 0;
  356. continue;
  357. }
  358. G_debug(4, "%d lines read to cache", pg_info->cache.lines_num);
  359. /* store fid as offset to be used (used for topo access only */
  360. Map->head.last_offset = pg_info->cache.fid;
  361. }
  362. /* get data from cache */
  363. itype = pg_info->cache.lines_types[pg_info->cache.lines_next];
  364. iline = pg_info->cache.lines[pg_info->cache.lines_next];
  365. G_debug(4, "read next cached line %d (type = %d)",
  366. pg_info->cache.lines_next, itype);
  367. /* apply constraints */
  368. if (Map->constraint.type_flag && !ignore_constraints) {
  369. /* skip feature by type */
  370. if (!(itype & Map->constraint.type))
  371. continue;
  372. }
  373. if (line_p && Map->constraint.region_flag && !ignore_constraints) {
  374. /* skip feature by region */
  375. Vect_line_box(iline, &lbox);
  376. if (!Vect_box_overlap(&lbox, &mbox))
  377. continue;
  378. }
  379. /* skip feature by field ignored */
  380. if (line_p)
  381. Vect_append_points(line_p, iline, GV_FORWARD);
  382. if (line_c) {
  383. int cat;
  384. if (!pg_info->toposchema_name) /* simple features access */
  385. cat = (int)pg_info->cache.fid;
  386. else /* PostGIS Topology (cats are cached) */
  387. cat = pg_info->cache.lines_cats[pg_info->cache.lines_next];
  388. if (cat != -1)
  389. Vect_cat_set(line_c, 1, cat);
  390. }
  391. pg_info->cache.lines_next++;
  392. return itype;
  393. }
  394. return -1; /* not reached */
  395. }
  396. /*!
  397. \brief Read feature geometry
  398. Geometry is stored in lines cache.
  399. \param[in,out] pg_info pointer to Format_info_pg struct
  400. \param fid feature id to be read (-1 for next)
  401. \param type feature type (GV_POINT, GV_LINE, ...) - use only for topological access
  402. \return simple feature type (SF_POINT, SF_LINESTRING, ...)
  403. \return -1 on error
  404. */
  405. SF_FeatureType get_feature(struct Format_info_pg *pg_info, int fid, int type)
  406. {
  407. int seq_type;
  408. int force_type; /* force type (GV_BOUNDARY or GV_CENTROID) for topo access only */
  409. char *data;
  410. if (!pg_info->geom_column && !pg_info->topogeom_column) {
  411. G_warning(_("No geometry or topo geometry column defined"));
  412. return -1;
  413. }
  414. if (fid < 1) { /* sequantial access */
  415. if (pg_info->cursor_name == NULL &&
  416. Vect__open_cursor_next_line_pg(pg_info, FALSE) != 0)
  417. return -1;
  418. }
  419. else { /* random access */
  420. if (!pg_info->fid_column && !pg_info->toposchema_name) {
  421. G_warning(_("Random access not supported. "
  422. "Primary key not defined."));
  423. return -1;
  424. }
  425. #ifdef USE_CURSOR_RND
  426. if (pg_info->cursor_fid > 0)
  427. pg_info->next_line = fid - pg_info->cursor_fid;
  428. else
  429. pg_info->next_line = 0;
  430. if (pg_info->next_line < 0 || pg_info->next_line > CURSOR_PAGE)
  431. Vect__close_cursor_pg(pg_info);
  432. if (pg_info->cursor_name == NULL &&
  433. Vect__open_cursor_line_pg(pg_info, fid, type) != 0)
  434. return -1;
  435. #else
  436. pg_info->next_line = 0;
  437. if (Vect__select_line_pg(pg_info, fid, type) != 0)
  438. return -1;
  439. #endif
  440. }
  441. /* do we need to fetch more records ? */
  442. if (PQntuples(pg_info->res) == CURSOR_PAGE &&
  443. PQntuples(pg_info->res) == pg_info->next_line) {
  444. char stmt[DB_SQL_MAX];
  445. PQclear(pg_info->res);
  446. sprintf(stmt, "FETCH %d in %s", CURSOR_PAGE, pg_info->cursor_name);
  447. G_debug(3, "SQL: %s", stmt);
  448. pg_info->res = PQexec(pg_info->conn, stmt);
  449. if (!pg_info->res || PQresultStatus(pg_info->res) != PGRES_TUPLES_OK) {
  450. error_tuples(pg_info);
  451. return -1;
  452. }
  453. pg_info->next_line = 0;
  454. }
  455. G_debug(3, "get_feature(): next_line = %d", pg_info->next_line);
  456. /* out of results ? */
  457. if (PQntuples(pg_info->res) == pg_info->next_line) {
  458. if (Vect__close_cursor_pg(pg_info) != 0)
  459. return -1; /* failure */
  460. else
  461. return -2; /* nothing to read */
  462. }
  463. force_type = -1;
  464. if (pg_info->toposchema_name) {
  465. if (fid < 0) {
  466. /* sequatial access */
  467. seq_type = atoi(PQgetvalue(pg_info->res, pg_info->next_line, 2));
  468. if (seq_type == GV_BOUNDARY ||
  469. (seq_type == GV_LINE && pg_info->feature_type == SF_POLYGON))
  470. force_type = GV_BOUNDARY;
  471. else if (seq_type == GV_CENTROID)
  472. force_type = GV_CENTROID;
  473. }
  474. else {
  475. /* random access: check topological elemenent type consistency */
  476. if (type & GV_POINTS) {
  477. if (type == GV_POINT &&
  478. strlen(PQgetvalue(pg_info->res, pg_info->next_line, 1)) != 0)
  479. G_warning(_("Inconsistency in topology: detected centroid (should be point)"));
  480. }
  481. else {
  482. int left_face, right_face;
  483. left_face = atoi(PQgetvalue(pg_info->res, pg_info->next_line, 1));
  484. right_face = atoi(PQgetvalue(pg_info->res, pg_info->next_line, 2));
  485. if (type == GV_LINE &&
  486. (left_face != 0 || right_face != 0))
  487. G_warning(_("Inconsistency in topology: detected boundary (should be line)"));
  488. }
  489. }
  490. }
  491. /* get geometry data */
  492. data = (char *)PQgetvalue(pg_info->res, pg_info->next_line, 0);
  493. /* load feature to the cache */
  494. pg_info->cache.sf_type = Vect__cache_feature_pg(data,
  495. FALSE, force_type,
  496. &(pg_info->cache), NULL);
  497. /* cache also categories (only for PostGIS Topology) */
  498. if (pg_info->toposchema_name) {
  499. if (!PQgetisnull(pg_info->res, pg_info->next_line, 3))
  500. pg_info->cache.lines_cats[pg_info->cache.lines_next] =
  501. atoi(PQgetvalue(pg_info->res, pg_info->next_line, 3));
  502. else
  503. pg_info->cache.lines_cats[pg_info->cache.lines_next] = -1; /* no cat */
  504. }
  505. /* set feature id */
  506. if (fid < 0) {
  507. pg_info->cache.fid =
  508. atoi(PQgetvalue(pg_info->res, pg_info->next_line, 1));
  509. pg_info->next_line++;
  510. }
  511. else {
  512. pg_info->cache.fid = fid;
  513. }
  514. return pg_info->cache.sf_type;
  515. }
  516. /*!
  517. \brief Convert HEX to WKB data
  518. \param hex_data HEX data
  519. \param[out] nbytes number of bytes in output buffer
  520. \return pointer to WKB data buffer
  521. */
  522. unsigned char *hex_to_wkb(const char *hex_data, int *nbytes)
  523. {
  524. unsigned int length;
  525. int i;
  526. length = strlen(hex_data) / 2 + 1;
  527. if (length > wkb_data_length) {
  528. wkb_data_length = length;
  529. wkb_data = G_realloc(wkb_data, wkb_data_length);
  530. }
  531. *nbytes = length - 1;
  532. for (i = 0; i < (*nbytes); i++) {
  533. wkb_data[i] =
  534. (unsigned
  535. char)((hex_data[2 * i] >
  536. 'F' ? hex_data[2 * i] - 0x57 : hex_data[2 * i] >
  537. '9' ? hex_data[2 * i] - 0x37 : hex_data[2 * i] -
  538. 0x30) << 4);
  539. wkb_data[i] |=
  540. (unsigned char)(hex_data[2 * i + 1] >
  541. 'F' ? hex_data[2 * i + 1] -
  542. 0x57 : hex_data[2 * i + 1] >
  543. '9' ? hex_data[2 * i + 1] -
  544. 0x37 : hex_data[2 * i + 1] - 0x30);
  545. }
  546. wkb_data[(*nbytes)] = 0;
  547. return wkb_data;
  548. }
  549. /*!
  550. \brief Read geometry from HEX data
  551. This code is inspired by OGRGeometryFactory::createFromWkb() from
  552. GDAL/OGR library.
  553. \param data HEX data
  554. \param skip_polygon skip polygons (level 1)
  555. \param force_type force GV_BOUNDARY or GV_CENTROID (used for PostGIS topology only)
  556. \param[out] cache lines cache
  557. \param[out] fparts used for building pseudo-topology (or NULL)
  558. \return simple feature type
  559. \return SF_UNKNOWN on error
  560. */
  561. SF_FeatureType Vect__cache_feature_pg(const char *data, int skip_polygon,
  562. int force_type,
  563. struct Format_info_cache *cache,
  564. struct feat_parts * fparts)
  565. {
  566. int ret, byte_order, nbytes, is3D;
  567. unsigned char *wkb_data;
  568. unsigned int wkb_flags;
  569. SF_FeatureType ftype;
  570. /* reset cache */
  571. cache->lines_num = 0;
  572. cache->fid = -1;
  573. /* next to be read from cache */
  574. cache->lines_next = 0;
  575. if (fparts)
  576. fparts->n_parts = 0;
  577. wkb_flags = 0;
  578. wkb_data = hex_to_wkb(data, &nbytes);
  579. if (nbytes < 5) {
  580. /* G_free(wkb_data); */
  581. if (nbytes > 0) {
  582. G_debug(3, "Vect__cache_feature_pg(): invalid geometry");
  583. G_warning(_("Invalid WKB content: %d bytes"), nbytes);
  584. return SF_UNKNOWN;
  585. }
  586. else {
  587. G_debug(3, "Vect__cache_feature_pg(): no geometry");
  588. return SF_NONE;
  589. }
  590. }
  591. /* parsing M coordinate not supported */
  592. memcpy(&wkb_flags, wkb_data + 1, 4);
  593. byte_order = (wkb_data[0] == 0 ? ENDIAN_BIG : ENDIAN_LITTLE);
  594. if (byte_order == ENDIAN_BIG)
  595. wkb_flags = SWAP32(wkb_flags);
  596. if (wkb_flags & 0x40000000) {
  597. G_warning(_("Reading EWKB with 4-dimensional coordinates (XYZM) "
  598. "is not supported"));
  599. /* G_free(wkb_data); */
  600. return SF_UNKNOWN;
  601. }
  602. /* PostGIS EWKB format includes an SRID, but this won't be
  603. understood by OGR, so if the SRID flag is set, we remove the
  604. SRID (bytes at offset 5 to 8).
  605. */
  606. if (nbytes > 9 &&
  607. ((byte_order == ENDIAN_BIG && (wkb_data[1] & 0x20)) ||
  608. (byte_order == ENDIAN_LITTLE && (wkb_data[4] & 0x20)))) {
  609. memmove(wkb_data + 5, wkb_data + 9, nbytes - 9);
  610. nbytes -= 4;
  611. if (byte_order == ENDIAN_BIG)
  612. wkb_data[1] &= (~0x20);
  613. else
  614. wkb_data[4] &= (~0x20);
  615. }
  616. if (nbytes < 9 && nbytes != -1) {
  617. /* G_free(wkb_data); */
  618. return SF_UNKNOWN;
  619. }
  620. /* Get the geometry feature type. For now we assume that geometry
  621. type is between 0 and 255 so we only have to fetch one byte.
  622. */
  623. if (byte_order == ENDIAN_LITTLE) {
  624. ftype = (SF_FeatureType) wkb_data[1];
  625. is3D = wkb_data[4] & 0x80 || wkb_data[2] & 0x80;
  626. }
  627. else {
  628. ftype = (SF_FeatureType) wkb_data[4];
  629. is3D = wkb_data[1] & 0x80 || wkb_data[3] & 0x80;
  630. }
  631. G_debug(3, "Vect__cache_feature_pg(): sf_type = %d", ftype);
  632. /* allocate space in lines cache - be minimalistic
  633. more lines require eg. polygon with more rings, multi-features
  634. or geometry collections
  635. */
  636. if (!cache->lines) {
  637. reallocate_cache(cache, 1);
  638. }
  639. ret = -1;
  640. if (ftype == SF_POINT) {
  641. cache->lines_num = 1;
  642. cache->lines_types[0] = force_type == GV_CENTROID ? force_type : GV_POINT;
  643. ret = point_from_wkb(wkb_data, nbytes, byte_order,
  644. is3D, cache->lines[0]);
  645. add_fpart(fparts, ftype, 0, 1);
  646. }
  647. else if (ftype == SF_LINESTRING) {
  648. cache->lines_num = 1;
  649. cache->lines_types[0] = force_type == GV_BOUNDARY ? force_type : GV_LINE;
  650. ret = linestring_from_wkb(wkb_data, nbytes, byte_order,
  651. is3D, cache->lines[0], FALSE);
  652. add_fpart(fparts, ftype, 0, 1);
  653. }
  654. else if (ftype == SF_POLYGON && !skip_polygon) {
  655. int nrings;
  656. ret = polygon_from_wkb(wkb_data, nbytes, byte_order,
  657. is3D, cache, &nrings);
  658. add_fpart(fparts, ftype, 0, nrings);
  659. }
  660. else if (ftype == SF_MULTIPOINT ||
  661. ftype == SF_MULTILINESTRING ||
  662. ftype == SF_MULTIPOLYGON || ftype == SF_GEOMETRYCOLLECTION) {
  663. ret = geometry_collection_from_wkb(wkb_data, nbytes, byte_order,
  664. is3D, cache, fparts);
  665. }
  666. else {
  667. G_warning(_("Unsupported feature type %d"), ftype);
  668. }
  669. /* read next feature from cache */
  670. cache->lines_next = 0;
  671. /* G_free(wkb_data); */
  672. return ret > 0 ? ftype : SF_UNKNOWN;
  673. }
  674. /*!
  675. \brief Read point for WKB data
  676. See OGRPoint::importFromWkb() from GDAL/OGR library
  677. \param wkb_data WKB data
  678. \param nbytes number of bytes (WKB data buffer)
  679. \param byte_order byte order (ENDIAN_LITTLE, ENDIAN_BIG)
  680. \param with_z WITH_Z for 3D data
  681. \param[out] line_p point geometry (pointer to line_pnts struct)
  682. \return wkb size
  683. \return -1 on error
  684. */
  685. int point_from_wkb(const unsigned char *wkb_data, int nbytes, int byte_order,
  686. int with_z, struct line_pnts *line_p)
  687. {
  688. double x, y, z;
  689. if (nbytes < 21 && nbytes != -1)
  690. return -1;
  691. /* get vertex */
  692. memcpy(&x, wkb_data + 5, 8);
  693. memcpy(&y, wkb_data + 5 + 8, 8);
  694. if (byte_order == ENDIAN_BIG) {
  695. SWAPDOUBLE(&x);
  696. SWAPDOUBLE(&y);
  697. }
  698. if (with_z) {
  699. if (nbytes < 29 && nbytes != -1)
  700. return -1;
  701. memcpy(&z, wkb_data + 5 + 16, 8);
  702. if (byte_order == ENDIAN_BIG) {
  703. SWAPDOUBLE(&z);
  704. }
  705. }
  706. else {
  707. z = 0.0;
  708. }
  709. if (line_p) {
  710. Vect_reset_line(line_p);
  711. Vect_append_point(line_p, x, y, z);
  712. }
  713. return 5 + 8 * (with_z == WITH_Z ? 3 : 2);
  714. }
  715. /*!
  716. \brief Read line for WKB data
  717. See OGRLineString::importFromWkb() from GDAL/OGR library
  718. \param wkb_data WKB data
  719. \param nbytes number of bytes (WKB data buffer)
  720. \param byte_order byte order (ENDIAN_LITTLE, ENDIAN_BIG)
  721. \param with_z WITH_Z for 3D data
  722. \param[out] line_p line geometry (pointer to line_pnts struct)
  723. \return wkb size
  724. \return -1 on error
  725. */
  726. int linestring_from_wkb(const unsigned char *wkb_data, int nbytes,
  727. int byte_order, int with_z, struct line_pnts *line_p,
  728. int is_ring)
  729. {
  730. int npoints, point_size, buff_min_size, offset;
  731. int i;
  732. double x, y, z;
  733. if (is_ring)
  734. offset = 5;
  735. else
  736. offset = 0;
  737. if (is_ring && nbytes < 4 && nbytes != -1)
  738. return error_corrupted_data(NULL);
  739. /* get the vertex count */
  740. memcpy(&npoints, wkb_data + (5 - offset), 4);
  741. if (byte_order == ENDIAN_BIG) {
  742. npoints = SWAP32(npoints);
  743. }
  744. /* check if the wkb stream buffer is big enough to store fetched
  745. number of points. 16 or 24 - size of point structure
  746. */
  747. point_size = with_z ? 24 : 16;
  748. if (npoints < 0 || npoints > INT_MAX / point_size)
  749. return error_corrupted_data(NULL);
  750. buff_min_size = point_size * npoints;
  751. if (nbytes != -1 && buff_min_size > nbytes - (9 - offset))
  752. return error_corrupted_data(_("Length of input WKB is too small"));
  753. if (line_p)
  754. Vect_reset_line(line_p);
  755. /* get the vertex */
  756. for (i = 0; i < npoints; i++) {
  757. memcpy(&x, wkb_data + (9 - offset) + i * point_size, 8);
  758. memcpy(&y, wkb_data + (9 - offset) + 8 + i * point_size, 8);
  759. if (with_z)
  760. memcpy(&z, wkb_data + (9 - offset) + 16 + i * point_size, 8);
  761. else
  762. z = 0.0;
  763. if (byte_order == ENDIAN_BIG) {
  764. SWAPDOUBLE(&x);
  765. SWAPDOUBLE(&y);
  766. if (with_z)
  767. SWAPDOUBLE(&z);
  768. }
  769. if (line_p)
  770. Vect_append_point(line_p, x, y, z);
  771. }
  772. return (9 - offset) + (with_z == WITH_Z ? 3 : 2) * 8 * line_p->n_points;
  773. }
  774. /*!
  775. \brief Read polygon for WKB data
  776. See OGRPolygon::importFromWkb() from GDAL/OGR library
  777. \param wkb_data WKB data
  778. \param nbytes number of bytes (WKB data buffer)
  779. \param byte_order byte order (ENDIAN_LITTLE, ENDIAN_BIG)
  780. \param with_z WITH_Z for 3D data
  781. \param[out] line_p array of rings (pointer to line_pnts struct)
  782. \param[out] nrings number of rings
  783. \return wkb size
  784. \return -1 on error
  785. */
  786. int polygon_from_wkb(const unsigned char *wkb_data, int nbytes,
  787. int byte_order, int with_z,
  788. struct Format_info_cache *cache, int *nrings)
  789. {
  790. int data_offset, i, nsize, isize;
  791. struct line_pnts *line_i;
  792. if (nbytes < 9 && nbytes != -1)
  793. return -1;
  794. /* get the ring count */
  795. memcpy(nrings, wkb_data + 5, 4);
  796. if (byte_order == ENDIAN_BIG) {
  797. *nrings = SWAP32(*nrings);
  798. }
  799. if (*nrings < 0) {
  800. return -1;
  801. }
  802. /* reallocate space for islands if needed */
  803. reallocate_cache(cache, *nrings);
  804. cache->lines_num += *nrings;
  805. /* each ring has a minimum of 4 bytes (point count) */
  806. if (nbytes != -1 && nbytes - 9 < (*nrings) * 4) {
  807. return error_corrupted_data(_("Length of input WKB is too small"));
  808. }
  809. data_offset = 9;
  810. if (nbytes != -1)
  811. nbytes -= data_offset;
  812. /* get the rings */
  813. nsize = 9;
  814. for (i = 0; i < (*nrings); i++) {
  815. if (cache->lines_next >= cache->lines_num)
  816. G_fatal_error(_("Invalid cache index %d (max: %d)"),
  817. cache->lines_next, cache->lines_num);
  818. line_i = cache->lines[cache->lines_next];
  819. cache->lines_types[cache->lines_next++] = GV_BOUNDARY;
  820. linestring_from_wkb(wkb_data + data_offset, nbytes, byte_order,
  821. with_z, line_i, TRUE);
  822. if (nbytes != -1) {
  823. isize = 4 + 8 * (with_z == WITH_Z ? 3 : 2) * line_i->n_points;
  824. nbytes -= isize;
  825. }
  826. nsize += isize;
  827. data_offset += isize;
  828. }
  829. return nsize;
  830. }
  831. /*!
  832. \brief Read geometry collection for WKB data
  833. See OGRGeometryCollection::importFromWkbInternal() from GDAL/OGR library
  834. \param wkb_data WKB data
  835. \param nbytes number of bytes (WKB data buffer)
  836. \param byte_order byte order (ENDIAN_LITTLE, ENDIAN_BIG)
  837. \param with_z WITH_Z for 3D data
  838. \param ipart part to cache (starts at 0)
  839. \param[out] cache lines cache
  840. \param[in,out] fparts feature parts (required for building pseudo-topology)
  841. \return number of parts
  842. \return -1 on error
  843. */
  844. int geometry_collection_from_wkb(const unsigned char *wkb_data, int nbytes,
  845. int byte_order, int with_z,
  846. struct Format_info_cache *cache,
  847. struct feat_parts *fparts)
  848. {
  849. int ipart, nparts, data_offset, nsize;
  850. unsigned char *wkb_subdata;
  851. SF_FeatureType ftype;
  852. if (nbytes < 9 && nbytes != -1)
  853. return error_corrupted_data(NULL);
  854. /* get the geometry count */
  855. memcpy(&nparts, wkb_data + 5, 4);
  856. if (byte_order == ENDIAN_BIG) {
  857. nparts = SWAP32(nparts);
  858. }
  859. if (nparts < 0 || nparts > INT_MAX / 9) {
  860. return error_corrupted_data(NULL);
  861. }
  862. G_debug(5, "\t(geometry collections) parts: %d", nparts);
  863. /* each geometry has a minimum of 9 bytes */
  864. if (nbytes != -1 && nbytes - 9 < nparts * 9) {
  865. return error_corrupted_data(_("Length of input WKB is too small"));
  866. }
  867. data_offset = 9;
  868. if (nbytes != -1)
  869. nbytes -= data_offset;
  870. /* reallocate space for parts if needed */
  871. reallocate_cache(cache, nparts);
  872. /* get parts */
  873. for (ipart = 0; ipart < nparts; ipart++) {
  874. wkb_subdata = (unsigned char *)wkb_data + data_offset;
  875. if (nbytes < 9 && nbytes != -1)
  876. return error_corrupted_data(NULL);
  877. if (byte_order == ENDIAN_LITTLE) {
  878. ftype = (SF_FeatureType) wkb_subdata[1];
  879. }
  880. else {
  881. ftype = (SF_FeatureType) wkb_subdata[4];
  882. }
  883. if (ftype == SF_POINT) {
  884. cache->lines_types[cache->lines_next] = GV_POINT;
  885. nsize = point_from_wkb(wkb_subdata, nbytes, byte_order, with_z,
  886. cache->lines[cache->lines_next]);
  887. cache->lines_num++;
  888. add_fpart(fparts, ftype, cache->lines_next, 1);
  889. cache->lines_next++;
  890. }
  891. else if (ftype == SF_LINESTRING) {
  892. cache->lines_types[cache->lines_next] = GV_LINE;
  893. nsize =
  894. linestring_from_wkb(wkb_subdata, nbytes, byte_order, with_z,
  895. cache->lines[cache->lines_next], FALSE);
  896. cache->lines_num++;
  897. add_fpart(fparts, ftype, cache->lines_next, 1);
  898. cache->lines_next++;
  899. }
  900. else if (ftype == SF_POLYGON) {
  901. int idx, nrings;
  902. idx = cache->lines_next;
  903. nsize = polygon_from_wkb(wkb_subdata, nbytes, byte_order,
  904. with_z, cache, &nrings);
  905. add_fpart(fparts, ftype, idx, nrings);
  906. }
  907. else if (ftype == SF_GEOMETRYCOLLECTION ||
  908. ftype == SF_MULTIPOLYGON ||
  909. ftype == SF_MULTILINESTRING || ftype == SF_MULTIPOLYGON) {
  910. geometry_collection_from_wkb(wkb_subdata, nbytes, byte_order,
  911. with_z, cache, fparts);
  912. }
  913. else {
  914. G_warning(_("Unsupported feature type %d"), ftype);
  915. }
  916. if (nbytes != -1) {
  917. nbytes -= nsize;
  918. }
  919. data_offset += nsize;
  920. }
  921. return nparts;
  922. }
  923. /*!
  924. \brief Report error message
  925. \param msg message (NULL)
  926. \return -1
  927. */
  928. int error_corrupted_data(const char *msg)
  929. {
  930. if (msg)
  931. G_warning(_("Corrupted data. %s."), msg);
  932. else
  933. G_warning(_("Corrupted data"));
  934. return -1;
  935. }
  936. /*!
  937. \brief Create select cursor for sequential access (internal use only)
  938. Allocated cursor name should be freed by G_free().
  939. \param pg_info pointer to Format_info_pg struct
  940. \param fetch_all TRUE to fetch all records
  941. \param[out] cursor name
  942. \return 0 on success
  943. \return -1 on failure
  944. */
  945. int Vect__open_cursor_next_line_pg(struct Format_info_pg *pg_info, int fetch_all)
  946. {
  947. char stmt[DB_SQL_MAX];
  948. if (Vect__execute_pg(pg_info->conn, "BEGIN") == -1)
  949. return -1;
  950. /* set cursor name */
  951. G_asprintf(&(pg_info->cursor_name),
  952. "%s_%s_%p", pg_info->schema_name, pg_info->table_name, pg_info->conn);
  953. if (!pg_info->toposchema_name) {
  954. /* simple feature access (geom, fid) */
  955. /* TODO: start_fid */
  956. sprintf(stmt,
  957. "DECLARE %s CURSOR FOR SELECT %s,%s FROM \"%s\".\"%s\" ORDER BY %s",
  958. pg_info->cursor_name, pg_info->geom_column, pg_info->fid_column, pg_info->schema_name,
  959. pg_info->table_name, pg_info->fid_column);
  960. }
  961. else {
  962. /* topology access (geom,fid,type) */
  963. /* TODO: optimize SQL statement (for points/centroids) */
  964. sprintf(stmt,
  965. "DECLARE %s CURSOR FOR "
  966. "SELECT geom,id,type,fid FROM ("
  967. "SELECT tt.node_id AS id,tt.geom, %d AS type, ft.fid AS fid FROM \"%s\".node AS tt "
  968. "LEFT JOIN \"%s\" AS ft ON (%s).type = 1 AND (%s).id = node_id "
  969. "WHERE containing_face IS NULL AND node_id NOT IN "
  970. "(SELECT node FROM (SELECT start_node AS node FROM \"%s\".edge GROUP BY start_node UNION ALL "
  971. "SELECT end_node AS node FROM \"%s\".edge GROUP BY end_node) AS foo) UNION ALL "
  972. "SELECT tt.node_id AS id,tt.geom, %d AS type, ft.fid AS fid FROM \"%s\".node AS tt "
  973. "LEFT JOIN \"%s\" AS ft ON (%s).type = 3 AND (%s).id = containing_face "
  974. "WHERE containing_face IS NOT NULL AND node_id NOT IN "
  975. "(SELECT node FROM (SELECT start_node AS node FROM \"%s\".edge GROUP BY start_node UNION ALL "
  976. "SELECT end_node AS node FROM \"%s\".edge GROUP BY end_node) AS foo) UNION ALL "
  977. "SELECT tt.edge_id AS id, tt.geom, %d AS type, ft.fid AS fid FROM \"%s\".edge AS tt "
  978. "LEFT JOIN \"%s\" AS ft ON (%s).type = 2 AND (%s).id = edge_id "
  979. "WHERE left_face = 0 AND right_face = 0 UNION ALL "
  980. "SELECT tt.edge_id AS id, tt.geom, %d AS type, ft.fid AS fid FROM \"%s\".edge AS tt "
  981. "LEFT JOIN \"%s\" AS ft ON (%s).type = 2 AND (%s).id = edge_id "
  982. "WHERE left_face != 0 OR right_face != 0 ) AS foo ORDER BY type,id",
  983. pg_info->cursor_name,
  984. GV_POINT, pg_info->toposchema_name, pg_info->table_name, pg_info->topogeom_column, pg_info->topogeom_column,
  985. pg_info->toposchema_name, pg_info->toposchema_name,
  986. GV_CENTROID, pg_info->toposchema_name, pg_info->table_name, pg_info->topogeom_column, pg_info->topogeom_column,
  987. pg_info->toposchema_name, pg_info->toposchema_name,
  988. GV_LINE, pg_info->toposchema_name, pg_info->table_name, pg_info->topogeom_column, pg_info->topogeom_column,
  989. GV_BOUNDARY, pg_info->toposchema_name, pg_info->table_name, pg_info->topogeom_column, pg_info->topogeom_column);
  990. }
  991. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  992. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  993. return -1;
  994. }
  995. if (fetch_all)
  996. sprintf(stmt, "FETCH ALL in %s", pg_info->cursor_name);
  997. else
  998. sprintf(stmt, "FETCH %d in %s", CURSOR_PAGE, pg_info->cursor_name);
  999. G_debug(3, "SQL: %s", stmt);
  1000. pg_info->res = PQexec(pg_info->conn, stmt); /* fetch records from select cursor */
  1001. if (!pg_info->res || PQresultStatus(pg_info->res) != PGRES_TUPLES_OK) {
  1002. error_tuples(pg_info);
  1003. return -1;
  1004. }
  1005. pg_info->next_line = 0;
  1006. return 0;
  1007. }
  1008. /*!
  1009. \brief Open select cursor for random access (internal use only)
  1010. Fetch number of feature (given by CURSOR_PAGE) starting with
  1011. <em>fid</em>.
  1012. Allocated cursor name should be freed by G_free().
  1013. \param pg_info pointer to Format_info_pg struct
  1014. \param fid feature id to get
  1015. \param type feature type
  1016. \return 0 on success
  1017. \return -1 on failure
  1018. */
  1019. int Vect__open_cursor_line_pg(struct Format_info_pg *pg_info, int fid, int type)
  1020. {
  1021. char stmt[DB_SQL_MAX];
  1022. G_debug(3, "Vect__open_cursor_line_pg(): fid range = %d-%d, type = %d",
  1023. fid, fid + CURSOR_PAGE, type);
  1024. if (Vect__execute_pg(pg_info->conn, "BEGIN") == -1)
  1025. return -1;
  1026. pg_info->cursor_fid = fid;
  1027. G_asprintf(&(pg_info->cursor_name),
  1028. "%s_%s_%d_%p", pg_info->schema_name, pg_info->table_name, fid, pg_info->conn);
  1029. if (!pg_info->toposchema_name) {
  1030. /* simple feature access (geom) */
  1031. sprintf(stmt,
  1032. "DECLARE %s CURSOR FOR SELECT %s FROM \"%s\".\"%s\" "
  1033. "WHERE %s BETWEEN %d AND %d ORDER BY %s", pg_info->cursor_name,
  1034. pg_info->geom_column, pg_info->schema_name, pg_info->table_name,
  1035. pg_info->fid_column, fid, fid + CURSOR_PAGE, pg_info->fid_column);
  1036. }
  1037. else {
  1038. /* topological access */
  1039. if (!(type & (GV_POINTS | GV_LINES))) {
  1040. G_warning(_("Unsupported feature type %d"), type);
  1041. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  1042. return -1;
  1043. }
  1044. if (type & GV_POINTS) {
  1045. /* points (geom,containing_face) */
  1046. sprintf(stmt,
  1047. "DECLARE %s CURSOR FOR SELECT geom,containing_face "
  1048. " FROM \"%s\".node WHERE node_id BETWEEN %d AND %d ORDER BY node_id",
  1049. pg_info->cursor_name,
  1050. pg_info->toposchema_name, fid, fid + CURSOR_PAGE);
  1051. }
  1052. else {
  1053. /* edges (geom,left_face,right_face) */
  1054. sprintf(stmt,
  1055. "DECLARE %s CURSOR FOR SELECT geom,left_face,right_face "
  1056. " FROM \"%s\".edge WHERE edge_id BETWEEN %d AND %d ORDER BY edge_id",
  1057. pg_info->cursor_name,
  1058. pg_info->toposchema_name, fid, fid + CURSOR_PAGE);
  1059. }
  1060. }
  1061. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  1062. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  1063. return -1;
  1064. }
  1065. pg_info->next_line = 0;
  1066. sprintf(stmt, "FETCH ALL in %s", pg_info->cursor_name);
  1067. pg_info->res = PQexec(pg_info->conn, stmt);
  1068. if (!pg_info->res || PQresultStatus(pg_info->res) != PGRES_TUPLES_OK) {
  1069. error_tuples(pg_info);
  1070. return -1;
  1071. }
  1072. return 0;
  1073. }
  1074. /*!
  1075. \brief Close select cursor
  1076. \param pg_info pointer to Format_info_pg struct
  1077. \return 0 on success
  1078. \return -1 on failure
  1079. */
  1080. int Vect__close_cursor_pg(struct Format_info_pg *pg_info)
  1081. {
  1082. if (pg_info->res) {
  1083. PQclear(pg_info->res);
  1084. pg_info->res = NULL;
  1085. }
  1086. if (pg_info->cursor_name) {
  1087. char stmt[DB_SQL_MAX];
  1088. sprintf(stmt, "CLOSE %s", pg_info->cursor_name);
  1089. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  1090. G_warning(_("Unable to close cursor %s"), pg_info->cursor_name);
  1091. return -1;
  1092. }
  1093. Vect__execute_pg(pg_info->conn, "COMMIT");
  1094. G_free(pg_info->cursor_name);
  1095. pg_info->cursor_name = NULL;
  1096. }
  1097. return 0;
  1098. }
  1099. /*!
  1100. \brief Select feature (internal use only)
  1101. \param pg_info pointer to Format_info_pg struct
  1102. \param fid feature id to get
  1103. \param type feature type
  1104. \return 0 on success
  1105. \return -1 on failure
  1106. */
  1107. int Vect__select_line_pg(struct Format_info_pg *pg_info, int fid, int type)
  1108. {
  1109. char stmt[DB_SQL_MAX];
  1110. if (!pg_info->toposchema_name) {
  1111. /* simple feature access */
  1112. sprintf(stmt,
  1113. "SELECT %s FROM \"%s\".\"%s\" WHERE %s = %d",
  1114. pg_info->geom_column, pg_info->schema_name, pg_info->table_name,
  1115. pg_info->fid_column, fid);
  1116. }
  1117. else {
  1118. /* topological access */
  1119. if (!(type & (GV_POINTS | GV_LINES))) {
  1120. G_warning(_("Unsupported feature type %d"), type);
  1121. return -1;
  1122. }
  1123. if (type & GV_POINTS) {
  1124. sprintf(stmt,
  1125. "SELECT tt.geom,tt.containing_face,ft.fid FROM \"%s\".node AS tt "
  1126. "LEFT JOIN \"%s\" AS ft ON (%s).type = 1 and (%s).id = edge_id "
  1127. "WHERE node_id = %d",
  1128. pg_info->toposchema_name, pg_info->table_name, pg_info->topogeom_column,
  1129. pg_info->topogeom_column, fid);
  1130. }
  1131. else {
  1132. sprintf(stmt,
  1133. "SELECT tt.geom,tt.left_face,tt.right_face,ft.fid FROM \"%s\".edge AS tt "
  1134. "LEFT JOIN \"%s\" AS ft ON (%s).type = 2 and (%s).id = edge_id "
  1135. "WHERE edge_id = %d",
  1136. pg_info->toposchema_name, pg_info->table_name, pg_info->topogeom_column,
  1137. pg_info->topogeom_column, fid);
  1138. }
  1139. }
  1140. G_debug(3, "SQL: %s", stmt);
  1141. pg_info->next_line = 0;
  1142. pg_info->res = PQexec(pg_info->conn, stmt);
  1143. if (!pg_info->res || PQresultStatus(pg_info->res) != PGRES_TUPLES_OK) {
  1144. error_tuples(pg_info);
  1145. return -1;
  1146. }
  1147. return 0;
  1148. }
  1149. /*!
  1150. \brief Execute SQL statement
  1151. See pg_local_proto.h
  1152. \param conn pointer to PGconn
  1153. \param stmt query
  1154. \return 0 on success
  1155. \return -1 on error
  1156. */
  1157. int Vect__execute_pg(PGconn * conn, const char *stmt)
  1158. {
  1159. PGresult *result;
  1160. result = NULL;
  1161. G_debug(3, "Vect__execute_pg(): %s", stmt);
  1162. result = PQexec(conn, stmt);
  1163. if (!result || PQresultStatus(result) != PGRES_COMMAND_OK) {
  1164. PQclear(result);
  1165. G_warning(_("Execution failed: %s"), PQerrorMessage(conn));
  1166. return -1;
  1167. }
  1168. PQclear(result);
  1169. return 0;
  1170. }
  1171. /*!
  1172. \brief Execute SQL statement and get value.
  1173. \param conn pointer to PGconn
  1174. \param stmt query
  1175. \return value on success
  1176. \return -1 on error
  1177. */
  1178. int Vect__execute_get_value_pg(PGconn *conn, const char *stmt)
  1179. {
  1180. int ret;
  1181. PGresult *result;
  1182. result = NULL;
  1183. G_debug(3, "Vect__execute_get_value_pg(): %s", stmt);
  1184. result = PQexec(conn, stmt);
  1185. if (!result || PQresultStatus(result) != PGRES_TUPLES_OK ||
  1186. PQntuples(result) != 1) {
  1187. PQclear(result);
  1188. G_warning(_("Execution failed: %s"), PQerrorMessage(conn));
  1189. return -1;
  1190. }
  1191. ret = atoi(PQgetvalue(result, 0, 0));
  1192. PQclear(result);
  1193. return ret;
  1194. }
  1195. /*!
  1196. \brief Reallocate lines cache
  1197. */
  1198. void reallocate_cache(struct Format_info_cache *cache, int num)
  1199. {
  1200. int i;
  1201. if (cache->lines_alloc >= num)
  1202. return;
  1203. if (!cache->lines) {
  1204. /* most of features requires only one line cache */
  1205. cache->lines_alloc = 1;
  1206. }
  1207. else {
  1208. cache->lines_alloc += num;
  1209. }
  1210. cache->lines = (struct line_pnts **)G_realloc(cache->lines,
  1211. cache->lines_alloc *
  1212. sizeof(struct line_pnts *));
  1213. cache->lines_types = (int *)G_realloc(cache->lines_types,
  1214. cache->lines_alloc * sizeof(int));
  1215. cache->lines_cats = (int *)G_realloc(cache->lines_cats,
  1216. cache->lines_alloc * sizeof(int));
  1217. if (cache->lines_alloc > 1) {
  1218. for (i = cache->lines_alloc - num; i < cache->lines_alloc; i++) {
  1219. cache->lines[i] = Vect_new_line_struct();
  1220. cache->lines_types[i] = -1;
  1221. cache->lines_cats[i] = -1;
  1222. }
  1223. }
  1224. else {
  1225. cache->lines[0] = Vect_new_line_struct();
  1226. cache->lines_types[0] = -1;
  1227. cache->lines_cats[0] = -1;
  1228. }
  1229. }
  1230. void add_fpart(struct feat_parts *fparts, SF_FeatureType ftype,
  1231. int idx, int nlines)
  1232. {
  1233. if (!fparts)
  1234. return;
  1235. if (fparts->a_parts == 0 || fparts->n_parts >= fparts->a_parts) {
  1236. if (fparts->a_parts == 0)
  1237. fparts->a_parts = 1;
  1238. else
  1239. fparts->a_parts += fparts->n_parts;
  1240. fparts->ftype = (SF_FeatureType *) G_realloc(fparts->ftype,
  1241. fparts->a_parts *
  1242. sizeof(SF_FeatureType));
  1243. fparts->nlines =
  1244. (int *)G_realloc(fparts->nlines, fparts->a_parts * sizeof(int));
  1245. fparts->idx =
  1246. (int *)G_realloc(fparts->idx, fparts->a_parts * sizeof(int));
  1247. }
  1248. fparts->ftype[fparts->n_parts] = ftype;
  1249. fparts->idx[fparts->n_parts] = idx;
  1250. fparts->nlines[fparts->n_parts] = nlines;
  1251. fparts->n_parts++;
  1252. }
  1253. /*
  1254. \brief Get centroid
  1255. \param pg_info pointer to Format_info_pg
  1256. \param centroid centroid id
  1257. \param[out] line_p output geometry
  1258. \return GV_CENTROID on success
  1259. \return -1 on error
  1260. */
  1261. int get_centroid(struct Map_info *Map, int centroid,
  1262. struct line_pnts *line_p)
  1263. {
  1264. int i, found;
  1265. struct bound_box box;
  1266. struct boxlist list;
  1267. struct P_line *Line;
  1268. struct P_topo_c *topo;
  1269. Line = Map->plus.Line[centroid];
  1270. topo = (struct P_topo_c *)Line->topo;
  1271. /* get area bbox */
  1272. Vect_get_area_box(Map, topo->area, &box);
  1273. /* search in spatial index for centroid with area bbox */
  1274. dig_init_boxlist(&list, TRUE);
  1275. Vect_select_lines_by_box(Map, &box, Line->type, &list);
  1276. found = -1;
  1277. for (i = 0; i < list.n_values; i++) {
  1278. if (list.id[i] == centroid) {
  1279. found = i;
  1280. break;
  1281. }
  1282. }
  1283. if (found == -1)
  1284. return -1;
  1285. if (line_p) {
  1286. Vect_reset_line(line_p);
  1287. Vect_append_point(line_p, list.box[found].E, list.box[found].N, 0.0);
  1288. }
  1289. return GV_CENTROID;
  1290. }
  1291. void error_tuples(struct Format_info_pg *pg_info)
  1292. {
  1293. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  1294. G_warning(_("Unable to read features. Reason:\n%s"),
  1295. PQresultErrorMessage(pg_info->res));
  1296. if (pg_info->res) {
  1297. PQclear(pg_info->res);
  1298. pg_info->res = NULL;
  1299. }
  1300. }
  1301. #endif