build_ogr.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. G_message(_("Using external data format '%s' (feature type '%s')"),
  68. Vect_get_finfo_format_info(Map),
  69. Vect_get_finfo_geometry_type(Map));
  70. return Vect__build_sfa(Map, build);
  71. #else
  72. G_fatal_error(_("GRASS is not compiled with OGR support"));
  73. return 0;
  74. #endif
  75. }
  76. /*!
  77. \brief Save feature index file for vector map
  78. \param Map pointer to Map_info structure
  79. \param offset pointer to Format_info_offset struct
  80. (see Format_info_ogr and Format_info_pg struct for implementation issues)
  81. \return 1 on success
  82. \return 0 on error
  83. */
  84. int Vect_save_fidx(struct Map_info *Map,
  85. struct Format_info_offset *offset)
  86. {
  87. #ifdef HAVE_OGR
  88. char fname[GPATH_MAX], elem[GPATH_MAX];
  89. char buf[5];
  90. long length;
  91. struct gvfile fp;
  92. struct Port_info port;
  93. if (strcmp(Map->mapset, G_mapset()) != 0 ||
  94. Map->support_updated == FALSE ||
  95. Map->plus.built != GV_BUILD_ALL)
  96. return 1;
  97. length = 9;
  98. sprintf(elem, "%s/%s", GV_DIRECTORY, Map->name);
  99. G_file_name(fname, elem, GV_FIDX_ELEMENT, Map->mapset);
  100. G_debug(4, "Open fidx: %s", fname);
  101. dig_file_init(&fp);
  102. fp.file = fopen(fname, "w");
  103. if (fp.file == NULL) {
  104. G_warning(_("Unable to open fidx file for write <%s>"), fname);
  105. return 0;
  106. }
  107. dig_init_portable(&port, dig__byte_order_out());
  108. dig_set_cur_port(&port);
  109. /* Header */
  110. /* bytes 1 - 5 */
  111. buf[0] = 5;
  112. buf[1] = 0;
  113. buf[2] = 5;
  114. buf[3] = 0;
  115. buf[4] = (char)dig__byte_order_out();
  116. if (0 >= dig__fwrite_port_C(buf, 5, &fp))
  117. return 0;
  118. /* bytes 6 - 9 : header size */
  119. if (0 >= dig__fwrite_port_L(&length, 1, &fp))
  120. return 0;
  121. /* Body */
  122. /* number of records */
  123. if (0 >= dig__fwrite_port_I(&(offset->array_num), 1, &fp))
  124. return 0;
  125. /* offsets */
  126. if (0 >= dig__fwrite_port_I(offset->array,
  127. offset->array_num, &fp))
  128. return 0;
  129. fclose(fp.file);
  130. return 1;
  131. #else
  132. G_fatal_error(_("GRASS is not compiled with OGR support"));
  133. return 0;
  134. #endif
  135. }