PostGIS  2.2.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 * Copyright 2014 Kashif Rasul <kashif.rasul@gmail.com> and
7 * Shoaib Burq <saburq@gmail.com>
8 *
9 * This is free software; you can redistribute and/or modify it under
10 * the terms of the GNU General Public Licence. See the COPYING file.
11 *
12 **********************************************************************/
13 
14 #include <assert.h>
15 #include <string.h>
16 #include "liblwgeom.h"
17 #include "../postgis_config.h"
18 
19 LWGEOM*
20 lwgeom_from_encoded_polyline(const char *encodedpolyline, int precision)
21 {
22  LWGEOM *geom = NULL;
23  POINTARRAY *pa = NULL;
24  int length = strlen(encodedpolyline);
25  int idx = 0;
26  double scale = pow(10,precision);
27 
28  float latitude = 0.0f;
29  float longitude = 0.0f;
30 
32 
33  while (idx < length) {
34  POINT4D pt;
35  char byte = 0;
36 
37  int res = 0;
38  char shift = 0;
39  do {
40  byte = encodedpolyline[idx++] - 63;
41  res |= (byte & 0x1F) << shift;
42  shift += 5;
43  } while (byte >= 0x20);
44  float deltaLat = ((res & 1) ? ~(res >> 1) : (res >> 1));
45  latitude += deltaLat;
46 
47  shift = 0;
48  res = 0;
49  do {
50  byte = encodedpolyline[idx++] - 63;
51  res |= (byte & 0x1F) << shift;
52  shift += 5;
53  } while (byte >= 0x20);
54  float deltaLon = ((res & 1) ? ~(res >> 1) : (res >> 1));
55  longitude += deltaLon;
56 
57  pt.x = longitude/scale;
58  pt.y = latitude/scale;
59  pt.m = pt.z = 0.0;
61  }
62 
63  geom = (LWGEOM *)lwline_construct(4326, NULL, pa);
64  lwgeom_add_bbox(geom);
65 
66  return geom;
67 }
double x
Definition: liblwgeom.h:336
tuple res
Definition: window.py:78
double m
Definition: liblwgeom.h:336
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_TRUE, then a duplicate point will not be added.
Definition: ptarray.c:156
#define LW_FALSE
Definition: liblwgeom.h:62
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:29
uint8_t precision
Definition: cu_in_twkb.c:25
double z
Definition: liblwgeom.h:336
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:599
double y
Definition: liblwgeom.h:336
This library is the generic geometry handling section of PostGIS.