GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-535c39c9fc
geos_to_wktb.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/Vlib/geos_to_wktb.c
3 
4  \brief Vector library - GEOS powered WKT and WKB export
5 
6  Higher level functions for reading/writing/manipulating vectors.
7 
8  (C) 2015 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 Soeren Gebbert <soerengebbert googlemail.com>
14  */
15 
16 #include <stdlib.h>
17 #include <grass/vector.h>
18 #include <grass/glocale.h>
19 
20 #ifdef HAVE_GEOS
21 
22 /*!
23  \brief Read vector area and return it as Well Known Binary (WKB)
24  unsigned char array
25 
26  \param Map pointer to Map_info structure
27  \param area area id
28  \param size The size of the returned unsigned char array
29 
30  \return pointer to unsigned char array
31  \return NULL on error
32  */
33 unsigned char *Vect_read_area_to_wkb(struct Map_info *Map, int area,
34  size_t *size)
35 {
36  static int init = 0;
37 
38  /* The writer is static for performance reasons */
39  static GEOSWKBWriter *writer = NULL;
40  unsigned char *wkb = NULL;
41 
42  if (init == 0) {
43  initGEOS(NULL, NULL);
44  writer = GEOSWKBWriter_create();
45  init += 1;
46  }
47 
48  GEOSWKBWriter_setOutputDimension(writer, 2);
49 
50  GEOSGeometry *geom = Vect_read_area_geos(Map, area);
51 
52  if (!geom) {
53  return (NULL);
54  }
55 
56  wkb = GEOSWKBWriter_write(writer, geom, size);
57 
58  GEOSGeom_destroy(geom);
59 
60  return (wkb);
61 }
62 
63 /*!
64  \brief Read vector area and return it as Well Known Text (WKT)
65  unsigned char array
66 
67  \param Map pointer to Map_info structure
68  \param area area id
69  \param size The size of the returned unsigned char array
70 
71  \return pointer to char array
72  \return NULL on error
73  */
74 char *Vect_read_area_to_wkt(struct Map_info *Map, int area)
75 {
76  static int init = 0;
77 
78  /* The writer is static for performance reasons */
79  static GEOSWKTWriter *writer = NULL;
80  char *wkt = NULL;
81 
82  if (init == 0) {
83  initGEOS(NULL, NULL);
84  writer = GEOSWKTWriter_create();
85  init += 1;
86  }
87 
88  GEOSWKTWriter_setOutputDimension(writer, 2);
89 
90  GEOSGeometry *geom = Vect_read_area_geos(Map, area);
91 
92  if (!geom) {
93  return (NULL);
94  }
95 
96  wkt = GEOSWKTWriter_write(writer, geom);
97 
98  GEOSGeom_destroy(geom);
99 
100  return (wkt);
101 }
102 
103 /*!
104  \brief Read a Well Known Binary (WKB) representation of
105  a given feature id.
106 
107  This function reads a specific feature and converts it into a
108  WKB representation. line_pnts and line_cats structures can be provided
109  to store the result of the read operation. That is meaningful in case
110  the category values of the feature are needed.
111  This function is not thread safe, it uses static variables for speedup.
112 
113  Supported feature types:
114  - GV_POINT -> POINT
115  - GV_CENTROID -> POINT
116  - GV_LINE -> LINESTRING
117  - GV_BOUNDARY -> LINEARRING
118 
119  \param Map pointer to Map_info structure
120  \param line_p pointer to line_pnts structure to use, or NULL
121  \param line_c pointer to line_cats structure to use, or NULL
122  \param line The id of the feature to read
123  \param size The size of the returned unsigned char array
124 
125  \return pointer to unsigned char array
126  \return NULL on error
127  */
128 unsigned char *Vect_read_line_to_wkb(struct Map_info *Map,
129  struct line_pnts *line_p,
130  struct line_cats *line_c, int line,
131  size_t *size, int *error)
132 {
133  static int init = 0;
134 
135  /* The writer is static for performance reasons */
136  static GEOSWKBWriter *writer = NULL;
137  unsigned char *wkb = NULL;
138  int destroy_line = 0, destroy_cats = 0;
139 
140  if (init == 0) {
141  initGEOS(NULL, NULL);
142  writer = GEOSWKBWriter_create();
143  init += 1;
144  }
145 
146  if (line_p == NULL) {
147  destroy_line = 1;
148  line_p = Vect_new_line_struct();
149  }
150 
151  if (line_c == NULL) {
152  destroy_cats = 1;
153  line_c = Vect_new_cats_struct();
154  }
155 
156  int f_type = Vect_read_line(Map, line_p, line_c, line);
157 
158  /* Save the error state */
159  *error = f_type;
160 
161  if (f_type < 0)
162  return (NULL);
163 
164  GEOSWKBWriter_setOutputDimension(writer, Vect_is_3d(Map) ? 3 : 2);
165 
166  GEOSGeometry *geom = Vect_line_to_geos(line_p, f_type, Vect_is_3d(Map));
167 
168  if (destroy_cats == 1)
169  Vect_destroy_cats_struct(line_c);
170 
171  if (destroy_line == 1)
172  Vect_destroy_line_struct(line_p);
173 
174  if (!geom) {
175  return (NULL);
176  }
177 
178  wkb = GEOSWKBWriter_write(writer, geom, size);
179 
180  GEOSGeom_destroy(geom);
181 
182  return (wkb);
183 }
184 
185 /*!
186  \brief Create a Well Known Binary (WKB) representation of
187  given feature type from points.
188 
189  This function is not thread safe, it uses static variables for speedup.
190 
191  Supported feature types:
192  - GV_POINT -> POINT
193  - GV_CENTROID -> POINT
194  - GV_LINE -> LINESTRING
195  - GV_BOUNDARY -> LINEARRING
196 
197  \param points pointer to line_pnts structure
198  \param type feature type (see supported types)
199  \param with_z Set to 1 if the feature is 3d, 0 otherwise
200  \param size The size of the returned byte array
201 
202  \return pointer to char array
203  \return NULL on error
204  */
205 unsigned char *Vect_line_to_wkb(const struct line_pnts *points, int type,
206  int with_z, size_t *size)
207 {
208  static int init = 0;
209 
210  /* The writer is static for performance reasons */
211  static GEOSWKBWriter *writer = NULL;
212  unsigned char *wkb = NULL;
213 
214  if (init == 0) {
215  initGEOS(NULL, NULL);
216  writer = GEOSWKBWriter_create();
217  init += 1;
218  }
219 
220  GEOSWKBWriter_setOutputDimension(writer, with_z ? 3 : 2);
221 
222  GEOSGeometry *geom = Vect_line_to_geos(points, type, with_z);
223 
224  if (!geom) {
225  return (NULL);
226  }
227 
228  wkb = GEOSWKBWriter_write(writer, geom, size);
229 
230  GEOSGeom_destroy(geom);
231 
232  return (wkb);
233 }
234 
235 /*!
236  \brief Create a Well Known Text (WKT) representation of
237  given feature type from points.
238 
239  This function is not thread safe, it uses static variables for speedup.
240 
241  Supported types:
242  - GV_POINT -> POINT
243  - GV_CENTROID -> POINT
244  - GV_LINE -> LINESTRING
245  - GV_BOUNDARY -> LINEARRING
246 
247  \param points pointer to line_pnts structure
248  \param type feature type (see supported types)
249  \param with_z Set to 1 if the feature is 3d, 0 otherwise
250 
251  \return pointer to char array
252  \return NULL on error
253  */
254 char *Vect_line_to_wkt(const struct line_pnts *points, int type, int with_z)
255 {
256  static int init = 0;
257 
258  /* The writer is static for performance reasons */
259  static GEOSWKTWriter *writer = NULL;
260  char *wkt = NULL;
261 
262  if (init == 0) {
263  initGEOS(NULL, NULL);
264  writer = GEOSWKTWriter_create();
265  init += 1;
266  }
267 
268  GEOSWKTWriter_setOutputDimension(writer, with_z ? 3 : 2);
269 
270  GEOSGeometry *geom = Vect_line_to_geos(points, type, with_z);
271 
272  if (!geom) {
273  return (NULL);
274  }
275 
276  wkt = GEOSWKTWriter_write(writer, geom);
277 
278  GEOSGeom_destroy(geom);
279 
280  return (wkt);
281 }
282 
283 #endif /* HAVE_GEOS */
void init(double work[])
Definition: as177.c:61
#define NULL
Definition: ccmath.h:32
void Vect_destroy_line_struct(struct line_pnts *)
Frees all memory associated with a line_pnts structure, including the structure itself.
Definition: line.c:77
void Vect_destroy_cats_struct(struct line_cats *)
Frees all memory associated with line_cats structure, including the struct itself.
int Vect_read_line(struct Map_info *, struct line_pnts *, struct line_cats *, int)
Read vector feature (topological level required)
GEOSGeometry * Vect_read_area_geos(struct Map_info *, int)
Read vector area and stores it as GEOSGeometry instance (polygon)
Definition: geos.c:84
struct line_pnts * Vect_new_line_struct(void)
Creates and initializes a line_pnts structure.
Definition: line.c:45
GEOSGeometry * Vect_line_to_geos(const struct line_pnts *, int, int)
Create GEOSGeometry of given type from feature points.
Definition: geos.c:137
struct line_cats * Vect_new_cats_struct(void)
Creates and initializes line_cats structure.
int Vect_is_3d(struct Map_info *)
Check if vector map is 3D.
unsigned char * Vect_read_line_to_wkb(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, int line, size_t *size, int *error)
Read a Well Known Binary (WKB) representation of a given feature id.
Definition: geos_to_wktb.c:128
char * Vect_read_area_to_wkt(struct Map_info *Map, int area)
Read vector area and return it as Well Known Text (WKT) unsigned char array.
Definition: geos_to_wktb.c:74
char * Vect_line_to_wkt(const struct line_pnts *points, int type, int with_z)
Create a Well Known Text (WKT) representation of given feature type from points.
Definition: geos_to_wktb.c:254
unsigned char * Vect_line_to_wkb(const struct line_pnts *points, int type, int with_z, size_t *size)
Create a Well Known Binary (WKB) representation of given feature type from points.
Definition: geos_to_wktb.c:205
unsigned char * Vect_read_area_to_wkb(struct Map_info *Map, int area, size_t *size)
Read vector area and return it as Well Known Binary (WKB) unsigned char array.
Definition: geos_to_wktb.c:33
Vector map info.
Definition: dig_structs.h:1243
Feature category info.
Definition: dig_structs.h:1677
Feature geometry info - coordinates.
Definition: dig_structs.h:1651
struct GEOSGeom_t GEOSGeometry
Definition: vector.h:9