PostGIS  2.5.0dev-r@@SVN_REVISION@@
lwgeom_in_geohash.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 2012 J Smith <dark.panda@gmail.com>
22  *
23  **********************************************************************/
24 
25 
26 #include <assert.h>
27 
28 #include "postgres.h"
29 
30 #include "../postgis_config.h"
31 #include "lwgeom_pg.h"
32 #include "liblwgeom.h"
33 #include "liblwgeom_internal.h"/* for decode_geohash_bbox */
34 
35 Datum box2d_from_geohash(PG_FUNCTION_ARGS);
36 Datum point_from_geohash(PG_FUNCTION_ARGS);
37 
38 static void geohash_lwpgerror(char *msg, int error_code)
39 {
40  POSTGIS_DEBUGF(3, "ST_Box2dFromGeoHash ERROR %i", error_code);
41  lwpgerror("%s", msg);
42 }
43 
44 #include "lwgeom_export.h"
45 
46 static GBOX*
47 parse_geohash(char *geohash, int precision)
48 {
49  GBOX *box = NULL;
50  double lat[2], lon[2];
51 
52  POSTGIS_DEBUG(2, "parse_geohash called.");
53 
54  if (NULL == geohash)
55  {
56  geohash_lwpgerror("invalid GeoHash representation", 2);
57  }
58 
59  decode_geohash_bbox(geohash, lat, lon, precision);
60 
61  POSTGIS_DEBUGF(2, "ST_Box2dFromGeoHash sw: %.20f, %.20f", lon[0], lat[0]);
62  POSTGIS_DEBUGF(2, "ST_Box2dFromGeoHash ne: %.20f, %.20f", lon[1], lat[1]);
63 
64  box = gbox_new(gflags(0, 0, 1));
65 
66  box->xmin = lon[0];
67  box->ymin = lat[0];
68 
69  box->xmax = lon[1];
70  box->ymax = lat[1];
71 
72  POSTGIS_DEBUG(2, "parse_geohash finished.");
73  return box;
74 }
75 
77 Datum box2d_from_geohash(PG_FUNCTION_ARGS)
78 {
79  GBOX *box = NULL;
80  text *geohash_input = NULL;
81  char *geohash = NULL;
82  int precision = -1;
83 
84  if (PG_ARGISNULL(0))
85  {
86  PG_RETURN_NULL();
87  }
88 
89  if (!PG_ARGISNULL(1))
90  {
91  precision = PG_GETARG_INT32(1);
92  }
93 
94  geohash_input = PG_GETARG_TEXT_P(0);
95  geohash = text2cstring(geohash_input);
96 
97  box = parse_geohash(geohash, precision);
98 
99  PG_RETURN_POINTER(box);
100 }
101 
103 Datum point_from_geohash(PG_FUNCTION_ARGS)
104 {
105  GBOX *box = NULL;
106  LWPOINT *point = NULL;
107  GSERIALIZED *result = NULL;
108  text *geohash_input = NULL;
109  char *geohash = NULL;
110  double lon, lat;
111  int precision = -1;
112 
113  if (PG_ARGISNULL(0))
114  {
115  PG_RETURN_NULL();
116  }
117 
118  if (!PG_ARGISNULL(1))
119  {
120  precision = PG_GETARG_INT32(1);
121  }
122 
123  geohash_input = PG_GETARG_TEXT_P(0);
124  geohash = text2cstring(geohash_input);
125 
126  box = parse_geohash(geohash, precision);
127 
128  lon = box->xmin + (box->xmax - box->xmin) / 2;
129  lat = box->ymin + (box->ymax - box->ymin) / 2;
130 
131  point = lwpoint_make2d(SRID_UNKNOWN, lon, lat);
132  result = geometry_serialize((LWGEOM *) point);
133 
134  lwfree(box);
135 
136  PG_RETURN_POINTER(result);
137 }
GBOX * gbox_new(uint8_t flags)
Create a new gbox with the dimensionality indicated by the flags.
Definition: g_box.c:39
void lwfree(void *mem)
Definition: lwutil.c:244
LWPOINT * lwpoint_make2d(int srid, double x, double y)
Definition: lwpoint.c:163
double xmax
Definition: liblwgeom.h:292
void decode_geohash_bbox(char *geohash, double *lat, double *lon, int precision)
Definition: lwalgorithm.c:703
Datum point_from_geohash(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(box2d_from_geohash)
double ymin
Definition: liblwgeom.h:293
double xmin
Definition: liblwgeom.h:291
static void geohash_lwpgerror(char *msg, int error_code)
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:187
char * text2cstring(const text *textptr)
uint8_t precision
Definition: cu_in_twkb.c:25
double ymax
Definition: liblwgeom.h:294
Datum box2d_from_geohash(PG_FUNCTION_ARGS)
uint8_t gflags(int hasz, int hasm, int geodetic)
Construct a new flags char.
Definition: g_util.c:145
static GBOX * parse_geohash(char *geohash, int precision)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
This library is the generic geometry handling section of PostGIS.