geos_to_wktb.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*!
  2. \file lib/vector/Vlib/geos_to_wktb.c
  3. \brief Vector library - GEOS powered WKT and WKB export
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2015 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Soeren Gebbert <soerengebbert googlemail.com>
  9. */
  10. #include <stdlib.h>
  11. #include <grass/vector.h>
  12. #include <grass/glocale.h>
  13. #ifdef HAVE_GEOS
  14. /*!
  15. \brief Read vector area and stores it as WKB unsigned char array
  16. \param Map pointer to Map_info structure
  17. \param area area idmetry instance
  18. \param size The size of the returned byte array
  19. \return NULL on error
  20. \return pointer to char array
  21. \return NULL on error
  22. */
  23. unsigned char *Vect_read_area_to_wkb(struct Map_info * Map, int area, size_t *size)
  24. {
  25. static int init = 0;
  26. /* The writer is static for performance reasons */
  27. static GEOSWKBWriter *writer = NULL;
  28. unsigned char *wkb = NULL;
  29. if(init == 0) {
  30. initGEOS(NULL, NULL);
  31. writer = GEOSWKBWriter_create();
  32. init += 1;
  33. }
  34. GEOSWKBWriter_setOutputDimension(writer, 2);
  35. GEOSGeometry *geom = Vect_read_area_geos(Map, area);
  36. if(!geom) {
  37. return(NULL);
  38. }
  39. wkb = GEOSWKBWriter_write(writer, geom, size);
  40. GEOSGeom_destroy(geom);
  41. return(wkb);
  42. }
  43. /*!
  44. \brief Read vector area and stores it as WKT char array
  45. \param Map pointer to Map_info structure
  46. \param area area idmetry instance
  47. \return NULL on error
  48. \return pointer to char array
  49. \return NULL on error
  50. */
  51. char *Vect_read_area_to_wkt(struct Map_info * Map, int area)
  52. {
  53. static int init = 0;
  54. /* The writer is static for performance reasons */
  55. static GEOSWKTWriter *writer = NULL;
  56. char *wkt = NULL;
  57. if(init == 0) {
  58. initGEOS(NULL, NULL);
  59. writer = GEOSWKTWriter_create();
  60. init += 1;
  61. }
  62. GEOSWKTWriter_setOutputDimension(writer, 2);
  63. GEOSGeometry *geom = Vect_read_area_geos(Map, area);
  64. if(!geom) {
  65. return(NULL);
  66. }
  67. wkt = GEOSWKTWriter_write(writer, geom);
  68. GEOSGeom_destroy(geom);
  69. return(wkt);
  70. }
  71. /*!
  72. \brief Create a WKB representation of given type from feature points.
  73. This function is not thread safe, it uses static variables for speedup.
  74. Supported types:
  75. - GV_POINT -> POINT
  76. - GV_CENTROID -> POINT
  77. - GV_LINE -> LINESTRING
  78. - GV_BOUNDARY -> LINEARRING
  79. \param points pointer to line_pnts structure
  80. \param type feature type (see supported types)
  81. \param with_z Set to 1 if the feature is 3d, 0 otherwise
  82. \param size The size of the returned byte array
  83. \return pointer to char array
  84. \return NULL on error
  85. */
  86. unsigned char *Vect_line_to_wkb(const struct line_pnts *points,
  87. int type, int with_z, size_t *size)
  88. {
  89. static int init = 0;
  90. /* The writer is static for performance reasons */
  91. static GEOSWKBWriter *writer = NULL;
  92. unsigned char *wkb = NULL;
  93. if(init == 0) {
  94. initGEOS(NULL, NULL);
  95. writer = GEOSWKBWriter_create();
  96. init += 1;
  97. }
  98. GEOSWKBWriter_setOutputDimension(writer, with_z ? 3 : 2);
  99. GEOSGeometry *geom = Vect_line_to_geos(points, type, with_z);
  100. if(!geom) {
  101. return(NULL);
  102. }
  103. wkb = GEOSWKBWriter_write(writer, geom, size);
  104. GEOSGeom_destroy(geom);
  105. return(wkb);
  106. }
  107. /*!
  108. \brief Create a WKT representation of given type from feature points.
  109. This function is not thread safe, it uses static variables for speedup.
  110. Supported types:
  111. - GV_POINT -> POINT
  112. - GV_CENTROID -> POINT
  113. - GV_LINE -> LINESTRING
  114. - GV_BOUNDARY -> LINEARRING
  115. \param points pointer to line_pnts structure
  116. \param type feature type (see supported types)
  117. \param with_z Set to 1 if the feature is 3d, 0 otherwise
  118. \param with_z Set to 1 if the feature is 3d, 0 otherwise
  119. \return pointer to char array
  120. \return NULL on error
  121. */
  122. char *Vect_line_to_wkt(const struct line_pnts *points,
  123. int type, int with_z)
  124. {
  125. static int init = 0;
  126. /* The writer is static for performance reasons */
  127. static GEOSWKTWriter *writer = NULL;
  128. char *wkt = NULL;
  129. if(init == 0) {
  130. initGEOS(NULL, NULL);
  131. writer = GEOSWKTWriter_create();
  132. init += 1;
  133. }
  134. GEOSWKTWriter_setOutputDimension(writer, with_z ? 3 : 2);
  135. GEOSGeometry *geom = Vect_line_to_geos(points, type, with_z);
  136. if(!geom) {
  137. return(NULL);
  138. }
  139. wkt = GEOSWKTWriter_write(writer, geom);
  140. GEOSGeom_destroy(geom);
  141. return(wkt);
  142. }
  143. #endif /* HAVE_GEOS */