find_vect.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*!
  2. \file gis/find_vect.c
  3. \brief GIS library - Find a vector map
  4. (C) 2001-2009 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. */
  9. #include <string.h>
  10. #include <grass/config.h>
  11. #include <grass/gis.h>
  12. #include <grass/vect/dig_defines.h>
  13. #include <grass/glocale.h>
  14. #ifdef HAVE_OGR
  15. #include <ogr_api.h>
  16. #endif
  17. static const char *find_ogr(const char *, const char *);
  18. /*!
  19. \brief Finds a vector map
  20. Searches for a vector map from the mapset search list or in a
  21. specified mapset. Returns the mapset name where the vector map was
  22. found.
  23. NOTES:
  24. If the user specifies a fully qualified vector map which exists,
  25. then G_find_vector() modifies <i>name</i> by removing the
  26. "@<i>mapset</i>" part.
  27. Rejects all names that begin with "."
  28. \param name vector map name
  29. \param mapset mapset name or "" for search path
  30. \return pointer to a string with name of mapset where vector map was found
  31. \return NULL if not found
  32. */
  33. const char *G_find_vector(char *name, const char *mapset)
  34. {
  35. return G_find_file(GV_DIRECTORY, name, mapset);
  36. }
  37. /*!
  38. * \brief Find a vector map (look but don't touch)
  39. *
  40. * The same as G_find_vector() but doesn't remove the "@<i>mapset</i>"
  41. * qualification from <i>name</i>, if present.
  42. *
  43. * Returns NULL if the map wasn't found, or the mapset the vector was
  44. * found in if it was.
  45. *
  46. * \param name vector map name
  47. * \param mapset mapset name where to search
  48. *
  49. * \return pointer to buffer containing mapset name
  50. * \return NULL when vector map not found
  51. */
  52. const char *G_find_vector2(const char *name, const char *mapset)
  53. {
  54. const char *ogr_mapset;
  55. /* check OGR mapset first */
  56. ogr_mapset = find_ogr(name, mapset);
  57. if (ogr_mapset)
  58. return ogr_mapset;
  59. return G_find_file2(GV_DIRECTORY, name, mapset);
  60. }
  61. const char *find_ogr(const char *name, const char *mapset)
  62. {
  63. const char *pname, *pmapset;
  64. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  65. if(G_name_is_fully_qualified(name, xname, xmapset)) {
  66. pname = xname;
  67. pmapset = xmapset;
  68. }
  69. else {
  70. pname = name;
  71. pmapset = mapset;
  72. }
  73. if(pmapset && strcmp(pmapset, "OGR") == 0) {
  74. /* unique mapset "OGR", check OGR datasource instead */
  75. #ifdef HAVE_OGR
  76. OGRDataSourceH Ogr_ds;
  77. G_debug(1, "OGR mapset detected");
  78. OGRRegisterAll();
  79. /* datasource handle */
  80. Ogr_ds = OGROpen(pname, FALSE, NULL);
  81. if (Ogr_ds == NULL)
  82. G_fatal_error(_("Unable to open OGR data source '%s'"),
  83. pname);
  84. return "OGR";
  85. #else
  86. G_fatal_error(_("Unique OGR mapset detected, OGR support is missing"));
  87. #endif
  88. }
  89. /* OGR mapset not detected */
  90. return NULL;
  91. }