File: lwout_encoded_polyline.c

package info (click to toggle)
postgis 2.3.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 58,660 kB
  • ctags: 10,181
  • sloc: ansic: 132,858; sql: 131,148; xml: 46,460; sh: 4,832; perl: 4,476; makefile: 2,749; python: 1,198; yacc: 442; lex: 131
file content (129 lines) | stat: -rw-r--r-- 4,042 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**********************************************************************
 *
 * PostGIS - Spatial Types for PostgreSQL
 * http://postgis.net
 *
 * PostGIS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * PostGIS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PostGIS.  If not, see <http://www.gnu.org/licenses/>.
 *
 **********************************************************************
 *
* Copyright 2014 Kashif Rasul <kashif.rasul@gmail.com> and
 *
 **********************************************************************/


#include "stringbuffer.h"
#include "liblwgeom_internal.h"

static char * lwline_to_encoded_polyline(const LWLINE*, int precision);
static char * lwmmpoint_to_encoded_polyline(const LWMPOINT*, int precision);
static char * pointarray_to_encoded_polyline(const POINTARRAY*, int precision);

/* takes a GEOMETRY and returns an Encoded Polyline representation */
extern char *
lwgeom_to_encoded_polyline(const LWGEOM *geom, int precision)
{
	int type = geom->type;
	switch (type)
	{
	case LINETYPE:
		return lwline_to_encoded_polyline((LWLINE*)geom, precision);
	case MULTIPOINTTYPE:
		return lwmmpoint_to_encoded_polyline((LWMPOINT*)geom, precision);
	default:
		lwerror("lwgeom_to_encoded_polyline: '%s' geometry type not supported", lwtype_name(type));
		return NULL;
	}
}

static
char * lwline_to_encoded_polyline(const LWLINE *line, int precision)
{
	return pointarray_to_encoded_polyline(line->points, precision);
}

static
char * lwmmpoint_to_encoded_polyline(const LWMPOINT *mpoint, int precision)
{
	LWLINE *line = lwline_from_lwmpoint(mpoint->srid, mpoint);
	char *encoded_polyline = lwline_to_encoded_polyline(line, precision);

	lwline_free(line);
	return encoded_polyline;
}

static
char * pointarray_to_encoded_polyline(const POINTARRAY *pa, int precision)
{
	int i;
	const POINT2D *prevPoint;
	int *delta = lwalloc(2*sizeof(int)*pa->npoints);
	char *encoded_polyline = NULL;
	stringbuffer_t *sb;
	double scale = pow(10,precision);

	/* Take the double value and multiply it by 1x10^percision, rounding the result */
	prevPoint = getPoint2d_cp(pa, 0);
	delta[0] = round(prevPoint->y*scale);
	delta[1] = round(prevPoint->x*scale);

	/*  points only include the offset from the previous point */
	for (i=1; i<pa->npoints; i++)
	{
		const POINT2D *point = getPoint2d_cp(pa, i);
		delta[2*i] = round(point->y*scale) - round(prevPoint->y*scale);
		delta[(2*i)+1] = round(point->x*scale) - round(prevPoint->x*scale);
		prevPoint = point;
	}

	/* value to binary: a negative value must be calculated using its two's complement */
	for (i=0; i<pa->npoints*2; i++)
	{
		/* Left-shift the binary value one bit */
		delta[i] <<= 1;
		/* if value is negative, invert this encoding */
		if (delta[i] < 0) {
			delta[i] = ~(delta[i]);
		}
	}

	sb = stringbuffer_create();
	for (i=0; i<pa->npoints*2; i++)
	{
		int numberToEncode = delta[i];

		while (numberToEncode >= 0x20) {
			/* Place the 5-bit chunks into reverse order or
			 each value with 0x20 if another bit chunk follows and add 63*/
			int nextValue = (0x20 | (numberToEncode & 0x1f)) + 63;
			stringbuffer_aprintf(sb, "%c", (char)nextValue);
			if(92 == nextValue)
				stringbuffer_aprintf(sb, "%c", (char)nextValue);

			/* Break the binary value out into 5-bit chunks */
			numberToEncode >>= 5;
		}

		numberToEncode += 63;
		stringbuffer_aprintf(sb, "%c", (char)numberToEncode);
		if(92 == numberToEncode)
			stringbuffer_aprintf(sb, "%c", (char)numberToEncode);
	}

	lwfree(delta);
	encoded_polyline = stringbuffer_getstringcopy(sb);
	stringbuffer_destroy(sb);

	return encoded_polyline;
}