GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
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 SPDX-FileCopyrightText: 2015 GRASS Development Team
9 SPDX-License-Identifier: GPL-2.0-or-later
10
11 \author Soeren Gebbert <soerengebbert googlemail.com>
12 */
13
14#include <stdbool.h>
15#include <stdlib.h>
16#include <grass/vector.h>
17#include <grass/glocale.h>
18
19#ifdef HAVE_GEOS
20
21/*!
22 \brief Read vector area and return it as Well Known Binary (WKB)
23 unsigned char array
24
25 \param Map pointer to Map_info structure
26 \param area area id
27 \param size The size of the returned unsigned char array
28
29 \return pointer to unsigned char array
30 \return NULL on error
31 */
32unsigned char *Vect_read_area_to_wkb(struct Map_info *Map, int area,
33 size_t *size)
34{
35 static int init = 0;
36
37 /* The writer is static for performance reasons */
38 static GEOSWKBWriter *writer = NULL;
39 unsigned char *wkb = NULL;
40
41 if (init == 0) {
44 init += 1;
45 }
46
48
50
51 if (!geom) {
52 return (NULL);
53 }
54
56
58
59 return (wkb);
60}
61
62/*!
63 \brief Read vector area and return it as Well Known Text (WKT)
64 unsigned char array
65
66 Calls Vect_read_area_to_wkt2() with trim set to false.
67
68 */
69char *Vect_read_area_to_wkt(struct Map_info *Map, int area)
70{
71 return Vect_read_area_to_wkt2(Map, area, false);
72}
73
74/*!
75 \brief Read vector area and return it as Well Known Text (WKT)
76 unsigned char array
77
78 \param Map pointer to Map_info structure
79 \param area area id
80 \param trim Set the number trimming option on, With trim set to true, the
81 writer will strip trailing 0's from the output coordinates.
82
83 \return pointer to string (allocated)
84 \return NULL on error
85 */
86char *Vect_read_area_to_wkt2(struct Map_info *Map, int area, bool trim)
87{
88 static int init = 0;
89
90 /* The writer is static for performance reasons */
91 static GEOSWKTWriter *writer = NULL;
92 char *wkt = NULL;
93
94 if (init == 0) {
97 init += 1;
98 }
99
102
104
105 if (!geom) {
106 return NULL;
107 }
108
110 char *wkt_out = G_store(wkt);
111
113 GEOSFree(wkt);
114
115 return wkt_out;
116}
117
118/*!
119 \brief Read a Well Known Binary (WKB) representation of
120 a given feature id.
121
122 This function reads a specific feature and converts it into a
123 WKB representation. line_pnts and line_cats structures can be provided
124 to store the result of the read operation. That is meaningful in case
125 the category values of the feature are needed.
126 This function is not thread safe, it uses static variables for speedup.
127
128 Supported feature types:
129 - GV_POINT -> POINT
130 - GV_CENTROID -> POINT
131 - GV_LINE -> LINESTRING
132 - GV_BOUNDARY -> LINEARRING
133
134 \param Map pointer to Map_info structure
135 \param line_p pointer to line_pnts structure to use, or NULL
136 \param line_c pointer to line_cats structure to use, or NULL
137 \param line The id of the feature to read
138 \param size The size of the returned unsigned char array
139
140 \return pointer to unsigned char array
141 \return NULL on error
142 */
143unsigned char *Vect_read_line_to_wkb(struct Map_info *Map,
144 struct line_pnts *line_p,
145 struct line_cats *line_c, int line,
146 size_t *size, int *error)
147{
148 static int init = 0;
149
150 /* The writer is static for performance reasons */
151 static GEOSWKBWriter *writer = NULL;
152 unsigned char *wkb = NULL;
153 int destroy_line = 0, destroy_cats = 0;
154
155 if (init == 0) {
158 init += 1;
159 }
160
161 if (line_p == NULL) {
162 destroy_line = 1;
164 }
165
166 if (line_c == NULL) {
167 destroy_cats = 1;
169 }
170
171 int f_type = Vect_read_line(Map, line_p, line_c, line);
172
173 /* Save the error state */
174 *error = f_type;
175
176 if (f_type < 0)
177 return (NULL);
178
180
182
183 if (destroy_cats == 1)
185
186 if (destroy_line == 1)
188
189 if (!geom) {
190 return (NULL);
191 }
192
194
196
197 return (wkb);
198}
199
200/*!
201 \brief Create a Well Known Binary (WKB) representation of
202 given feature type from points.
203
204 This function is not thread safe, it uses static variables for speedup.
205
206 Supported feature types:
207 - GV_POINT -> POINT
208 - GV_CENTROID -> POINT
209 - GV_LINE -> LINESTRING
210 - GV_BOUNDARY -> LINEARRING
211
212 \param points pointer to line_pnts structure
213 \param type feature type (see supported types)
214 \param with_z Set to 1 if the feature is 3d, 0 otherwise
215 \param size The size of the returned byte array
216
217 \return pointer to string (allocated)
218 \return NULL on error
219 */
220unsigned char *Vect_line_to_wkb(const struct line_pnts *points, int type,
221 int with_z, size_t *size)
222{
223 static int init = 0;
224
225 /* The writer is static for performance reasons */
226 static GEOSWKBWriter *writer = NULL;
227 unsigned char *wkb = NULL;
228
229 if (init == 0) {
232 init += 1;
233 }
234
236
237 GEOSGeometry *geom = Vect_line_to_geos(points, type, with_z);
238
239 if (!geom) {
240 return (NULL);
241 }
242
244
246
247 return (wkb);
248}
249
250/*!
251 \brief Create a Well Known Text (WKT) representation of
252 given feature type from points.
253
254 Calls Vect_line_to_wkt2() with trim set to false.
255 */
256char *Vect_line_to_wkt(const struct line_pnts *points, int type, bool with_z)
257{
258 return Vect_line_to_wkt2(points, type, with_z, false);
259}
260
261/*!
262 \brief Create a Well Known Text (WKT) representation of
263 given feature type from points.
264
265 This function is not thread safe, it uses static variables for speedup.
266
267 Supported types:
268 - GV_POINT -> POINT
269 - GV_CENTROID -> POINT
270 - GV_LINE -> LINESTRING
271 - GV_BOUNDARY -> LINEARRING
272
273 \param points pointer to line_pnts structure
274 \param type feature type (see supported types)
275 \param with_z Set to true if the feature is 3d, false otherwise
276 \param trim Set the number trimming option on, With trim set to true, the
277 writer will strip trailing 0's from the output coordinates.
278
279 \return pointer to char array
280 \return NULL on error
281 */
282char *Vect_line_to_wkt2(const struct line_pnts *points, int type, bool with_z,
283 bool trim)
284{
285 static int init = 0;
286
287 /* The writer is static for performance reasons */
288 static GEOSWKTWriter *writer = NULL;
289 char *wkt = NULL;
290
291 if (init == 0) {
294 init += 1;
295 }
296
299
300 GEOSGeometry *geom = Vect_line_to_geos(points, type, with_z);
301
302 if (!geom) {
303 return NULL;
304 }
305
307 char *wkt_out = G_store(wkt);
308
310 GEOSFree(wkt);
311
312 return wkt_out;
313}
314
315#endif /* HAVE_GEOS */
void init(double work[])
Definition as177.c:61
#define NULL
Definition ccmath.h:32
char * G_store(const char *)
Copy string to allocated memory.
Definition strings.c:85
void Vect_destroy_line_struct(struct line_pnts *)
Frees all memory associated with a line_pnts structure, including the structure itself.
Definition line.c:75
GEOSGeometry * Vect_line_to_geos(const struct line_pnts *, int, int)
Create GEOSGeometry of given type from feature points.
Definition geos.c:135
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)
struct line_cats * Vect_new_cats_struct(void)
Creates and initializes line_cats structure.
GEOSGeometry * Vect_read_area_geos(struct Map_info *, int)
Read vector area and stores it as GEOSGeometry instance (polygon)
Definition geos.c:82
struct line_pnts * Vect_new_line_struct(void)
Creates and initializes a line_pnts structure.
Definition line.c:43
int Vect_is_3d(struct Map_info *)
Check if vector map is 3D.
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.
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.
char * Vect_line_to_wkt(const struct line_pnts *points, int type, bool with_z)
Create a Well Known Text (WKT) representation of given feature type from points.
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.
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.
char * Vect_read_area_to_wkt2(struct Map_info *Map, int area, bool trim)
Read vector area and return it as Well Known Text (WKT) unsigned char array.
char * Vect_line_to_wkt2(const struct line_pnts *points, int type, bool with_z, bool trim)
Create a Well Known Text (WKT) representation of given feature type from points.
Vector map info.
Feature category info.
Feature geometry info - coordinates.
struct GEOSGeom_t GEOSGeometry
Definition vector.h:9