]> git.openstreetmap.org Git - rails.git/blobdiff - db/functions/maptile.c
Add support for logical deletion of diary entries and comments.
[rails.git] / db / functions / maptile.c
index f96f9c23e5e052078c90c2348ae70852b41bd2d2..ed83bcce29f1f88605c59163d2b470f42ccc4ad3 100644 (file)
@@ -1,3 +1,31 @@
+#ifndef USE_MYSQL
+#ifndef USE_PGSQL
+#error One of USE_MYSQL or USE_PGSQL must be defined
+#endif
+#endif
+
+#include <math.h>
+
+/* The real maptile-for-point functionality is here */
+
+static long long internal_maptile_for_point(double lat, double lon, long long zoom)
+{
+   double       scale = pow(2, zoom);
+   double       r_per_d = M_PI / 180;
+   unsigned int x;
+   unsigned int y;
+
+   x = floor((lon + 180.0) * scale / 360.0);
+   y = floor((1 - log(tan(lat * r_per_d) + 1.0 / cos(lat * r_per_d)) / M_PI) * scale / 2.0);
+
+   return (x << zoom) | y;
+}
+
+#ifdef USE_MYSQL
+#ifdef USE_PGSQL
+#error ONLY one of USE_MYSQL and USE_PGSQL should be defined
+#endif
+
 #include <my_global.h>
 #include <my_sys.h>
 #include <m_string.h>
@@ -27,13 +55,43 @@ long long maptile_for_point(UDF_INIT *initid, UDF_ARGS *args, char *is_null, cha
    double       lat = *(long long *)args->args[0] / 10000000.0;
    double       lon = *(long long *)args->args[1] / 10000000.0;
    long long    zoom = *(long long *)args->args[2];
-   double       scale = pow(2, zoom);
-   double       r_per_d = M_PI / 180;
-   unsigned int x;
-   unsigned int y;
 
-   x = floor((lon + 180.0) * scale / 360.0);
-   y = floor((1 - log(tan(lat * r_per_d) + 1.0 / cos(lat * r_per_d)) / M_PI) * scale / 2.0);
+   return internal_maptile_for_point(lat, lon, zoom);
+}
+#endif
 
-   return (x << zoom) | y;
+#ifdef USE_PGSQL
+#ifdef USE_MYSQL
+#error ONLY one of USE_MYSQL and USE_PGSQL should be defined
+#endif
+
+#include <postgres.h>
+#include <fmgr.h>
+
+Datum
+maptile_for_point(PG_FUNCTION_ARGS)
+{
+  double lat = PG_GETARG_INT64(0) / 10000000.0;
+  double lon = PG_GETARG_INT64(1) / 10000000.0;
+  int zoom = PG_GETARG_INT32(2);
+
+  PG_RETURN_INT32(internal_maptile_for_point(lat, lon, zoom));
 }
+
+PG_FUNCTION_INFO_V1(maptile_for_point);
+
+/*
+ * To bind this into PGSQL, try something like:
+ *
+ * CREATE FUNCTION maptile_for_point(int8, int8, int4) RETURNS int4
+ *  AS '/path/to/rails-port/db/functions/libpgosm', 'maptile_for_point'
+ *  LANGUAGE C STRICT;
+ *
+ * (without all the *s)
+ */
+
+#ifdef PG_MODULE_MAGIC
+PG_MODULE_MAGIC;
+#endif
+
+#endif