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