geos.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*!
  2. * \file geos.c
  3. *
  4. * \brief Vector library - GEOS support
  5. *
  6. * Higher level functions for reading/writing/manipulating vectors.
  7. *
  8. * (C) 2009 by the GRASS Development Team
  9. *
  10. * This program is free software under the GNU General Public License
  11. * (>=v2). Read the file COPYING that comes with GRASS for details.
  12. *
  13. * \author Martin Landa <landa.martin gmail.com>
  14. */
  15. #include <grass/config.h>
  16. #include <grass/gis.h>
  17. #include <grass/Vect.h>
  18. #include <grass/glocale.h>
  19. #ifdef HAVE_GEOS
  20. #include <geos_c.h>
  21. static struct line_pnts *Points;
  22. /*!
  23. \brief Read vector feature and stores it as GEOSGeometry instance
  24. Note: Free allocated memory by GEOSGeom_destroy().
  25. \param Map pointer to Map_info structure
  26. \param line feature id
  27. \return pointer to GEOSGeometry instance
  28. \return NULL on error
  29. */
  30. GEOSGeometry *Vect_read_line_geos(const struct Map_info *Map, int line)
  31. {
  32. int type, dim;
  33. if (!Points)
  34. Points = Vect_new_line_struct();
  35. G_debug(3, "Vect_read_line_geos(line=%d)", line);
  36. if (Vect_is_3d(Map))
  37. dim = 3;
  38. else
  39. dim = 2;
  40. type = Vect_read_line(Map, Points, NULL, line);
  41. if (type < 1) /* ignore dead lines */
  42. return NULL;
  43. return Vect_line_to_geos(Map, Points, type);
  44. }
  45. /*!
  46. \brief Read vector area and stores it as GEOSGeometry instance
  47. Note: Free allocated memory by GEOSGeom_destroy().
  48. \param Map pointer to Map_info structure
  49. \param area area id
  50. \return pointer to GEOSGeometry instance
  51. \return NULL on error
  52. */
  53. GEOSGeometry *Vect_read_area_geos(const struct Map_info * Map, int area)
  54. {
  55. int i, dim, nholes, isle;
  56. GEOSGeometry *boundary, **holes;
  57. if (!Points)
  58. Points = Vect_new_line_struct();
  59. G_debug(3, "Vect_read_area_geos(area=%d)", area);
  60. if (Vect_is_3d(Map))
  61. dim = 3;
  62. else
  63. dim = 2;
  64. if (Vect_get_area_points(Map, area, Points) == -1) {
  65. G_fatal_error(_("Vect_read_area_geos(): unable to read area id %d"),
  66. area);
  67. }
  68. boundary = Vect_line_to_geos(Map, Points, GV_BOUNDARY);
  69. if (!boundary) {
  70. G_fatal_error(_("Vect_read_area_geos(): unable to read area id %d"),
  71. area);
  72. }
  73. nholes = Vect_get_area_num_isles(Map, area);
  74. holes = (GEOSGeometry **) G_malloc(nholes * sizeof(GEOSGeometry *));
  75. for (i = 0; i < nholes; i++) {
  76. isle = Vect_get_area_isle(Map, area, i);
  77. if (isle < 1)
  78. continue;
  79. if (Vect_get_isle_points(Map, isle, Points) < 0)
  80. G_fatal_error(_("Vect_read_area_geos(): unable to read isle id %d of area id %d"),
  81. isle, area);
  82. holes[i] = Vect_line_to_geos(Map, Points, GV_BOUNDARY);
  83. }
  84. return GEOSGeom_createPolygon(boundary, holes, nholes);
  85. }
  86. /*!
  87. \brief Create GEOSGeometry of given type from feature points.
  88. Supported types:
  89. - GV_POINT -> POINT
  90. - GV_LINE -> LINESTRING
  91. - GV_BOUNDARY -> LINERING
  92. Note: Free allocated memory by GEOSGeom_destroy().
  93. \param Map pointer to Map_info structure
  94. \param type feature type (see supported types)
  95. \return pointer to GEOSGeometry instance
  96. \return NULL on error
  97. */
  98. GEOSGeometry *Vect_line_to_geos(const struct Map_info * Map,
  99. const struct line_pnts * points, int type)
  100. {
  101. int i, dim;
  102. GEOSGeometry *geom;
  103. GEOSCoordSequence *pseq;
  104. G_debug(3, "Vect_line_to_geos(type=%d)", type);
  105. if (Vect_is_3d(Map))
  106. dim = 3;
  107. else
  108. dim = 2;
  109. if (type == GV_POINT) {
  110. if (points->n_points != 1)
  111. return NULL;
  112. }
  113. else { /* GV_LINES */
  114. if (points->n_points < 2)
  115. return NULL;
  116. }
  117. if (!(type & (GV_POINT | GV_LINES)))
  118. return NULL;
  119. pseq = GEOSCoordSeq_create(points->n_points, dim);
  120. for (i = 0; i < points->n_points; i++) {
  121. GEOSCoordSeq_setX(pseq, i, points->x[i]);
  122. GEOSCoordSeq_setY(pseq, i, points->y[i]);
  123. if (dim == 3)
  124. GEOSCoordSeq_setZ(pseq, i, points->z[i]);
  125. }
  126. if (type == GV_POINT)
  127. geom = GEOSGeom_createPoint(pseq);
  128. else if (type == GV_LINE)
  129. geom = GEOSGeom_createLineString(pseq);
  130. else { /* GV_BOUNDARY */
  131. geom = GEOSGeom_createLineString(pseq);
  132. if (GEOSisRing(geom)) {
  133. /* GEOSGeom_destroy(geom); */
  134. geom = GEOSGeom_createLinearRing(pseq);
  135. }
  136. }
  137. /* GEOSCoordSeq_destroy(pseq); */
  138. return geom;
  139. }
  140. #endif /* HAVE_GEOS */