PostGIS  2.5.0dev-r@@SVN_REVISION@@
static char * pointarray_to_encoded_polyline ( const POINTARRAY pa,
int  precision 
)
static

Definition at line 68 of file lwout_encoded_polyline.c.

References getPoint2d_cp(), lwalloc(), lwfree(), POINTARRAY::npoints, stringbuffer_aprintf(), stringbuffer_create(), stringbuffer_destroy(), stringbuffer_getstringcopy(), POINT2D::x, and POINT2D::y.

Referenced by lwline_to_encoded_polyline().

69 {
70  uint32_t i;
71  const POINT2D* prevPoint;
72  int* delta;
73  char* encoded_polyline = NULL;
74  stringbuffer_t* sb;
75  double scale = pow(10, precision);
76 
77  /* Empty input is empty string */
78  if (pa->npoints == 0) {
79  encoded_polyline = lwalloc(1 * sizeof(char));
80  encoded_polyline[0] = 0;
81  return encoded_polyline;
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);
135  encoded_polyline = stringbuffer_getstringcopy(sb);
137 
138  return encoded_polyline;
139 }
stringbuffer_t * stringbuffer_create(void)
Allocate a new stringbuffer_t.
Definition: stringbuffer.c:35
void lwfree(void *mem)
Definition: lwutil.c:244
char * stringbuffer_getstringcopy(stringbuffer_t *s)
Returns a newly allocated string large enough to contain the current state of the string...
Definition: stringbuffer.c:160
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:253
unsigned int uint32_t
Definition: uthash.h:78
double x
Definition: liblwgeom.h:327
uint8_t precision
Definition: cu_in_twkb.c:25
double y
Definition: liblwgeom.h:327
void stringbuffer_destroy(stringbuffer_t *s)
Free the stringbuffer_t and all memory managed within it.
Definition: stringbuffer.c:78
void * lwalloc(size_t size)
Definition: lwutil.c:229
const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from...
Definition: lwgeom_api.c:364
uint32_t npoints
Definition: liblwgeom.h:370

Here is the call graph for this function:

Here is the caller graph for this function: