read_sfa.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*!
  2. \file lib/vector/Vlib/read_sfa.c
  3. \brief Vector library - reading features - simple feature access
  4. Higher level functions for reading/writing/manipulating vectors.
  5. See read_ogr.c (OGR interface) and read_pg.c (PostGIS interface)
  6. for imlementation issues.
  7. (C) 2011-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 Martin Landa <landa.martin gmail.com>
  11. */
  12. #include <grass/vector.h>
  13. #include <grass/glocale.h>
  14. /*!
  15. \brief Reads feature from OGR/PostGIS layer on topological level.
  16. This function implements random access on level 2.
  17. Note: Topology must be built at level >= GV_BUILD_BASE
  18. \param Map pointer to Map_info structure
  19. \param[out] line_p container used to store line points within
  20. (pointer to line_pnts struct)
  21. \param[out] line_c container used to store line categories within
  22. (pointer to line_cats struct)
  23. \param line feature id (starts at 1)
  24. \return feature type
  25. \return -2 no more features
  26. \return -1 on failure
  27. */
  28. int V2_read_line_sfa(struct Map_info *Map, struct line_pnts *line_p,
  29. struct line_cats *line_c, int line)
  30. {
  31. #if defined HAVE_OGR || defined HAVE_POSTGRES
  32. int type;
  33. struct P_line *Line;
  34. G_debug(4, "V2_read_line_sfa() line = %d", line);
  35. if (line < 1 || line > Map->plus.n_lines) {
  36. G_warning(_("Attempt to access feature with invalid id (%d)"), line);
  37. return -1;
  38. }
  39. Line = Map->plus.Line[line];
  40. if (Line == NULL) {
  41. G_warning(_("Attempt to access dead feature %d"), line);
  42. return -1;
  43. }
  44. if (Line->type == GV_CENTROID) {
  45. /* read centroid for topo */
  46. if (line_p != NULL) {
  47. int i, found;
  48. struct bound_box box;
  49. struct boxlist list;
  50. struct P_topo_c *topo = (struct P_topo_c *)Line->topo;
  51. G_debug(4, "Centroid: area = %d", topo->area);
  52. Vect_reset_line(line_p);
  53. if (topo->area > 0 && topo->area <= Map->plus.n_areas) {
  54. /* get area bbox */
  55. Vect_get_area_box(Map, topo->area, &box);
  56. /* search in spatial index for centroid with area bbox */
  57. dig_init_boxlist(&list, TRUE);
  58. Vect_select_lines_by_box(Map, &box, Line->type, &list);
  59. found = -1;
  60. for (i = 0; i < list.n_values; i++) {
  61. if (list.id[i] == line) {
  62. found = i;
  63. break;
  64. }
  65. }
  66. if (found > -1) {
  67. Vect_append_point(line_p, list.box[found].E, list.box[found].N, 0.0);
  68. }
  69. else {
  70. G_warning(_("Unable to construct centroid for area %d. Skipped."),
  71. topo->area);
  72. }
  73. }
  74. else {
  75. G_warning(_("Centroid %d: invalid area %d"), line, topo->area);
  76. }
  77. }
  78. if (line_c != NULL) {
  79. /* cat = fid and offset = fid for centroid */
  80. Vect_reset_cats(line_c);
  81. Vect_cat_set(line_c, 1, (int) Line->offset);
  82. }
  83. return GV_CENTROID;
  84. }
  85. if (!line_p && !line_c)
  86. return Line->type;
  87. if (Map->format == GV_FORMAT_POSTGIS)
  88. type = V1_read_line_pg(Map, line_p, line_c, Line->offset);
  89. else
  90. type = V1_read_line_ogr(Map, line_p, line_c, Line->offset);
  91. if (type != Line->type) {
  92. G_warning(_("Unexpected feature type (%d) - should be (%d)"),
  93. type, Line->type);
  94. return -1;
  95. }
  96. return type;
  97. #else
  98. G_fatal_error(_("GRASS is not compiled with OGR/PostgreSQL support"));
  99. return -1;
  100. #endif
  101. }