frmt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: Vector library
  5. *
  6. * AUTHOR(S): Radim Blazek
  7. *
  8. * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
  9. *
  10. * COPYRIGHT: (C) 2001, 2012 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General
  13. * Public License (>=v2). Read the file COPYING that
  14. * comes with GRASS for details.
  15. *
  16. *****************************************************************************/
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <grass/vector.h>
  20. #include <grass/glocale.h>
  21. /*!
  22. \brief Read external vector format file
  23. \param dascii format file (frmt)
  24. \param[out] finfo pointer to Format_info structure
  25. \return format code
  26. \return -1 on error
  27. */
  28. int dig_read_frmt_ascii(FILE * dascii, struct Format_info *finfo)
  29. {
  30. char buff[2001], buf1[2001];
  31. char *ptr;
  32. int frmt = -1;
  33. G_debug(3, "dig_read_frmt_ascii()");
  34. /* read first line which must be FORMAT: */
  35. if (G_getl2(buff, 2000, dascii)) {
  36. G_chop(buff);
  37. if (!(ptr = strchr(buff, ':'))) {
  38. G_warning(_("Vector format not recognized: %s"), buff);
  39. return -1;
  40. }
  41. strcpy(buf1, buff);
  42. buf1[ptr - buff] = '\0';
  43. ptr++; /* Search for the start of text */
  44. while (*ptr == ' ')
  45. ptr++;
  46. if (G_strcasecmp(buf1, "FORMAT") == 0) {
  47. #ifdef HAVE_OGR
  48. if (G_strcasecmp(ptr, "ogr") == 0) {
  49. frmt = GV_FORMAT_OGR;
  50. }
  51. #endif
  52. #ifdef HAVE_POSTGRES
  53. if (G_strcasecmp(ptr, "postgis") == 0) {
  54. frmt = GV_FORMAT_POSTGIS;
  55. }
  56. #endif
  57. }
  58. }
  59. if (frmt == -1) {
  60. G_warning(_("Vector format not recognized: %s"), buff);
  61. return -1;
  62. }
  63. /* init format info values */
  64. #ifdef HAVE_OGR
  65. G_zero(&(finfo->ogr), sizeof(struct Format_info_ogr));
  66. #else
  67. if (frmt == GV_FORMAT_OGR) {
  68. G_warning(_("Vector format '%s' not supported"), ptr);
  69. return -1;
  70. }
  71. #endif
  72. #ifdef HAVE_POSTGRES
  73. G_zero(&(finfo->pg), sizeof(struct Format_info_pg));
  74. #else
  75. if (frmt == GV_FORMAT_POSTGIS) {
  76. G_warning(_("Vector format '%s' not supported"), ptr);
  77. return -1;
  78. }
  79. #endif
  80. while (G_getl2(buff, 2000, dascii)) {
  81. G_chop(buff);
  82. if (!(ptr = strchr(buff, ':'))) {
  83. G_warning(_("Format definition is not correct: %s"), buff);
  84. continue;
  85. }
  86. strcpy(buf1, buff);
  87. buf1[ptr - buff] = '\0';
  88. ptr++; /* Search for the start of text */
  89. while (*ptr == ' ')
  90. ptr++;
  91. #ifdef HAVE_OGR
  92. if (frmt == GV_FORMAT_OGR) {
  93. if (G_strcasecmp(buf1, "DSN") == 0)
  94. finfo->ogr.dsn = G_store(ptr);
  95. if (G_strcasecmp(buf1, "LAYER") == 0)
  96. finfo->ogr.layer_name = G_store(ptr);
  97. if (G_strcasecmp(buf1, "WHERE") == 0)
  98. finfo->ogr.where = G_store(ptr);
  99. }
  100. #endif
  101. #ifdef HAVE_POSTGRES
  102. if (frmt == GV_FORMAT_POSTGIS) {
  103. if (G_strcasecmp(buf1, "CONNINFO") == 0)
  104. finfo->pg.conninfo = G_store(ptr);
  105. if (G_strcasecmp(buf1, "SCHEMA") == 0)
  106. finfo->pg.schema_name = G_store(ptr);
  107. if (G_strcasecmp(buf1, "TABLE") == 0)
  108. finfo->pg.table_name = G_store(ptr);
  109. if (G_strcasecmp(buf1, "FID") == 0)
  110. finfo->pg.fid_column = G_store(ptr);
  111. if (G_strcasecmp(buf1, "WHERE") == 0)
  112. finfo->pg.where = G_store(ptr);
  113. }
  114. #endif
  115. }
  116. #ifdef HAVE_POSTGRES
  117. /* if schema not defined, use 'public' */
  118. if (frmt == GV_FORMAT_POSTGIS &&
  119. !finfo->pg.schema_name) {
  120. finfo->pg.schema_name = G_store("public");
  121. }
  122. /* if fid column not defined, use default value */
  123. if (frmt == GV_FORMAT_POSTGIS &&
  124. !finfo->pg.fid_column) {
  125. finfo->pg.fid_column = G_store(GV_PG_FID_COLUMN);
  126. }
  127. #endif
  128. return frmt;
  129. }
  130. /* Write vector format, currently does not work
  131. * Parse also connection string.
  132. *
  133. * Returns: 0 OK
  134. * -1 on error
  135. */
  136. int dig_write_frmt_ascii(FILE * dascii, struct Format_info *finfo, int format)
  137. {
  138. G_debug(3, "dig_write_frmt_ascii()");
  139. G_fatal_error("Format not supported by dig_write_frmt_ascii()");
  140. return 0;
  141. }