]> git.openstreetmap.org Git - rails.git/blob - db/functions/maptile.c
Tidy up message sensitisation a bit more, and add sensitisation of
[rails.git] / db / functions / maptile.c
1 #include <my_global.h>
2 #include <my_sys.h>
3 #include <m_string.h>
4 #include <mysql.h>
5
6 my_bool maptile_for_point_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
7 {
8    if ( args->arg_count != 3 ||
9         args->arg_type[0] != INT_RESULT ||
10         args->arg_type[1] != INT_RESULT ||
11         args->arg_type[2] != INT_RESULT )
12    {
13       strcpy( message, "Your maptile_for_point arguments are bogus!" );
14       return 1;
15    }
16
17    return 0;
18 }
19
20 void maptile_for_point_deinit(UDF_INIT *initid)
21 {
22    return;
23 }
24
25 long long maptile_for_point(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
26 {
27    double       lat = *(long long *)args->args[0] / 10000000.0;
28    double       lon = *(long long *)args->args[1] / 10000000.0;
29    long long    zoom = *(long long *)args->args[2];
30    double       scale = pow(2, zoom);
31    double       r_per_d = M_PI / 180;
32    unsigned int x;
33    unsigned int y;
34
35    x = floor((lon + 180.0) * scale / 360.0);
36    y = floor((1 - log(tan(lat * r_per_d) + 1.0 / cos(lat * r_per_d)) / M_PI) * scale / 2.0);
37
38    return (x << zoom) | y;
39 }