PostGIS  2.3.8dev-r@@SVN_REVISION@@
lwin_encoded_polyline.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 2014 Kashif Rasul <kashif.rasul@gmail.com> and
22  *
23  **********************************************************************/
24 
25 
26 #include <assert.h>
27 #include <string.h>
28 #include "liblwgeom.h"
29 #include "../postgis_config.h"
30 
31 LWGEOM*
32 lwgeom_from_encoded_polyline(const char *encodedpolyline, int precision)
33 {
34  LWGEOM *geom = NULL;
35  POINTARRAY *pa = NULL;
36  int length = strlen(encodedpolyline);
37  int idx = 0;
38  double scale = pow(10,precision);
39 
40  float latitude = 0.0f;
41  float longitude = 0.0f;
42 
44 
45  while (idx < length) {
46  POINT4D pt;
47  char byte = 0;
48 
49  int res = 0;
50  char shift = 0;
51  do {
52  byte = encodedpolyline[idx++] - 63;
53  res |= (byte & 0x1F) << shift;
54  shift += 5;
55  } while (byte >= 0x20);
56  float deltaLat = ((res & 1) ? ~(res >> 1) : (res >> 1));
57  latitude += deltaLat;
58 
59  shift = 0;
60  res = 0;
61  do {
62  byte = encodedpolyline[idx++] - 63;
63  res |= (byte & 0x1F) << shift;
64  shift += 5;
65  } while (byte >= 0x20);
66  float deltaLon = ((res & 1) ? ~(res >> 1) : (res >> 1));
67  longitude += deltaLon;
68 
69  pt.x = longitude/scale;
70  pt.y = latitude/scale;
71  pt.m = pt.z = 0.0;
73  }
74 
75  geom = (LWGEOM *)lwline_construct(4326, NULL, pa);
76  lwgeom_add_bbox(geom);
77 
78  return geom;
79 }
double x
Definition: liblwgeom.h:351
tuple res
Definition: window.py:78
double m
Definition: liblwgeom.h:351
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
LWGEOM * lwgeom_from_encoded_polyline(const char *encodedpolyline, int precision)
Create an LWGEOM object from an Encoded Polyline representation.
int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int allow_duplicates)
Append a point to the end of an existing POINTARRAY If allow_duplicate is LW_FALSE, then a duplicate point will not be added.
Definition: ptarray.c:156
#define LW_FALSE
Definition: liblwgeom.h:76
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
uint8_t precision
Definition: cu_in_twkb.c:25
double z
Definition: liblwgeom.h:351
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:612
double y
Definition: liblwgeom.h:351
This library is the generic geometry handling section of PostGIS.