Add a maptile_for_point() function for MySQL to work out which slippy
map tile a given point lies in.
This commit is contained in:
parent
e0fd80c428
commit
250466e58e
3 changed files with 46 additions and 13 deletions
13
db/README
13
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 <uid> -p openstreetmap
|
|||
|
||||
(change <uid> 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
|
||||
|
|
|
@ -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
|
||||
|
|
39
db/functions/maptile.c
Normal file
39
db/functions/maptile.c
Normal file
|
@ -0,0 +1,39 @@
|
|||
#include <my_global.h>
|
||||
#include <my_sys.h>
|
||||
#include <m_string.h>
|
||||
#include <mysql.h>
|
||||
|
||||
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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue