read_pg.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  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 *);
  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);
  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 = (int) Line->offset;
  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_UNKNOWN || 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_UNKNOWN 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_UNKNOWN;
  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_UNKNOWN;
  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_UNKNOWN;
  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_UNKNOWN;
  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. struct line_pnts *line_i;
  852. if (nbytes < 9 && nbytes != -1)
  853. return -1;
  854. /* get the ring count */
  855. memcpy(nrings, wkb_data + 5, 4);
  856. if (byte_order == ENDIAN_BIG) {
  857. *nrings = SWAP32(*nrings);
  858. }
  859. if (*nrings < 0) {
  860. return -1;
  861. }
  862. /* reallocate space for islands if needed */
  863. Vect__reallocate_cache(cache, *nrings, FALSE);
  864. cache->lines_num += *nrings;
  865. /* each ring has a minimum of 4 bytes (point count) */
  866. if (nbytes != -1 && nbytes - 9 < (*nrings) * 4) {
  867. return error_corrupted_data(_("Length of input WKB is too small"));
  868. }
  869. data_offset = 9;
  870. if (nbytes != -1)
  871. nbytes -= data_offset;
  872. /* get the rings */
  873. nsize = 9;
  874. for (i = 0; i < (*nrings); i++) {
  875. if (cache->lines_next >= cache->lines_num)
  876. G_fatal_error(_("Invalid cache index %d (max: %d)"),
  877. cache->lines_next, cache->lines_num);
  878. line_i = cache->lines[cache->lines_next];
  879. cache->lines_types[cache->lines_next++] = GV_BOUNDARY;
  880. linestring_from_wkb(wkb_data + data_offset, nbytes, byte_order,
  881. with_z, line_i, TRUE);
  882. if (nbytes != -1) {
  883. isize = 4 + 8 * (with_z == WITH_Z ? 3 : 2) * line_i->n_points;
  884. nbytes -= isize;
  885. }
  886. nsize += isize;
  887. data_offset += isize;
  888. }
  889. return nsize;
  890. }
  891. /*!
  892. \brief Read geometry collection for WKB data
  893. See OGRGeometryCollection::importFromWkbInternal() from GDAL/OGR library
  894. \param wkb_data WKB data
  895. \param nbytes number of bytes (WKB data buffer)
  896. \param byte_order byte order (ENDIAN_LITTLE, ENDIAN_BIG)
  897. \param with_z WITH_Z for 3D data
  898. \param ipart part to cache (starts at 0)
  899. \param[out] cache lines cache
  900. \param[in,out] fparts feature parts (required for building pseudo-topology)
  901. \return number of parts
  902. \return -1 on error
  903. */
  904. int geometry_collection_from_wkb(const unsigned char *wkb_data, int nbytes,
  905. int byte_order, int with_z,
  906. struct Format_info_cache *cache,
  907. struct feat_parts *fparts)
  908. {
  909. int ipart, nparts, data_offset, nsize;
  910. unsigned char *wkb_subdata;
  911. SF_FeatureType ftype;
  912. if (nbytes < 9 && nbytes != -1)
  913. return error_corrupted_data(NULL);
  914. /* get the geometry count */
  915. memcpy(&nparts, wkb_data + 5, 4);
  916. if (byte_order == ENDIAN_BIG) {
  917. nparts = SWAP32(nparts);
  918. }
  919. if (nparts < 0 || nparts > INT_MAX / 9) {
  920. return error_corrupted_data(NULL);
  921. }
  922. G_debug(5, "\t(geometry collections) parts: %d", nparts);
  923. /* each geometry has a minimum of 9 bytes */
  924. if (nbytes != -1 && nbytes - 9 < nparts * 9) {
  925. return error_corrupted_data(_("Length of input WKB is too small"));
  926. }
  927. data_offset = 9;
  928. if (nbytes != -1)
  929. nbytes -= data_offset;
  930. /* reallocate space for parts if needed */
  931. Vect__reallocate_cache(cache, nparts, FALSE);
  932. /* get parts */
  933. for (ipart = 0; ipart < nparts; ipart++) {
  934. wkb_subdata = (unsigned char *)wkb_data + data_offset;
  935. if (nbytes < 9 && nbytes != -1)
  936. return error_corrupted_data(NULL);
  937. if (byte_order == ENDIAN_LITTLE) {
  938. ftype = (SF_FeatureType) wkb_subdata[1];
  939. }
  940. else {
  941. ftype = (SF_FeatureType) wkb_subdata[4];
  942. }
  943. if (ftype == SF_POINT) {
  944. cache->lines_types[cache->lines_next] = GV_POINT;
  945. nsize = point_from_wkb(wkb_subdata, nbytes, byte_order, with_z,
  946. cache->lines[cache->lines_next]);
  947. cache->lines_num++;
  948. add_fpart(fparts, ftype, cache->lines_next, 1);
  949. cache->lines_next++;
  950. }
  951. else if (ftype == SF_LINESTRING) {
  952. cache->lines_types[cache->lines_next] = GV_LINE;
  953. nsize =
  954. linestring_from_wkb(wkb_subdata, nbytes, byte_order, with_z,
  955. cache->lines[cache->lines_next], FALSE);
  956. cache->lines_num++;
  957. add_fpart(fparts, ftype, cache->lines_next, 1);
  958. cache->lines_next++;
  959. }
  960. else if (ftype == SF_POLYGON) {
  961. int idx, nrings;
  962. idx = cache->lines_next;
  963. nsize = polygon_from_wkb(wkb_subdata, nbytes, byte_order,
  964. with_z, cache, &nrings);
  965. add_fpart(fparts, ftype, idx, nrings);
  966. }
  967. else if (ftype == SF_GEOMETRYCOLLECTION ||
  968. ftype == SF_MULTIPOLYGON ||
  969. ftype == SF_MULTILINESTRING || ftype == SF_MULTIPOLYGON) {
  970. geometry_collection_from_wkb(wkb_subdata, nbytes, byte_order,
  971. with_z, cache, fparts);
  972. }
  973. else {
  974. G_warning(_("Unsupported feature type %d"), ftype);
  975. }
  976. if (nbytes != -1) {
  977. nbytes -= nsize;
  978. }
  979. data_offset += nsize;
  980. }
  981. return nparts;
  982. }
  983. /*!
  984. \brief Report error message
  985. \param msg message (NULL)
  986. \return -1
  987. */
  988. int error_corrupted_data(const char *msg)
  989. {
  990. if (msg)
  991. G_warning(_("Corrupted data. %s."), msg);
  992. else
  993. G_warning(_("Corrupted data"));
  994. return -1;
  995. }
  996. /*!
  997. \brief Create select cursor for sequential access (internal use only)
  998. Allocated cursor name should be freed by G_free().
  999. \param pg_info pointer to Format_info_pg struct
  1000. \param fetch_all TRUE to fetch all records
  1001. \param[out] cursor name
  1002. \return 0 on success
  1003. \return -1 on failure
  1004. */
  1005. int Vect__open_cursor_next_line_pg(struct Format_info_pg *pg_info, int fetch_all, int built_level)
  1006. {
  1007. char stmt[DB_SQL_MAX];
  1008. if (Vect__execute_pg(pg_info->conn, "BEGIN") == -1)
  1009. return -1;
  1010. /* set cursor name */
  1011. G_asprintf(&(pg_info->cursor_name),
  1012. "%s_%s_%p", pg_info->schema_name, pg_info->table_name, pg_info->conn);
  1013. if (!pg_info->toposchema_name) {
  1014. /* simple feature access (geom, fid) */
  1015. /* TODO: start_fid */
  1016. sprintf(stmt,
  1017. "DECLARE %s CURSOR FOR SELECT %s,%s FROM \"%s\".\"%s\" ORDER BY %s",
  1018. pg_info->cursor_name, pg_info->geom_column, pg_info->fid_column, pg_info->schema_name,
  1019. pg_info->table_name, pg_info->fid_column);
  1020. }
  1021. else {
  1022. /* topology access (geom,id,fid,type) */
  1023. /* TODO: optimize SQL statement (for points/centroids) */
  1024. sprintf(stmt,
  1025. "DECLARE %s CURSOR FOR "
  1026. "SELECT geom,id,type,fid FROM ("
  1027. "SELECT tt.node_id AS id,tt.geom, %d AS type, ft.%s AS fid FROM \"%s\".node AS tt "
  1028. "LEFT JOIN \"%s\".\"%s\" AS ft ON (%s).type = 1 AND (%s).id = node_id "
  1029. "WHERE containing_face IS NULL AND node_id NOT IN "
  1030. "(SELECT node FROM (SELECT start_node AS node FROM \"%s\".edge GROUP BY start_node UNION ALL "
  1031. "SELECT end_node AS node FROM \"%s\".edge GROUP BY end_node) AS foo) UNION ALL "
  1032. "SELECT tt.node_id AS id,tt.geom, %d AS type, ft.%s AS fid FROM \"%s\".node AS tt "
  1033. "LEFT JOIN \"%s\".\"%s\" AS ft ON (%s).type = 3 AND (%s).id = %s "
  1034. "WHERE containing_face IS NOT NULL AND node_id NOT IN "
  1035. "(SELECT node FROM (SELECT start_node AS node FROM \"%s\".edge GROUP BY start_node UNION ALL "
  1036. "SELECT end_node AS node FROM \"%s\".edge GROUP BY end_node) AS foo) UNION ALL "
  1037. "SELECT tt.edge_id AS id, tt.geom, %d AS type, ft.%s AS fid FROM \"%s\".edge AS tt "
  1038. "LEFT JOIN \"%s\".\"%s\" AS ft ON (%s).type = 2 AND (%s).id = edge_id "
  1039. "WHERE left_face = 0 AND right_face = 0 UNION ALL "
  1040. "SELECT tt.edge_id AS id, tt.geom, %d AS type, ft.%s AS fid FROM \"%s\".edge AS tt "
  1041. "LEFT JOIN \"%s\".\"%s\" AS ft ON (%s).type = 2 AND (%s).id = edge_id "
  1042. "WHERE left_face != 0 OR right_face != 0 ) AS foo ORDER BY type,id",
  1043. pg_info->cursor_name,
  1044. GV_POINT, pg_info->fid_column, pg_info->toposchema_name, pg_info->schema_name, pg_info->table_name,
  1045. pg_info->topogeom_column, pg_info->topogeom_column, pg_info->toposchema_name, pg_info->toposchema_name,
  1046. GV_CENTROID, pg_info->fid_column, pg_info->toposchema_name, pg_info->schema_name, pg_info->table_name,
  1047. pg_info->topogeom_column, pg_info->topogeom_column,
  1048. built_level >= GV_BUILD_CENTROIDS ? "containing_face" : "node_id",
  1049. pg_info->toposchema_name, pg_info->toposchema_name,
  1050. GV_LINE, pg_info->fid_column, pg_info->toposchema_name, pg_info->schema_name, pg_info->table_name,
  1051. pg_info->topogeom_column, pg_info->topogeom_column,
  1052. GV_BOUNDARY, pg_info->fid_column, pg_info->toposchema_name, pg_info->schema_name, pg_info->table_name,
  1053. pg_info->topogeom_column, pg_info->topogeom_column);
  1054. }
  1055. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  1056. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  1057. return -1;
  1058. }
  1059. if (fetch_all)
  1060. sprintf(stmt, "FETCH ALL in %s", pg_info->cursor_name);
  1061. else
  1062. sprintf(stmt, "FETCH %d in %s", CURSOR_PAGE, pg_info->cursor_name);
  1063. G_debug(3, "SQL: %s", stmt);
  1064. pg_info->res = PQexec(pg_info->conn, stmt); /* fetch records from select cursor */
  1065. if (!pg_info->res || PQresultStatus(pg_info->res) != PGRES_TUPLES_OK) {
  1066. error_tuples(pg_info);
  1067. return -1;
  1068. }
  1069. pg_info->next_line = 0;
  1070. return 0;
  1071. }
  1072. /*!
  1073. \brief Open select cursor for random access (internal use only)
  1074. Fetch number of feature (given by CURSOR_PAGE) starting with
  1075. <em>fid</em>.
  1076. Allocated cursor name should be freed by G_free().
  1077. \param pg_info pointer to Format_info_pg struct
  1078. \param fid feature id to get
  1079. \param type feature type
  1080. \return 0 on success
  1081. \return -1 on failure
  1082. */
  1083. int Vect__open_cursor_line_pg(struct Format_info_pg *pg_info, int fid, int type)
  1084. {
  1085. char stmt[DB_SQL_MAX];
  1086. G_debug(3, "Vect__open_cursor_line_pg(): fid range = %d-%d, type = %d",
  1087. fid, fid + CURSOR_PAGE, type);
  1088. if (Vect__execute_pg(pg_info->conn, "BEGIN") == -1)
  1089. return -1;
  1090. pg_info->cursor_fid = fid;
  1091. G_asprintf(&(pg_info->cursor_name),
  1092. "%s_%s_%d_%p", pg_info->schema_name, pg_info->table_name, fid, pg_info->conn);
  1093. if (!pg_info->toposchema_name) {
  1094. /* simple feature access (geom) */
  1095. sprintf(stmt,
  1096. "DECLARE %s CURSOR FOR SELECT %s FROM \"%s\".\"%s\" "
  1097. "WHERE %s BETWEEN %d AND %d ORDER BY %s", pg_info->cursor_name,
  1098. pg_info->geom_column, pg_info->schema_name, pg_info->table_name,
  1099. pg_info->fid_column, fid, fid + CURSOR_PAGE, pg_info->fid_column);
  1100. }
  1101. else {
  1102. /* topological access */
  1103. if (!(type & (GV_POINTS | GV_LINES))) {
  1104. G_warning(_("Unsupported feature type %d"), type);
  1105. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  1106. return -1;
  1107. }
  1108. if (type & GV_POINTS) {
  1109. /* points (geom,containing_face) */
  1110. sprintf(stmt,
  1111. "DECLARE %s CURSOR FOR SELECT geom,containing_face "
  1112. " FROM \"%s\".node WHERE node_id BETWEEN %d AND %d ORDER BY node_id",
  1113. pg_info->cursor_name,
  1114. pg_info->toposchema_name, fid, fid + CURSOR_PAGE);
  1115. }
  1116. else {
  1117. /* edges (geom,left_face,right_face) */
  1118. sprintf(stmt,
  1119. "DECLARE %s CURSOR FOR SELECT geom,left_face,right_face "
  1120. " FROM \"%s\".edge WHERE edge_id BETWEEN %d AND %d ORDER BY edge_id",
  1121. pg_info->cursor_name,
  1122. pg_info->toposchema_name, fid, fid + CURSOR_PAGE);
  1123. }
  1124. }
  1125. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  1126. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  1127. return -1;
  1128. }
  1129. pg_info->next_line = 0;
  1130. sprintf(stmt, "FETCH ALL in %s", pg_info->cursor_name);
  1131. pg_info->res = PQexec(pg_info->conn, stmt);
  1132. if (!pg_info->res || PQresultStatus(pg_info->res) != PGRES_TUPLES_OK) {
  1133. error_tuples(pg_info);
  1134. return -1;
  1135. }
  1136. return 0;
  1137. }
  1138. /*!
  1139. \brief Close select cursor
  1140. \param pg_info pointer to Format_info_pg struct
  1141. \return 0 on success
  1142. \return -1 on failure
  1143. */
  1144. int Vect__close_cursor_pg(struct Format_info_pg *pg_info)
  1145. {
  1146. if (pg_info->res) {
  1147. PQclear(pg_info->res);
  1148. pg_info->res = NULL;
  1149. }
  1150. if (pg_info->cursor_name) {
  1151. char stmt[DB_SQL_MAX];
  1152. sprintf(stmt, "CLOSE %s", pg_info->cursor_name);
  1153. if (Vect__execute_pg(pg_info->conn, stmt) == -1) {
  1154. G_warning(_("Unable to close cursor %s"), pg_info->cursor_name);
  1155. return -1;
  1156. }
  1157. Vect__execute_pg(pg_info->conn, "COMMIT");
  1158. G_free(pg_info->cursor_name);
  1159. pg_info->cursor_name = NULL;
  1160. }
  1161. return 0;
  1162. }
  1163. /*!
  1164. \brief Select feature (internal use only)
  1165. \param pg_info pointer to Format_info_pg struct
  1166. \param fid feature id to get
  1167. \param type feature type
  1168. \return 0 on success
  1169. \return -1 on failure
  1170. */
  1171. int Vect__select_line_pg(struct Format_info_pg *pg_info, int fid, int type)
  1172. {
  1173. char stmt[DB_SQL_MAX];
  1174. if (!pg_info->toposchema_name) {
  1175. /* simple feature access */
  1176. sprintf(stmt,
  1177. "SELECT %s FROM \"%s\".\"%s\" WHERE %s = %d",
  1178. pg_info->geom_column, pg_info->schema_name, pg_info->table_name,
  1179. pg_info->fid_column, fid);
  1180. }
  1181. else {
  1182. /* topological access */
  1183. if (!(type & (GV_POINTS | GV_LINES))) {
  1184. G_warning(_("Unsupported feature type %d"), type);
  1185. return -1;
  1186. }
  1187. if (type & GV_POINTS) {
  1188. int topotype;
  1189. char *nodeid;
  1190. if (type == GV_POINT) {
  1191. topotype = 1;
  1192. nodeid = pg_info->fid_column;
  1193. }
  1194. else { /* assuming GV_CENTROID */
  1195. topotype = 3;
  1196. nodeid = "containing_face";
  1197. }
  1198. sprintf(stmt,
  1199. "SELECT tt.geom,tt.containing_face,ft.%s FROM \"%s\".node AS tt "
  1200. "LEFT JOIN \"%s\".\"%s\" AS ft ON (%s).type = %d and (%s).id = %s "
  1201. "WHERE node_id = %d",
  1202. pg_info->fid_column, pg_info->toposchema_name,
  1203. pg_info->schema_name, pg_info->table_name, pg_info->topogeom_column,
  1204. topotype, pg_info->topogeom_column, nodeid, fid);
  1205. }
  1206. else {
  1207. sprintf(stmt,
  1208. "SELECT tt.geom,tt.left_face,tt.right_face,ft.%s FROM \"%s\".edge AS tt "
  1209. "LEFT JOIN \"%s\".\"%s\" AS ft ON (%s).type = 2 and (%s).id = edge_id "
  1210. "WHERE edge_id = %d",
  1211. pg_info->fid_column, pg_info->toposchema_name,
  1212. pg_info->schema_name, pg_info->table_name, pg_info->topogeom_column,
  1213. pg_info->topogeom_column, fid);
  1214. }
  1215. }
  1216. G_debug(3, "SQL: %s", stmt);
  1217. pg_info->next_line = 0;
  1218. pg_info->res = PQexec(pg_info->conn, stmt);
  1219. if (!pg_info->res || PQresultStatus(pg_info->res) != PGRES_TUPLES_OK) {
  1220. error_tuples(pg_info);
  1221. return -1;
  1222. }
  1223. return 0;
  1224. }
  1225. /*!
  1226. \brief Execute SQL statement
  1227. See pg_local_proto.h
  1228. \param conn pointer to PGconn
  1229. \param stmt query
  1230. \return 0 on success
  1231. \return -1 on error
  1232. */
  1233. int Vect__execute_pg(PGconn * conn, const char *stmt)
  1234. {
  1235. PGresult *result;
  1236. result = NULL;
  1237. G_debug(3, "Vect__execute_pg(): %s", stmt);
  1238. result = PQexec(conn, stmt);
  1239. if (!result || PQresultStatus(result) != PGRES_COMMAND_OK) {
  1240. PQclear(result);
  1241. G_warning(_("Execution failed: %s\nReason: %s"), stmt,
  1242. PQerrorMessage(conn));
  1243. return -1;
  1244. }
  1245. PQclear(result);
  1246. return 0;
  1247. }
  1248. /*!
  1249. \brief Execute SQL statement and get value.
  1250. \param conn pointer to PGconn
  1251. \param stmt query
  1252. \return value on success
  1253. \return -1 on error
  1254. */
  1255. int Vect__execute_get_value_pg(PGconn *conn, const char *stmt)
  1256. {
  1257. int ret;
  1258. PGresult *result;
  1259. result = NULL;
  1260. G_debug(3, "Vect__execute_get_value_pg(): %s", stmt);
  1261. result = PQexec(conn, stmt);
  1262. if (!result || PQresultStatus(result) != PGRES_TUPLES_OK ||
  1263. PQntuples(result) != 1) {
  1264. PQclear(result);
  1265. G_warning(_("Execution failed: %s\nReason: %s"), stmt,
  1266. PQerrorMessage(conn));
  1267. return -1;
  1268. }
  1269. ret = atoi(PQgetvalue(result, 0, 0));
  1270. PQclear(result);
  1271. return ret;
  1272. }
  1273. /*!
  1274. \brief Reallocate lines cache
  1275. */
  1276. void Vect__reallocate_cache(struct Format_info_cache *cache, int num, int incr)
  1277. {
  1278. int i;
  1279. if (!incr && cache->lines_alloc >= num)
  1280. return;
  1281. if (!incr && !cache->lines) {
  1282. /* most of features requires only one line cache */
  1283. cache->lines_alloc = 1;
  1284. }
  1285. else {
  1286. cache->lines_alloc += num;
  1287. }
  1288. cache->lines = (struct line_pnts **)G_realloc(cache->lines,
  1289. cache->lines_alloc *
  1290. sizeof(struct line_pnts *));
  1291. cache->lines_types = (int *)G_realloc(cache->lines_types,
  1292. cache->lines_alloc * sizeof(int));
  1293. cache->lines_cats = (int *)G_realloc(cache->lines_cats,
  1294. cache->lines_alloc * sizeof(int));
  1295. if (cache->lines_alloc > 1) {
  1296. for (i = cache->lines_alloc - num; i < cache->lines_alloc; i++) {
  1297. cache->lines[i] = Vect_new_line_struct();
  1298. cache->lines_types[i] = -1;
  1299. cache->lines_cats[i] = -1;
  1300. }
  1301. }
  1302. else {
  1303. cache->lines[0] = Vect_new_line_struct();
  1304. cache->lines_types[0] = -1;
  1305. cache->lines_cats[0] = -1;
  1306. }
  1307. }
  1308. void add_fpart(struct feat_parts *fparts, SF_FeatureType ftype,
  1309. int idx, int nlines)
  1310. {
  1311. if (!fparts)
  1312. return;
  1313. if (fparts->a_parts == 0 || fparts->n_parts >= fparts->a_parts) {
  1314. if (fparts->a_parts == 0)
  1315. fparts->a_parts = 1;
  1316. else
  1317. fparts->a_parts += fparts->n_parts;
  1318. fparts->ftype = (SF_FeatureType *) G_realloc(fparts->ftype,
  1319. fparts->a_parts *
  1320. sizeof(SF_FeatureType));
  1321. fparts->nlines =
  1322. (int *)G_realloc(fparts->nlines, fparts->a_parts * sizeof(int));
  1323. fparts->idx =
  1324. (int *)G_realloc(fparts->idx, fparts->a_parts * sizeof(int));
  1325. }
  1326. fparts->ftype[fparts->n_parts] = ftype;
  1327. fparts->idx[fparts->n_parts] = idx;
  1328. fparts->nlines[fparts->n_parts] = nlines;
  1329. fparts->n_parts++;
  1330. }
  1331. /*
  1332. \brief Get centroid
  1333. \param pg_info pointer to Format_info_pg
  1334. \param centroid centroid id
  1335. \param[out] line_p output geometry
  1336. \return GV_CENTROID on success
  1337. \return -1 on error
  1338. */
  1339. int get_centroid(struct Map_info *Map, int centroid,
  1340. struct line_pnts *line_p)
  1341. {
  1342. int i, found;
  1343. struct bound_box box;
  1344. struct boxlist list;
  1345. struct P_line *Line;
  1346. struct P_topo_c *topo;
  1347. Line = Map->plus.Line[centroid];
  1348. topo = (struct P_topo_c *)Line->topo;
  1349. /* get area bbox */
  1350. Vect_get_area_box(Map, topo->area, &box);
  1351. /* search in spatial index for centroid with area bbox */
  1352. dig_init_boxlist(&list, TRUE);
  1353. Vect_select_lines_by_box(Map, &box, Line->type, &list);
  1354. found = -1;
  1355. for (i = 0; i < list.n_values; i++) {
  1356. if (list.id[i] == centroid) {
  1357. found = i;
  1358. break;
  1359. }
  1360. }
  1361. if (found == -1)
  1362. return -1;
  1363. if (line_p) {
  1364. Vect_reset_line(line_p);
  1365. Vect_append_point(line_p, list.box[found].E, list.box[found].N, 0.0);
  1366. }
  1367. return GV_CENTROID;
  1368. }
  1369. void error_tuples(struct Format_info_pg *pg_info)
  1370. {
  1371. Vect__execute_pg(pg_info->conn, "ROLLBACK");
  1372. G_warning(_("Unable to read features. Reason:\n%s"),
  1373. PQresultErrorMessage(pg_info->res));
  1374. if (pg_info->res) {
  1375. PQclear(pg_info->res);
  1376. pg_info->res = NULL;
  1377. }
  1378. }
  1379. #endif