build_ogr.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*!
  2. \file lib/vector/Vlib/build_ogr.c
  3. \brief Vector library - Building topology for OGR
  4. Higher level functions for reading/writing/manipulating vectors.
  5. Category: FID, not all layer have FID, OGRNullFID is defined
  6. (5/2004) as -1, so FID should be only >= 0
  7. (C) 2001-2010, 2012 by the GRASS Development Team
  8. This program is free software under the GNU General Public License
  9. (>=v2). Read the file COPYING that comes with GRASS for details.
  10. \author Radim Blazek, Piero Cavalieri
  11. \author Various updates for GRASS 7 by Martin Landa <landa.martin gmail.com>
  12. */
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <grass/vector.h>
  17. #include <grass/glocale.h>
  18. #ifdef HAVE_OGR
  19. #include <ogr_api.h>
  20. #endif
  21. #include "local_proto.h"
  22. /*!
  23. \brief Build pseudo-topology (simple features) for OGR layer
  24. Build levels:
  25. - GV_BUILD_NONE
  26. - GV_BUILD_BASE
  27. - GV_BUILD_ATTACH_ISLES
  28. - GV_BUILD_CENTROIDS
  29. - GV_BUILD_ALL
  30. \param Map pointer to Map_info structure
  31. \param build build level
  32. \return 1 on success
  33. \return 0 on error
  34. */
  35. int Vect_build_ogr(struct Map_info *Map, int build)
  36. {
  37. #ifdef HAVE_OGR
  38. struct Plus_head *plus;
  39. struct Format_info_ogr *ogr_info;
  40. plus = &(Map->plus);
  41. ogr_info = &(Map->fInfo.ogr);
  42. G_debug(1, "Vect_build_ogr(): dsn='%s' layer='%s', build=%d",
  43. ogr_info->dsn, ogr_info->layer_name, build);
  44. if (build == plus->built)
  45. return 1; /* do nothing */
  46. /* TODO move this init to better place (Vect_open_ ?), because in
  47. theory build may be reused on level2 */
  48. if (build >= plus->built && build > GV_BUILD_BASE) {
  49. G_free((void *) ogr_info->offset.array);
  50. G_zero(&(ogr_info->offset), sizeof(struct Format_info_offset));
  51. }
  52. if (!ogr_info->layer) {
  53. G_warning(_("Empty OGR layer, nothing to build"));
  54. return 0;
  55. }
  56. if (OGR_L_TestCapability(ogr_info->layer, OLCTransactions))
  57. OGR_L_CommitTransaction(ogr_info->layer);
  58. /* test layer capabilities */
  59. if (!OGR_L_TestCapability(ogr_info->layer, OLCRandomRead)) {
  60. if (strcmp(OGR_Dr_GetName(OGR_DS_GetDriver(Map->fInfo.ogr.ds)),
  61. "PostgreSQL") == 0)
  62. G_warning(_("Feature table <%s> has no primary key defined"),
  63. ogr_info->layer_name);
  64. G_warning(_("Random read is not supported by OGR for this layer. "
  65. "Unable to build topology."));
  66. return 0;
  67. }
  68. if (build > GV_BUILD_NONE)
  69. G_message(_("Using external data format '%s' (feature type '%s')"),
  70. Vect_get_finfo_format_info(Map),
  71. Vect_get_finfo_geometry_type(Map));
  72. return Vect__build_sfa(Map, build);
  73. #else
  74. G_fatal_error(_("GRASS is not compiled with OGR support"));
  75. return 0;
  76. #endif
  77. }
  78. /*!
  79. \brief Save feature index file for vector map
  80. \param Map pointer to Map_info structure
  81. \param offset pointer to Format_info_offset struct
  82. (see Format_info_ogr and Format_info_pg struct for implementation issues)
  83. \return 1 on success
  84. \return 0 on error
  85. */
  86. int Vect_save_fidx(struct Map_info *Map,
  87. struct Format_info_offset *offset)
  88. {
  89. #ifdef HAVE_OGR
  90. char fname[GPATH_MAX], elem[GPATH_MAX];
  91. char buf[5];
  92. long length;
  93. struct gvfile fp;
  94. struct Port_info port;
  95. if (strcmp(Map->mapset, G_mapset()) != 0 ||
  96. Map->support_updated == FALSE ||
  97. Map->plus.built != GV_BUILD_ALL)
  98. return 1;
  99. length = 9;
  100. sprintf(elem, "%s/%s", GV_DIRECTORY, Map->name);
  101. Vect__get_element_path(fname, Map, GV_FIDX_ELEMENT);
  102. G_debug(4, "Open fidx: %s", fname);
  103. dig_file_init(&fp);
  104. fp.file = fopen(fname, "w");
  105. if (fp.file == NULL) {
  106. G_warning(_("Unable to open fidx file for write <%s>"), fname);
  107. return 0;
  108. }
  109. dig_init_portable(&port, dig__byte_order_out());
  110. dig_set_cur_port(&port);
  111. /* Header */
  112. /* bytes 1 - 5 */
  113. buf[0] = 5;
  114. buf[1] = 0;
  115. buf[2] = 5;
  116. buf[3] = 0;
  117. buf[4] = (char)dig__byte_order_out();
  118. if (0 >= dig__fwrite_port_C(buf, 5, &fp))
  119. return 0;
  120. /* bytes 6 - 9 : header size */
  121. if (0 >= dig__fwrite_port_L(&length, 1, &fp))
  122. return 0;
  123. /* Body */
  124. /* number of records */
  125. if (0 >= dig__fwrite_port_I(&(offset->array_num), 1, &fp))
  126. return 0;
  127. /* offsets */
  128. if (0 >= dig__fwrite_port_I(offset->array,
  129. offset->array_num, &fp))
  130. return 0;
  131. G_debug(3, "Vect_save_fidx(): offset_num = %d", offset->array_num);
  132. fclose(fp.file);
  133. return 1;
  134. #else
  135. G_fatal_error(_("GRASS is not compiled with OGR support"));
  136. return 0;
  137. #endif
  138. }