PostGIS  3.0.6dev-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 <math.h>
29 
30 #include "liblwgeom.h"
31 #include "../postgis_config.h"
32 
33 LWGEOM*
34 lwgeom_from_encoded_polyline(const char *encodedpolyline, int precision)
35 {
36  LWGEOM *geom = NULL;
37  POINTARRAY *pa = NULL;
38  int length = strlen(encodedpolyline);
39  int idx = 0;
40  double scale = pow(10,precision);
41 
42  float latitude = 0.0f;
43  float longitude = 0.0f;
44 
46 
47  while (idx < length) {
48  POINT4D pt;
49  char byte = 0;
50 
51  int res = 0;
52  char shift = 0;
53  do {
54  byte = encodedpolyline[idx++] - 63;
55  res |= (byte & 0x1F) << shift;
56  shift += 5;
57  } while (byte >= 0x20);
58  float deltaLat = ((res & 1) ? ~(res >> 1) : (res >> 1));
59  latitude += deltaLat;
60 
61  shift = 0;
62  res = 0;
63  do {
64  byte = encodedpolyline[idx++] - 63;
65  res |= (byte & 0x1F) << shift;
66  shift += 5;
67  } while (byte >= 0x20);
68  float deltaLon = ((res & 1) ? ~(res >> 1) : (res >> 1));
69  longitude += deltaLon;
70 
71  pt.x = longitude/scale;
72  pt.y = latitude/scale;
73  pt.m = pt.z = 0.0;
75  }
76 
77  geom = (LWGEOM *)lwline_construct(4326, NULL, pa);
78  lwgeom_add_bbox(geom);
79 
80  return geom;
81 }
static uint8_t precision
Definition: cu_in_twkb.c:25
#define LW_FALSE
Definition: liblwgeom.h:108
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:59
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,...
Definition: ptarray.c:147
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:677
This library is the generic geometry handling section of PostGIS.
LWGEOM * lwgeom_from_encoded_polyline(const char *encodedpolyline, int precision)
Create an LWGEOM object from an Encoded Polyline representation.
tuple res
Definition: window.py:79
double m
Definition: liblwgeom.h:400
double x
Definition: liblwgeom.h:400
double z
Definition: liblwgeom.h:400
double y
Definition: liblwgeom.h:400