geos_to_wktb.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 return it as Well Known Binary (WKB)
  16. unsigned char array
  17. \param Map pointer to Map_info structure
  18. \param area area id
  19. \param size The size of the returned unsigned char array
  20. \return pointer to unsigned 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 return it as Well Known Text (WKT)
  45. unsigned char array
  46. \param Map pointer to Map_info structure
  47. \param area area id
  48. \param size The size of the returned unsigned char array
  49. \return pointer to unsigned char array
  50. \return NULL on error
  51. */
  52. char *Vect_read_area_to_wkt(struct Map_info * Map, int area)
  53. {
  54. static int init = 0;
  55. /* The writer is static for performance reasons */
  56. static GEOSWKTWriter *writer = NULL;
  57. char *wkt = NULL;
  58. if(init == 0) {
  59. initGEOS(NULL, NULL);
  60. writer = GEOSWKTWriter_create();
  61. init += 1;
  62. }
  63. GEOSWKTWriter_setOutputDimension(writer, 2);
  64. GEOSGeometry *geom = Vect_read_area_geos(Map, area);
  65. if(!geom) {
  66. return(NULL);
  67. }
  68. wkt = GEOSWKTWriter_write(writer, geom);
  69. GEOSGeom_destroy(geom);
  70. return(wkt);
  71. }
  72. /*!
  73. \brief Create a Well Known Binary (WKB) representation of
  74. given feature type from points.
  75. This function is not thread safe, it uses static variables for speedup.
  76. Supported feature types:
  77. - GV_POINT -> POINT
  78. - GV_CENTROID -> POINT
  79. - GV_LINE -> LINESTRING
  80. - GV_BOUNDARY -> LINEARRING
  81. \param points pointer to line_pnts structure
  82. \param type feature type (see supported types)
  83. \param with_z Set to 1 if the feature is 3d, 0 otherwise
  84. \param size The size of the returned byte array
  85. \return pointer to char array
  86. \return NULL on error
  87. */
  88. unsigned char *Vect_line_to_wkb(const struct line_pnts *points,
  89. int type, int with_z, size_t *size)
  90. {
  91. static int init = 0;
  92. /* The writer is static for performance reasons */
  93. static GEOSWKBWriter *writer = NULL;
  94. unsigned char *wkb = NULL;
  95. if(init == 0) {
  96. initGEOS(NULL, NULL);
  97. writer = GEOSWKBWriter_create();
  98. init += 1;
  99. }
  100. GEOSWKBWriter_setOutputDimension(writer, with_z ? 3 : 2);
  101. GEOSGeometry *geom = Vect_line_to_geos(points, type, with_z);
  102. if(!geom) {
  103. return(NULL);
  104. }
  105. wkb = GEOSWKBWriter_write(writer, geom, size);
  106. GEOSGeom_destroy(geom);
  107. return(wkb);
  108. }
  109. /*!
  110. \brief Create a Well Known Text (WKT) representation of
  111. given feature type from points.
  112. This function is not thread safe, it uses static variables for speedup.
  113. Supported types:
  114. - GV_POINT -> POINT
  115. - GV_CENTROID -> POINT
  116. - GV_LINE -> LINESTRING
  117. - GV_BOUNDARY -> LINEARRING
  118. \param points pointer to line_pnts structure
  119. \param type feature type (see supported types)
  120. \param with_z Set to 1 if the feature is 3d, 0 otherwise
  121. \return pointer to char array
  122. \return NULL on error
  123. */
  124. char *Vect_line_to_wkt(const struct line_pnts *points,
  125. int type, int with_z)
  126. {
  127. static int init = 0;
  128. /* The writer is static for performance reasons */
  129. static GEOSWKTWriter *writer = NULL;
  130. char *wkt = NULL;
  131. if(init == 0) {
  132. initGEOS(NULL, NULL);
  133. writer = GEOSWKTWriter_create();
  134. init += 1;
  135. }
  136. GEOSWKTWriter_setOutputDimension(writer, with_z ? 3 : 2);
  137. GEOSGeometry *geom = Vect_line_to_geos(points, type, with_z);
  138. if(!geom) {
  139. return(NULL);
  140. }
  141. wkt = GEOSWKTWriter_write(writer, geom);
  142. GEOSGeom_destroy(geom);
  143. return(wkt);
  144. }
  145. #endif /* HAVE_GEOS */