PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ pointarray_to_encoded_polyline()

static lwvarlena_t * pointarray_to_encoded_polyline ( const POINTARRAY pa,
int  precision 
)
static

Definition at line 68 of file lwout_encoded_polyline.c.

69 {
70  uint32_t i;
71  const POINT2D* prevPoint;
72  int* delta;
73  stringbuffer_t* sb;
74  double scale = pow(10, precision);
75 
76  /* Empty input is empty string */
77  if (pa->npoints == 0)
78  {
81  return v;
82  }
83 
84  delta = lwalloc(2 * sizeof(int) * pa->npoints);
85 
86  /* Take the double value and multiply it by 1x10^precision, rounding the
87  * result */
88  prevPoint = getPoint2d_cp(pa, 0);
89  delta[0] = round(prevPoint->y * scale);
90  delta[1] = round(prevPoint->x * scale);
91 
92  /* Points only include the offset from the previous point */
93  for (i = 1; i < pa->npoints; i++)
94  {
95  const POINT2D* point = getPoint2d_cp(pa, i);
96  delta[2 * i] = round(point->y * scale) - round(prevPoint->y * scale);
97  delta[(2 * i) + 1] =
98  round(point->x * scale) - round(prevPoint->x * scale);
99  prevPoint = point;
100  }
101 
102  /* value to binary: a negative value must be calculated using its two's
103  * complement */
104  for (i = 0; i < pa->npoints * 2; i++)
105  {
106  /* Multiply by 2 for a signed left shift */
107  delta[i] *= 2;
108  /* if value is negative, invert this encoding */
109  if (delta[i] < 0) {
110  delta[i] = ~(delta[i]);
111  }
112  }
113 
114  sb = stringbuffer_create();
115  for (i = 0; i < pa->npoints * 2; i++)
116  {
117  int numberToEncode = delta[i];
118 
119  while (numberToEncode >= 0x20)
120  {
121  /* Place the 5-bit chunks into reverse order or
122  each value with 0x20 if another bit chunk follows and add 63*/
123  int nextValue = (0x20 | (numberToEncode & 0x1f)) + 63;
124  stringbuffer_aprintf(sb, "%c", (char)nextValue);
125 
126  /* Break the binary value out into 5-bit chunks */
127  numberToEncode >>= 5;
128  }
129 
130  numberToEncode += 63;
131  stringbuffer_aprintf(sb, "%c", (char)numberToEncode);
132  }
133 
134  lwfree(delta);
137 
138  return v;
139 }
static uint8_t precision
Definition: cu_in_twkb.c:25
#define LWVARHDRSZ
Definition: liblwgeom.h:311
void lwfree(void *mem)
Definition: lwutil.c:242
#define LWSIZE_SET(varsize, len)
Definition: liblwgeom.h:325
void * lwalloc(size_t size)
Definition: lwutil.c:227
static const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition: lwinline.h:101
int stringbuffer_aprintf(stringbuffer_t *s, const char *fmt,...)
Appends a formatted string to the current string buffer, using the format and argument list provided.
Definition: stringbuffer.c:247
lwvarlena_t * stringbuffer_getvarlenacopy(stringbuffer_t *s)
Definition: stringbuffer.c:151
stringbuffer_t * stringbuffer_create(void)
Allocate a new stringbuffer_t.
Definition: stringbuffer.c:33
void stringbuffer_destroy(stringbuffer_t *s)
Free the stringbuffer_t and all memory managed within it.
Definition: stringbuffer.c:85
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390
uint32_t npoints
Definition: liblwgeom.h:427
uint32_t size
Definition: liblwgeom.h:307

References getPoint2d_cp(), lwalloc(), lwfree(), LWSIZE_SET, LWVARHDRSZ, POINTARRAY::npoints, precision, lwvarlena_t::size, stringbuffer_aprintf(), stringbuffer_create(), stringbuffer_destroy(), stringbuffer_getvarlenacopy(), POINT2D::x, and POINT2D::y.

Referenced by lwline_to_encoded_polyline().

Here is the call graph for this function:
Here is the caller graph for this function: