PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches
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#include "utils/builtins.h"
30
31#include "../postgis_config.h"
32#include "lwgeom_pg.h"
33#include "liblwgeom.h"
34#include "liblwgeom_internal.h"/* for decode_geohash_bbox */
35
36Datum box2d_from_geohash(PG_FUNCTION_ARGS);
37Datum point_from_geohash(PG_FUNCTION_ARGS);
38
39static void geohash_lwpgerror(char *msg, __attribute__((__unused__)) int error_code)
40{
41 POSTGIS_DEBUGF(3, "ST_Box2dFromGeoHash ERROR %i", error_code);
42 lwpgerror("%s", msg);
43}
44
45
46static GBOX*
47parse_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(lwflags(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
77Datum 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 = text_to_cstring(geohash_input);
96
97 box = parse_geohash(geohash, precision);
98
99 PG_RETURN_POINTER(box);
100}
101
103Datum 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 = text_to_cstring(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}
static uint8_t precision
Definition cu_in_twkb.c:25
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition cu_print.c:267
GBOX * gbox_new(lwflags_t flags)
Create a new gbox with the dimensionality indicated by the flags.
Definition gbox.c:32
void lwfree(void *mem)
Definition lwutil.c:248
LWPOINT * lwpoint_make2d(int32_t srid, double x, double y)
Definition lwpoint.c:163
#define __attribute__(x)
Definition liblwgeom.h:228
lwflags_t lwflags(int hasz, int hasm, int geodetic)
Construct a new flags bitmask.
Definition lwutil.c:477
#define SRID_UNKNOWN
Unknown SRID value.
Definition liblwgeom.h:215
This library is the generic geometry handling section of PostGIS.
void decode_geohash_bbox(char *geohash, double *lat, double *lon, int precision)
Datum box2d_from_geohash(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(box2d_from_geohash)
static void geohash_lwpgerror(char *msg, __attribute__((__unused__)) int error_code)
static GBOX * parse_geohash(char *geohash, int precision)
Datum point_from_geohash(PG_FUNCTION_ARGS)
double ymax
Definition liblwgeom.h:357
double xmax
Definition liblwgeom.h:355
double ymin
Definition liblwgeom.h:356
double xmin
Definition liblwgeom.h:354