From: Daniel Silverstone Date: Sat, 8 Nov 2008 19:18:22 +0000 (+0000) Subject: Support building a postgresql binding for maptile_for_point X-Git-Tag: live~8131^2~191 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/ee656dd1f3ee556e9c6793a70781d4346abfe01e?hp=43b3998657c9807a68e71609c568748c8337b95e Support building a postgresql binding for maptile_for_point --- diff --git a/db/functions/Makefile b/db/functions/Makefile index 7652862fa..1bdddce71 100644 --- a/db/functions/Makefile +++ b/db/functions/Makefile @@ -1,5 +1,7 @@ QTDIR=../../lib/quad_tile +PGSQLINC=/usr/include/postgresql/8.3/server/ + OS=$(shell uname -s) ifeq (${OS},Darwin) LDFLAGS=-bundle @@ -7,11 +9,22 @@ else LDFLAGS=-shared endif -libmyosm.so: quadtile.o maptile.o - cc ${LDFLAGS} -o libmyosm.so quadtile.o maptile.o +all: libmyosm.so libpgosm.so + +clean: + $(RM) *.so *.o + +libmyosm.so: quadtile.o maptile-mysql.o + cc ${LDFLAGS} -o libmyosm.so quadtile.o maptile-mysql.o + +libpgosm.so: maptile-pgsql.o + cc ${LDFLAGS} -o libpgosm.so maptile-pgsql.o quadtile.o: quadtile.c ${QTDIR}/quad_tile.h cc `mysql_config --include` -I${QTDIR} -fPIC -O3 -c -o quadtile.o quadtile.c -maptile.o: maptile.c - cc `mysql_config --include` -fPIC -O3 -c -o maptile.o maptile.c +maptile-mysql.o: maptile.c + cc `mysql_config --include` -fPIC -O3 -DUSE_MYSQL -c -o maptile-mysql.o maptile.c + +maptile-pgsql.o: maptile.c + cc -I${PGSQLINC} -O3 -fPIC -DUSE_PGSQL -c -o maptile-pgsql.o maptile.c \ No newline at end of file diff --git a/db/functions/maptile.c b/db/functions/maptile.c index f96f9c23e..2c15d2b41 100644 --- a/db/functions/maptile.c +++ b/db/functions/maptile.c @@ -1,3 +1,31 @@ +#ifndef USE_MYSQL +#ifndef USE_PGSQL +#error One of USE_MYSQL or USE_PGSQL must be defined +#endif +#endif + +#include + +/* 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 #include #include @@ -27,13 +55,29 @@ 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; + + return internal_maptile_for_point(lat, lon, zoom); +} +#endif - 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); +#ifdef USE_PGSQL +#include +#include - return (x << zoom) | y; +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); + +#ifdef PG_MODULE_MAGIC +PG_MODULE_MAGIC; +#endif + +#endif