read_pg.c 42 KB

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