open_ogr.c 4.7 KB

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