header_finfo.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*!
  2. \file lib/vector/Vlib/header_finfo.c
  3. \brief Vector library - header manipulation (relevant for external
  4. formats)
  5. Higher level functions for reading/writing/manipulating vectors.
  6. (C) 2001-2010, 2012 by the GRASS Development Team
  7. This program is free software under the GNU General Public License
  8. (>=v2). Read the file COPYING that comes with GRASS for details.
  9. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  10. \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  11. \author Update to GRASS 7 (OGR/PostGIS support) by Martin Landa <landa.martin gmail.com>
  12. */
  13. #include <grass/vector.h>
  14. #include <grass/glocale.h>
  15. /*!
  16. \brief Get datasource name (relevant only for non-native formats)
  17. Returns:
  18. - datasource name for OGR format (GV_FORMAT_OGR and GV_FORMAT_OGR_DIRECT)
  19. - database name for PostGIS format (GV_FORMAT_POSTGIS)
  20. \param Map pointer to Map_info structure
  21. \return string containing OGR/PostGIS datasource name
  22. \return NULL on error (map format is native)
  23. */
  24. const char *Vect_get_finfo_dsn_name(const struct Map_info *Map)
  25. {
  26. if (Map->format == GV_FORMAT_OGR ||
  27. Map->format == GV_FORMAT_OGR_DIRECT) {
  28. #ifndef HAVE_OGR
  29. G_warning(_("GRASS is not compiled with OGR support"));
  30. #endif
  31. return Map->fInfo.ogr.dsn;
  32. }
  33. else if (Map->format == GV_FORMAT_POSTGIS) {
  34. #ifndef HAVE_POSTGRES
  35. G_warning(_("GRASS is not compiled with PostgreSQL support"));
  36. #endif
  37. return Map->fInfo.pg.db_name;
  38. }
  39. G_warning(_("Native vector format detected for <%s>"),
  40. Vect_get_full_name(Map));
  41. return NULL;
  42. }
  43. /*!
  44. \brief Get layer name (relevant only for non-native formats)
  45. Returns:
  46. - layer name for OGR format (GV_FORMAT_OGR and GV_FORMAT_OGR_DIRECT)
  47. - table name for PostGIS format (GV_FORMAT_POSTGIS) including schema (\<schema\>.\<table\>)
  48. Note: allocated string should be freed by G_free()
  49. \param Map pointer to Map_info structure
  50. \return string containing layer name
  51. \return NULL on error (map format is native)
  52. */
  53. char *Vect_get_finfo_layer_name(const struct Map_info *Map)
  54. {
  55. char *name;
  56. name = NULL;
  57. if (Map->format == GV_FORMAT_OGR ||
  58. Map->format == GV_FORMAT_OGR_DIRECT) {
  59. #ifndef HAVE_OGR
  60. G_warning(_("GRASS is not compiled with OGR support"));
  61. #endif
  62. name = G_store(Map->fInfo.ogr.layer_name);
  63. }
  64. else if (Map->format == GV_FORMAT_POSTGIS) {
  65. #ifndef HAVE_POSTGRES
  66. G_warning(_("GRASS is not compiled with PostgreSQL support"));
  67. #endif
  68. G_asprintf(&name, "%s.%s", Map->fInfo.pg.schema_name,
  69. Map->fInfo.pg.table_name);
  70. }
  71. else {
  72. G_warning(_("Native vector format detected for <%s>"),
  73. Vect_get_full_name(Map));
  74. }
  75. return name;
  76. }
  77. /*!
  78. \brief Get format info (relevant only for non-native formats)
  79. \param Map pointer to Map_info structure
  80. \return string containing name of OGR format
  81. \return "PostgreSQL" for PostGIS format (GV_FORMAT_POSTGIS)
  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 header info for non-native formats
  164. Prints a warning for native format.
  165. \param Map pointer to Ma_info structure
  166. \return pointer to Format_info structure
  167. */
  168. const struct Format_info* Vect_get_finfo(const struct Map_info *Map)
  169. {
  170. if (Map->format == GV_FORMAT_NATIVE)
  171. G_warning(_("Native vector format detected for <%s>"),
  172. Vect_get_full_name(Map));
  173. return &(Map->fInfo);
  174. }