write_pg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*!
  2. \file lib/vector/Vlib/write_pg.c
  3. \brief Vector library - write vector feature (PostGIS format)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. Inspired by OGR PostgreSQL driver.
  6. \todo PostGIS version of V2__add_line_to_topo_nat()
  7. \todo OGR version of V2__delete_area_cats_from_cidx_nat()
  8. \todo function to delete corresponding entry in fidx
  9. \todo OGR version of V2__add_area_cats_to_cidx_nat
  10. \todo OGR version of V2__add_line_to_topo_nat
  11. (C) 2012 by Martin Landa, and the GRASS Development Team
  12. This program is free software under the GNU General Public License
  13. (>=v2). Read the file COPYING that comes with GRASS for details.
  14. \author Martin Landa <landa.martin gmail.com>
  15. */
  16. #include <grass/vector.h>
  17. #include <grass/glocale.h>
  18. #ifdef HAVE_POSTGRES
  19. #include "pg_local_proto.h"
  20. #define WKBSRIDFLAG 0x20000000
  21. static char *binary_to_hex(int, const unsigned char *);
  22. static unsigned char *point_to_wkb(int, const struct line_pnts *,
  23. int, int*);
  24. static unsigned char *linestring_to_wkb(int, const struct line_pnts *,
  25. int, int*);
  26. static unsigned char *polygon_to_wkb(int, const struct line_pnts *,
  27. int, int*);
  28. static int write_feature(const struct Format_info_pg *,
  29. int, const struct line_pnts *, int, int);
  30. #endif
  31. /*!
  32. \brief Writes feature on level 1 (PostGIS interface)
  33. Note:
  34. - centroids are not supported in PostGIS, pseudotopo holds virtual
  35. centroids
  36. - boundaries are not supported in PostGIS, pseudotopo treats polygons
  37. as boundaries
  38. \param Map pointer to Map_info structure
  39. \param type feature type (GV_POINT, GV_LINE, ...)
  40. \param points pointer to line_pnts structure (feature geometry)
  41. \param cats pointer to line_cats structure (feature categories)
  42. \return feature offset into file
  43. \return -1 on error
  44. */
  45. off_t V1_write_line_pg(struct Map_info *Map, int type,
  46. const struct line_pnts *points,
  47. const struct line_cats *cats)
  48. {
  49. #ifdef HAVE_POSTGRES
  50. int cat;
  51. off_t offset;
  52. SF_FeatureType sf_type;
  53. struct field_info *Fi;
  54. struct Format_info_pg *pg_info;
  55. struct Format_info_offset *offset_info;
  56. pg_info = &(Map->fInfo.pg);
  57. offset_info = &(pg_info->offset);
  58. /* create PostGIS layer if doesn't exist ? */
  59. cat = -1; /* no attributes to be written */
  60. if (cats->n_cats > 0 && Vect_get_num_dblinks(Map) > 0) {
  61. /* check for attributes */
  62. Fi = Vect_get_dblink(Map, 0);
  63. if (Fi) {
  64. if (!Vect_cat_get(cats, Fi->number, &cat))
  65. G_warning(_("No category defined for layer %d"), Fi->number);
  66. if (cats->n_cats > 1) {
  67. G_warning(_("Feature has more categories, using "
  68. "category %d (from layer %d)"),
  69. cat, cats->field[0]);
  70. }
  71. }
  72. }
  73. sf_type = pg_info->feature_type;
  74. /* determine matching PostGIS feature geometry type */
  75. if (type & (GV_POINT | GV_KERNEL)) {
  76. if (sf_type != SF_POINT &&
  77. sf_type != SF_POINT25D) {
  78. G_warning(_("Feature is not a point. Skipping."));
  79. return -1;
  80. }
  81. }
  82. else if (type & GV_LINE) {
  83. if (sf_type != SF_LINESTRING &&
  84. sf_type != SF_LINESTRING25D) {
  85. G_warning(_("Feature is not a line. Skipping."));
  86. return -1;
  87. }
  88. }
  89. else if (type & GV_BOUNDARY) {
  90. if (sf_type != SF_POLYGON) {
  91. G_warning(_("Feature is not a polygon. Skipping."));
  92. return -1;
  93. }
  94. }
  95. else if (type & GV_FACE) {
  96. if (sf_type != SF_POLYGON25D) {
  97. G_warning(_("Feature is not a face. Skipping."));
  98. return -1;
  99. }
  100. }
  101. else {
  102. G_warning(_("Unsupported feature type (%d)"), type);
  103. return -1;
  104. }
  105. G_debug(3, "V1_write_line_pg(): type = %d n_points = %d cat = %d",
  106. type, points->n_points, cat);
  107. if (sf_type == SF_POLYGON || sf_type == SF_POLYGON25D) {
  108. int npoints;
  109. npoints = points->n_points - 1;
  110. if (points->x[0] != points->x[npoints] ||
  111. points->y[0] != points->y[npoints] ||
  112. points->z[0] != points->z[npoints]) {
  113. G_warning(_("Boundary is not closed. Skipping."));
  114. return -1;
  115. }
  116. }
  117. if (execute(pg_info->conn, "BEGIN") == -1)
  118. return -1;
  119. /* write feature's geometry and fid */
  120. if (-1 == write_feature(pg_info, type, points,
  121. Vect_is_3d(Map) ? WITH_Z : WITHOUT_Z, cat))
  122. return -1;
  123. /* write attributes */
  124. /* ? */
  125. if (execute(pg_info->conn, "COMMIT") == -1)
  126. return -1;
  127. /* update offset array */
  128. if (offset_info->array_num >= offset_info->array_alloc) {
  129. offset_info->array_alloc += 1000;
  130. offset_info->array = (int *) G_realloc(offset_info->array,
  131. offset_info->array_alloc *
  132. sizeof(int));
  133. }
  134. offset = offset_info->array_num;
  135. offset_info->array[offset_info->array_num++] = (int) cat;
  136. if (sf_type == SF_POLYGON || sf_type == SF_POLYGON25D) {
  137. /* register exterior ring in offset array */
  138. offset_info->array[offset_info->array_num++] = 0;
  139. }
  140. G_debug(3, "V1_write_line_ogr(): -> offset = %lu", (unsigned long) offset);
  141. return offset;
  142. #else
  143. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  144. return -1;
  145. #endif
  146. }
  147. #ifdef HAVE_POSTGRES
  148. /*!
  149. \brief Binary data to HEX
  150. Allocated buffer should be freed by G_free().
  151. \param nbytes number of bytes to allocate
  152. \param wkb_data WKB data
  153. \return allocated buffer with HEX data
  154. */
  155. char *binary_to_hex(int nbytes, const unsigned char *wkb_data)
  156. {
  157. char *hex_data;
  158. int i, nlow, nhigh;
  159. static const char ach_hex[] = "0123456789ABCDEF";
  160. hex_data = (char *) G_malloc(nbytes * 2 + 1);
  161. hex_data[nbytes * 2] = '\0';
  162. for (i = 0; i < nbytes; i++) {
  163. nlow = wkb_data[i] & 0x0f;
  164. nhigh = (wkb_data[i] & 0xf0) >> 4;
  165. hex_data[i * 2] = ach_hex[nhigh];
  166. hex_data[i * 2 + 1] = ach_hex[nlow];
  167. }
  168. return hex_data;
  169. }
  170. /*!
  171. \bried Write point into WKB buffer
  172. See OGRPoint::exportToWkb from GDAL/OGR library
  173. \param byte_order byte order (LITTLE_ENDIAN or BIG_ENDIAN)
  174. \param points feature geometry
  175. \param with_z WITH_Z for 3D data
  176. \param[out] nsize buffer size
  177. \return allocated WKB buffer
  178. \return NULL on error
  179. */
  180. unsigned char *point_to_wkb(int byte_order,
  181. const struct line_pnts *points, int with_z,
  182. int *nsize)
  183. {
  184. unsigned char *wkb_data;
  185. unsigned int sf_type;
  186. if (points->n_points != 1)
  187. return NULL;
  188. /* allocate buffer */
  189. *nsize = with_z ? 29 : 21;
  190. wkb_data = G_malloc(*nsize);
  191. G_zero(wkb_data, *nsize);
  192. G_debug(5, "\t->point size=%d (with_z = %d)", *nsize, with_z);
  193. /* set the byte order */
  194. if (byte_order == LITTLE_ENDIAN)
  195. wkb_data[0] = '\001';
  196. else
  197. wkb_data[0] = '\000';
  198. /* set the geometry feature type */
  199. sf_type = with_z ? SF_POINT25D : SF_POINT;
  200. if (byte_order == LITTLE_ENDIAN)
  201. sf_type = LSBWORD32(sf_type);
  202. else
  203. sf_type = MSBWORD32(sf_type);
  204. memcpy(wkb_data + 1, &sf_type, 4);
  205. /* copy in the raw data */
  206. memcpy(wkb_data + 5, &(points->x[0]), 8);
  207. memcpy(wkb_data + 5 + 8, &(points->y[0]), 8);
  208. if (with_z) {
  209. memcpy(wkb_data + 5 + 16, &(points->z[0]), 8);
  210. }
  211. /* swap if needed */
  212. if (byte_order == ENDIAN_BIG) {
  213. SWAPDOUBLE(wkb_data + 5);
  214. SWAPDOUBLE(wkb_data + 5 + 8);
  215. if (with_z)
  216. SWAPDOUBLE(wkb_data + 5 + 16);
  217. }
  218. return wkb_data;
  219. }
  220. /*!
  221. \bried Write linestring into WKB buffer
  222. See OGRLineString::exportToWkb from GDAL/OGR library
  223. \param byte_order byte order (LITTLE_ENDIAN or BIG_ENDIAN)
  224. \param points feature geometry
  225. \param with_z WITH_Z for 3D data
  226. \param[out] nsize buffer size
  227. \return allocated WKB buffer
  228. \return NULL on error
  229. */
  230. unsigned char *linestring_to_wkb(int byte_order,
  231. const struct line_pnts *points, int with_z,
  232. int *nsize)
  233. {
  234. int i, point_size;
  235. unsigned char *wkb_data;
  236. unsigned int sf_type;
  237. if (points->n_points < 1)
  238. return NULL;
  239. /* allocate buffer */
  240. point_size = 8 * (with_z ? 3 : 2);
  241. *nsize = 5 + 4 + points->n_points * point_size;
  242. wkb_data = G_malloc(*nsize);
  243. G_zero(wkb_data, *nsize);
  244. G_debug(5, "\t->linestring size=%d (with_z = %d)", *nsize, with_z);
  245. /* set the byte order */
  246. if (byte_order == LITTLE_ENDIAN)
  247. wkb_data[0] = '\001';
  248. else
  249. wkb_data[0] = '\000';
  250. /* set the geometry feature type */
  251. sf_type = with_z ? SF_LINESTRING25D : SF_LINESTRING;
  252. if (byte_order == LITTLE_ENDIAN)
  253. sf_type = LSBWORD32(sf_type);
  254. else
  255. sf_type = MSBWORD32(sf_type);
  256. memcpy(wkb_data + 1, &sf_type, 4);
  257. /* copy in the data count */
  258. memcpy(wkb_data + 5, &(points->n_points), 4);
  259. /* copy in the raw data */
  260. for (i = 0; i < points->n_points; i++) {
  261. memcpy(wkb_data + 9 + point_size * i, &(points->x[i]), 8);
  262. memcpy(wkb_data + 9 + 8 + point_size * i, &(points->y[i]), 8);
  263. if (with_z) {
  264. memcpy(wkb_data + 9 + 16 + point_size * i, &(points->z[i]), 8);
  265. }
  266. }
  267. /* swap if needed */
  268. if (byte_order == ENDIAN_BIG) {
  269. int npoints, nitems;
  270. npoints = SWAP32(points->n_points);
  271. memcpy(wkb_data+5, &npoints, 4);
  272. nitems = (with_z ? 3 : 2) * points->n_points;
  273. for(i = 0; i < nitems; i++ )
  274. {
  275. SWAPDOUBLE(wkb_data + 9 + 4 + 8 * i);
  276. }
  277. }
  278. return wkb_data;
  279. }
  280. /*!
  281. \bried Write polygon into WKB buffer
  282. See OGRPolygon::exportToWkb from GDAL/OGR library
  283. \param byte_order byte order (LITTLE_ENDIAN or BIG_ENDIAN)
  284. \param points feature geometry
  285. \param with_z WITH_Z for 3D data
  286. \param[out] nsize buffer size
  287. \return allocated WKB buffer
  288. \return NULL on error
  289. */
  290. unsigned char *polygon_to_wkb(int byte_order,
  291. const struct line_pnts *points, int with_z,
  292. int *nsize)
  293. {
  294. int i, point_size, nrings;
  295. unsigned char *wkb_data;
  296. unsigned int sf_type;
  297. if (points->n_points < 3)
  298. return NULL;
  299. /* allocate buffer */
  300. point_size = 8 * (with_z ? 3 : 2);
  301. /* one ring only */
  302. nrings = 1;
  303. *nsize = 9 + (4 + point_size * points->n_points);
  304. wkb_data = G_malloc(*nsize);
  305. G_zero(wkb_data, *nsize);
  306. G_debug(5, "\t->polygon size=%d (with_z = %d)", *nsize, with_z);
  307. /* set the byte order */
  308. if (byte_order == LITTLE_ENDIAN)
  309. wkb_data[0] = '\001';
  310. else
  311. wkb_data[0] = '\000';
  312. /* set the geometry feature type */
  313. sf_type = with_z ? SF_POLYGON25D : SF_POLYGON;
  314. if (byte_order == LITTLE_ENDIAN)
  315. sf_type = LSBWORD32(sf_type);
  316. else
  317. sf_type = MSBWORD32(sf_type);
  318. memcpy(wkb_data + 1, &sf_type, 4);
  319. /* copy in the raw data */
  320. if (byte_order == ENDIAN_BIG) {
  321. int ncount;
  322. ncount = CPL_SWAP32(nrings);
  323. memcpy(wkb_data + 5, &ncount, 4);
  324. }
  325. else {
  326. memcpy(wkb_data + 5, &nrings, 4);
  327. }
  328. /* serialize ring */
  329. memcpy(wkb_data + 9, &(points->n_points), 4);
  330. for (i = 0; i < points->n_points; i++) {
  331. memcpy(wkb_data + 9 + 4 + point_size * i, &(points->x[i]), 8);
  332. memcpy(wkb_data + 9 + 4 + 8 + point_size * i, &(points->y[i]), 8);
  333. if (with_z) {
  334. memcpy(wkb_data + 9 + 4 + 16 + point_size * i, &(points->z[i]), 8);
  335. }
  336. }
  337. /* swap if needed */
  338. if (byte_order == ENDIAN_BIG) {
  339. int npoints, nitems;
  340. npoints = SWAP32(points->n_points);
  341. memcpy(wkb_data+5, &npoints, 4);
  342. nitems = (with_z ? 3 : 2) * points->n_points;
  343. for(i = 0; i < nitems; i++ )
  344. {
  345. CPL_SWAPDOUBLE(wkb_data + 9 + 4 + 8 * i);
  346. }
  347. }
  348. return wkb_data;
  349. }
  350. /*!
  351. \brief Insert feature into table
  352. \param pg_info pointer to Format_info_pg struct
  353. \param type feature type (GV_POINT, GV_LINE, ...)
  354. \param points pointer to line_pnts struct
  355. \param with_z WITH_Z for 3D data
  356. \param fid feature id
  357. \return -1 on error
  358. \retirn 0 on success
  359. */
  360. int write_feature(const struct Format_info_pg *pg_info,
  361. int type, const struct line_pnts *points, int with_z,
  362. int fid)
  363. {
  364. int byte_order, nbytes, nsize;
  365. unsigned int sf_type;
  366. unsigned char *wkb_data;
  367. char *stmt, *text_data, *text_data_p, *hex_data;
  368. if (with_z && pg_info->coor_dim != 3) {
  369. G_warning(_("Trying to insert 3D data into feature table "
  370. "which store 2D data only"));
  371. return -1;
  372. }
  373. if (!with_z && pg_info->coor_dim != 2) {
  374. G_warning(_("Trying to insert 2D data into feature table "
  375. "which store 3D data only"));
  376. return -1;
  377. }
  378. byte_order = LITTLE_ENDIAN; /* ? */
  379. /* get wkb data */
  380. nbytes = -1;
  381. wkb_data = NULL;
  382. if (type == GV_POINT)
  383. wkb_data = point_to_wkb(byte_order, points, with_z, &nbytes);
  384. else if (type == GV_LINE)
  385. wkb_data = linestring_to_wkb(byte_order, points, with_z, &nbytes);
  386. else if (type == GV_BOUNDARY)
  387. wkb_data = polygon_to_wkb(byte_order, points, with_z, &nbytes);
  388. if (!wkb_data || nbytes < 1) {
  389. G_warning(_("Unsupported feature type %d"), type);
  390. return -1;
  391. }
  392. /* When converting to hex, each byte takes 2 hex characters. In
  393. addition we add in 8 characters to represent the SRID integer
  394. in hex, and one for a null terminator */
  395. nsize = nbytes * 2 + 8 + 1;
  396. text_data = text_data_p = (char *) G_malloc(nsize);
  397. /* convert the 1st byte, which is the endianess flag, to hex */
  398. hex_data = binary_to_hex(1, wkb_data);
  399. strcpy(text_data_p, hex_data);
  400. G_free (hex_data);
  401. text_data_p += 2;
  402. /* get the geom type which is bytes 2 through 5 */
  403. memcpy(&sf_type, wkb_data + 1, 4);
  404. /* add the SRID flag if an SRID is provided */
  405. if (pg_info->srid > 0) {
  406. unsigned int srs_flag;
  407. /* change the flag to little endianess */
  408. srs_flag = LSBWORD32(WKBSRIDFLAG);
  409. /* apply the flag */
  410. sf_type = sf_type | srs_flag;
  411. }
  412. /* write the geom type which is 4 bytes */
  413. hex_data = binary_to_hex(4, (unsigned char*) &sf_type);
  414. strcpy(text_data_p, hex_data);
  415. G_free(hex_data);
  416. text_data_p += 8;
  417. /* include SRID if provided */
  418. if (pg_info->srid > 0) {
  419. unsigned int srs_id;
  420. /* force the srsid to little endianess */
  421. srs_id = LSBWORD32(pg_info->srid);
  422. hex_data = binary_to_hex(sizeof(srs_id), (unsigned char*) &srs_id);
  423. strcpy(text_data_p, hex_data);
  424. G_free(hex_data);
  425. text_data_p += 8;
  426. }
  427. /* copy the rest of the data over - subtract 5 since we already
  428. copied 5 bytes above */
  429. hex_data = binary_to_hex(nbytes - 5, wkb_data + 5);
  430. strcpy(text_data_p, hex_data);
  431. G_free(hex_data);
  432. /* build INSERT statement */
  433. stmt = NULL;
  434. G_asprintf(&stmt, "INSERT INTO %s (%s, %s) VALUES (%d, '%s'::GEOMETRY)",
  435. pg_info->table_name, pg_info->fid_column, pg_info->geom_column,
  436. fid, text_data);
  437. G_debug(2, "SQL: %s", stmt);
  438. if (execute(pg_info->conn, stmt) == -1)
  439. return -1;
  440. G_free(wkb_data);
  441. G_free(text_data);
  442. G_free(stmt);
  443. return 0;
  444. }
  445. #endif