read_sfa.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. struct P_line *Line;
  32. G_debug(4, "V2_read_line_sfa() line = %d", line);
  33. Line = Map->plus.Line[line];
  34. if (Line == NULL) {
  35. G_warning(_("Attempt to read dead feature %d"), line);
  36. return -1;
  37. }
  38. if (Line->type == GV_CENTROID) {
  39. /* read centroid for topo */
  40. if (line_p != NULL) {
  41. int i, found;
  42. struct bound_box box;
  43. struct boxlist list;
  44. struct P_topo_c *topo = (struct P_topo_c *)Line->topo;
  45. G_debug(4, "Centroid: area = %d", topo->area);
  46. Vect_reset_line(line_p);
  47. if (topo->area > 0 && topo->area <= Map->plus.n_areas) {
  48. /* get area bbox */
  49. Vect_get_area_box(Map, topo->area, &box);
  50. /* search in spatial index for centroid with area bbox */
  51. dig_init_boxlist(&list, TRUE);
  52. Vect_select_lines_by_box(Map, &box, Line->type, &list);
  53. found = -1;
  54. for (i = 0; i < list.n_values; i++) {
  55. if (list.id[i] == line) {
  56. found = i;
  57. break;
  58. }
  59. }
  60. if (found > -1) {
  61. Vect_append_point(line_p, list.box[found].E, list.box[found].N, 0.0);
  62. }
  63. else {
  64. G_warning(_("Unable to construct centroid for area %d. Skipped."),
  65. topo->area);
  66. }
  67. }
  68. else {
  69. G_warning(_("Centroid %d: invalid area %d"), line, topo->area);
  70. }
  71. }
  72. if (line_c != NULL) {
  73. /* cat = fid and offset = fid for centroid */
  74. Vect_reset_cats(line_c);
  75. Vect_cat_set(line_c, 1, (int) Line->offset);
  76. }
  77. return GV_CENTROID;
  78. }
  79. if (!line_p && !line_c)
  80. return Line->type;
  81. if (Map->format == GV_FORMAT_POSTGIS)
  82. return V1_read_line_pg(Map, line_p, line_c, Line->offset);
  83. return V1_read_line_ogr(Map, line_p, line_c, Line->offset);
  84. #else
  85. G_fatal_error(_("GRASS is not compiled with OGR/PostgreSQL support"));
  86. return -1;
  87. #endif
  88. }