read_sfa.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. \param Map pointer to Map_info structure
  18. \param[out] line_p container used to store line points within
  19. (pointer to line_pnts struct)
  20. \param[out] line_c container used to store line categories within
  21. (pointer to line_cats struct)
  22. \param line feature id (starts at 1)
  23. \return feature type
  24. \return -2 no more features
  25. \return -1 on failure
  26. */
  27. int V2_read_line_sfa(struct Map_info *Map, struct line_pnts *line_p,
  28. struct line_cats *line_c, int line)
  29. {
  30. #if defined HAVE_OGR || defined HAVE_POSTGRES
  31. int type;
  32. struct P_line *Line;
  33. G_debug(4, "V2_read_line_sfa() line = %d", line);
  34. Line = Map->plus.Line[line];
  35. if (Line == NULL) {
  36. G_warning(_("Attempt to read dead feature %d"), line);
  37. return -1;
  38. }
  39. if (Line->type == GV_CENTROID) {
  40. /* read centroid for topo */
  41. if (line_p != NULL) {
  42. int i, found;
  43. struct bound_box box;
  44. struct boxlist list;
  45. struct P_topo_c *topo = (struct P_topo_c *)Line->topo;
  46. G_debug(4, "Centroid: area = %d", topo->area);
  47. Vect_reset_line(line_p);
  48. if (topo->area > 0 && topo->area <= Map->plus.n_areas) {
  49. /* get area bbox */
  50. Vect_get_area_box(Map, topo->area, &box);
  51. /* search in spatial index for centroid with area bbox */
  52. dig_init_boxlist(&list, TRUE);
  53. Vect_select_lines_by_box(Map, &box, Line->type, &list);
  54. found = -1;
  55. for (i = 0; i < list.n_values; i++) {
  56. if (list.id[i] == line) {
  57. found = i;
  58. break;
  59. }
  60. }
  61. if (found > -1) {
  62. Vect_append_point(line_p, list.box[found].E, list.box[found].N, 0.0);
  63. }
  64. else {
  65. G_warning(_("Unable to construct centroid for area %d. Skipped."),
  66. topo->area);
  67. }
  68. }
  69. else {
  70. G_warning(_("Centroid %d: invalid area %d"), line, topo->area);
  71. }
  72. }
  73. if (line_c != NULL) {
  74. /* cat = fid and offset = fid for centroid */
  75. Vect_reset_cats(line_c);
  76. Vect_cat_set(line_c, 1, (int) Line->offset);
  77. }
  78. return GV_CENTROID;
  79. }
  80. if (!line_p && !line_c)
  81. return Line->type;
  82. if (Map->format == GV_FORMAT_POSTGIS)
  83. type = V1_read_line_pg(Map, line_p, line_c, Line->offset);
  84. else
  85. type = V1_read_line_ogr(Map, line_p, line_c, Line->offset);
  86. if (type != Line->type)
  87. G_fatal_error(_("Unexpected feature type (%d) - should be (%d)"),
  88. type, Line->type);
  89. return type;
  90. #else
  91. G_fatal_error(_("GRASS is not compiled with OGR/PostgreSQL support"));
  92. return -1;
  93. #endif
  94. }