build_ogr.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /*!
  22. \brief Build pseudo-topology (simple features) for OGR layer
  23. Build levels:
  24. - GV_BUILD_NONE
  25. - GV_BUILD_BASE
  26. - GV_BUILD_ATTACH_ISLES
  27. - GV_BUILD_CENTROIDS
  28. - GV_BUILD_ALL
  29. \param Map pointer to Map_info structure
  30. \param build build level
  31. \return 1 on success
  32. \return 0 on error
  33. */
  34. int Vect_build_ogr(struct Map_info *Map, int build)
  35. {
  36. #ifdef HAVE_OGR
  37. struct Plus_head *plus;
  38. struct Format_info_ogr *ogr_info;
  39. plus = &(Map->plus);
  40. ogr_info = &(Map->fInfo.ogr);
  41. G_debug(1, "Vect_build_ogr(): dsn='%s' layer='%s', build=%d",
  42. ogr_info->dsn, ogr_info->layer_name, build);
  43. if (build == plus->built)
  44. return 1; /* do nothing */
  45. /* TODO move this init to better place (Vect_open_ ?), because in
  46. theory build may be reused on level2 */
  47. if (build >= plus->built && build > GV_BUILD_BASE) {
  48. G_free((void *) ogr_info->offset.array);
  49. G_zero(&(ogr_info->offset), sizeof(struct Format_info_offset));
  50. }
  51. if (!ogr_info->layer) {
  52. G_warning(_("Empty OGR layer, nothing to build"));
  53. return 0;
  54. }
  55. if (OGR_L_TestCapability(ogr_info->layer, OLCTransactions))
  56. OGR_L_CommitTransaction(ogr_info->layer);
  57. /* test layer capabilities */
  58. if (!OGR_L_TestCapability(ogr_info->layer, OLCRandomRead)) {
  59. if (strcmp(OGR_Dr_GetName(OGR_DS_GetDriver(Map->fInfo.ogr.ds)),
  60. "PostgreSQL") == 0)
  61. G_warning(_("Feature table <%s> has no primary key defined"),
  62. ogr_info->layer_name);
  63. G_warning(_("Random read is not supported by OGR for this layer. "
  64. "Unable to build topology."));
  65. return 0;
  66. }
  67. if (build > GV_BUILD_NONE)
  68. G_message(_("Using external data format '%s' (feature type '%s')"),
  69. Vect_get_finfo_format_info(Map),
  70. Vect_get_finfo_geometry_type(Map));
  71. return Vect__build_sfa(Map, build);
  72. #else
  73. G_fatal_error(_("GRASS is not compiled with OGR support"));
  74. return 0;
  75. #endif
  76. }
  77. /*!
  78. \brief Save feature index file for vector map
  79. \param Map pointer to Map_info structure
  80. \param offset pointer to Format_info_offset struct
  81. (see Format_info_ogr and Format_info_pg struct for implementation issues)
  82. \return 1 on success
  83. \return 0 on error
  84. */
  85. int Vect_save_fidx(struct Map_info *Map,
  86. struct Format_info_offset *offset)
  87. {
  88. #ifdef HAVE_OGR
  89. char fname[GPATH_MAX], elem[GPATH_MAX];
  90. char buf[5];
  91. long length;
  92. struct gvfile fp;
  93. struct Port_info port;
  94. if (strcmp(Map->mapset, G_mapset()) != 0 ||
  95. Map->support_updated == FALSE ||
  96. Map->plus.built != GV_BUILD_ALL)
  97. return 1;
  98. length = 9;
  99. sprintf(elem, "%s/%s", GV_DIRECTORY, Map->name);
  100. G_file_name(fname, elem, GV_FIDX_ELEMENT, Map->mapset);
  101. G_debug(4, "Open fidx: %s", fname);
  102. dig_file_init(&fp);
  103. fp.file = fopen(fname, "w");
  104. if (fp.file == NULL) {
  105. G_warning(_("Unable to open fidx file for write <%s>"), fname);
  106. return 0;
  107. }
  108. dig_init_portable(&port, dig__byte_order_out());
  109. dig_set_cur_port(&port);
  110. /* Header */
  111. /* bytes 1 - 5 */
  112. buf[0] = 5;
  113. buf[1] = 0;
  114. buf[2] = 5;
  115. buf[3] = 0;
  116. buf[4] = (char)dig__byte_order_out();
  117. if (0 >= dig__fwrite_port_C(buf, 5, &fp))
  118. return 0;
  119. /* bytes 6 - 9 : header size */
  120. if (0 >= dig__fwrite_port_L(&length, 1, &fp))
  121. return 0;
  122. /* Body */
  123. /* number of records */
  124. if (0 >= dig__fwrite_port_I(&(offset->array_num), 1, &fp))
  125. return 0;
  126. /* offsets */
  127. if (0 >= dig__fwrite_port_I(offset->array,
  128. offset->array_num, &fp))
  129. return 0;
  130. G_debug(3, "Vect_save_fidx(): offset_num = %d", offset->array_num);
  131. fclose(fp.file);
  132. return 1;
  133. #else
  134. G_fatal_error(_("GRASS is not compiled with OGR support"));
  135. return 0;
  136. #endif
  137. }