build_pg.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*!
  2. \file lib/vector/Vlib/build_pg.c
  3. \brief Vector library - Building topology for PostGIS layers
  4. Higher level functions for reading/writing/manipulating vectors.
  5. \todo Implement build_topo()
  6. Line offset is
  7. - centroids : FID
  8. - other types : index of the first record (which is FID) in offset array.
  9. (C) 2012 by the GRASS Development Team
  10. This program is free software under the GNU General Public License
  11. (>=v2). Read the file COPYING that comes with GRASS for details.
  12. \author Martin Landa <landa.martin gmail.com>
  13. */
  14. #include <grass/vector.h>
  15. #include <grass/glocale.h>
  16. #ifdef HAVE_POSTGRES
  17. #include <libpq-fe.h>
  18. static int build_topo(struct Map_info *, int);
  19. #endif
  20. /*!
  21. \brief Build topology for PostGIS layer
  22. Build levels:
  23. - GV_BUILD_NONE
  24. - GV_BUILD_BASE
  25. - GV_BUILD_ATTACH_ISLES
  26. - GV_BUILD_CENTROIDS
  27. - GV_BUILD_ALL
  28. \param Map pointer to Map_info structure
  29. \param build build level
  30. \return 1 on success
  31. \return 0 on error
  32. */
  33. int Vect_build_pg(struct Map_info *Map, int build)
  34. {
  35. #ifdef HAVE_POSTGRES
  36. struct Plus_head *plus;
  37. struct Format_info_pg *pg_info;
  38. plus = &(Map->plus);
  39. pg_info = &(Map->fInfo.pg);
  40. G_debug(1, "Vect_build_pg(): db='%s' table='%s', build=%d",
  41. pg_info->db_name, pg_info->table_name, build);
  42. if (build == plus->built)
  43. return 1; /* do nothing */
  44. /* TODO move this init to better place (Vect_open_ ?), because in
  45. theory build may be reused on level2 */
  46. if (build >= plus->built && build > GV_BUILD_BASE) {
  47. G_free((void *) pg_info->offset.array);
  48. G_zero(&(pg_info->offset), sizeof(struct Format_info_offset));
  49. }
  50. if (!pg_info->conn) {
  51. G_warning(_("No DB connection"));
  52. return 0;
  53. }
  54. if (!pg_info->fid_column) {
  55. G_warning(_("Feature table <%s> has no primary key defined"),
  56. pg_info->table_name);
  57. G_warning(_("Random read is not supported by OGR for this layer. "
  58. "Unable to build topology."));
  59. return 0;
  60. }
  61. G_message(_("Using external data format '%s' (feature type '%s')"),
  62. Vect_get_finfo_format_info(Map),
  63. Vect_get_finfo_geometry_type(Map));
  64. return Vect__build_sfa(Map, build);
  65. #else
  66. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  67. return 0;
  68. #endif
  69. }
  70. #ifdef HAVE_POSTGRES
  71. int build_topo(struct Map_info *Map, int build)
  72. {
  73. G_fatal_error(_("Not implemented"));
  74. return 1;
  75. }
  76. #endif