PostGIS  2.3.8dev-r@@SVN_REVISION@@
lwout_twkb.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * PostGIS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * PostGIS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  **********************************************************************
20  *
21  * Copyright (C) 2013 Nicklas Avén
22  *
23  **********************************************************************/
24 
25 
26 #include "lwout_twkb.h"
27 
28 /*
29 * GeometryType, and dimensions
30 */
31 static uint8_t lwgeom_twkb_type(const LWGEOM *geom)
32 {
33  uint8_t twkb_type = 0;
34 
35  LWDEBUGF(2, "Entered lwgeom_twkb_type",0);
36 
37  switch ( geom->type )
38  {
39  case POINTTYPE:
40  twkb_type = WKB_POINT_TYPE;
41  break;
42  case LINETYPE:
43  twkb_type = WKB_LINESTRING_TYPE;
44  break;
45  case POLYGONTYPE:
46  twkb_type = WKB_POLYGON_TYPE;
47  break;
48  case MULTIPOINTTYPE:
49  twkb_type = WKB_MULTIPOINT_TYPE;
50  break;
51  case MULTILINETYPE:
52  twkb_type = WKB_MULTILINESTRING_TYPE;
53  break;
54  case MULTIPOLYGONTYPE:
55  twkb_type = WKB_MULTIPOLYGON_TYPE;
56  break;
57  case COLLECTIONTYPE:
58  twkb_type = WKB_GEOMETRYCOLLECTION_TYPE;
59  break;
60  default:
61  lwerror("Unsupported geometry type: %s [%d]",
62  lwtype_name(geom->type), geom->type);
63  }
64  return twkb_type;
65 }
66 
67 
72 static size_t sizeof_bbox(TWKB_STATE *ts, int ndims)
73 {
74  int i;
75  uint8_t buf[16];
76  size_t size = 0;
77  LWDEBUGF(2, "Entered %s", __func__);
78  for ( i = 0; i < ndims; i++ )
79  {
80  size += varint_s64_encode_buf(ts->bbox_min[i], buf);
81  size += varint_s64_encode_buf((ts->bbox_max[i] - ts->bbox_min[i]), buf);
82  }
83  return size;
84 }
89 static void write_bbox(TWKB_STATE *ts, int ndims)
90 {
91  int i;
92  LWDEBUGF(2, "Entered %s", __func__);
93  for ( i = 0; i < ndims; i++ )
94  {
96  bytebuffer_append_varint(ts->header_buf, (ts->bbox_max[i] - ts->bbox_min[i]));
97  }
98 }
99 
100 
106 static int ptarray_to_twkb_buf(const POINTARRAY *pa, TWKB_GLOBALS *globals, TWKB_STATE *ts, int register_npoints, int minpoints)
107 {
108  int ndims = FLAGS_NDIMS(pa->flags);
109  int i, j;
110  bytebuffer_t b;
111  bytebuffer_t *b_p;
112  int64_t nextdelta[MAX_N_DIMS];
113  int npoints = 0;
114  size_t npoints_offset = 0;
115 
116  LWDEBUGF(2, "Entered %s", __func__);
117 
118  /* Dispense with the empty case right away */
119  if ( pa->npoints == 0 && register_npoints )
120  {
121  LWDEBUGF(4, "Register npoints:%d", pa->npoints);
123  return 0;
124  }
125 
126  /* If npoints is more than 127 it is unpredictable how many bytes npoints will need */
127  /* Then we have to store the deltas in a temp buffer to later add them after npoints */
128  /* If noints is below 128 we know 1 byte will be needed */
129  /* Then we can make room for that 1 byte at once and write to */
130  /* ordinary buffer */
131  if( pa->npoints > 127 )
132  {
133  /* Independent buffer to hold the coordinates, so we can put the npoints */
134  /* into the stream once we know how many points we actually have */
135  bytebuffer_init_with_size(&b, 3 * ndims * pa->npoints);
136  b_p = &b;
137  }
138  else
139  {
140  /* We give an alias to our ordinary buffer */
141  b_p = ts->geom_buf;
142  if ( register_npoints )
143  {
144  /* We do not store a pointer to the place where we want the npoints value */
145  /* Instead we store how far from the beginning of the buffer we want the value */
146  /* That is because we otherwise will get in trouble if the buffer is reallocated */
147  npoints_offset = b_p->writecursor - b_p->buf_start;
148 
149  /* We just move the cursor 1 step to make room for npoints byte */
150  /* We use the function append_byte even if we have no value yet, */
151  /* since that gives us the check for big enough buffer and moves the cursor */
152  bytebuffer_append_byte(b_p, 0);
153  }
154  }
155 
156  for ( i = 0; i < pa->npoints; i++ )
157  {
158  double *dbl_ptr = (double*)getPoint_internal(pa, i);
159  int diff = 0;
160 
161  /* Write this coordinate to the buffer as a varint */
162  for ( j = 0; j < ndims; j++ )
163  {
164  /* To get the relative coordinate we don't get the distance */
165  /* from the last point but instead the distance from our */
166  /* last accumulated point. This is important to not build up an */
167  /* accumulated error when rounding the coordinates */
168  nextdelta[j] = (int64_t) llround(globals->factor[j] * dbl_ptr[j]) - ts->accum_rels[j];
169  LWDEBUGF(4, "deltavalue: %d, ", nextdelta[j]);
170  diff += llabs(nextdelta[j]);
171  }
172 
173  /* Skipping the first point is not allowed */
174  /* If the sum(abs()) of all the deltas was zero, */
175  /* then this was a duplicate point, so we can ignore it */
176  if ( i > minpoints && diff == 0 )
177  continue;
178 
179  /* We really added a point, so... */
180  npoints++;
181 
182  /* Write this vertex to the temporary buffer as varints */
183  for ( j = 0; j < ndims; j++ )
184  {
185  ts->accum_rels[j] += nextdelta[j];
186  bytebuffer_append_varint(b_p, nextdelta[j]);
187  }
188 
189  /* See if this coordinate expands the bounding box */
190  if( globals->variant & TWKB_BBOX )
191  {
192  for ( j = 0; j < ndims; j++ )
193  {
194  if( ts->accum_rels[j] > ts->bbox_max[j] )
195  ts->bbox_max[j] = ts->accum_rels[j];
196 
197  if( ts->accum_rels[j] < ts->bbox_min[j] )
198  ts->bbox_min[j] = ts->accum_rels[j];
199  }
200  }
201 
202  }
203 
204  if ( pa->npoints > 127 )
205  {
206  /* Now write the temporary results into the main buffer */
207  /* First the npoints */
208  if ( register_npoints )
209  bytebuffer_append_uvarint(ts->geom_buf, npoints);
210  /* Now the coordinates */
212 
213  /* Clear our temporary buffer */
214  lwfree(b.buf_start);
215  }
216  else
217  {
218  /* If we didn't use a temp buffer, we just write that npoints value */
219  /* to where it belongs*/
220  if ( register_npoints )
221  varint_u64_encode_buf(npoints, b_p->buf_start + npoints_offset);
222  }
223 
224  return 0;
225 }
226 
227 /******************************************************************
228 * POINTS
229 *******************************************************************/
230 
231 static int lwpoint_to_twkb_buf(const LWPOINT *pt, TWKB_GLOBALS *globals, TWKB_STATE *ts)
232 {
233  LWDEBUGF(2, "Entered %s", __func__);
234 
235  /* Set the coordinates (don't write npoints) */
236  ptarray_to_twkb_buf(pt->point, globals, ts, 0, 1);
237  return 0;
238 }
239 
240 /******************************************************************
241 * LINESTRINGS
242 *******************************************************************/
243 
244 static int lwline_to_twkb_buf(const LWLINE *line, TWKB_GLOBALS *globals, TWKB_STATE *ts)
245 {
246  LWDEBUGF(2, "Entered %s", __func__);
247 
248  /* Set the coordinates (do write npoints) */
249  ptarray_to_twkb_buf(line->points, globals, ts, 1, 2);
250  return 0;
251 }
252 
253 /******************************************************************
254 * POLYGONS
255 *******************************************************************/
256 
257 static int lwpoly_to_twkb_buf(const LWPOLY *poly, TWKB_GLOBALS *globals, TWKB_STATE *ts)
258 {
259  int i;
260 
261  /* Set the number of rings */
262  bytebuffer_append_uvarint(ts->geom_buf, (uint64_t) poly->nrings);
263 
264  for ( i = 0; i < poly->nrings; i++ )
265  {
266  /* Set the coordinates (do write npoints) */
267  ptarray_to_twkb_buf(poly->rings[i], globals, ts, 1, 4);
268  }
269 
270  return 0;
271 }
272 
273 
274 
275 /******************************************************************
276 * MULTI-GEOMETRYS (MultiPoint, MultiLinestring, MultiPolygon)
277 *******************************************************************/
278 
279 static int lwmulti_to_twkb_buf(const LWCOLLECTION *col, TWKB_GLOBALS *globals, TWKB_STATE *ts)
280 {
281  int i;
282  int nempty = 0;
283 
284  LWDEBUGF(2, "Entered %s", __func__);
285  LWDEBUGF(4, "Number of geometries in multi is %d", col->ngeoms);
286 
287  /* Deal with special case for MULTIPOINT: skip any empty points */
288  if ( col->type == MULTIPOINTTYPE )
289  {
290  for ( i = 0; i < col->ngeoms; i++ )
291  if ( lwgeom_is_empty(col->geoms[i]) )
292  nempty++;
293  }
294 
295  /* Set the number of geometries */
296  bytebuffer_append_uvarint(ts->geom_buf, (uint64_t) (col->ngeoms - nempty));
297 
298  /* We've been handed an idlist, so write it in */
299  if ( ts->idlist )
300  {
301  for ( i = 0; i < col->ngeoms; i++ )
302  {
303  /* Skip empty points in multipoints, we can't represent them */
304  if ( col->type == MULTIPOINTTYPE && lwgeom_is_empty(col->geoms[i]) )
305  continue;
306 
308  }
309 
310  /* Empty it out to nobody else uses it now */
311  ts->idlist = NULL;
312  }
313 
314  for ( i = 0; i < col->ngeoms; i++ )
315  {
316  /* Skip empty points in multipoints, we can't represent them */
317  if ( col->type == MULTIPOINTTYPE && lwgeom_is_empty(col->geoms[i]) )
318  continue;
319 
320  lwgeom_to_twkb_buf(col->geoms[i], globals, ts);
321  }
322  return 0;
323 }
324 
325 /******************************************************************
326 * GEOMETRYCOLLECTIONS
327 *******************************************************************/
328 
329 static int lwcollection_to_twkb_buf(const LWCOLLECTION *col, TWKB_GLOBALS *globals, TWKB_STATE *ts)
330 {
331  int i;
332 
333  LWDEBUGF(2, "Entered %s", __func__);
334  LWDEBUGF(4, "Number of geometries in collection is %d", col->ngeoms);
335 
336  /* Set the number of geometries */
337  bytebuffer_append_uvarint(ts->geom_buf, (uint64_t) col->ngeoms);
338 
339  /* We've been handed an idlist, so write it in */
340  if ( ts->idlist )
341  {
342  for ( i = 0; i < col->ngeoms; i++ )
344 
345  /* Empty it out to nobody else uses it now */
346  ts->idlist = NULL;
347  }
348 
349  /* Write in the sub-geometries */
350  for ( i = 0; i < col->ngeoms; i++ )
351  {
352  lwgeom_write_to_buffer(col->geoms[i], globals, ts);
353  }
354  return 0;
355 }
356 
357 
358 /******************************************************************
359 * Handle whole TWKB
360 *******************************************************************/
361 
362 static int lwgeom_to_twkb_buf(const LWGEOM *geom, TWKB_GLOBALS *globals, TWKB_STATE *ts)
363 {
364  LWDEBUGF(2, "Entered %s", __func__);
365 
366  switch ( geom->type )
367  {
368  case POINTTYPE:
369  {
370  LWDEBUGF(4,"Type found is Point, %d", geom->type);
371  return lwpoint_to_twkb_buf((LWPOINT*) geom, globals, ts);
372  }
373  case LINETYPE:
374  {
375  LWDEBUGF(4,"Type found is Linestring, %d", geom->type);
376  return lwline_to_twkb_buf((LWLINE*) geom, globals, ts);
377  }
378  /* Polygon has 'nrings' and 'rings' elements */
379  case POLYGONTYPE:
380  {
381  LWDEBUGF(4,"Type found is Polygon, %d", geom->type);
382  return lwpoly_to_twkb_buf((LWPOLY*)geom, globals, ts);
383  }
384 
385  /* All these Collection types have 'ngeoms' and 'geoms' elements */
386  case MULTIPOINTTYPE:
387  case MULTILINETYPE:
388  case MULTIPOLYGONTYPE:
389  {
390  LWDEBUGF(4,"Type found is Multi, %d", geom->type);
391  return lwmulti_to_twkb_buf((LWCOLLECTION*)geom, globals, ts);
392  }
393  case COLLECTIONTYPE:
394  {
395  LWDEBUGF(4,"Type found is collection, %d", geom->type);
396  return lwcollection_to_twkb_buf((LWCOLLECTION*) geom, globals, ts);
397  }
398  /* Unknown type! */
399  default:
400  lwerror("Unsupported geometry type: %s [%d]", lwtype_name((geom)->type), (geom)->type);
401  }
402 
403  return 0;
404 }
405 
406 
407 static int lwgeom_write_to_buffer(const LWGEOM *geom, TWKB_GLOBALS *globals, TWKB_STATE *parent_state)
408 {
409  int i, is_empty, has_z, has_m, ndims;
410  size_t bbox_size = 0, optional_precision_byte = 0;
411  uint8_t flag = 0, type_prec = 0;
412 
413  TWKB_STATE child_state;
414  memset(&child_state, 0, sizeof(TWKB_STATE));
415  child_state.header_buf = bytebuffer_create_with_size(16);
416  child_state.geom_buf = bytebuffer_create_with_size(64);
417  child_state.idlist = parent_state->idlist;
418 
419  /* Read dimensionality from input */
420  has_z = lwgeom_has_z(geom);
421  has_m = lwgeom_has_m(geom);
422  ndims = lwgeom_ndims(geom);
423  is_empty = lwgeom_is_empty(geom);
424 
425  /* Do we need extended precision? If we have a Z or M we do. */
426  optional_precision_byte = (has_z || has_m);
427 
428  /* Both X and Y dimension use the same precision */
429  globals->factor[0] = pow(10, globals->prec_xy);
430  globals->factor[1] = globals->factor[0];
431 
432  /* Z and M dimensions have their own precisions */
433  if ( has_z )
434  globals->factor[2] = pow(10, globals->prec_z);
435  if ( has_m )
436  globals->factor[2 + has_z] = pow(10, globals->prec_m);
437 
438  /* Reset stats */
439  for ( i = 0; i < MAX_N_DIMS; i++ )
440  {
441  /* Reset bbox calculation */
442  child_state.bbox_max[i] = INT64_MIN;
443  child_state.bbox_min[i] = INT64_MAX;
444  /* Reset acumulated delta values to get absolute values on next point */
445  child_state.accum_rels[i] = 0;
446  }
447 
448  /* TYPE/PRECISION BYTE */
449  if ( abs(globals->prec_xy) > 7 )
450  lwerror("%s: X/Z precision cannot be greater than 7 or less than -7", __func__);
451 
452  /* Read the TWKB type number from the geometry */
453  TYPE_PREC_SET_TYPE(type_prec, lwgeom_twkb_type(geom));
454  /* Zig-zag the precision value before encoding it since it is a signed value */
455  TYPE_PREC_SET_PREC(type_prec, zigzag8(globals->prec_xy));
456  /* Write the type and precision byte */
457  bytebuffer_append_byte(child_state.header_buf, type_prec);
458 
459  /* METADATA BYTE */
460  /* Set first bit if we are going to store bboxes */
461  FIRST_BYTE_SET_BBOXES(flag, (globals->variant & TWKB_BBOX) && ! is_empty);
462  /* Set second bit if we are going to store resulting size */
463  FIRST_BYTE_SET_SIZES(flag, globals->variant & TWKB_SIZE);
464  /* There will be no ID-list (for now) */
465  FIRST_BYTE_SET_IDLIST(flag, parent_state->idlist && ! is_empty);
466  /* Are there higher dimensions */
467  FIRST_BYTE_SET_EXTENDED(flag, optional_precision_byte);
468  /* Empty? */
469  FIRST_BYTE_SET_EMPTY(flag, is_empty);
470  /* Write the header byte */
471  bytebuffer_append_byte(child_state.header_buf, flag);
472 
473  /* EXTENDED PRECISION BYTE (OPTIONAL) */
474  /* If needed, write the extended dim byte */
475  if( optional_precision_byte )
476  {
477  uint8_t flag = 0;
478 
479  if ( has_z && ( globals->prec_z > 7 || globals->prec_z < 0 ) )
480  lwerror("%s: Z precision cannot be negative or greater than 7", __func__);
481 
482  if ( has_m && ( globals->prec_m > 7 || globals->prec_m < 0 ) )
483  lwerror("%s: M precision cannot be negative or greater than 7", __func__);
484 
485  HIGHER_DIM_SET_HASZ(flag, has_z);
486  HIGHER_DIM_SET_HASM(flag, has_m);
487  HIGHER_DIM_SET_PRECZ(flag, globals->prec_z);
488  HIGHER_DIM_SET_PRECM(flag, globals->prec_m);
489  bytebuffer_append_byte(child_state.header_buf, flag);
490  }
491 
492  /* It the geometry is empty, we're almost done */
493  if ( is_empty )
494  {
495  /* If this output is sized, write the size of */
496  /* all following content, which is zero because */
497  /* there is none */
498  if ( globals->variant & TWKB_SIZE )
499  bytebuffer_append_byte(child_state.header_buf, 0);
500 
501  bytebuffer_append_bytebuffer(parent_state->geom_buf, child_state.header_buf);
502  bytebuffer_destroy(child_state.header_buf);
503  bytebuffer_destroy(child_state.geom_buf);
504  return 0;
505  }
506 
507  /* Write the TWKB into the output buffer */
508  lwgeom_to_twkb_buf(geom, globals, &child_state);
509 
510  /*If we have a header_buf, we know that this function is called inside a collection*/
511  /*and then we have to merge the bboxes of the included geometries*/
512  /*and put the result to the parent (the collection)*/
513  if( (globals->variant & TWKB_BBOX) && parent_state->header_buf )
514  {
515  LWDEBUG(4,"Merge bboxes");
516  for ( i = 0; i < ndims; i++ )
517  {
518  if(child_state.bbox_min[i]<parent_state->bbox_min[i])
519  parent_state->bbox_min[i] = child_state.bbox_min[i];
520  if(child_state.bbox_max[i]>parent_state->bbox_max[i])
521  parent_state->bbox_max[i] = child_state.bbox_max[i];
522  }
523  }
524 
525  /* Did we have a box? If so, how big? */
526  bbox_size = 0;
527  if( globals->variant & TWKB_BBOX )
528  {
529  LWDEBUG(4,"We want boxes and will calculate required size");
530  bbox_size = sizeof_bbox(&child_state, ndims);
531  }
532 
533  /* Write the size if wanted */
534  if( globals->variant & TWKB_SIZE )
535  {
536  /* Here we have to add what we know will be written to header */
537  /* buffer after size value is written */
538  size_t size_to_register = bytebuffer_getlength(child_state.geom_buf);
539  size_to_register += bbox_size;
540  bytebuffer_append_uvarint(child_state.header_buf, size_to_register);
541  }
542 
543  if( globals->variant & TWKB_BBOX )
544  write_bbox(&child_state, ndims);
545 
546  bytebuffer_append_bytebuffer(parent_state->geom_buf,child_state.header_buf);
547  bytebuffer_append_bytebuffer(parent_state->geom_buf,child_state.geom_buf);
548 
549  bytebuffer_destroy(child_state.header_buf);
550  bytebuffer_destroy(child_state.geom_buf);
551  return 0;
552 }
553 
554 
559 uint8_t*
560 lwgeom_to_twkb_with_idlist(const LWGEOM *geom, int64_t *idlist, uint8_t variant,
561  int8_t precision_xy, int8_t precision_z, int8_t precision_m,
562  size_t *twkb_size)
563 {
564  LWDEBUGF(2, "Entered %s", __func__);
565  LWDEBUGF(2, "variant value %x", variant);
566 
567  TWKB_GLOBALS tg;
568  TWKB_STATE ts;
569 
570  uint8_t *twkb;
571 
572  memset(&ts, 0, sizeof(TWKB_STATE));
573  memset(&tg, 0, sizeof(TWKB_GLOBALS));
574 
575  tg.variant = variant;
576  tg.prec_xy = precision_xy;
577  tg.prec_z = precision_z;
578  tg.prec_m = precision_m;
579 
580  if ( idlist && ! lwgeom_is_collection(geom) )
581  {
582  lwerror("Only collections can support ID lists");
583  return NULL;
584  }
585 
586  if ( ! geom )
587  {
588  LWDEBUG(4,"Cannot convert NULL into TWKB.");
589  lwerror("Cannot convert NULL into TWKB");
590  return NULL;
591  }
592 
593  ts.idlist = idlist;
594  ts.header_buf = NULL;
596  lwgeom_write_to_buffer(geom, &tg, &ts);
597 
598  if ( twkb_size )
599  *twkb_size = bytebuffer_getlength(ts.geom_buf);
600 
601  twkb = ts.geom_buf->buf_start;
602  lwfree(ts.geom_buf);
603  return twkb;
604 }
605 
606 
607 uint8_t*
608 lwgeom_to_twkb(const LWGEOM *geom, uint8_t variant,
609  int8_t precision_xy, int8_t precision_z, int8_t precision_m,
610  size_t *twkb_size)
611 {
612  return lwgeom_to_twkb_with_idlist(geom, NULL, variant, precision_xy, precision_z, precision_m, twkb_size);
613 }
614 
615 
static int lwmulti_to_twkb_buf(const LWCOLLECTION *col, TWKB_GLOBALS *globals, TWKB_STATE *ts)
Definition: lwout_twkb.c:279
#define LINETYPE
Definition: liblwgeom.h:85
#define FIRST_BYTE_SET_EMPTY(flag, bool)
Definition: lwout_twkb.h:57
float factor[4]
Definition: lwout_twkb.h:82
uint8_t * writecursor
Definition: bytebuffer.h:41
#define HIGHER_DIM_SET_PRECM(flag, prec)
Definition: lwout_twkb.h:73
#define TYPE_PREC_SET_TYPE(flag, type)
Macros for manipulating the &#39;type_precision&#39; int.
Definition: lwout_twkb.h:66
uint8_t variant
Definition: cu_in_twkb.c:26
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1004
void bytebuffer_append_bytebuffer(bytebuffer_t *write_to, bytebuffer_t *write_from)
Writes a uint8_t value to the buffer.
Definition: bytebuffer.c:172
bytebuffer_t * bytebuffer_create(void)
Allocate a new bytebuffer_t.
Definition: bytebuffer.c:34
void lwfree(void *mem)
Definition: lwutil.c:242
int npoints
Definition: liblwgeom.h:370
uint8_t type
Definition: liblwgeom.h:502
uint8_t variant
Definition: lwout_twkb.h:78
static int lwgeom_to_twkb_buf(const LWGEOM *geom, TWKB_GLOBALS *globals, TWKB_STATE *ts)
Definition: lwout_twkb.c:362
#define POLYGONTYPE
Definition: liblwgeom.h:86
void bytebuffer_init_with_size(bytebuffer_t *b, size_t size)
Allocate just the internal buffer of an existing bytebuffer_t struct.
Definition: bytebuffer.c:63
void bytebuffer_append_byte(bytebuffer_t *s, const uint8_t val)
Writes a uint8_t value to the buffer.
Definition: bytebuffer.c:145
#define MULTIPOINTTYPE
Definition: liblwgeom.h:87
static uint8_t lwgeom_twkb_type(const LWGEOM *geom)
Definition: lwout_twkb.c:31
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
bytebuffer_t * bytebuffer_create_with_size(size_t size)
Allocate a new bytebuffer_t.
Definition: bytebuffer.c:44
static void write_bbox(TWKB_STATE *ts, int ndims)
Writes the bbox in varints in the form: xmin, xdelta, ymin, ydelta.
Definition: lwout_twkb.c:89
static int lwpoly_to_twkb_buf(const LWPOLY *poly, TWKB_GLOBALS *globals, TWKB_STATE *ts)
Definition: lwout_twkb.c:257
#define HIGHER_DIM_SET_HASZ(flag, bool)
Definition: lwout_twkb.h:69
static int lwline_to_twkb_buf(const LWLINE *line, TWKB_GLOBALS *globals, TWKB_STATE *ts)
Definition: lwout_twkb.c:244
POINTARRAY * point
Definition: liblwgeom.h:410
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:849
int lwgeom_ndims(const LWGEOM *geom)
Return the number of dimensions (2, 3, 4) in a geometry.
Definition: lwgeom.c:863
int8_t prec_z
Definition: lwout_twkb.h:80
uint8_t zigzag8(int8_t val)
Definition: varint.c:183
bytebuffer_t * header_buf
Definition: lwout_twkb.h:88
static size_t sizeof_bbox(TWKB_STATE *ts, int ndims)
Calculates the size of the bbox in varints in the form: xmin, xdelta, ymin, ydelta.
Definition: lwout_twkb.c:72
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216
int64_t accum_rels[MAX_N_DIMS]
Definition: lwout_twkb.h:95
const int64_t * idlist
Definition: lwout_twkb.h:92
#define WKB_POLYGON_TYPE
#define FIRST_BYTE_SET_SIZES(flag, bool)
Definition: lwout_twkb.h:54
void bytebuffer_append_uvarint(bytebuffer_t *b, const uint64_t val)
Writes a unsigned varInt to the buffer.
Definition: bytebuffer.c:200
uint8_t flags
Definition: liblwgeom.h:368
uint8_t * lwgeom_to_twkb_with_idlist(const LWGEOM *geom, int64_t *idlist, uint8_t variant, int8_t precision_xy, int8_t precision_z, int8_t precision_m, size_t *twkb_size)
Convert LWGEOM to a char* in TWKB format.
Definition: lwout_twkb.c:560
LWGEOM ** geoms
Definition: liblwgeom.h:508
static int ptarray_to_twkb_buf(const POINTARRAY *pa, TWKB_GLOBALS *globals, TWKB_STATE *ts, int register_npoints, int minpoints)
Stores a pointarray as varints in the buffer , controls whether an npoints entry is added to the buff...
Definition: lwout_twkb.c:106
int8_t prec_xy
Definition: lwout_twkb.h:79
uint8_t * getPoint_internal(const POINTARRAY *pa, int n)
Definition: ptarray.c:1706
POINTARRAY ** rings
Definition: liblwgeom.h:456
int nrings
Definition: liblwgeom.h:454
#define WKB_POINT_TYPE
Well-Known Binary (WKB) Geometry Types.
#define FIRST_BYTE_SET_BBOXES(flag, bool)
Header true/false flags.
Definition: lwout_twkb.h:53
int8_t prec_m
Definition: lwout_twkb.h:81
#define WKB_LINESTRING_TYPE
#define TYPE_PREC_SET_PREC(flag, prec)
Definition: lwout_twkb.h:67
#define WKB_MULTIPOLYGON_TYPE
#define MAX_N_DIMS
Definition: lwout_twkb.h:43
#define HIGHER_DIM_SET_PRECZ(flag, prec)
Definition: lwout_twkb.h:72
#define WKB_MULTILINESTRING_TYPE
void bytebuffer_destroy(bytebuffer_t *s)
Free the bytebuffer_t and all memory managed within it.
Definition: bytebuffer.c:75
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:89
#define TWKB_BBOX
Definition: liblwgeom.h:2063
#define FIRST_BYTE_SET_EXTENDED(flag, bool)
Definition: lwout_twkb.h:56
#define WKB_MULTIPOINT_TYPE
static int lwgeom_write_to_buffer(const LWGEOM *geom, TWKB_GLOBALS *globals, TWKB_STATE *parent_state)
Definition: lwout_twkb.c:407
int64_t bbox_max[MAX_N_DIMS]
Definition: lwout_twkb.h:94
size_t varint_u64_encode_buf(uint64_t val, uint8_t *buf)
Definition: varint.c:76
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:84
static int lwpoint_to_twkb_buf(const LWPOINT *pt, TWKB_GLOBALS *globals, TWKB_STATE *ts)
Definition: lwout_twkb.c:231
#define TWKB_SIZE
Definition: liblwgeom.h:2064
uint8_t type
Definition: liblwgeom.h:395
type
Definition: ovdump.py:41
size_t varint_s64_encode_buf(int64_t val, uint8_t *buf)
Definition: varint.c:89
#define HIGHER_DIM_SET_HASM(flag, bool)
Definition: lwout_twkb.h:70
uint8_t * buf_start
Definition: bytebuffer.h:40
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1310
bytebuffer_t * geom_buf
Definition: lwout_twkb.h:89
#define MULTILINETYPE
Definition: liblwgeom.h:88
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:151
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:856
#define FIRST_BYTE_SET_IDLIST(flag, bool)
Definition: lwout_twkb.h:55
void bytebuffer_append_varint(bytebuffer_t *b, const int64_t val)
Writes a signed varInt to the buffer.
Definition: bytebuffer.c:187
size_t bytebuffer_getlength(bytebuffer_t *s)
Returns the length of the current buffer.
Definition: bytebuffer.c:325
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:102
static int lwcollection_to_twkb_buf(const LWCOLLECTION *col, TWKB_GLOBALS *globals, TWKB_STATE *ts)
Definition: lwout_twkb.c:329
#define COLLECTIONTYPE
Definition: liblwgeom.h:90
#define WKB_GEOMETRYCOLLECTION_TYPE
int64_t bbox_min[MAX_N_DIMS]
Definition: lwout_twkb.h:93
uint8_t * lwgeom_to_twkb(const LWGEOM *geom, uint8_t variant, int8_t precision_xy, int8_t precision_z, int8_t precision_m, size_t *twkb_size)
Definition: lwout_twkb.c:608
POINTARRAY * points
Definition: liblwgeom.h:421