open_ogr.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*!
  2. \file vector/Vlib/open_ogr.c
  3. \brief Vector library - Open OGR layer as vector map layer
  4. Higher level functions for reading/writing/manipulating vectors.
  5. \todo Implement V1_open_new_ogr()
  6. (C) 2001-2009 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.0 Martin Landa <landa.martin gmail.com> (2009)
  12. */
  13. #include <grass/config.h>
  14. #include <unistd.h>
  15. #include <string.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <grass/vector.h>
  19. #include <grass/gis.h>
  20. #include <grass/glocale.h>
  21. #ifdef HAVE_OGR
  22. #include <ogr_api.h>
  23. /*!
  24. \brief Open existing OGR layer (level 1 - without feature index file)
  25. Map->name, Map->mapset, Map->fInfo.ogr.dsn and
  26. Map->fInfo.ogr.layer_name must be set before.
  27. \param[in,out] Map pointer to Map_info structure
  28. \param update non-zero for write mode, otherwise read-only
  29. (write mode is currently not supported)
  30. \return 0 success
  31. \return -1 error
  32. */
  33. int V1_open_old_ogr(struct Map_info *Map, int update)
  34. {
  35. int i, layer, nLayers;
  36. OGRDataSourceH Ogr_ds;
  37. OGRLayerH Ogr_layer;
  38. OGRFeatureDefnH Ogr_featuredefn;
  39. Ogr_layer = NULL;
  40. /*
  41. if (update) {
  42. G_warning(_("Write mode is not supported for OGR format"));
  43. return -1;
  44. }
  45. */
  46. if (!Map->fInfo.ogr.dsn) {
  47. G_fatal_error(_("OGR datasource not defined"));
  48. return -1;
  49. }
  50. if (!Map->fInfo.ogr.layer_name) {
  51. G_fatal_error(_("OGR layer not defined"));
  52. return -1;
  53. }
  54. G_debug(2, "V1_open_old_ogr(): dsn = %s layer = %s", Map->fInfo.ogr.dsn,
  55. Map->fInfo.ogr.layer_name);
  56. OGRRegisterAll();
  57. /* open data source handle */
  58. Ogr_ds = OGROpen(Map->fInfo.ogr.dsn, FALSE, NULL);
  59. if (Ogr_ds == NULL)
  60. G_fatal_error(_("Unable to open OGR data source '%s'"),
  61. Map->fInfo.ogr.dsn);
  62. Map->fInfo.ogr.ds = Ogr_ds;
  63. /* get layer number */
  64. layer = -1;
  65. nLayers = OGR_DS_GetLayerCount(Ogr_ds);
  66. G_debug(2, "%d layers found in data source", nLayers);
  67. for (i = 0; i < nLayers; i++) {
  68. Ogr_layer = OGR_DS_GetLayer(Ogr_ds, i);
  69. Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
  70. if (strcmp(OGR_FD_GetName(Ogr_featuredefn), Map->fInfo.ogr.layer_name) == 0) {
  71. layer = i;
  72. break;
  73. }
  74. }
  75. if (layer == -1) {
  76. OGR_DS_Destroy(Ogr_ds);
  77. G_fatal_error(_("OGR layer <%s> not found"),
  78. Map->fInfo.ogr.layer_name);
  79. }
  80. G_debug(2, "OGR layer %d opened", layer);
  81. Map->fInfo.ogr.layer = Ogr_layer;
  82. Map->fInfo.ogr.lines = NULL;
  83. Map->fInfo.ogr.lines_types = NULL;
  84. Map->fInfo.ogr.lines_alloc = 0;
  85. Map->fInfo.ogr.lines_num = 0;
  86. Map->fInfo.ogr.lines_next = 0;
  87. Map->head.with_z = WITHOUT_Z; /* TODO: 3D */
  88. Map->fInfo.ogr.feature_cache = NULL;
  89. Map->fInfo.ogr.feature_cache_id = -1; /* FID >= 0 */
  90. return (0);
  91. }
  92. /*!
  93. \brief Open existing OGR layer (level 2 - feature index)
  94. \param[in,out] Map pointer to Map_info structure
  95. \return 0 success
  96. \return -1 error
  97. */
  98. int V2_open_old_ogr(struct Map_info *Map)
  99. {
  100. char elem[GPATH_MAX];
  101. char buf[5]; /* used for format version */
  102. long length;
  103. struct gvfile fp;
  104. struct Port_info port;
  105. int Version_Major, Version_Minor, Back_Major, Back_Minor, byte_order;
  106. G_debug(3, "V2_open_old_ogr()");
  107. sprintf(elem, "%s/%s", GV_DIRECTORY, Map->name);
  108. dig_file_init(&fp);
  109. fp.file = G_fopen_old(elem, GV_FIDX_ELEMENT, Map->mapset);
  110. if (fp.file == NULL) {
  111. G_warning(_("Unable to open fidx file for vector map <%s@%s>"),
  112. Map->name, Map->mapset);
  113. return -1;
  114. }
  115. /* Header */
  116. if (0 >= dig__fread_port_C(buf, 5, &fp))
  117. return (-1);
  118. Version_Major = buf[0];
  119. Version_Minor = buf[1];
  120. Back_Major = buf[2];
  121. Back_Minor = buf[3];
  122. byte_order = buf[4];
  123. /* check version numbers */
  124. if (Version_Major > 5 || Version_Minor > 0) {
  125. if (Back_Major > 5 || Back_Minor > 0) {
  126. G_fatal_error(_("Feature index format version %d.%d is not supported by this release."
  127. " Try to rebuild topology or upgrade GRASS."),
  128. Version_Major, Version_Minor);
  129. return (-1);
  130. }
  131. G_warning(_("Your GRASS version does not fully support feature index format %d.%d of the vector."
  132. " Consider to rebuild topology or upgrade GRASS."),
  133. Version_Major, Version_Minor);
  134. }
  135. dig_init_portable(&port, byte_order);
  136. dig_set_cur_port(&port);
  137. /* Body */
  138. /* bytes 6 - 9 : header size */
  139. if (0 >= dig__fread_port_L(&length, 1, &fp))
  140. return (-1);
  141. G_debug(3, " header size %ld", length);
  142. G_fseek(fp.file, length, SEEK_SET);
  143. /* number of records */
  144. if (0 >= dig__fread_port_I(&(Map->fInfo.ogr.offset_num), 1, &fp))
  145. return (-1);
  146. /* alloc space */
  147. Map->fInfo.ogr.offset =
  148. (int *)G_malloc(Map->fInfo.ogr.offset_num * sizeof(int));
  149. Map->fInfo.ogr.offset_alloc = Map->fInfo.ogr.offset_num;
  150. /* offsets */
  151. if (0 >= dig__fread_port_I(Map->fInfo.ogr.offset,
  152. Map->fInfo.ogr.offset_num, &fp))
  153. return (-1);
  154. fclose(fp.file);
  155. G_debug(3, "%d records read from fidx", Map->fInfo.ogr.offset_num);
  156. Map->fInfo.ogr.next_line = 1;
  157. return 0;
  158. }
  159. #endif