header_finfo.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*!
  2. \file lib/vector/Vlib/header_finfo.c
  3. \brief Vector library - header manipulation (external formats)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2010, 2012 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  9. \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  10. \author Update to GRASS 7 (OGR support) by Martin Landa <landa.martin gmail.com>
  11. */
  12. #include <grass/vector.h>
  13. #include <grass/glocale.h>
  14. /*!
  15. \brief Get datasource name (relevant only for non-native formats)
  16. Returns:
  17. - datasource name for OGR format (GV_FORMAT_OGR and GV_FORMAT_OGR_DIRECT)
  18. - database name for PostGIS format (GV_FORMAT_POSTGIS)
  19. \param Map pointer to Map_info structure
  20. \return string containing OGR/PostGIS datasource name
  21. \return NULL on error (map format is native)
  22. */
  23. const char *Vect_get_finfo_dsn_name(const struct Map_info *Map)
  24. {
  25. if (Map->format == GV_FORMAT_OGR ||
  26. Map->format == GV_FORMAT_OGR_DIRECT) {
  27. #ifndef HAVE_OGR
  28. G_warning(_("GRASS is not compiled with OGR support"));
  29. #endif
  30. return Map->fInfo.ogr.dsn;
  31. }
  32. else if (Map->format == GV_FORMAT_POSTGIS) {
  33. #ifndef HAVE_POSTGRES
  34. G_warning(_("GRASS is not compiled with PostgreSQL support"));
  35. #endif
  36. return Map->fInfo.pg.db_name;
  37. }
  38. G_warning(_("Native vector format detected for <%s>"),
  39. Vect_get_full_name(Map));
  40. return NULL;
  41. }
  42. /*!
  43. \brief Get layer name (relevant only for non-native formats)
  44. Returns:
  45. - layer name for OGR format (GV_FORMAT_OGR and GV_FORMAT_OGR_DIRECT)
  46. - table name for PostGIS format (GV_FORMAT_POSTGIS)
  47. Note: allocated string should be freed by G_free()
  48. \param Map pointer to Map_info structure
  49. \return string containing layer name
  50. \return NULL on error (map format is native)
  51. */
  52. char *Vect_get_finfo_layer_name(const struct Map_info *Map)
  53. {
  54. char *name;
  55. name = NULL;
  56. if (Map->format == GV_FORMAT_OGR ||
  57. Map->format == GV_FORMAT_OGR_DIRECT) {
  58. #ifndef HAVE_OGR
  59. G_warning(_("GRASS is not compiled with OGR support"));
  60. #endif
  61. name = G_store(Map->fInfo.ogr.layer_name);
  62. }
  63. else if (Map->format == GV_FORMAT_POSTGIS) {
  64. #ifndef HAVE_POSTGRES
  65. G_warning(_("GRASS is not compiled with PostgreSQL support"));
  66. #endif
  67. G_asprintf(&name, "%s.%s", Map->fInfo.pg.schema_name,
  68. Map->fInfo.pg.table_name);
  69. }
  70. else {
  71. G_warning(_("Native vector format detected for <%s>"),
  72. Vect_get_full_name(Map));
  73. }
  74. return name;
  75. }
  76. /*!
  77. \brief Get format info (relevant only for non-native formats)
  78. Returns:
  79. - layer name for OGR format (GV_FORMAT_OGR and GV_FORMAT_OGR_DIRECT)
  80. \param Map pointer to Map_info structure
  81. \return string containing name of OGR format (allocated by G_store())
  82. \return NULL on error (or on missing OGR/PostgreSQL support)
  83. */
  84. const char *Vect_get_finfo_format_info(const struct Map_info *Map)
  85. {
  86. if (Map->format == GV_FORMAT_OGR ||
  87. Map->format == GV_FORMAT_OGR_DIRECT) {
  88. #ifndef HAVE_OGR
  89. G_warning(_("GRASS is not compiled with OGR support"));
  90. #else
  91. if (!Map->fInfo.ogr.ds)
  92. return NULL;
  93. return OGR_Dr_GetName(OGR_DS_GetDriver(Map->fInfo.ogr.ds));
  94. #endif
  95. }
  96. else if (Map->format == GV_FORMAT_POSTGIS) {
  97. #ifndef HAVE_OGR
  98. G_warning(_("GRASS is not compiled with PostgreSQL support"));
  99. #else
  100. return "PostgreSQL";
  101. #endif
  102. }
  103. return NULL;
  104. }
  105. /*!
  106. \brief Get geometry type (relevant only for non-native formats)
  107. Note: All inner spaces are removed, function returns feature type in
  108. lowercase.
  109. \param Map pointer to Map_info structure
  110. \return allocated string containing geometry type info
  111. (point, linestring, polygon, ...)
  112. \return NULL on error (map format is native)
  113. */
  114. const char *Vect_get_finfo_geometry_type(const struct Map_info *Map)
  115. {
  116. char *ftype, *ftype_tmp;
  117. ftype_tmp = ftype = NULL;
  118. if (Map->format == GV_FORMAT_OGR ||
  119. Map->format == GV_FORMAT_OGR_DIRECT) {
  120. #ifndef HAVE_OGR
  121. G_warning(_("GRASS is not compiled with OGR support"));
  122. #else
  123. OGRwkbGeometryType Ogr_geom_type;
  124. OGRFeatureDefnH Ogr_feature_defn;
  125. if (!Map->fInfo.ogr.layer)
  126. return NULL;
  127. Ogr_feature_defn = OGR_L_GetLayerDefn(Map->fInfo.ogr.layer);
  128. Ogr_geom_type = wkbFlatten(OGR_FD_GetGeomType(Ogr_feature_defn));
  129. ftype_tmp = G_store(OGRGeometryTypeToName(Ogr_geom_type));
  130. #endif
  131. }
  132. else if (Map->format == GV_FORMAT_POSTGIS) {
  133. #ifndef HAVE_POSTGRES
  134. G_warning(_("GRASS is not compiled with PostgreSQL support"));
  135. #else
  136. char stmt[DB_SQL_MAX];
  137. const struct Format_info_pg *pg_info;
  138. PGresult *res;
  139. pg_info = &(Map->fInfo.pg);
  140. sprintf(stmt, "SELECT type FROM geometry_columns "
  141. "WHERE f_table_schema = '%s' AND f_table_name = '%s'",
  142. pg_info->schema_name, pg_info->table_name);
  143. G_debug(2, "SQL: %s", stmt);
  144. res = PQexec(pg_info->conn, stmt);
  145. if (!res || PQresultStatus(res) != PGRES_TUPLES_OK ||
  146. PQntuples(res) != 1) {
  147. G_warning("%s\n%s", _("Unable to get feature type"),
  148. PQresultErrorMessage(res));
  149. return NULL;
  150. }
  151. ftype_tmp = G_store(PQgetvalue(res, 0, 0));
  152. PQclear(res);
  153. #endif
  154. }
  155. if (!ftype_tmp)
  156. return NULL;
  157. ftype = G_str_replace(ftype_tmp, " ", "");
  158. G_free(ftype_tmp);
  159. G_str_to_lower(ftype);
  160. return ftype;
  161. }
  162. /*!
  163. \brief Get geometry column (relevant only for non-native DB formats)
  164. \param Map pointer to Map_info structure
  165. \return allocated string with geometry column name
  166. \return NULL on error (map format is native)
  167. */
  168. const char *Vect_get_finfo_geometry_column(const struct Map_info *Map)
  169. {
  170. if (Map->format == GV_FORMAT_POSTGIS) {
  171. #ifndef HAVE_POSTGRES
  172. G_warning(_("GRASS is not compiled with PostgreSQL support"));
  173. #else
  174. return Map->fInfo.pg.geom_column;
  175. #endif
  176. }
  177. return NULL;
  178. }