Added wkb support to MYDDAS

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@2144 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
davidvaz 2008-03-13 17:13:34 +00:00
parent d02bc3de81
commit e59b7fb409
5 changed files with 451 additions and 13 deletions

View File

@ -29,6 +29,7 @@
#include "myddas_structs.h"
#include "myddas_statistics.h"
#endif
#include "myddas_wkb2prolog.h"
#define IS_SQL_INT(FIELD) FIELD == FIELD_TYPE_INT24 || \
FIELD == FIELD_TYPE_LONG || \
@ -40,6 +41,7 @@
FIELD == FIELD_TYPE_DOUBLE || \
FIELD == FIELD_TYPE_FLOAT
#define IS_SQL_GEOMETRY(FIELD) FIELD == FIELD_TYPE_GEOMETRY
static Int null_id = 0;
@ -537,6 +539,11 @@ c_db_my_row(void) {
if (!Yap_unify(head, MkFloatTerm(atof(row[i]))))
continue;
}
else if (IS_SQL_GEOMETRY(field->type))
{
if (!Yap_unify(head, wkb2prolog(row[i])))
continue;
}
else
{
if (!Yap_unify(head, MkAtomTerm(Yap_LookupAtom(row[i]))))

25
MYDDAS/myddas_wkb.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef MYDDAS_WKB_H_
#define MYDDAS_WKB_H_
typedef char byte;
typedef unsigned int uint32;
#define WKBXDR 0
#define WKBNDR 1
#define WKBMINTYPE 1
#define WKBPOINT 1
#define WKBLINESTRING 2
#define WKBPOLYGON 3
#define WKBMULTIPOINT 4
#define WKBMULTILINESTRING 5
#define WKBMULTIPOLYGON 6
#define WKBGEOMETRYCOLLECTION 7
#define WKBMAXTYPE 7
#define WKBGEOMETRY 0
#endif /* MYDDAS_WKB_H_ */

395
MYDDAS/myddas_wkb2prolog.c Normal file
View File

@ -0,0 +1,395 @@
#if defined MYDDAS_MYSQL
#include <stdio.h>
#include <stdlib.h>
#include "Yap.h"
#include <netinet/in.h>
#include "myddas_wkb.h"
#include "myddas_wkb2prolog.h"
static void readswap4(uint32 *buf);
static void readswap8(double *buf);
static byte get_hostbyteorder(void);
static byte get_inbyteorder(void);
static uint32 get_wkbType(void);
static Term get_point(char *functor);
static Term get_linestring(char *functor);
static Term get_polygon(char *functor);
static Term get_geometry(uint32 type);
static int swaporder;
static byte inbyteorder, hostbyteorder;
static byte *cursor;
Term wkb2prolog(char *wkb) {
uint32 type;
cursor = wkb;
/*ignorar o SRID 4 bytes*/
cursor += 4;
/*byteorder*/
hostbyteorder = get_hostbyteorder();
inbyteorder = get_inbyteorder();
swaporder = 0;
if ( hostbyteorder != inbyteorder )
swaporder = 1;
type = get_wkbType();
return get_geometry(type);
}
static byte get_hostbyteorder(void){
uint16_t host = 5;
uint16_t net;
net = htons(host);
if ( net == host )
return(WKBXDR);
else
return(WKBNDR);
}
static byte get_inbyteorder(void){
byte b = cursor[0];
if (b != WKBNDR && b != WKBXDR) {
fprintf(stderr, "Unknown byteorder: %d\n",b);
exit(0);
}
/*avançar o cursor*/
cursor++;
return(b);
}
static uint32 get_wkbType(void){
uint32 u;
/* read the type */
readswap4(&u);
if (u > WKBMAXTYPE || u < WKBMINTYPE) {
fprintf(stderr, "Unknown type: %d\n",u);
exit(0);
}
return(u);
}
static void readswap4(uint32 *buf){
((byte *) buf)[0] = cursor[0];
((byte *) buf)[1] = cursor[1];
((byte *) buf)[2] = cursor[2];
((byte *) buf)[3] = cursor[3];
if ( swaporder ) {
if ( inbyteorder == WKBXDR ) {
*buf = (uint32)ntohl((u_long)*buf);
} else {
byte u[4];
u[0] = ((byte *) buf)[3];
u[1] = ((byte *) buf)[2];
u[2] = ((byte *) buf)[1];
u[3] = ((byte *) buf)[0];
((byte *) buf)[0] = u[0];
((byte *) buf)[1] = u[1];
((byte *) buf)[2] = u[2];
((byte *) buf)[3] = u[3];
}
}
/*avançar cursor*/
cursor += 4;
}
static void readswap8(double *buf) {
((byte *) buf)[0] = cursor[0];
((byte *) buf)[1] = cursor[1];
((byte *) buf)[2] = cursor[2];
((byte *) buf)[3] = cursor[3];
((byte *) buf)[4] = cursor[4];
((byte *) buf)[5] = cursor[5];
((byte *) buf)[6] = cursor[6];
((byte *) buf)[7] = cursor[7];
if ( swaporder ) {
if ( inbyteorder == WKBXDR ) {
u_long u[2];
u[0] = ((u_long *) buf)[0];
u[1] = ((u_long *) buf)[1];
((u_long *) buf)[1] = ntohl(u[0]);
((u_long *) buf)[0] = ntohl(u[1]);
} else {
byte u[8];
u[0] = ((byte *) buf)[7];
u[1] = ((byte *) buf)[6];
u[2] = ((byte *) buf)[5];
u[3] = ((byte *) buf)[4];
u[4] = ((byte *) buf)[3];
u[5] = ((byte *) buf)[2];
u[6] = ((byte *) buf)[1];
u[7] = ((byte *) buf)[0];
((byte *) buf)[0] = u[0];
((byte *) buf)[1] = u[1];
((byte *) buf)[2] = u[2];
((byte *) buf)[3] = u[3];
((byte *) buf)[4] = u[4];
((byte *) buf)[5] = u[5];
((byte *) buf)[6] = u[6];
((byte *) buf)[7] = u[7];
}
}
/*avançar cursor*/
cursor += 8;
}
static Term get_point(char *func){
Term args[2];
Functor functor;
double d;
if(func == NULL)
/*functor "," => (_,_)*/
functor = Yap_MkFunctor(Yap_LookupAtom(","), 2);
else
functor = Yap_MkFunctor(Yap_LookupAtom(func), 2);
/* read the X */
readswap8(&d);
args[0] = MkFloatTerm(d);
/* read the Y */
readswap8(&d);
args[1] = MkFloatTerm(d);
return Yap_MkApplTerm(functor, 2, args);
}
static Term get_linestring(char *func){
Term *c_list;
Term list;
Functor functor;
uint32 n;
int i;
/* read the number of vertices */
readswap4(&n);
/*alocar o espaço para os argumentos*/
c_list = (Term *) calloc(sizeof(Term),n);
for ( i = 0; i < n; i++) {
c_list[i] = get_point(NULL);
}
/*criar a lista*/
list = MkAtomTerm(Yap_LookupAtom("[]"));
for (i = n - 1; i >= 0; i--) {
/*percorrer a lista em ordem inversa para o resultado ser o original*/
list = MkPairTerm(c_list[i],list);
}
if(func == NULL)
return list;
else{
functor = Yap_MkFunctor(Yap_LookupAtom(func), 1);
return Yap_MkApplTerm(functor, 1, &list);
}
}
static Term get_polygon(char *func){
uint32 r;
int i;
Functor functor;
Term *c_list;
Term list;
/* read the number of rings */
readswap4(&r);
/*alocar o espaço para os aneis*/
c_list = (Term *) calloc(sizeof(Term),r);
for ( i = 0; i < r; i++ ) {
c_list[i] = get_linestring(NULL);
}
/*criar a lista*/
list = MkAtomTerm(Yap_LookupAtom("[]"));
for (i = r - 1; i >= 0; i--) {
/*percorrer a lista em ordem inversa para o resultado ser o original*/
list = MkPairTerm(c_list[i],list);
}
if(func == NULL)
return list;
else{
functor = Yap_MkFunctor(Yap_LookupAtom("polygon"), 1);
return Yap_MkApplTerm(functor, 1, &list);
}
}
static Term get_geometry(uint32 type){
switch(type) {
case WKBPOINT:
return get_point("point");
case WKBLINESTRING:
return get_linestring("linestring");
case WKBPOLYGON:
return get_polygon("polygon");
case WKBMULTIPOINT:
{
byte b;
uint32 n, u;
int i;
Functor functor;
Term *c_list;
Term list;
/* read the number of points */
readswap4(&n);
/*alocar o espaço para os pontos*/
c_list = (Term *) calloc(sizeof(Term),n);
for ( i = 0; i < n; i++ ) {
/* read (and ignore) the byteorder and type */
b = get_inbyteorder();
u = get_wkbType();
c_list[i] = get_point(NULL);
}
/*criar a lista*/
list = MkAtomTerm(Yap_LookupAtom("[]"));
for (i = n - 1; i >= 0; i--) {
/*percorrer a lista em ordem inversa para o resultado ser o original*/
list = MkPairTerm(c_list[i],list);
}
functor = Yap_MkFunctor(Yap_LookupAtom("multipoint"), 1);
return Yap_MkApplTerm(functor, 1, &list);
}
case WKBMULTILINESTRING:
{
byte b;
uint32 n, u;
int i;
Functor functor;
Term *c_list;
Term list;
/* read the number of polygons */
readswap4(&n);
/*alocar o espaço para os polygons*/
c_list = (Term *) calloc(sizeof(Term),n);
for ( i = 0; i < n; i++ ) {
/* read (and ignore) the byteorder and type */
b = get_inbyteorder();
u = get_wkbType();
c_list[i] = get_linestring(NULL);
}
/*criar a lista*/
list = MkAtomTerm(Yap_LookupAtom("[]"));
for (i = n - 1; i >= 0; i--) {
/*percorrer a lista em ordem inversa para o resultado ser o original*/
list = MkPairTerm(c_list[i],list);
}
functor = Yap_MkFunctor(Yap_LookupAtom("multilinestring"), 1);
return Yap_MkApplTerm(functor, 1, &list);
}
case WKBMULTIPOLYGON:
{
byte b;
uint32 n, u;
int i;
Functor functor;
Term *c_list;
Term list;
/* read the number of polygons */
readswap4(&n);
/*alocar o espaço para os polygons*/
c_list = (Term *) calloc(sizeof(Term),n);
for ( i = 0; i < n; i++ ) {
/* read (and ignore) the byteorder and type */
b = get_inbyteorder();
u = get_wkbType();
c_list[i] = get_polygon(NULL);
}
/*criar a lista*/
list = MkAtomTerm(Yap_LookupAtom("[]"));
for (i = n - 1; i >= 0; i--) {
/*percorrer a lista em ordem inversa para o resultado ser o original*/
list = MkPairTerm(c_list[i],list);
}
functor = Yap_MkFunctor(Yap_LookupAtom("multipolygon"), 1);
return Yap_MkApplTerm(functor, 1, &list);
}
case WKBGEOMETRYCOLLECTION:
{
byte b;
uint32 n;
int i;
Functor functor;
Term *c_list;
Term list;
/* read the number of geometries */
readswap4(&n);
/*alocar o espaço para as geometrys*/
c_list = (Term *) calloc(sizeof(Term),n);
for ( i = 0; i < n; i++ ) {
b = get_inbyteorder();
c_list[i] = get_geometry(get_wkbType());
}
/*criar a lista*/
list = MkAtomTerm(Yap_LookupAtom("[]"));
for (i = n - 1; i >= 0; i--) {
/*percorrer a lista em ordem inversa para o resultado ser o original*/
list = MkPairTerm(c_list[i],list);
}
functor = Yap_MkFunctor(Yap_LookupAtom("geometrycollection"), 1);
return Yap_MkApplTerm(functor, 1, &list);
}
}
return MkAtomTerm(Yap_LookupAtom("[]"));
}
#endif /*MYDDAS_MYSQL*/

View File

@ -0,0 +1,6 @@
#ifndef MYDDAS_WKB2PROLOG_H_
# define MYDDAS_WKB2PROLOG_H_
Term wkb2prolog(char *wkb) ;
#endif /* !MYDDAS_WKB2PROLOG_H_ */

View File

@ -33,7 +33,7 @@ INFODIR=$(SHAREDIR)/info
# -DCOROUTINING: implement extra control primitives
# -DANALYST: low level profiler
# -DDEPTH_LIMIT: support depth-limits in Prolog execution
# -DLOW_LEVEL_TRACER: low level tracing
# -DLOW_LEVEL_TRACER: low level tracing
#
# very experimental stuff, you'll need to contact the developers
# if you want to use this:
@ -45,7 +45,7 @@ INFODIR=$(SHAREDIR)/info
#
# check also optimisation options in INSTALL file.
#
YAP_EXTRAS=@YAP_EXTRAS@
YAP_EXTRAS=@YAP_EXTRAS@
#
# Please do not add YAP_EXTRA flags here: if you do check the flags
@ -127,7 +127,8 @@ HEADERS = \
$(srcdir)/H/cut_c.h \
$(srcdir)/MYDDAS/myddas.h $(srcdir)/MYDDAS/myddas_structs.h \
$(srcdir)/MYDDAS/myddas_statistics.h \
$(srcdir)/MYDDAS/myddas_statistics_structs.h
$(srcdir)/MYDDAS/myddas_statistics_structs.h \
$(srcdir)/MYDDAS/myddas_wkb.h $(srcdir)/MYDDAS/myddas_wkb2prolog.h
C_SOURCES= \
$(srcdir)/C/absmi.c $(srcdir)/C/adtdefs.c \
@ -177,7 +178,8 @@ C_SOURCES= \
$(srcdir)/MYDDAS/myddas_initialization.c \
$(srcdir)/MYDDAS/myddas_shared.c \
$(srcdir)/MYDDAS/myddas_statistics.c \
$(srcdir)/MYDDAS/myddas_top_level.c
$(srcdir)/MYDDAS/myddas_top_level.c \
$(srcdir)/MYDDAS/myddas_wkb2prolog.c
PL_SOURCES= \
$(srcdir)/pl/arith.yap $(srcdir)/pl/arrays.yap $(srcdir)/pl/boot.yap \
@ -207,7 +209,7 @@ PL_SOURCES= \
$(srcdir)/pl/yapor.yap $(srcdir)/pl/yio.yap
YAPDOCS=$(srcdir)/docs/yap.tex $(srcdir)/docs/chr.tex \
$(srcdir)/docs/clpr.tex $(srcdir)/docs/swi.tex
$(srcdir)/docs/clpr.tex $(srcdir)/docs/swi.tex
ENGINE_OBJECTS = \
agc.o absmi.o adtdefs.o alloc.o amasm.o analyst.o arrays.o \
@ -220,7 +222,7 @@ ENGINE_OBJECTS = \
iopreds.o depth_bound.o mavar.o \
myddas_mysql.o myddas_odbc.o myddas_shared.o myddas_initialization.o \
myddas_util.o myddas_statistics.o myddas_top_level.o \
modules.o other.o \
myddas_wkb2prolog.o modules.o other.o \
parser.o readutil.o save.o scanner.o sort.o stdpreds.o \
sysbits.o threads.o tracer.o \
unify.o userpreds.o utilpreds.o \
@ -238,7 +240,7 @@ OR_OBJECTS = \
tab.tries.o tab.suspend.o
BEAM_OBJECTS = \
eamamasm.o eam_showcode.o eamindex.o eam_am.o
eamamasm.o eam_showcode.o eamindex.o eam_am.o
STATIC_OBJECTS = \
@STATIC_MODE@sys.o random.o regexp.o @NO_BUILTIN_REGEXP@ regcomp.o regerror.o regfree.o regexec.o
@ -255,7 +257,7 @@ all: Makefile startup
Makefile: $(srcdir)/Makefile.in
absmi.o: $(srcdir)/C/absmi.c
absmi.o: $(srcdir)/C/absmi.c
$(CC) -c $(CFLAGS) $(ABSMI_FLAGS) $(srcdir)/C/absmi.c -o $@
adtdefs.o: $(srcdir)/C/adtdefs.c
@ -461,6 +463,9 @@ myddas_initialization.o: $(srcdir)/MYDDAS/myddas_initialization.c
myddas_top_level.o: $(srcdir)/MYDDAS/myddas_top_level.c
$(CC) -c $(CFLAGS) $(srcdir)/MYDDAS/myddas_top_level.c -o $@
myddas_wkb2prolog.o: $(srcdir)/MYDDAS/myddas_wkb2prolog.c
$(CC) -c $(CFLAGS) $(srcdir)/MYDDAS/myddas_wkb2prolog.c -o $@
myddas_statistics.o: $(srcdir)/MYDDAS/myddas_statistics.c
$(CC) -c $(CFLAGS) $(srcdir)/MYDDAS/myddas_statistics.c -o $@
@ -515,13 +520,13 @@ eamamasm.o: $(srcdir)/BEAM/eamamasm.c
eamindex.o: $(srcdir)/BEAM/eamindex.c
$(CC) -c $(CFLAGS) $(srcdir)/BEAM/eamindex.c -o $@
sys.o: $(srcdir)/library/system/sys.c
sys.o: $(srcdir)/library/system/sys.c
$(CC) -c $(CFLAGS) -I$(srcdir)/include $(srcdir)/library/system/sys.c -o $@
yap2swi.o: $(srcdir)/library/yap2swi/yap2swi.c
yap2swi.o: $(srcdir)/library/yap2swi/yap2swi.c
$(CC) -c $(CFLAGS) -I$(srcdir)/include $(srcdir)/library/yap2swi/yap2swi.c -o $@
random.o: $(srcdir)/library/random/random.c
random.o: $(srcdir)/library/random/random.c
$(CC) -c $(CFLAGS) -I$(srcdir)/include $(srcdir)/library/random/random.c -o $@
regexp.o: $(srcdir)/library/regex/regexp.c @NO_BUILTIN_REGEXP@ $(srcdir)/library/regex/regex2.h $(srcdir)/library/regex/engine.c
@ -545,7 +550,7 @@ regexec.o: $(srcdir)/library/regex/regexec.c
%.i : $(srcdir)/C/%.c
$(CC) -E $< $(CFLAGS) > $@
absmi.i: $(srcdir)/C/absmi.c
absmi.i: $(srcdir)/C/absmi.c
$(CC) -E $(CFLAGS) $(ABSMI_FLAGS) $(srcdir)/C/absmi.c > $@
c_interface.i: $(srcdir)/C/c_interface.c $(srcdir)/include/c_interface.h
@ -655,7 +660,7 @@ install_data:
TAGS: $(C_SOURCES) $(PL_SOURCES) $(HEADERS)
etags $(C_SOURCES) $(PL_SOURCES) $(HEADERS)
depend: $(HEADERS) $(C_SOURCES)
depend: $(HEADERS) $(C_SOURCES)
-@if test "$(GCC)" = yes; then\
$(CC) -MM $(CFLAGS) -I$(srcdir)/include $(C_SOURCES) > .depend;\
else\