From 250466e58e229dcebbb32c9888aeb197fd4072d3 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Tue, 9 Oct 2007 22:59:32 +0000 Subject: [PATCH 1/1] Add a maptile_for_point() function for MySQL to work out which slippy map tile a given point lies in. --- db/README | 13 ++----------- db/functions/Makefile | 7 +++++-- db/functions/maptile.c | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 db/functions/maptile.c diff --git a/db/README b/db/README index 03e95fff3..4e705e8fb 100644 --- a/db/README +++ b/db/README @@ -32,11 +32,6 @@ Run this command in the db/functions directory: $ make -The above command should work for linux and most other Unix systems -that use ELF shared objects. For MacOS X you will need to do: - -$ make libquadtile.dylib - Make sure the db/functions directory is on the MySQL server's library path and restart the MySQL server. On linux the easiest way to do this is to create /etc/ld.so.conf.d/osm.conf and place the path to the @@ -50,12 +45,8 @@ $ mysql -u -p openstreetmap (change with appropriate username of administrative user eg. root ) -> create function tile_for_point returns integer soname 'libquadtile.so'; -> exit - -or, for MacOS X: - -> create function tile_for_point returns integer soname 'libquadtile.dylib'; +> create function tile_for_point returns integer soname 'libmyosm.so'; +> create function maptile_for_point returns integer soname 'libmyosm.so'; > exit Creating database skeleton tables diff --git a/db/functions/Makefile b/db/functions/Makefile index 2f2abe525..7652862fa 100644 --- a/db/functions/Makefile +++ b/db/functions/Makefile @@ -7,8 +7,11 @@ else LDFLAGS=-shared endif -libquadtile.so: quadtile.o - cc ${LDFLAGS} -o libquadtile.so quadtile.o +libmyosm.so: quadtile.o maptile.o + cc ${LDFLAGS} -o libmyosm.so quadtile.o maptile.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 diff --git a/db/functions/maptile.c b/db/functions/maptile.c new file mode 100644 index 000000000..f96f9c23e --- /dev/null +++ b/db/functions/maptile.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +my_bool maptile_for_point_init(UDF_INIT *initid, UDF_ARGS *args, char *message) +{ + if ( args->arg_count != 3 || + args->arg_type[0] != INT_RESULT || + args->arg_type[1] != INT_RESULT || + args->arg_type[2] != INT_RESULT ) + { + strcpy( message, "Your maptile_for_point arguments are bogus!" ); + return 1; + } + + return 0; +} + +void maptile_for_point_deinit(UDF_INIT *initid) +{ + return; +} + +long long maptile_for_point(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) +{ + 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 (x << zoom) | y; +} -- 2.43.2