PostGIS  2.2.8dev-r@@SVN_REVISION@@
lwgeom_in_geohash.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * Copyright 2012 J Smith <dark.panda@gmail.com>
5  *
6  * This is free software; you can redistribute and/or modify it under
7  * the terms of the GNU General Public Licence. See the COPYING file.
8  *
9  **********************************************************************/
10 
11 #include <assert.h>
12 
13 #include "postgres.h"
14 
15 #include "../postgis_config.h"
16 #include "lwgeom_pg.h"
17 #include "liblwgeom.h"
18 #include "liblwgeom_internal.h"/* for decode_geohash_bbox */
19 
20 Datum box2d_from_geohash(PG_FUNCTION_ARGS);
21 Datum point_from_geohash(PG_FUNCTION_ARGS);
22 
23 static void geohash_lwpgerror(char *msg, int error_code)
24 {
25  POSTGIS_DEBUGF(3, "ST_Box2dFromGeoHash ERROR %i", error_code);
26  lwpgerror("%s", msg);
27 }
28 
29 #include "lwgeom_export.h"
30 
31 static GBOX*
32 parse_geohash(char *geohash, int precision)
33 {
34  GBOX *box = NULL;
35  double lat[2], lon[2];
36 
37  POSTGIS_DEBUG(2, "parse_geohash called.");
38 
39  if (NULL == geohash)
40  {
41  geohash_lwpgerror("invalid GeoHash representation", 2);
42  }
43 
44  decode_geohash_bbox(geohash, lat, lon, precision);
45 
46  POSTGIS_DEBUGF(2, "ST_Box2dFromGeoHash sw: %.20f, %.20f", lon[0], lat[0]);
47  POSTGIS_DEBUGF(2, "ST_Box2dFromGeoHash ne: %.20f, %.20f", lon[1], lat[1]);
48 
49  box = gbox_new(gflags(0, 0, 1));
50 
51  box->xmin = lon[0];
52  box->ymin = lat[0];
53 
54  box->xmax = lon[1];
55  box->ymax = lat[1];
56 
57  POSTGIS_DEBUG(2, "parse_geohash finished.");
58  return box;
59 }
60 
62 Datum box2d_from_geohash(PG_FUNCTION_ARGS)
63 {
64  GBOX *box = NULL;
65  text *geohash_input = NULL;
66  char *geohash = NULL;
67  int precision = -1;
68 
69  if (PG_ARGISNULL(0))
70  {
71  PG_RETURN_NULL();
72  }
73 
74  if (!PG_ARGISNULL(1))
75  {
76  precision = PG_GETARG_INT32(1);
77  }
78 
79  geohash_input = PG_GETARG_TEXT_P(0);
80  geohash = text2cstring(geohash_input);
81 
82  box = parse_geohash(geohash, precision);
83 
84  PG_RETURN_POINTER(box);
85 }
86 
88 Datum point_from_geohash(PG_FUNCTION_ARGS)
89 {
90  GBOX *box = NULL;
91  LWPOINT *point = NULL;
92  GSERIALIZED *result = NULL;
93  text *geohash_input = NULL;
94  char *geohash = NULL;
95  double lon, lat;
96  int precision = -1;
97 
98  if (PG_ARGISNULL(0))
99  {
100  PG_RETURN_NULL();
101  }
102 
103  if (!PG_ARGISNULL(1))
104  {
105  precision = PG_GETARG_INT32(1);
106  }
107 
108  geohash_input = PG_GETARG_TEXT_P(0);
109  geohash = text2cstring(geohash_input);
110 
111  box = parse_geohash(geohash, precision);
112 
113  lon = box->xmin + (box->xmax - box->xmin) / 2;
114  lat = box->ymin + (box->ymax - box->ymin) / 2;
115 
116  point = lwpoint_make2d(SRID_UNKNOWN, lon, lat);
117  result = geometry_serialize((LWGEOM *) point);
118 
119  lwfree(box);
120 
121  PG_RETURN_POINTER(result);
122 }
GBOX * gbox_new(uint8_t flags)
Create a new gbox with the dimensionality indicated by the flags.
Definition: g_box.c:28
void lwfree(void *mem)
Definition: lwutil.c:214
LWPOINT * lwpoint_make2d(int srid, double x, double y)
Definition: lwpoint.c:132
double xmax
Definition: liblwgeom.h:277
void decode_geohash_bbox(char *geohash, double *lat, double *lon, int precision)
Definition: lwalgorithm.c:689
Datum point_from_geohash(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(box2d_from_geohash)
double ymin
Definition: liblwgeom.h:278
double xmin
Definition: liblwgeom.h:276
static void geohash_lwpgerror(char *msg, int error_code)
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:172
char * text2cstring(const text *textptr)
uint8_t precision
Definition: cu_in_twkb.c:25
double ymax
Definition: liblwgeom.h:279
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:130
static GBOX * parse_geohash(char *geohash, int precision)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
This library is the generic geometry handling section of PostGIS.