]> git.openstreetmap.org Git - rails.git/blob - db/functions/quadtile.c
Merge branch 'master' of git://git.openstreetmap.org/rails into openstreetbugs
[rails.git] / db / functions / quadtile.c
1 #ifndef USE_MYSQL
2 #ifndef USE_PGSQL
3 #error One of USE_MYSQL or USE_PGSQL must be defined
4 #endif
5 #endif
6
7 #include <math.h>
8 #include <quad_tile.h>
9
10 #ifdef USE_MYSQL
11 #ifdef USE_PGSQL
12 #error ONLY one of USE_MYSQL and USE_PGSQL should be defined
13 #endif
14
15 #include <my_global.h>
16 #include <my_sys.h>
17 #include <m_string.h>
18 #include <mysql.h>
19
20 my_bool tile_for_point_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
21 {
22    if ( args->arg_count != 2 ||
23         args->arg_type[0] != INT_RESULT ||
24         args->arg_type[1] != INT_RESULT )
25    {
26       strcpy( message, "Your tile_for_point arguments are bogus!" );
27       return 1;
28    }
29
30    return 0;
31 }
32
33 void tile_for_point_deinit(UDF_INIT *initid)
34 {
35    return;
36 }
37
38 long long tile_for_point(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
39 {
40    long long lat = *(long long *)args->args[0];
41    long long lon = *(long long *)args->args[1];
42
43    return xy2tile(lon2x(lon / 10000000.0), lat2y(lat / 10000000.0));
44 }
45 #endif
46
47 #ifdef USE_PGSQL
48 #ifdef USE_MYSQL
49 #error ONLY one of USE_MYSQL and USE_PGSQL should be defined
50 #endif
51
52 #include <postgres.h>
53 #include <fmgr.h>
54
55 Datum
56 tile_for_point(PG_FUNCTION_ARGS)
57 {
58   double lat = PG_GETARG_INT32(0) / 10000000.0;
59   double lon = PG_GETARG_INT32(1) / 10000000.0;
60
61   PG_RETURN_INT64(xy2tile(lon2x(lon), lat2y(lat)));
62 }
63
64 PG_FUNCTION_INFO_V1(tile_for_point);
65
66 /*
67  * To bind this into PGSQL, try something like:
68  *
69  * CREATE FUNCTION tile_for_point(int4, int4) RETURNS int8
70  *  AS '/path/to/rails-port/db/functions/libpgosm', 'tile_for_point'
71  *  LANGUAGE C STRICT;
72  *
73  * (without all the *s)
74  */
75
76 #endif