read_pg.c 38 KB

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