open_ogr.c 4.8 KB

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