]> granicus.if.org Git - postgis/commitdiff
Initial working version - based on Ralph Masons WKT and WKB parser/writer.
authorDavid Blasby <dblasby@gmail.com>
Mon, 26 Apr 2004 23:05:20 +0000 (23:05 +0000)
committerDavid Blasby <dblasby@gmail.com>
Mon, 26 Apr 2004 23:05:20 +0000 (23:05 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@508 b70326c6-7e19-0410-871a-916f4a2858ee

13 files changed:
lwgeom/Makefile
lwgeom/lex.yy.c [new file with mode: 0644]
lwgeom/lwgeom.h
lwgeom/lwgeom.sql.in
lwgeom/lwgeom_api.c
lwgeom/lwgeom_functions_basic.c
lwgeom/lwgeom_gist.c
lwgeom/lwgeom_inout.c
lwgeom/lwgparse.c [new file with mode: 0644]
lwgeom/wktparse.h [new file with mode: 0644]
lwgeom/wktparse.tab.c [new file with mode: 0644]
lwgeom/wktparse.tab.h [new file with mode: 0644]
lwgeom/wktunparse.c [new file with mode: 0644]

index b8f345beaeddf9c2e7db518ce5caaf329635f124..665c0d2bcf7169abafa99712c0c6ac39c8ef214e 100644 (file)
@@ -3,7 +3,7 @@
 #---------------------------------------------------------------
 # Set USE_PROJ to 1 for Proj4 reprojection support
 #
-USE_PROJ=1
+USE_PROJ=0
 ifeq (${PROJ_DIR},) 
        PROJ_DIR=/usr/local
 endif
@@ -12,7 +12,7 @@ endif
 # Set USE_GEOS to 1 for GEOS spatial predicate and operator
 # support
 #
-USE_GEOS=1
+USE_GEOS=0
 ifeq (${GEOS_DIR},) 
        GEOS_DIR=/usr/local
 endif
@@ -94,7 +94,8 @@ POSTGIS_VERSION = $(SO_MAJOR_VERSION).$(SO_MINOR_VERSION) USE_GEOS=$(USE_GEOS) U
 
 #---------------------------------------------------------------
 
-override CFLAGS += -g -fexceptions  
+override CFLAGS += -g 
+
 override CFLAGS += -I$(srcdir) -DFRONTEND -DSYSCONFDIR='"$(sysconfdir)"' 
 override CFLAGS += -DUSE_VERSION=$(USE_VERSION)
 
@@ -134,7 +135,8 @@ ifeq ($(USE_GEOS),1)
        GEOS_WRAPPER=postgis_geos_wrapper.o
 endif
 
-OBJS=lwgeom_api.o lwgeom_functions_analytic.o lwgeom_geos.o lwgeom_inout.o lwgeom_estimate.o lwgeom_functions_basic.o lwgeom_gist.o lwgeom_transform.o stringBuffer.o
+OBJS=lwgeom_api.o lwgeom_functions_analytic.o lwgeom_geos.o lwgeom_inout.o lwgeom_estimate.o lwgeom_functions_basic.o lwgeom_gist.o lwgeom_transform.o stringBuffer.o lwgeom_box2dfloat4.o lex.yy.o wktparse.tab.o lwgparse.o wktunparse.o 
+
 
 
 
diff --git a/lwgeom/lex.yy.c b/lwgeom/lex.yy.c
new file mode 100644 (file)
index 0000000..d72d004
--- /dev/null
@@ -0,0 +1,3309 @@
+#define yy_create_buffer lwg_parse_yy_create_buffer
+#define yy_delete_buffer lwg_parse_yy_delete_buffer
+#define yy_scan_buffer lwg_parse_yy_scan_buffer
+#define yy_scan_string lwg_parse_yy_scan_string
+#define yy_scan_bytes lwg_parse_yy_scan_bytes
+#define yy_flex_debug lwg_parse_yy_flex_debug
+#define yy_init_buffer lwg_parse_yy_init_buffer
+#define yy_flush_buffer lwg_parse_yy_flush_buffer
+#define yy_load_buffer_state lwg_parse_yy_load_buffer_state
+#define yy_switch_to_buffer lwg_parse_yy_switch_to_buffer
+#define yyin lwg_parse_yyin
+#define yyleng lwg_parse_yyleng
+#define yylex lwg_parse_yylex
+#define yyout lwg_parse_yyout
+#define yyrestart lwg_parse_yyrestart
+#define yytext lwg_parse_yytext
+#define yywrap lwg_parse_yywrap
+
+#line 20 "lex.yy.c"
+/* A lexical scanner generated by flex */
+
+/* Scanner skeleton version:
+ * $Header$
+ */
+
+#define FLEX_SCANNER
+#define YY_FLEX_MAJOR_VERSION 2
+#define YY_FLEX_MINOR_VERSION 5
+
+#include <stdio.h>
+#include <unistd.h>
+
+
+/* cfront 1.2 defines "c_plusplus" instead of "__cplusplus" */
+#ifdef c_plusplus
+#ifndef __cplusplus
+#define __cplusplus
+#endif
+#endif
+
+
+#ifdef __cplusplus
+
+#include <stdlib.h>
+
+/* Use prototypes in function declarations. */
+#define YY_USE_PROTOS
+
+/* The "const" storage-class-modifier is valid. */
+#define YY_USE_CONST
+
+#else  /* ! __cplusplus */
+
+#if __STDC__
+
+#define YY_USE_PROTOS
+#define YY_USE_CONST
+
+#endif /* __STDC__ */
+#endif /* ! __cplusplus */
+
+#ifdef __TURBOC__
+ #pragma warn -rch
+ #pragma warn -use
+#include <io.h>
+#include <stdlib.h>
+#define YY_USE_CONST
+#define YY_USE_PROTOS
+#endif
+
+#ifdef YY_USE_CONST
+#define yyconst const
+#else
+#define yyconst
+#endif
+
+
+#ifdef YY_USE_PROTOS
+#define YY_PROTO(proto) proto
+#else
+#define YY_PROTO(proto) ()
+#endif
+
+/* Returned upon end-of-file. */
+#define YY_NULL 0
+
+/* Promotes a possibly negative, possibly signed char to an unsigned
+ * integer for use as an array index.  If the signed char is negative,
+ * we want to instead treat it as an 8-bit unsigned char, hence the
+ * double cast.
+ */
+#define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c)
+
+/* Enter a start condition.  This macro really ought to take a parameter,
+ * but we do it the disgusting crufty way forced on us by the ()-less
+ * definition of BEGIN.
+ */
+#define BEGIN yy_start = 1 + 2 *
+
+/* Translate the current start state into a value that can be later handed
+ * to BEGIN to return to the state.  The YYSTATE alias is for lex
+ * compatibility.
+ */
+#define YY_START ((yy_start - 1) / 2)
+#define YYSTATE YY_START
+
+/* Action number for EOF rule of a given start state. */
+#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1)
+
+/* Special action meaning "start processing a new file". */
+#define YY_NEW_FILE yyrestart( yyin )
+
+#define YY_END_OF_BUFFER_CHAR 0
+
+/* Size of default input buffer. */
+#define YY_BUF_SIZE 16384
+
+typedef struct yy_buffer_state *YY_BUFFER_STATE;
+
+extern int yyleng;
+extern FILE *yyin, *yyout;
+
+#define EOB_ACT_CONTINUE_SCAN 0
+#define EOB_ACT_END_OF_FILE 1
+#define EOB_ACT_LAST_MATCH 2
+
+/* The funky do-while in the following #define is used to turn the definition
+ * int a single C statement (which needs a semi-colon terminator).  This
+ * avoids problems with code like:
+ *
+ *     if ( condition_holds )
+ *             yyless( 5 );
+ *     else
+ *             do_something_else();
+ *
+ * Prior to using the do-while the compiler would get upset at the
+ * "else" because it interpreted the "if" statement as being all
+ * done when it reached the ';' after the yyless() call.
+ */
+
+/* Return all but the first 'n' matched characters back to the input stream. */
+
+#define yyless(n) \
+       do \
+               { \
+               /* Undo effects of setting up yytext. */ \
+               *yy_cp = yy_hold_char; \
+               YY_RESTORE_YY_MORE_OFFSET \
+               yy_c_buf_p = yy_cp = yy_bp + n - YY_MORE_ADJ; \
+               YY_DO_BEFORE_ACTION; /* set up yytext again */ \
+               } \
+       while ( 0 )
+
+#define unput(c) yyunput( c, yytext_ptr )
+
+/* The following is because we cannot portably get our hands on size_t
+ * (without autoconf's help, which isn't available because we want
+ * flex-generated scanners to compile on their own).
+ */
+typedef unsigned int yy_size_t;
+
+
+struct yy_buffer_state
+       {
+       FILE *yy_input_file;
+
+       char *yy_ch_buf;                /* input buffer */
+       char *yy_buf_pos;               /* current position in input buffer */
+
+       /* Size of input buffer in bytes, not including room for EOB
+        * characters.
+        */
+       yy_size_t yy_buf_size;
+
+       /* Number of characters read into yy_ch_buf, not including EOB
+        * characters.
+        */
+       int yy_n_chars;
+
+       /* Whether we "own" the buffer - i.e., we know we created it,
+        * and can realloc() it to grow it, and should free() it to
+        * delete it.
+        */
+       int yy_is_our_buffer;
+
+       /* Whether this is an "interactive" input source; if so, and
+        * if we're using stdio for input, then we want to use getc()
+        * instead of fread(), to make sure we stop fetching input after
+        * each newline.
+        */
+       int yy_is_interactive;
+
+       /* Whether we're considered to be at the beginning of a line.
+        * If so, '^' rules will be active on the next match, otherwise
+        * not.
+        */
+       int yy_at_bol;
+
+       /* Whether to try to fill the input buffer when we reach the
+        * end of it.
+        */
+       int yy_fill_buffer;
+
+       int yy_buffer_status;
+#define YY_BUFFER_NEW 0
+#define YY_BUFFER_NORMAL 1
+       /* When an EOF's been seen but there's still some text to process
+        * then we mark the buffer as YY_EOF_PENDING, to indicate that we
+        * shouldn't try reading from the input source any more.  We might
+        * still have a bunch of tokens to match, though, because of
+        * possible backing-up.
+        *
+        * When we actually see the EOF, we change the status to "new"
+        * (via yyrestart()), so that the user can continue scanning by
+        * just pointing yyin at a new input file.
+        */
+#define YY_BUFFER_EOF_PENDING 2
+       };
+
+static YY_BUFFER_STATE yy_current_buffer = 0;
+
+/* We provide macros for accessing buffer states in case in the
+ * future we want to put the buffer states in a more general
+ * "scanner state".
+ */
+#define YY_CURRENT_BUFFER yy_current_buffer
+
+
+/* yy_hold_char holds the character lost when yytext is formed. */
+static char yy_hold_char;
+
+static int yy_n_chars;         /* number of characters read into yy_ch_buf */
+
+
+int yyleng;
+
+/* Points to current character in buffer. */
+static char *yy_c_buf_p = (char *) 0;
+static int yy_init = 1;                /* whether we need to initialize */
+static int yy_start = 0;       /* start state number */
+
+/* Flag which is used to allow yywrap()'s to do buffer switches
+ * instead of setting up a fresh yyin.  A bit of a hack ...
+ */
+static int yy_did_buffer_switch_on_eof;
+
+void yyrestart YY_PROTO(( FILE *input_file ));
+
+void yy_switch_to_buffer YY_PROTO(( YY_BUFFER_STATE new_buffer ));
+void yy_load_buffer_state YY_PROTO(( void ));
+YY_BUFFER_STATE yy_create_buffer YY_PROTO(( FILE *file, int size ));
+void yy_delete_buffer YY_PROTO(( YY_BUFFER_STATE b ));
+void yy_init_buffer YY_PROTO(( YY_BUFFER_STATE b, FILE *file ));
+void yy_flush_buffer YY_PROTO(( YY_BUFFER_STATE b ));
+#define YY_FLUSH_BUFFER yy_flush_buffer( yy_current_buffer )
+
+YY_BUFFER_STATE yy_scan_buffer YY_PROTO(( char *base, yy_size_t size ));
+YY_BUFFER_STATE yy_scan_string YY_PROTO(( yyconst char *yy_str ));
+YY_BUFFER_STATE yy_scan_bytes YY_PROTO(( yyconst char *bytes, int len ));
+
+static void *yy_flex_alloc YY_PROTO(( yy_size_t ));
+static void *yy_flex_realloc YY_PROTO(( void *, yy_size_t ));
+static void yy_flex_free YY_PROTO(( void * ));
+
+#define yy_new_buffer yy_create_buffer
+
+#define yy_set_interactive(is_interactive) \
+       { \
+       if ( ! yy_current_buffer ) \
+               yy_current_buffer = yy_create_buffer( yyin, YY_BUF_SIZE ); \
+       yy_current_buffer->yy_is_interactive = is_interactive; \
+       }
+
+#define yy_set_bol(at_bol) \
+       { \
+       if ( ! yy_current_buffer ) \
+               yy_current_buffer = yy_create_buffer( yyin, YY_BUF_SIZE ); \
+       yy_current_buffer->yy_at_bol = at_bol; \
+       }
+
+#define YY_AT_BOL() (yy_current_buffer->yy_at_bol)
+
+typedef char YY_CHAR;
+FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0;
+typedef int yy_state_type;
+extern char *yytext;
+#define yytext_ptr yytext
+static yyconst short yy_nxt[][128] =
+    {
+    {
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0
+    },
+
+    {
+        5,    6,    6,    6,    6,    6,    6,    6,    6,    7,
+        8,    6,    6,    7,    6,    6,    6,    6,    6,    6,
+        6,    6,    6,    6,    6,    6,    6,    6,    6,    6,
+        6,    6,    7,    6,    6,    6,    6,    6,    6,    6,
+        9,   10,    6,    6,   11,    6,    6,    6,   12,    6,
+        6,    6,    6,    6,    6,    6,    6,    6,    6,   13,
+        6,   14,    6,    6,    6,    6,    6,    6,    6,   15,
+
+        6,   16,    6,    6,    6,    6,   17,   18,    6,    6,
+       19,    6,    6,   20,    6,    6,    6,    6,    6,    6,
+        6,    6,    6,    6,    6,    6,    6,    6,    6,    6,
+        6,   15,    6,   16,    6,    6,    6,    6,   17,   18,
+        6,    6,   19,    6,    6,   20,    6,    6,    6,    6,
+        6,    6,    6,    6,    6,    6,    6,    6
+    },
+
+    {
+        5,    6,    6,    6,    6,    6,    6,    6,    6,    7,
+        8,    6,    6,    7,    6,    6,    6,    6,    6,    6,
+        6,    6,    6,    6,    6,    6,    6,    6,    6,    6,
+        6,    6,    7,    6,    6,    6,    6,    6,    6,    6,
+
+        9,   10,    6,    6,   11,    6,    6,    6,   12,    6,
+        6,    6,    6,    6,    6,    6,    6,    6,    6,   13,
+        6,   14,    6,    6,    6,    6,    6,    6,    6,   15,
+        6,   16,    6,    6,    6,    6,   17,   18,    6,    6,
+       19,    6,    6,   20,    6,    6,    6,    6,    6,    6,
+        6,    6,    6,    6,    6,    6,    6,    6,    6,    6,
+        6,   15,    6,   16,    6,    6,    6,    6,   17,   18,
+        6,    6,   19,    6,    6,   20,    6,    6,    6,    6,
+        6,    6,    6,    6,    6,    6,    6,    6
+    },
+
+    {
+        5,    6,    6,    6,    6,    6,    6,    6,    6,    7,
+
+        8,    6,    6,    7,    6,    6,    6,    6,    6,    6,
+        6,    6,    6,    6,    6,    6,    6,    6,    6,    6,
+        6,    6,    7,    6,    6,    6,    6,    6,    6,    6,
+        9,   10,    6,    6,   11,   21,   22,    6,   23,   23,
+       23,   23,   23,   23,   23,   23,   23,   23,    6,   13,
+        6,   14,    6,    6,    6,    6,    6,    6,    6,   15,
+        6,   16,    6,    6,    6,    6,   17,   18,    6,    6,
+       19,    6,    6,   20,    6,    6,    6,    6,    6,    6,
+        6,    6,    6,    6,    6,    6,    6,    6,    6,    6,
+        6,   15,    6,   16,    6,    6,    6,    6,   17,   18,
+
+        6,    6,   19,    6,    6,   20,    6,    6,    6,    6,
+        6,    6,    6,    6,    6,    6,    6,    6
+    },
+
+    {
+        5,    6,    6,    6,    6,    6,    6,    6,    6,    7,
+        8,    6,    6,    7,    6,    6,    6,    6,    6,    6,
+        6,    6,    6,    6,    6,    6,    6,    6,    6,    6,
+        6,    6,    7,    6,    6,    6,    6,    6,    6,    6,
+        9,   10,    6,    6,   11,   21,   22,    6,   23,   23,
+       23,   23,   23,   23,   23,   23,   23,   23,    6,   13,
+        6,   14,    6,    6,    6,    6,    6,    6,    6,   15,
+        6,   16,    6,    6,    6,    6,   17,   18,    6,    6,
+
+       19,    6,    6,   20,    6,    6,    6,    6,    6,    6,
+        6,    6,    6,    6,    6,    6,    6,    6,    6,    6,
+        6,   15,    6,   16,    6,    6,    6,    6,   17,   18,
+        6,    6,   19,    6,    6,   20,    6,    6,    6,    6,
+        6,    6,    6,    6,    6,    6,    6,    6
+    },
+
+    {
+       -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,
+       -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,
+       -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,
+       -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,
+       -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,
+
+       -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,
+       -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,
+       -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,
+       -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,
+       -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,
+       -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,
+       -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5,
+       -5,   -5,   -5,   -5,   -5,   -5,   -5,   -5
+    },
+
+    {
+        5,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,
+       -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,
+
+       -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,
+       -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,
+       -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,
+       -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,
+       -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,
+       -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,
+       -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,
+       -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,
+       -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,
+       -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6,
+
+       -6,   -6,   -6,   -6,   -6,   -6,   -6,   -6
+    },
+
+    {
+        5,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   24,
+       24,   -7,   -7,   24,   -7,   -7,   -7,   -7,   -7,   -7,
+       -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,
+       -7,   -7,   24,   -7,   -7,   -7,   -7,   -7,   -7,   -7,
+       -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,
+       -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,
+       -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,
+       -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,
+       -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,
+
+       -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,
+       -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,
+       -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7,
+       -7,   -7,   -7,   -7,   -7,   -7,   -7,   -7
+    },
+
+    {
+        5,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   24,
+       24,   -8,   -8,   24,   -8,   -8,   -8,   -8,   -8,   -8,
+       -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,
+       -8,   -8,   24,   -8,   -8,   -8,   -8,   -8,   -8,   -8,
+       -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,
+       -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,
+
+       -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,
+       -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,
+       -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,
+       -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,
+       -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,
+       -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8,
+       -8,   -8,   -8,   -8,   -8,   -8,   -8,   -8
+    },
+
+    {
+        5,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,
+       -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,
+       -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,
+
+       -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,
+       -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,
+       -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,
+       -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,
+       -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,
+       -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,
+       -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,
+       -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,
+       -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9,
+       -9,   -9,   -9,   -9,   -9,   -9,   -9,   -9
+
+    },
+
+    {
+        5,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,
+      -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,
+      -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,
+      -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,
+      -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,
+      -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,
+      -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,
+      -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,
+      -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,
+      -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,
+
+      -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,
+      -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10,
+      -10,  -10,  -10,  -10,  -10,  -10,  -10,  -10
+    },
+
+    {
+        5,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,
+      -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,
+      -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,
+      -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,
+      -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,
+      -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,
+      -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,
+
+      -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,
+      -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,
+      -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,
+      -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,
+      -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11,
+      -11,  -11,  -11,  -11,  -11,  -11,  -11,  -11
+    },
+
+    {
+        5,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,
+      -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,
+      -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,
+      -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,
+
+      -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,   25,   26,
+      -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,
+      -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,
+      -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,
+      -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,
+      -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,
+      -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,
+      -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12,
+      -12,  -12,  -12,  -12,  -12,  -12,  -12,  -12
+    },
+
+    {
+        5,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,
+
+      -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,
+      -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,
+      -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,
+      -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,
+      -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,
+      -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,
+      -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,
+      -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,
+      -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,
+      -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,
+
+      -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13,
+      -13,  -13,  -13,  -13,  -13,  -13,  -13,  -13
+    },
+
+    {
+        5,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,
+      -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,
+      -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,
+      -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,
+      -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,
+      -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,
+      -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,
+      -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,
+
+      -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,
+      -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,
+      -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,
+      -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14,
+      -14,  -14,  -14,  -14,  -14,  -14,  -14,  -14
+    },
+
+    {
+        5,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,
+      -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,
+      -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,
+      -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,
+      -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,
+
+      -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,
+      -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,
+      -15,  -15,  -15,  -15,  -15,  -15,  -15,   27,  -15,  -15,
+      -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,
+      -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,
+      -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,   27,
+      -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15,
+      -15,  -15,  -15,  -15,  -15,  -15,  -15,  -15
+    },
+
+    {
+        5,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,
+      -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,
+
+      -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,
+      -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,
+      -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,
+      -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,
+      -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,   28,
+      -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,
+      -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,
+      -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,
+      -16,   28,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,
+      -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16,
+
+      -16,  -16,  -16,  -16,  -16,  -16,  -16,  -16
+    },
+
+    {
+        5,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,
+      -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,
+      -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,
+      -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,
+      -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,
+      -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,
+      -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,
+      -17,  -17,  -17,   29,  -17,  -17,  -17,  -17,  -17,  -17,
+      -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,
+
+      -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,
+      -17,  -17,  -17,  -17,  -17,   29,  -17,  -17,  -17,  -17,
+      -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17,
+      -17,  -17,  -17,  -17,  -17,  -17,  -17,  -17
+    },
+
+    {
+        5,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,
+      -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,
+      -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,
+      -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,
+      -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,
+      -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,
+
+      -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,
+      -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,
+      -18,  -18,  -18,  -18,  -18,   30,  -18,  -18,  -18,  -18,
+      -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,
+      -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18,
+      -18,  -18,  -18,  -18,  -18,  -18,  -18,   30,  -18,  -18,
+      -18,  -18,  -18,  -18,  -18,  -18,  -18,  -18
+    },
+
+    {
+        5,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,
+      -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,
+      -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,
+
+      -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,
+      -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,
+      -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,
+      -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,
+      -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,   31,
+      -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,
+      -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,
+      -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,
+      -19,   31,  -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19,
+      -19,  -19,  -19,  -19,  -19,  -19,  -19,  -19
+
+    },
+
+    {
+        5,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,
+      -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,
+      -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,
+      -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,
+      -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,
+      -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,
+      -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,
+      -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,
+      -20,  -20,   32,  -20,  -20,  -20,  -20,  -20,  -20,  -20,
+      -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,
+
+      -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20,
+      -20,  -20,  -20,  -20,   32,  -20,  -20,  -20,  -20,  -20,
+      -20,  -20,  -20,  -20,  -20,  -20,  -20,  -20
+    },
+
+    {
+        5,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,
+      -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,
+      -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,
+      -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,
+      -21,  -21,  -21,  -21,  -21,  -21,   33,  -21,   34,   34,
+       34,   34,   34,   34,   34,   34,   34,   34,  -21,  -21,
+      -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,
+
+      -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,
+      -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,
+      -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,
+      -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,
+      -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21,
+      -21,  -21,  -21,  -21,  -21,  -21,  -21,  -21
+    },
+
+    {
+        5,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,
+      -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,
+      -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,
+      -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,
+
+      -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,   35,   35,
+       35,   35,   35,   35,   35,   35,   35,   35,  -22,  -22,
+      -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,
+      -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,
+      -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,
+      -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,
+      -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,
+      -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22,
+      -22,  -22,  -22,  -22,  -22,  -22,  -22,  -22
+    },
+
+    {
+        5,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,
+
+      -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,
+      -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,
+      -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,
+      -23,  -23,  -23,  -23,  -23,  -23,   33,  -23,   34,   34,
+       34,   34,   34,   34,   34,   34,   34,   34,  -23,  -23,
+      -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,
+      -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,
+      -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,
+      -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,
+      -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,
+
+      -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23,
+      -23,  -23,  -23,  -23,  -23,  -23,  -23,  -23
+    },
+
+    {
+        5,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,   24,
+       24,  -24,  -24,   24,  -24,  -24,  -24,  -24,  -24,  -24,
+      -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,
+      -24,  -24,   24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,
+      -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,
+      -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,
+      -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,
+      -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,
+
+      -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,
+      -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,
+      -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,
+      -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24,
+      -24,  -24,  -24,  -24,  -24,  -24,  -24,  -24
+    },
+
+    {
+        5,  -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,
+      -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,
+      -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,
+      -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,
+      -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,   36,   36,
+
+       36,   36,   36,   36,   36,   36,   36,   36,  -25,  -25,
+      -25,  -25,  -25,  -25,  -25,   36,   36,   36,   36,   36,
+       36,  -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,
+      -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,
+      -25,  -25,  -25,  -25,  -25,  -25,  -25,   36,   36,   36,
+       36,   36,   36,  -25,  -25,  -25,  -25,  -25,  -25,  -25,
+      -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25,
+      -25,  -25,  -25,  -25,  -25,  -25,  -25,  -25
+    },
+
+    {
+        5,  -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,
+      -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,
+
+      -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,
+      -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,
+      -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,   37,   37,
+       37,   37,   37,   37,   37,   37,   37,   37,  -26,  -26,
+      -26,  -26,  -26,  -26,  -26,   37,   37,   37,   37,   37,
+       37,  -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,
+      -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,
+      -26,  -26,  -26,  -26,  -26,  -26,  -26,   37,   37,   37,
+       37,   37,   37,  -26,  -26,  -26,  -26,  -26,  -26,  -26,
+      -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26,
+
+      -26,  -26,  -26,  -26,  -26,  -26,  -26,  -26
+    },
+
+    {
+        5,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,
+      -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,
+      -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,
+      -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,
+      -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,
+      -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,
+      -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,
+      -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,
+       38,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,
+
+      -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,
+      -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27,
+      -27,  -27,   38,  -27,  -27,  -27,  -27,  -27,  -27,  -27,
+      -27,  -27,  -27,  -27,  -27,  -27,  -27,  -27
+    },
+
+    {
+        5,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,
+      -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,
+      -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,
+      -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,
+      -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,
+      -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,
+
+      -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,
+      -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,   39,
+      -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,
+      -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,
+      -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,
+      -28,   39,  -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28,
+      -28,  -28,  -28,  -28,  -28,  -28,  -28,  -28
+    },
+
+    {
+        5,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,
+      -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,
+      -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,
+
+      -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,
+      -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,
+      -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,
+      -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,
+      -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,   40,  -29,
+      -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,
+      -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,
+      -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,
+       40,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29,
+      -29,  -29,  -29,  -29,  -29,  -29,  -29,  -29
+
+    },
+
+    {
+        5,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,
+      -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,
+      -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,
+      -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,
+      -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,
+      -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,
+      -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,
+      -30,  -30,  -30,  -30,  -30,  -30,   41,  -30,  -30,  -30,
+      -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,
+      -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,
+
+      -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,   41,  -30,
+      -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30,
+      -30,  -30,  -30,  -30,  -30,  -30,  -30,  -30
+    },
+
+    {
+        5,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,
+      -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,
+      -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,
+      -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,
+      -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,
+      -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,
+      -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,
+
+      -31,  -31,  -31,   42,  -31,  -31,   43,  -31,  -31,  -31,
+      -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,
+      -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,
+      -31,  -31,  -31,  -31,  -31,   42,  -31,  -31,   43,  -31,
+      -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31,
+      -31,  -31,  -31,  -31,  -31,  -31,  -31,  -31
+    },
+
+    {
+        5,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,
+      -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,
+      -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,
+      -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,
+
+      -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,
+      -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,
+      -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,
+      -32,  -32,  -32,   44,  -32,  -32,  -32,  -32,  -32,  -32,
+      -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,
+      -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,
+      -32,  -32,  -32,  -32,  -32,   44,  -32,  -32,  -32,  -32,
+      -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32,
+      -32,  -32,  -32,  -32,  -32,  -32,  -32,  -32
+    },
+
+    {
+        5,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,
+
+      -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,
+      -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,
+      -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,
+      -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,   35,   35,
+       35,   35,   35,   35,   35,   35,   35,   35,  -33,  -33,
+      -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,
+      -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,
+      -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,
+      -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,
+      -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,
+
+      -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33,
+      -33,  -33,  -33,  -33,  -33,  -33,  -33,  -33
+    },
+
+    {
+        5,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,
+      -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,
+      -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,
+      -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,
+      -34,  -34,  -34,  -34,  -34,  -34,   33,  -34,   34,   34,
+       34,   34,   34,   34,   34,   34,   34,   34,  -34,  -34,
+      -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,
+      -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,
+
+      -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,
+      -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,
+      -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,
+      -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34,
+      -34,  -34,  -34,  -34,  -34,  -34,  -34,  -34
+    },
+
+    {
+        5,  -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,
+      -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,
+      -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,
+      -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,
+      -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,   35,   35,
+
+       35,   35,   35,   35,   35,   35,   35,   35,  -35,  -35,
+      -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,   45,
+      -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,
+      -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,
+      -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,
+      -35,   45,  -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,
+      -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35,
+      -35,  -35,  -35,  -35,  -35,  -35,  -35,  -35
+    },
+
+    {
+        5,  -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,
+      -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,
+
+      -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,
+      -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,
+      -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,   36,   36,
+       36,   36,   36,   36,   36,   36,   36,   36,  -36,  -36,
+      -36,  -36,  -36,  -36,  -36,   36,   36,   36,   36,   36,
+       36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,
+      -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,
+      -36,  -36,  -36,  -36,  -36,  -36,  -36,   36,   36,   36,
+       36,   36,   36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,
+      -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36,
+
+      -36,  -36,  -36,  -36,  -36,  -36,  -36,  -36
+    },
+
+    {
+        5,  -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,
+      -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,
+      -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,
+      -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,
+      -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,   37,   37,
+       37,   37,   37,   37,   37,   37,   37,   37,  -37,  -37,
+      -37,  -37,  -37,  -37,  -37,   37,   37,   37,   37,   37,
+       37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,
+      -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,
+
+      -37,  -37,  -37,  -37,  -37,  -37,  -37,   37,   37,   37,
+       37,   37,   37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,
+      -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37,
+      -37,  -37,  -37,  -37,  -37,  -37,  -37,  -37
+    },
+
+    {
+        5,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,
+      -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,
+      -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,
+      -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,
+      -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,
+      -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,
+
+      -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,
+      -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,
+      -38,  -38,  -38,  -38,   46,  -38,  -38,  -38,  -38,  -38,
+      -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,
+      -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38,
+      -38,  -38,  -38,  -38,  -38,  -38,   46,  -38,  -38,  -38,
+      -38,  -38,  -38,  -38,  -38,  -38,  -38,  -38
+    },
+
+    {
+        5,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,
+      -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,
+      -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,
+
+      -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,
+      -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,
+      -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,
+      -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,
+      -39,  -39,  -39,  -39,  -39,  -39,  -39,   47,  -39,  -39,
+      -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,
+      -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,
+      -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,   47,
+      -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39,
+      -39,  -39,  -39,  -39,  -39,  -39,  -39,  -39
+
+    },
+
+    {
+        5,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,
+      -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,
+      -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,
+      -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,
+      -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,
+      -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,
+      -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,   48,
+      -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,
+      -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,
+      -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,
+
+      -40,   48,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,
+      -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40,
+      -40,  -40,  -40,  -40,  -40,  -40,  -40,  -40
+    },
+
+    {
+        5,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,
+      -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,
+      -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,
+      -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,
+      -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,
+      -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,
+      -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,
+
+      -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,
+      -41,  -41,  -41,  -41,   49,  -41,  -41,  -41,  -41,  -41,
+      -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,
+      -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41,
+      -41,  -41,  -41,  -41,  -41,  -41,   49,  -41,  -41,  -41,
+      -41,  -41,  -41,  -41,  -41,  -41,  -41,  -41
+    },
+
+    {
+        5,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,
+      -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,
+      -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,
+      -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,
+
+      -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,
+      -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,
+      -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,
+      -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,   50,  -42,
+      -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,
+      -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,
+      -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,
+       50,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42,
+      -42,  -42,  -42,  -42,  -42,  -42,  -42,  -42
+    },
+
+    {
+        5,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,
+
+      -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,
+      -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,
+      -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,
+      -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,
+      -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,
+      -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,
+      -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,
+      -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,   51,
+      -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,
+      -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,
+
+      -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,  -43,
+      -43,   51,  -43,  -43,  -43,  -43,  -43,  -43
+    },
+
+    {
+        5,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,
+      -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,
+      -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,
+      -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,
+      -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,
+      -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,
+      -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,   52,  -44,
+      -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,
+
+      -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,
+      -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,
+       52,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,
+      -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44,
+      -44,  -44,  -44,  -44,  -44,  -44,  -44,  -44
+    },
+
+    {
+        5,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,
+      -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,
+      -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,
+      -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,
+      -45,  -45,  -45,   53,  -45,   53,  -45,  -45,   54,   54,
+
+       54,   54,   54,   54,   54,   54,   54,   54,  -45,  -45,
+      -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,
+      -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,
+      -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,
+      -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,
+      -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,
+      -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45,
+      -45,  -45,  -45,  -45,  -45,  -45,  -45,  -45
+    },
+
+    {
+        5,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,
+      -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,
+
+      -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,
+      -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,
+      -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,
+      -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,
+      -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,
+      -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,
+      -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,   55,
+      -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,
+      -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,
+      -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,  -46,
+
+      -46,   55,  -46,  -46,  -46,  -46,  -46,  -46
+    },
+
+    {
+        5,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,
+      -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,
+      -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,
+      -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,
+      -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,
+      -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,
+      -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,   56,
+      -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,
+      -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,
+
+      -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,
+      -47,   56,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,
+      -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47,
+      -47,  -47,  -47,  -47,  -47,  -47,  -47,  -47
+    },
+
+    {
+        5,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,
+      -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,
+      -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,
+      -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,
+      -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,
+      -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,
+
+      -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,
+      -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,
+      -48,  -48,  -48,   57,  -48,  -48,  -48,  -48,  -48,  -48,
+      -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,
+      -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48,
+      -48,  -48,  -48,  -48,  -48,   57,  -48,  -48,  -48,  -48,
+      -48,  -48,  -48,  -48,  -48,  -48,  -48,  -48
+    },
+
+    {
+        5,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,
+      -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,
+      -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,
+
+      -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,
+      -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,
+      -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,
+      -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,
+      -49,  -49,  -49,   58,  -49,  -49,  -49,  -49,  -49,  -49,
+      -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,
+      -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,
+      -49,  -49,  -49,  -49,  -49,   58,  -49,  -49,  -49,  -49,
+      -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49,
+      -49,  -49,  -49,  -49,  -49,  -49,  -49,  -49
+
+    },
+
+    {
+        5,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,
+      -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,
+      -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,
+      -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,
+      -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,
+      -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,
+      -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,
+      -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,
+      -50,  -50,  -50,  -50,   59,  -50,  -50,  -50,  -50,  -50,
+      -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,
+
+      -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50,
+      -50,  -50,  -50,  -50,  -50,  -50,   59,  -50,  -50,  -50,
+      -50,  -50,  -50,  -50,  -50,  -50,  -50,  -50
+    },
+
+    {
+        5,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,
+      -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,
+      -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,
+      -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,
+      -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,
+      -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,
+      -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,
+
+      -51,   60,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,
+      -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,
+      -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,
+      -51,  -51,  -51,   60,  -51,  -51,  -51,  -51,  -51,  -51,
+      -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51,
+      -51,  -51,  -51,  -51,  -51,  -51,  -51,  -51
+    },
+
+    {
+        5,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,
+      -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,
+      -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,
+      -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,
+
+      -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,
+      -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,
+      -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,
+      -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,
+      -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,
+      -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,
+      -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,
+      -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52,
+      -52,  -52,  -52,  -52,  -52,  -52,  -52,  -52
+    },
+
+    {
+        5,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,
+
+      -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,
+      -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,
+      -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,
+      -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,   54,   54,
+       54,   54,   54,   54,   54,   54,   54,   54,  -53,  -53,
+      -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,
+      -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,
+      -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,
+      -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,
+      -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,
+
+      -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53,
+      -53,  -53,  -53,  -53,  -53,  -53,  -53,  -53
+    },
+
+    {
+        5,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,
+      -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,
+      -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,
+      -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,
+      -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,   54,   54,
+       54,   54,   54,   54,   54,   54,   54,   54,  -54,  -54,
+      -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,
+      -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,
+
+      -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,
+      -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,
+      -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,
+      -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54,
+      -54,  -54,  -54,  -54,  -54,  -54,  -54,  -54
+    },
+
+    {
+        5,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,
+      -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,
+      -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,
+      -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,
+      -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,
+
+      -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,
+      -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,
+      -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,
+      -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,
+      -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,
+      -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,
+      -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55,
+      -55,  -55,  -55,  -55,  -55,  -55,  -55,  -55
+    },
+
+    {
+        5,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,
+      -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,
+
+      -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,
+      -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,
+      -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,
+      -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,
+      -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,
+      -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,
+      -56,  -56,  -56,  -56,   61,  -56,  -56,  -56,  -56,  -56,
+      -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,
+      -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56,
+      -56,  -56,  -56,  -56,  -56,  -56,   61,  -56,  -56,  -56,
+
+      -56,  -56,  -56,  -56,  -56,  -56,  -56,  -56
+    },
+
+    {
+        5,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,
+      -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,
+      -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,
+      -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,
+      -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,
+      -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,
+      -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,
+      -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,
+      -57,  -57,  -57,  -57,   62,  -57,  -57,  -57,  -57,  -57,
+
+      -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,
+      -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57,
+      -57,  -57,  -57,  -57,  -57,  -57,   62,  -57,  -57,  -57,
+      -57,  -57,  -57,  -57,  -57,  -57,  -57,  -57
+    },
+
+    {
+        5,  -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,
+      -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,
+      -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,
+      -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,
+      -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,
+      -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,
+
+      -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,
+      -58,  -58,  -58,  -58,  -58,  -58,   63,  -58,  -58,  -58,
+       64,  -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,
+      -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,
+      -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58,   63,  -58,
+      -58,  -58,   64,  -58,  -58,  -58,  -58,  -58,  -58,  -58,
+      -58,  -58,  -58,  -58,  -58,  -58,  -58,  -58
+    },
+
+    {
+        5,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,
+      -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,
+      -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,
+
+      -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,
+      -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,
+      -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,
+      -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,
+      -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,
+      -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,
+      -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,
+      -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,
+      -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59,
+      -59,  -59,  -59,  -59,  -59,  -59,  -59,  -59
+
+    },
+
+    {
+        5,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,
+      -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,
+      -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,
+      -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,
+      -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,
+      -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,
+      -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,
+      -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,   65,
+      -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,
+      -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,
+
+      -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,
+      -60,   65,  -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60,
+      -60,  -60,  -60,  -60,  -60,  -60,  -60,  -60
+    },
+
+    {
+        5,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,
+      -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,
+      -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,
+      -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,
+      -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,
+      -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,
+      -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,
+
+      -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,
+      -61,  -61,   66,  -61,  -61,  -61,  -61,  -61,  -61,  -61,
+      -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,
+      -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61,
+      -61,  -61,  -61,  -61,   66,  -61,  -61,  -61,  -61,  -61,
+      -61,  -61,  -61,  -61,  -61,  -61,  -61,  -61
+    },
+
+    {
+        5,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,
+      -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,
+      -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,
+      -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,
+
+      -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,
+      -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,
+      -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,
+      -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,
+      -62,  -62,   67,  -62,  -62,  -62,  -62,  -62,  -62,  -62,
+      -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,
+      -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62,
+      -62,  -62,  -62,  -62,   67,  -62,  -62,  -62,  -62,  -62,
+      -62,  -62,  -62,  -62,  -62,  -62,  -62,  -62
+    },
+
+    {
+        5,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,
+
+      -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,
+      -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,
+      -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,
+      -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,
+      -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,
+      -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,
+      -63,  -63,  -63,   68,  -63,  -63,  -63,  -63,  -63,  -63,
+      -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,
+      -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,
+      -63,  -63,  -63,  -63,  -63,   68,  -63,  -63,  -63,  -63,
+
+      -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63,
+      -63,  -63,  -63,  -63,  -63,  -63,  -63,  -63
+    },
+
+    {
+        5,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,
+      -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,
+      -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,
+      -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,
+      -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,
+      -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,
+      -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,
+      -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,   69,
+
+      -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,
+      -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,
+      -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,
+      -64,   69,  -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64,
+      -64,  -64,  -64,  -64,  -64,  -64,  -64,  -64
+    },
+
+    {
+        5,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,
+      -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,
+      -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,
+      -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,
+      -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,
+
+      -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,
+      -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,
+      -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,   70,  -65,
+      -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,
+      -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,
+      -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,
+       70,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65,
+      -65,  -65,  -65,  -65,  -65,  -65,  -65,  -65
+    },
+
+    {
+        5,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,
+      -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,
+
+      -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,
+      -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,
+      -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,
+      -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,
+      -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,
+      -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,
+      -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,   71,
+      -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,
+      -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,
+      -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,  -66,
+
+      -66,   71,  -66,  -66,  -66,  -66,  -66,  -66
+    },
+
+    {
+        5,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,
+      -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,
+      -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,
+      -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,
+      -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,
+      -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,
+      -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,
+      -67,  -67,  -67,   72,  -67,  -67,  -67,  -67,  -67,  -67,
+      -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,
+
+      -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,
+      -67,  -67,  -67,  -67,  -67,   72,  -67,  -67,  -67,  -67,
+      -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67,
+      -67,  -67,  -67,  -67,  -67,  -67,  -67,  -67
+    },
+
+    {
+        5,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,
+      -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,
+      -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,
+      -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,
+      -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,
+      -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,
+
+      -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,
+      -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,   73,  -68,
+      -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,
+      -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,
+      -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,
+       73,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68,
+      -68,  -68,  -68,  -68,  -68,  -68,  -68,  -68
+    },
+
+    {
+        5,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,
+      -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,
+      -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,
+
+      -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,
+      -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,
+      -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,
+      -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,
+      -69,  -69,  -69,   74,  -69,  -69,   75,  -69,  -69,  -69,
+      -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,
+      -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,
+      -69,  -69,  -69,  -69,  -69,   74,  -69,  -69,   75,  -69,
+      -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69,
+      -69,  -69,  -69,  -69,  -69,  -69,  -69,  -69
+
+    },
+
+    {
+        5,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,
+      -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,
+      -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,
+      -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,
+      -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,
+      -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,
+      -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,
+      -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,
+      -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,
+      -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,
+
+      -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,
+      -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70,
+      -70,  -70,  -70,  -70,  -70,  -70,  -70,  -70
+    },
+
+    {
+        5,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,
+      -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,
+      -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,
+      -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,
+      -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,
+      -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,
+      -71,  -71,  -71,  -71,  -71,  -71,  -71,   76,  -71,  -71,
+
+      -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,
+      -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,
+      -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,   76,
+      -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,
+      -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71,
+      -71,  -71,  -71,  -71,  -71,  -71,  -71,  -71
+    },
+
+    {
+        5,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,
+      -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,
+      -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,
+      -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,
+
+      -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,
+      -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,
+      -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,
+      -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,   77,  -72,
+      -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,
+      -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,
+      -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,
+       77,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72,
+      -72,  -72,  -72,  -72,  -72,  -72,  -72,  -72
+    },
+
+    {
+        5,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,
+
+      -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,
+      -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,
+      -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,
+      -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,
+      -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,
+      -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,   78,
+      -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,
+      -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,
+      -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,
+      -73,   78,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,
+
+      -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73,
+      -73,  -73,  -73,  -73,  -73,  -73,  -73,  -73
+    },
+
+    {
+        5,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,
+      -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,
+      -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,
+      -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,
+      -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,
+      -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,
+      -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,
+      -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,   79,  -74,
+
+      -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,
+      -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,
+      -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,
+       79,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74,
+      -74,  -74,  -74,  -74,  -74,  -74,  -74,  -74
+    },
+
+    {
+        5,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,
+      -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,
+      -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,
+      -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,
+      -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,
+
+      -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,
+      -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,
+      -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,
+      -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,   80,
+      -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,
+      -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,
+      -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,  -75,
+      -75,   80,  -75,  -75,  -75,  -75,  -75,  -75
+    },
+
+    {
+        5,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,
+      -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,
+
+      -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,
+      -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,
+      -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,
+      -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,
+      -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,
+      -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,   81,
+      -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,
+      -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,
+      -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,
+      -76,   81,  -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76,
+
+      -76,  -76,  -76,  -76,  -76,  -76,  -76,  -76
+    },
+
+    {
+        5,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,
+      -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,
+      -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,
+      -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,
+      -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,
+      -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,
+      -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,
+      -77,   82,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,
+      -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,
+
+      -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,
+      -77,  -77,  -77,   82,  -77,  -77,  -77,  -77,  -77,  -77,
+      -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77,
+      -77,  -77,  -77,  -77,  -77,  -77,  -77,  -77
+    },
+
+    {
+        5,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,
+      -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,
+      -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,
+      -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,
+      -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,
+      -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,
+
+      -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,
+      -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,
+      -78,  -78,  -78,   83,  -78,  -78,  -78,  -78,  -78,  -78,
+      -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,
+      -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78,
+      -78,  -78,  -78,  -78,  -78,   83,  -78,  -78,  -78,  -78,
+      -78,  -78,  -78,  -78,  -78,  -78,  -78,  -78
+    },
+
+    {
+        5,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,
+      -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,
+      -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,
+
+      -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,
+      -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,
+      -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,
+      -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,
+      -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,
+      -79,  -79,  -79,  -79,   84,  -79,  -79,  -79,  -79,  -79,
+      -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,
+      -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79,
+      -79,  -79,  -79,  -79,  -79,  -79,   84,  -79,  -79,  -79,
+      -79,  -79,  -79,  -79,  -79,  -79,  -79,  -79
+
+    },
+
+    {
+        5,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,
+      -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,
+      -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,
+      -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,
+      -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,
+      -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,
+      -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,
+      -80,   85,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,
+      -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,
+      -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,
+
+      -80,  -80,  -80,   85,  -80,  -80,  -80,  -80,  -80,  -80,
+      -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80,
+      -80,  -80,  -80,  -80,  -80,  -80,  -80,  -80
+    },
+
+    {
+        5,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,
+      -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,
+      -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,
+      -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,
+      -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,
+      -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,
+      -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,
+
+      -81,  -81,  -81,  -81,  -81,  -81,   86,  -81,  -81,  -81,
+      -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,
+      -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,
+      -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,   86,  -81,
+      -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81,
+      -81,  -81,  -81,  -81,  -81,  -81,  -81,  -81
+    },
+
+    {
+        5,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,
+      -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,
+      -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,
+      -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,
+
+      -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,
+      -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,
+      -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,
+      -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,
+      -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,
+      -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,
+      -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,
+      -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82,
+      -82,  -82,  -82,  -82,  -82,  -82,  -82,  -82
+    },
+
+    {
+        5,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,
+
+      -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,
+      -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,
+      -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,
+      -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,
+      -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,
+      -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,
+      -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,
+      -83,  -83,  -83,  -83,   87,  -83,  -83,  -83,  -83,  -83,
+      -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,
+      -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83,
+
+      -83,  -83,  -83,  -83,  -83,  -83,   87,  -83,  -83,  -83,
+      -83,  -83,  -83,  -83,  -83,  -83,  -83,  -83
+    },
+
+    {
+        5,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,
+      -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,
+      -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,
+      -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,
+      -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,
+      -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,
+      -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,
+      -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,
+
+      -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,
+      -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,
+      -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,
+      -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84,
+      -84,  -84,  -84,  -84,  -84,  -84,  -84,  -84
+    },
+
+    {
+        5,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,
+      -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,
+      -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,
+      -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,
+      -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,
+
+      -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,
+      -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,
+      -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,   88,
+      -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,
+      -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,
+      -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,
+      -85,   88,  -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85,
+      -85,  -85,  -85,  -85,  -85,  -85,  -85,  -85
+    },
+
+    {
+        5,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,
+      -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,
+
+      -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,
+      -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,
+      -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,
+      -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,
+      -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,
+      -86,  -86,  -86,  -86,  -86,  -86,   89,  -86,  -86,  -86,
+      -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,
+      -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,
+      -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,   89,  -86,
+      -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86,
+
+      -86,  -86,  -86,  -86,  -86,  -86,  -86,  -86
+    },
+
+    {
+        5,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,
+      -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,
+      -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,
+      -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,
+      -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,
+      -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,
+      -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,
+      -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,
+      -87,  -87,   90,  -87,  -87,  -87,  -87,  -87,  -87,  -87,
+
+      -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,
+      -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87,
+      -87,  -87,  -87,  -87,   90,  -87,  -87,  -87,  -87,  -87,
+      -87,  -87,  -87,  -87,  -87,  -87,  -87,  -87
+    },
+
+    {
+        5,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,
+      -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,
+      -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,
+      -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,
+      -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,
+      -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,
+
+      -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,
+      -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,   91,  -88,
+      -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,
+      -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,
+      -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,
+       91,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88,
+      -88,  -88,  -88,  -88,  -88,  -88,  -88,  -88
+    },
+
+    {
+        5,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,
+      -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,
+      -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,
+
+      -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,
+      -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,
+      -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,
+      -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,   92,
+      -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,
+      -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,
+      -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,
+      -89,   92,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,
+      -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89,
+      -89,  -89,  -89,  -89,  -89,  -89,  -89,  -89
+
+    },
+
+    {
+        5,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,
+      -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,
+      -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,
+      -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,
+      -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,
+      -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,
+      -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,
+      -90,  -90,  -90,   93,  -90,  -90,  -90,  -90,  -90,  -90,
+      -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,
+      -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,
+
+      -90,  -90,  -90,  -90,  -90,   93,  -90,  -90,  -90,  -90,
+      -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90,
+      -90,  -90,  -90,  -90,  -90,  -90,  -90,  -90
+    },
+
+    {
+        5,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,
+      -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,
+      -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,
+      -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,
+      -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,
+      -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,
+      -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,
+
+      -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,
+      -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,
+      -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,
+      -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,
+      -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91,
+      -91,  -91,  -91,  -91,  -91,  -91,  -91,  -91
+    },
+
+    {
+        5,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,
+      -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,
+      -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,
+      -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,
+
+      -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,
+      -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,
+      -92,  -92,  -92,  -92,  -92,  -92,  -92,   94,  -92,  -92,
+      -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,
+      -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,
+      -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,   94,
+      -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,
+      -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92,
+      -92,  -92,  -92,  -92,  -92,  -92,  -92,  -92
+    },
+
+    {
+        5,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,
+
+      -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,
+      -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,
+      -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,
+      -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,
+      -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,
+      -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,
+      -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,   95,  -93,
+      -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,
+      -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,
+      -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,
+
+       95,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93,
+      -93,  -93,  -93,  -93,  -93,  -93,  -93,  -93
+    },
+
+    {
+        5,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,
+      -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,
+      -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,
+      -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,
+      -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,
+      -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,
+      -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,
+      -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,
+
+      -94,  -94,  -94,  -94,   96,  -94,  -94,  -94,  -94,  -94,
+      -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,
+      -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94,
+      -94,  -94,  -94,  -94,  -94,  -94,   96,  -94,  -94,  -94,
+      -94,  -94,  -94,  -94,  -94,  -94,  -94,  -94
+    },
+
+    {
+        5,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,
+      -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,
+      -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,
+      -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,
+      -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,
+
+      -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,
+      -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,
+      -95,   97,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,
+      -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,
+      -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,
+      -95,  -95,  -95,   97,  -95,  -95,  -95,  -95,  -95,  -95,
+      -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95,
+      -95,  -95,  -95,  -95,  -95,  -95,  -95,  -95
+    },
+
+    {
+        5,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,
+      -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,
+
+      -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,
+      -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,
+      -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,
+      -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,
+      -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,
+      -96,  -96,  -96,   98,  -96,  -96,  -96,  -96,  -96,  -96,
+      -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,
+      -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,
+      -96,  -96,  -96,  -96,  -96,   98,  -96,  -96,  -96,  -96,
+      -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96,
+
+      -96,  -96,  -96,  -96,  -96,  -96,  -96,  -96
+    },
+
+    {
+        5,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,
+      -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,
+      -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,
+      -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,
+      -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,
+      -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,
+      -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,
+      -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,
+      -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,
+
+      -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,
+      -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,
+      -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97,
+      -97,  -97,  -97,  -97,  -97,  -97,  -97,  -97
+    },
+
+    {
+        5,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,
+      -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,
+      -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,
+      -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,
+      -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,
+      -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,
+
+      -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,
+      -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,   99,
+      -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,
+      -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,
+      -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,
+      -98,   99,  -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98,
+      -98,  -98,  -98,  -98,  -98,  -98,  -98,  -98
+    },
+
+    {
+        5,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,
+      -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,
+      -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,
+
+      -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,
+      -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,
+      -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,
+      -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,
+      -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  100,  -99,
+      -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,
+      -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,
+      -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,
+      100,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99,
+      -99,  -99,  -99,  -99,  -99,  -99,  -99,  -99
+
+    },
+
+    {
+        5, -100, -100, -100, -100, -100, -100, -100, -100, -100,
+     -100, -100, -100, -100, -100, -100, -100, -100, -100, -100,
+     -100, -100, -100, -100, -100, -100, -100, -100, -100, -100,
+     -100, -100, -100, -100, -100, -100, -100, -100, -100, -100,
+     -100, -100, -100, -100, -100, -100, -100, -100, -100, -100,
+     -100, -100, -100, -100, -100, -100, -100, -100, -100, -100,
+     -100, -100, -100, -100, -100, -100, -100, -100, -100, -100,
+     -100, -100, -100, -100, -100, -100, -100, -100, -100, -100,
+     -100, -100, -100, -100, -100, -100, -100, -100, -100, -100,
+     -100, -100, -100, -100, -100, -100, -100, -100, -100, -100,
+
+     -100, -100, -100, -100, -100, -100, -100, -100, -100, -100,
+     -100, -100, -100, -100, -100, -100, -100, -100, -100, -100,
+     -100, -100, -100, -100, -100, -100, -100, -100
+    },
+
+    } ;
+
+
+static yy_state_type yy_get_previous_state YY_PROTO(( void ));
+static yy_state_type yy_try_NUL_trans YY_PROTO(( yy_state_type current_state ));
+static int yy_get_next_buffer YY_PROTO(( void ));
+static void yy_fatal_error YY_PROTO(( yyconst char msg[] ));
+
+/* Done after the current pattern has been matched and before the
+ * corresponding action - sets up yytext.
+ */
+#define YY_DO_BEFORE_ACTION \
+       yytext_ptr = yy_bp; \
+       yyleng = (int) (yy_cp - yy_bp); \
+       yy_hold_char = *yy_cp; \
+       *yy_cp = '\0'; \
+       yy_c_buf_p = yy_cp;
+
+#define YY_NUM_RULES 20
+#define YY_END_OF_BUFFER 21
+static yyconst short int yy_accept[101] =
+    {   0,
+        0,    0,    1,    1,   21,   19,   18,   18,   13,   14,
+       15,   19,   17,   16,   19,   19,   19,   19,   19,   19,
+        1,   19,    1,   18,    2,    3,    0,    0,    0,    0,
+        0,    0,    0,    1,    1,    2,    3,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,   11,    0,    1,   12,    0,    0,    0,    4,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    6,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    5,    0,    7,    0,    0,    0,    0,    0,    0,
+        9,    0,    0,    0,    0,    0,    8,    0,    0,   10
+
+    } ;
+
+static yy_state_type yy_last_accepting_state;
+static char *yy_last_accepting_cpos;
+
+static yyconst yy_state_type yy_NUL_trans[101] =
+    {   0,
+        6,    6,    6,    6,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
+        0,    0,    0,    0,    0,    0,    0,    0,    0,    0
+
+    } ;
+
+/* The intent behind this definition is that it'll catch
+ * any uses of REJECT which flex missed.
+ */
+#define REJECT reject_used_but_not_detected
+#define yymore() yymore_used_but_not_detected
+#define YY_MORE_ADJ 0
+#define YY_RESTORE_YY_MORE_OFFSET
+char *yytext;
+#line 1 "wktparse.lex"
+#define INITIAL 0
+/*
+ * Written by Ralph Mason ralph.mason<at>telogis.com
+ *
+ * Copyright Telogis 2004
+ * www.telogis.com
+ *
+ */
+#define vals_ok 1
+
+#line 11 "wktparse.lex"
+#include "wktparse.tab.h"
+
+static YY_BUFFER_STATE buf_state;
+   void init_parser(char *src) { BEGIN(0);buf_state = lwg_parse_yy_scan_string(src); }
+   void close_parser() { lwg_parse_yy_delete_buffer(buf_state); }
+   int lwg_parse_yywrap(void){ return 1; }
+
+#line 2118 "lex.yy.c"
+
+/* Macros after this point can all be overridden by user definitions in
+ * section 1.
+ */
+
+#ifndef YY_SKIP_YYWRAP
+#ifdef __cplusplus
+extern "C" int yywrap YY_PROTO(( void ));
+#else
+extern int yywrap YY_PROTO(( void ));
+#endif
+#endif
+
+#ifndef YY_NO_UNPUT
+static void yyunput YY_PROTO(( int c, char *buf_ptr ));
+#endif
+
+#ifndef yytext_ptr
+static void yy_flex_strncpy YY_PROTO(( char *, yyconst char *, int ));
+#endif
+
+#ifdef YY_NEED_STRLEN
+static int yy_flex_strlen YY_PROTO(( yyconst char * ));
+#endif
+
+#ifndef YY_NO_INPUT
+#ifdef __cplusplus
+static int yyinput YY_PROTO(( void ));
+#else
+static int input YY_PROTO(( void ));
+#endif
+#endif
+
+#if YY_STACK_USED
+static int yy_start_stack_ptr = 0;
+static int yy_start_stack_depth = 0;
+static int *yy_start_stack = 0;
+#ifndef YY_NO_PUSH_STATE
+static void yy_push_state YY_PROTO(( int new_state ));
+#endif
+#ifndef YY_NO_POP_STATE
+static void yy_pop_state YY_PROTO(( void ));
+#endif
+#ifndef YY_NO_TOP_STATE
+static int yy_top_state YY_PROTO(( void ));
+#endif
+
+#else
+#define YY_NO_PUSH_STATE 1
+#define YY_NO_POP_STATE 1
+#define YY_NO_TOP_STATE 1
+#endif
+
+#ifdef YY_MALLOC_DECL
+YY_MALLOC_DECL
+#else
+#if __STDC__
+#ifndef __cplusplus
+#include <stdlib.h>
+#endif
+#else
+/* Just try to get by without declaring the routines.  This will fail
+ * miserably on non-ANSI systems for which sizeof(size_t) != sizeof(int)
+ * or sizeof(void*) != sizeof(int).
+ */
+#endif
+#endif
+
+/* Amount of stuff to slurp up with each read. */
+#ifndef YY_READ_BUF_SIZE
+#define YY_READ_BUF_SIZE 8192
+#endif
+
+/* Copy whatever the last rule matched to the standard output. */
+
+#ifndef ECHO
+/* This used to be an fputs(), but since the string might contain NUL's,
+ * we now use fwrite().
+ */
+#define ECHO (void) fwrite( yytext, yyleng, 1, yyout )
+#endif
+
+/* Gets input and stuffs it into "buf".  number of characters read, or YY_NULL,
+ * is returned in "result".
+ */
+#ifndef YY_INPUT
+#define YY_INPUT(buf,result,max_size) \
+       if ( (result = read( fileno(yyin), (char *) buf, max_size )) < 0 ) \
+               YY_FATAL_ERROR( "input in flex scanner failed" );
+#endif
+
+/* No semi-colon after return; correct usage is to write "yyterminate();" -
+ * we don't want an extra ';' after the "return" because that will cause
+ * some compilers to complain about unreachable statements.
+ */
+#ifndef yyterminate
+#define yyterminate() return YY_NULL
+#endif
+
+/* Number of entries by which start-condition stack grows. */
+#ifndef YY_START_STACK_INCR
+#define YY_START_STACK_INCR 25
+#endif
+
+/* Report a fatal error. */
+#ifndef YY_FATAL_ERROR
+#define YY_FATAL_ERROR(msg) yy_fatal_error( msg )
+#endif
+
+/* Default declaration of generated scanner - a define so the user can
+ * easily add parameters.
+ */
+#ifndef YY_DECL
+#define YY_DECL int yylex YY_PROTO(( void ))
+#endif
+
+/* Code executed at the beginning of each rule, after yytext and yyleng
+ * have been set up.
+ */
+#ifndef YY_USER_ACTION
+#define YY_USER_ACTION
+#endif
+
+/* Code executed at the end of each rule. */
+#ifndef YY_BREAK
+#define YY_BREAK break;
+#endif
+
+#define YY_RULE_SETUP \
+       YY_USER_ACTION
+
+YY_DECL
+       {
+       register yy_state_type yy_current_state;
+       register char *yy_cp = NULL, *yy_bp = NULL;
+       register int yy_act;
+
+#line 20 "wktparse.lex"
+
+
+
+#line 2260 "lex.yy.c"
+
+       if ( yy_init )
+               {
+               yy_init = 0;
+
+#ifdef YY_USER_INIT
+               YY_USER_INIT;
+#endif
+
+               if ( ! yy_start )
+                       yy_start = 1;   /* first start state */
+
+               if ( ! yyin )
+                       yyin = stdin;
+
+               if ( ! yyout )
+                       yyout = stdout;
+
+               if ( ! yy_current_buffer )
+                       yy_current_buffer =
+                               yy_create_buffer( yyin, YY_BUF_SIZE );
+
+               yy_load_buffer_state();
+               }
+
+       while ( 1 )             /* loops until end-of-file is reached */
+               {
+               yy_cp = yy_c_buf_p;
+
+               /* Support of yytext. */
+               *yy_cp = yy_hold_char;
+
+               /* yy_bp points to the position in yy_ch_buf of the start of
+                * the current run.
+                */
+               yy_bp = yy_cp;
+
+               yy_current_state = yy_start;
+yy_match:
+               while ( (yy_current_state = yy_nxt[yy_current_state][YY_SC_TO_UI(*yy_cp)]) > 0 )
+                       {
+                       if ( yy_accept[yy_current_state] )
+                               {
+                               yy_last_accepting_state = yy_current_state;
+                               yy_last_accepting_cpos = yy_cp;
+                               }
+
+                       ++yy_cp;
+                       }
+
+               yy_current_state = -yy_current_state;
+
+yy_find_action:
+               yy_act = yy_accept[yy_current_state];
+
+               YY_DO_BEFORE_ACTION;
+
+
+do_action:     /* This label is used only to access EOF actions. */
+
+
+               switch ( yy_act )
+       { /* beginning of action switch */
+                       case 0: /* must back up */
+                       /* undo the effects of YY_DO_BEFORE_ACTION */
+                       *yy_cp = yy_hold_char;
+                       yy_cp = yy_last_accepting_cpos + 1;
+                       yy_current_state = yy_last_accepting_state;
+                       goto yy_find_action;
+
+case 1:
+YY_RULE_SETUP
+#line 23 "wktparse.lex"
+{ lwg_parse_yylval.value=atof(lwg_parse_yytext); return VALUE; }
+       YY_BREAK
+case 2:
+YY_RULE_SETUP
+#line 25 "wktparse.lex"
+{  lwg_parse_yylval.wkb=lwg_parse_yytext; return WKB;}
+       YY_BREAK
+case 3:
+YY_RULE_SETUP
+#line 26 "wktparse.lex"
+{  lwg_parse_yylval.wkb=lwg_parse_yytext; return WKB;}
+       YY_BREAK
+case 4:
+YY_RULE_SETUP
+#line 28 "wktparse.lex"
+{ return POINT; }
+       YY_BREAK
+case 5:
+YY_RULE_SETUP
+#line 29 "wktparse.lex"
+{ return LINESTRING; }
+       YY_BREAK
+case 6:
+YY_RULE_SETUP
+#line 30 "wktparse.lex"
+{ return POLYGON; }
+       YY_BREAK
+case 7:
+YY_RULE_SETUP
+#line 31 "wktparse.lex"
+{ return MULTIPOINT; }
+       YY_BREAK
+case 8:
+YY_RULE_SETUP
+#line 32 "wktparse.lex"
+{ return MULTILINESTRING; }
+       YY_BREAK
+case 9:
+YY_RULE_SETUP
+#line 33 "wktparse.lex"
+{ return MULTIPOLYGON; }
+       YY_BREAK
+case 10:
+YY_RULE_SETUP
+#line 34 "wktparse.lex"
+{ return GEOMETRYCOLLECTION; }
+       YY_BREAK
+case 11:
+YY_RULE_SETUP
+#line 35 "wktparse.lex"
+{ BEGIN(vals_ok); return SRID; }
+       YY_BREAK
+case 12:
+YY_RULE_SETUP
+#line 36 "wktparse.lex"
+{ return EMPTY; }
+       YY_BREAK
+case 13:
+YY_RULE_SETUP
+#line 38 "wktparse.lex"
+{ BEGIN(vals_ok); return LPAREN; }
+       YY_BREAK
+case 14:
+YY_RULE_SETUP
+#line 39 "wktparse.lex"
+{ return RPAREN; }
+       YY_BREAK
+case 15:
+YY_RULE_SETUP
+#line 40 "wktparse.lex"
+{ return COMMA ; }
+       YY_BREAK
+case 16:
+YY_RULE_SETUP
+#line 41 "wktparse.lex"
+{ return EQUALS ; }
+       YY_BREAK
+case 17:
+YY_RULE_SETUP
+#line 42 "wktparse.lex"
+{ BEGIN(0); return SEMICOLON; }
+       YY_BREAK
+case 18:
+YY_RULE_SETUP
+#line 43 "wktparse.lex"
+/*eat whitespace*/
+       YY_BREAK
+case 19:
+YY_RULE_SETUP
+#line 44 "wktparse.lex"
+{ return lwg_parse_yytext[0]; }
+       YY_BREAK
+case 20:
+YY_RULE_SETUP
+#line 46 "wktparse.lex"
+ECHO;
+       YY_BREAK
+#line 2431 "lex.yy.c"
+case YY_STATE_EOF(INITIAL):
+case YY_STATE_EOF(vals_ok):
+       yyterminate();
+
+       case YY_END_OF_BUFFER:
+               {
+               /* Amount of text matched not including the EOB char. */
+               int yy_amount_of_matched_text = (int) (yy_cp - yytext_ptr) - 1;
+
+               /* Undo the effects of YY_DO_BEFORE_ACTION. */
+               *yy_cp = yy_hold_char;
+               YY_RESTORE_YY_MORE_OFFSET
+
+               if ( yy_current_buffer->yy_buffer_status == YY_BUFFER_NEW )
+                       {
+                       /* We're scanning a new file or input source.  It's
+                        * possible that this happened because the user
+                        * just pointed yyin at a new source and called
+                        * yylex().  If so, then we have to assure
+                        * consistency between yy_current_buffer and our
+                        * globals.  Here is the right place to do so, because
+                        * this is the first action (other than possibly a
+                        * back-up) that will match for the new input source.
+                        */
+                       yy_n_chars = yy_current_buffer->yy_n_chars;
+                       yy_current_buffer->yy_input_file = yyin;
+                       yy_current_buffer->yy_buffer_status = YY_BUFFER_NORMAL;
+                       }
+
+               /* Note that here we test for yy_c_buf_p "<=" to the position
+                * of the first EOB in the buffer, since yy_c_buf_p will
+                * already have been incremented past the NUL character
+                * (since all states make transitions on EOB to the
+                * end-of-buffer state).  Contrast this with the test
+                * in input().
+                */
+               if ( yy_c_buf_p <= &yy_current_buffer->yy_ch_buf[yy_n_chars] )
+                       { /* This was really a NUL. */
+                       yy_state_type yy_next_state;
+
+                       yy_c_buf_p = yytext_ptr + yy_amount_of_matched_text;
+
+                       yy_current_state = yy_get_previous_state();
+
+                       /* Okay, we're now positioned to make the NUL
+                        * transition.  We couldn't have
+                        * yy_get_previous_state() go ahead and do it
+                        * for us because it doesn't know how to deal
+                        * with the possibility of jamming (and we don't
+                        * want to build jamming into it because then it
+                        * will run more slowly).
+                        */
+
+                       yy_next_state = yy_try_NUL_trans( yy_current_state );
+
+                       yy_bp = yytext_ptr + YY_MORE_ADJ;
+
+                       if ( yy_next_state )
+                               {
+                               /* Consume the NUL. */
+                               yy_cp = ++yy_c_buf_p;
+                               yy_current_state = yy_next_state;
+                               goto yy_match;
+                               }
+
+                       else
+                               {
+                               yy_cp = yy_c_buf_p;
+                               goto yy_find_action;
+                               }
+                       }
+
+               else switch ( yy_get_next_buffer() )
+                       {
+                       case EOB_ACT_END_OF_FILE:
+                               {
+                               yy_did_buffer_switch_on_eof = 0;
+
+                               if ( yywrap() )
+                                       {
+                                       /* Note: because we've taken care in
+                                        * yy_get_next_buffer() to have set up
+                                        * yytext, we can now set up
+                                        * yy_c_buf_p so that if some total
+                                        * hoser (like flex itself) wants to
+                                        * call the scanner after we return the
+                                        * YY_NULL, it'll still work - another
+                                        * YY_NULL will get returned.
+                                        */
+                                       yy_c_buf_p = yytext_ptr + YY_MORE_ADJ;
+
+                                       yy_act = YY_STATE_EOF(YY_START);
+                                       goto do_action;
+                                       }
+
+                               else
+                                       {
+                                       if ( ! yy_did_buffer_switch_on_eof )
+                                               YY_NEW_FILE;
+                                       }
+                               break;
+                               }
+
+                       case EOB_ACT_CONTINUE_SCAN:
+                               yy_c_buf_p =
+                                       yytext_ptr + yy_amount_of_matched_text;
+
+                               yy_current_state = yy_get_previous_state();
+
+                               yy_cp = yy_c_buf_p;
+                               yy_bp = yytext_ptr + YY_MORE_ADJ;
+                               goto yy_match;
+
+                       case EOB_ACT_LAST_MATCH:
+                               yy_c_buf_p =
+                               &yy_current_buffer->yy_ch_buf[yy_n_chars];
+
+                               yy_current_state = yy_get_previous_state();
+
+                               yy_cp = yy_c_buf_p;
+                               yy_bp = yytext_ptr + YY_MORE_ADJ;
+                               goto yy_find_action;
+                       }
+               break;
+               }
+
+       default:
+               YY_FATAL_ERROR(
+                       "fatal flex scanner internal error--no action found" );
+       } /* end of action switch */
+               } /* end of scanning one token */
+       } /* end of yylex */
+
+
+/* yy_get_next_buffer - try to read in a new buffer
+ *
+ * Returns a code representing an action:
+ *     EOB_ACT_LAST_MATCH -
+ *     EOB_ACT_CONTINUE_SCAN - continue scanning from current position
+ *     EOB_ACT_END_OF_FILE - end of file
+ */
+
+static int yy_get_next_buffer()
+       {
+       register char *dest = yy_current_buffer->yy_ch_buf;
+       register char *source = yytext_ptr;
+       register int number_to_move, i;
+       int ret_val;
+
+       if ( yy_c_buf_p > &yy_current_buffer->yy_ch_buf[yy_n_chars + 1] )
+               YY_FATAL_ERROR(
+               "fatal flex scanner internal error--end of buffer missed" );
+
+       if ( yy_current_buffer->yy_fill_buffer == 0 )
+               { /* Don't try to fill the buffer, so this is an EOF. */
+               if ( yy_c_buf_p - yytext_ptr - YY_MORE_ADJ == 1 )
+                       {
+                       /* We matched a single character, the EOB, so
+                        * treat this as a final EOF.
+                        */
+                       return EOB_ACT_END_OF_FILE;
+                       }
+
+               else
+                       {
+                       /* We matched some text prior to the EOB, first
+                        * process it.
+                        */
+                       return EOB_ACT_LAST_MATCH;
+                       }
+               }
+
+       /* Try to read more data. */
+
+       /* First move last chars to start of buffer. */
+       number_to_move = (int) (yy_c_buf_p - yytext_ptr) - 1;
+
+       for ( i = 0; i < number_to_move; ++i )
+               *(dest++) = *(source++);
+
+       if ( yy_current_buffer->yy_buffer_status == YY_BUFFER_EOF_PENDING )
+               /* don't do the read, it's not guaranteed to return an EOF,
+                * just force an EOF
+                */
+               yy_current_buffer->yy_n_chars = yy_n_chars = 0;
+
+       else
+               {
+               int num_to_read =
+                       yy_current_buffer->yy_buf_size - number_to_move - 1;
+
+               while ( num_to_read <= 0 )
+                       { /* Not enough room in the buffer - grow it. */
+#ifdef YY_USES_REJECT
+                       YY_FATAL_ERROR(
+"input buffer overflow, can't enlarge buffer because scanner uses REJECT" );
+#else
+
+                       /* just a shorter name for the current buffer */
+                       YY_BUFFER_STATE b = yy_current_buffer;
+
+                       int yy_c_buf_p_offset =
+                               (int) (yy_c_buf_p - b->yy_ch_buf);
+
+                       if ( b->yy_is_our_buffer )
+                               {
+                               int new_size = b->yy_buf_size * 2;
+
+                               if ( new_size <= 0 )
+                                       b->yy_buf_size += b->yy_buf_size / 8;
+                               else
+                                       b->yy_buf_size *= 2;
+
+                               b->yy_ch_buf = (char *)
+                                       /* Include room in for 2 EOB chars. */
+                                       yy_flex_realloc( (void *) b->yy_ch_buf,
+                                                        b->yy_buf_size + 2 );
+                               }
+                       else
+                               /* Can't grow it, we don't own it. */
+                               b->yy_ch_buf = 0;
+
+                       if ( ! b->yy_ch_buf )
+                               YY_FATAL_ERROR(
+                               "fatal error - scanner input buffer overflow" );
+
+                       yy_c_buf_p = &b->yy_ch_buf[yy_c_buf_p_offset];
+
+                       num_to_read = yy_current_buffer->yy_buf_size -
+                                               number_to_move - 1;
+#endif
+                       }
+
+               if ( num_to_read > YY_READ_BUF_SIZE )
+                       num_to_read = YY_READ_BUF_SIZE;
+
+               /* Read in more data. */
+               YY_INPUT( (&yy_current_buffer->yy_ch_buf[number_to_move]),
+                       yy_n_chars, num_to_read );
+
+               yy_current_buffer->yy_n_chars = yy_n_chars;
+               }
+
+       if ( yy_n_chars == 0 )
+               {
+               if ( number_to_move == YY_MORE_ADJ )
+                       {
+                       ret_val = EOB_ACT_END_OF_FILE;
+                       yyrestart( yyin );
+                       }
+
+               else
+                       {
+                       ret_val = EOB_ACT_LAST_MATCH;
+                       yy_current_buffer->yy_buffer_status =
+                               YY_BUFFER_EOF_PENDING;
+                       }
+               }
+
+       else
+               ret_val = EOB_ACT_CONTINUE_SCAN;
+
+       yy_n_chars += number_to_move;
+       yy_current_buffer->yy_ch_buf[yy_n_chars] = YY_END_OF_BUFFER_CHAR;
+       yy_current_buffer->yy_ch_buf[yy_n_chars + 1] = YY_END_OF_BUFFER_CHAR;
+
+       yytext_ptr = &yy_current_buffer->yy_ch_buf[0];
+
+       return ret_val;
+       }
+
+
+/* yy_get_previous_state - get the state just before the EOB char was reached */
+
+static yy_state_type yy_get_previous_state()
+       {
+       register yy_state_type yy_current_state;
+       register char *yy_cp;
+
+       yy_current_state = yy_start;
+
+       for ( yy_cp = yytext_ptr + YY_MORE_ADJ; yy_cp < yy_c_buf_p; ++yy_cp )
+               {
+               if ( *yy_cp )
+                       {
+                       yy_current_state = yy_nxt[yy_current_state][YY_SC_TO_UI(*yy_cp)];
+                       }
+               else
+                       yy_current_state = yy_NUL_trans[yy_current_state];
+               if ( yy_accept[yy_current_state] )
+                       {
+                       yy_last_accepting_state = yy_current_state;
+                       yy_last_accepting_cpos = yy_cp;
+                       }
+               }
+
+       return yy_current_state;
+       }
+
+
+/* yy_try_NUL_trans - try to make a transition on the NUL character
+ *
+ * synopsis
+ *     next_state = yy_try_NUL_trans( current_state );
+ */
+
+#ifdef YY_USE_PROTOS
+static yy_state_type yy_try_NUL_trans( yy_state_type yy_current_state )
+#else
+static yy_state_type yy_try_NUL_trans( yy_current_state )
+yy_state_type yy_current_state;
+#endif
+       {
+       register int yy_is_jam;
+       register char *yy_cp = yy_c_buf_p;
+
+       yy_current_state = yy_NUL_trans[yy_current_state];
+       yy_is_jam = (yy_current_state == 0);
+
+       if ( ! yy_is_jam )
+               {
+               if ( yy_accept[yy_current_state] )
+                       {
+                       yy_last_accepting_state = yy_current_state;
+                       yy_last_accepting_cpos = yy_cp;
+                       }
+               }
+
+       return yy_is_jam ? 0 : yy_current_state;
+       }
+
+
+#ifndef YY_NO_UNPUT
+#ifdef YY_USE_PROTOS
+static void yyunput( int c, register char *yy_bp )
+#else
+static void yyunput( c, yy_bp )
+int c;
+register char *yy_bp;
+#endif
+       {
+       register char *yy_cp = yy_c_buf_p;
+
+       /* undo effects of setting up yytext */
+       *yy_cp = yy_hold_char;
+
+       if ( yy_cp < yy_current_buffer->yy_ch_buf + 2 )
+               { /* need to shift things up to make room */
+               /* +2 for EOB chars. */
+               register int number_to_move = yy_n_chars + 2;
+               register char *dest = &yy_current_buffer->yy_ch_buf[
+                                       yy_current_buffer->yy_buf_size + 2];
+               register char *source =
+                               &yy_current_buffer->yy_ch_buf[number_to_move];
+
+               while ( source > yy_current_buffer->yy_ch_buf )
+                       *--dest = *--source;
+
+               yy_cp += (int) (dest - source);
+               yy_bp += (int) (dest - source);
+               yy_current_buffer->yy_n_chars =
+                       yy_n_chars = yy_current_buffer->yy_buf_size;
+
+               if ( yy_cp < yy_current_buffer->yy_ch_buf + 2 )
+                       YY_FATAL_ERROR( "flex scanner push-back overflow" );
+               }
+
+       *--yy_cp = (char) c;
+
+
+       yytext_ptr = yy_bp;
+       yy_hold_char = *yy_cp;
+       yy_c_buf_p = yy_cp;
+       }
+#endif /* ifndef YY_NO_UNPUT */
+
+
+#ifdef __cplusplus
+static int yyinput()
+#else
+static int input()
+#endif
+       {
+       int c;
+
+       *yy_c_buf_p = yy_hold_char;
+
+       if ( *yy_c_buf_p == YY_END_OF_BUFFER_CHAR )
+               {
+               /* yy_c_buf_p now points to the character we want to return.
+                * If this occurs *before* the EOB characters, then it's a
+                * valid NUL; if not, then we've hit the end of the buffer.
+                */
+               if ( yy_c_buf_p < &yy_current_buffer->yy_ch_buf[yy_n_chars] )
+                       /* This was really a NUL. */
+                       *yy_c_buf_p = '\0';
+
+               else
+                       { /* need more input */
+                       int offset = yy_c_buf_p - yytext_ptr;
+                       ++yy_c_buf_p;
+
+                       switch ( yy_get_next_buffer() )
+                               {
+                               case EOB_ACT_LAST_MATCH:
+                                       /* This happens because yy_g_n_b()
+                                        * sees that we've accumulated a
+                                        * token and flags that we need to
+                                        * try matching the token before
+                                        * proceeding.  But for input(),
+                                        * there's no matching to consider.
+                                        * So convert the EOB_ACT_LAST_MATCH
+                                        * to EOB_ACT_END_OF_FILE.
+                                        */
+
+                                       /* Reset buffer status. */
+                                       yyrestart( yyin );
+
+                                       /* fall through */
+
+                               case EOB_ACT_END_OF_FILE:
+                                       {
+                                       if ( yywrap() )
+                                               return EOF;
+
+                                       if ( ! yy_did_buffer_switch_on_eof )
+                                               YY_NEW_FILE;
+#ifdef __cplusplus
+                                       return yyinput();
+#else
+                                       return input();
+#endif
+                                       }
+
+                               case EOB_ACT_CONTINUE_SCAN:
+                                       yy_c_buf_p = yytext_ptr + offset;
+                                       break;
+                               }
+                       }
+               }
+
+       c = *(unsigned char *) yy_c_buf_p;      /* cast for 8-bit char's */
+       *yy_c_buf_p = '\0';     /* preserve yytext */
+       yy_hold_char = *++yy_c_buf_p;
+
+
+       return c;
+       }
+
+
+#ifdef YY_USE_PROTOS
+void yyrestart( FILE *input_file )
+#else
+void yyrestart( input_file )
+FILE *input_file;
+#endif
+       {
+       if ( ! yy_current_buffer )
+               yy_current_buffer = yy_create_buffer( yyin, YY_BUF_SIZE );
+
+       yy_init_buffer( yy_current_buffer, input_file );
+       yy_load_buffer_state();
+       }
+
+
+#ifdef YY_USE_PROTOS
+void yy_switch_to_buffer( YY_BUFFER_STATE new_buffer )
+#else
+void yy_switch_to_buffer( new_buffer )
+YY_BUFFER_STATE new_buffer;
+#endif
+       {
+       if ( yy_current_buffer == new_buffer )
+               return;
+
+       if ( yy_current_buffer )
+               {
+               /* Flush out information for old buffer. */
+               *yy_c_buf_p = yy_hold_char;
+               yy_current_buffer->yy_buf_pos = yy_c_buf_p;
+               yy_current_buffer->yy_n_chars = yy_n_chars;
+               }
+
+       yy_current_buffer = new_buffer;
+       yy_load_buffer_state();
+
+       /* We don't actually know whether we did this switch during
+        * EOF (yywrap()) processing, but the only time this flag
+        * is looked at is after yywrap() is called, so it's safe
+        * to go ahead and always set it.
+        */
+       yy_did_buffer_switch_on_eof = 1;
+       }
+
+
+#ifdef YY_USE_PROTOS
+void yy_load_buffer_state( void )
+#else
+void yy_load_buffer_state()
+#endif
+       {
+       yy_n_chars = yy_current_buffer->yy_n_chars;
+       yytext_ptr = yy_c_buf_p = yy_current_buffer->yy_buf_pos;
+       yyin = yy_current_buffer->yy_input_file;
+       yy_hold_char = *yy_c_buf_p;
+       }
+
+
+#ifdef YY_USE_PROTOS
+YY_BUFFER_STATE yy_create_buffer( FILE *file, int size )
+#else
+YY_BUFFER_STATE yy_create_buffer( file, size )
+FILE *file;
+int size;
+#endif
+       {
+       YY_BUFFER_STATE b;
+
+       b = (YY_BUFFER_STATE) yy_flex_alloc( sizeof( struct yy_buffer_state ) );
+       if ( ! b )
+               YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
+
+       b->yy_buf_size = size;
+
+       /* yy_ch_buf has to be 2 characters longer than the size given because
+        * we need to put in 2 end-of-buffer characters.
+        */
+       b->yy_ch_buf = (char *) yy_flex_alloc( b->yy_buf_size + 2 );
+       if ( ! b->yy_ch_buf )
+               YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
+
+       b->yy_is_our_buffer = 1;
+
+       yy_init_buffer( b, file );
+
+       return b;
+       }
+
+
+#ifdef YY_USE_PROTOS
+void yy_delete_buffer( YY_BUFFER_STATE b )
+#else
+void yy_delete_buffer( b )
+YY_BUFFER_STATE b;
+#endif
+       {
+       if ( ! b )
+               return;
+
+       if ( b == yy_current_buffer )
+               yy_current_buffer = (YY_BUFFER_STATE) 0;
+
+       if ( b->yy_is_our_buffer )
+               yy_flex_free( (void *) b->yy_ch_buf );
+
+       yy_flex_free( (void *) b );
+       }
+
+
+
+#ifdef YY_USE_PROTOS
+void yy_init_buffer( YY_BUFFER_STATE b, FILE *file )
+#else
+void yy_init_buffer( b, file )
+YY_BUFFER_STATE b;
+FILE *file;
+#endif
+
+
+       {
+       yy_flush_buffer( b );
+
+       b->yy_input_file = file;
+       b->yy_fill_buffer = 1;
+
+#if YY_ALWAYS_INTERACTIVE
+       b->yy_is_interactive = 1;
+#else
+#if YY_NEVER_INTERACTIVE
+       b->yy_is_interactive = 0;
+#else
+       b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0;
+#endif
+#endif
+       }
+
+
+#ifdef YY_USE_PROTOS
+void yy_flush_buffer( YY_BUFFER_STATE b )
+#else
+void yy_flush_buffer( b )
+YY_BUFFER_STATE b;
+#endif
+
+       {
+       if ( ! b )
+               return;
+
+       b->yy_n_chars = 0;
+
+       /* We always need two end-of-buffer characters.  The first causes
+        * a transition to the end-of-buffer state.  The second causes
+        * a jam in that state.
+        */
+       b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR;
+       b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR;
+
+       b->yy_buf_pos = &b->yy_ch_buf[0];
+
+       b->yy_at_bol = 1;
+       b->yy_buffer_status = YY_BUFFER_NEW;
+
+       if ( b == yy_current_buffer )
+               yy_load_buffer_state();
+       }
+
+
+#ifndef YY_NO_SCAN_BUFFER
+#ifdef YY_USE_PROTOS
+YY_BUFFER_STATE yy_scan_buffer( char *base, yy_size_t size )
+#else
+YY_BUFFER_STATE yy_scan_buffer( base, size )
+char *base;
+yy_size_t size;
+#endif
+       {
+       YY_BUFFER_STATE b;
+
+       if ( size < 2 ||
+            base[size-2] != YY_END_OF_BUFFER_CHAR ||
+            base[size-1] != YY_END_OF_BUFFER_CHAR )
+               /* They forgot to leave room for the EOB's. */
+               return 0;
+
+       b = (YY_BUFFER_STATE) yy_flex_alloc( sizeof( struct yy_buffer_state ) );
+       if ( ! b )
+               YY_FATAL_ERROR( "out of dynamic memory in yy_scan_buffer()" );
+
+       b->yy_buf_size = size - 2;      /* "- 2" to take care of EOB's */
+       b->yy_buf_pos = b->yy_ch_buf = base;
+       b->yy_is_our_buffer = 0;
+       b->yy_input_file = 0;
+       b->yy_n_chars = b->yy_buf_size;
+       b->yy_is_interactive = 0;
+       b->yy_at_bol = 1;
+       b->yy_fill_buffer = 0;
+       b->yy_buffer_status = YY_BUFFER_NEW;
+
+       yy_switch_to_buffer( b );
+
+       return b;
+       }
+#endif
+
+
+#ifndef YY_NO_SCAN_STRING
+#ifdef YY_USE_PROTOS
+YY_BUFFER_STATE yy_scan_string( yyconst char *yy_str )
+#else
+YY_BUFFER_STATE yy_scan_string( yy_str )
+yyconst char *yy_str;
+#endif
+       {
+       int len;
+       for ( len = 0; yy_str[len]; ++len )
+               ;
+
+       return yy_scan_bytes( yy_str, len );
+       }
+#endif
+
+
+#ifndef YY_NO_SCAN_BYTES
+#ifdef YY_USE_PROTOS
+YY_BUFFER_STATE yy_scan_bytes( yyconst char *bytes, int len )
+#else
+YY_BUFFER_STATE yy_scan_bytes( bytes, len )
+yyconst char *bytes;
+int len;
+#endif
+       {
+       YY_BUFFER_STATE b;
+       char *buf;
+       yy_size_t n;
+       int i;
+
+       /* Get memory for full buffer, including space for trailing EOB's. */
+       n = len + 2;
+       buf = (char *) yy_flex_alloc( n );
+       if ( ! buf )
+               YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" );
+
+       for ( i = 0; i < len; ++i )
+               buf[i] = bytes[i];
+
+       buf[len] = buf[len+1] = YY_END_OF_BUFFER_CHAR;
+
+       b = yy_scan_buffer( buf, n );
+       if ( ! b )
+               YY_FATAL_ERROR( "bad buffer in yy_scan_bytes()" );
+
+       /* It's okay to grow etc. this buffer, and we should throw it
+        * away when we're done.
+        */
+       b->yy_is_our_buffer = 1;
+
+       return b;
+       }
+#endif
+
+
+#ifndef YY_NO_PUSH_STATE
+#ifdef YY_USE_PROTOS
+static void yy_push_state( int new_state )
+#else
+static void yy_push_state( new_state )
+int new_state;
+#endif
+       {
+       if ( yy_start_stack_ptr >= yy_start_stack_depth )
+               {
+               yy_size_t new_size;
+
+               yy_start_stack_depth += YY_START_STACK_INCR;
+               new_size = yy_start_stack_depth * sizeof( int );
+
+               if ( ! yy_start_stack )
+                       yy_start_stack = (int *) yy_flex_alloc( new_size );
+
+               else
+                       yy_start_stack = (int *) yy_flex_realloc(
+                                       (void *) yy_start_stack, new_size );
+
+               if ( ! yy_start_stack )
+                       YY_FATAL_ERROR(
+                       "out of memory expanding start-condition stack" );
+               }
+
+       yy_start_stack[yy_start_stack_ptr++] = YY_START;
+
+       BEGIN(new_state);
+       }
+#endif
+
+
+#ifndef YY_NO_POP_STATE
+static void yy_pop_state()
+       {
+       if ( --yy_start_stack_ptr < 0 )
+               YY_FATAL_ERROR( "start-condition stack underflow" );
+
+       BEGIN(yy_start_stack[yy_start_stack_ptr]);
+       }
+#endif
+
+
+#ifndef YY_NO_TOP_STATE
+static int yy_top_state()
+       {
+       return yy_start_stack[yy_start_stack_ptr - 1];
+       }
+#endif
+
+#ifndef YY_EXIT_FAILURE
+#define YY_EXIT_FAILURE 2
+#endif
+
+#ifdef YY_USE_PROTOS
+static void yy_fatal_error( yyconst char msg[] )
+#else
+static void yy_fatal_error( msg )
+char msg[];
+#endif
+       {
+       (void) fprintf( stderr, "%s\n", msg );
+       exit( YY_EXIT_FAILURE );
+       }
+
+
+
+/* Redefine yyless() so it works in section 3 code. */
+
+#undef yyless
+#define yyless(n) \
+       do \
+               { \
+               /* Undo effects of setting up yytext. */ \
+               yytext[yyleng] = yy_hold_char; \
+               yy_c_buf_p = yytext + n; \
+               yy_hold_char = *yy_c_buf_p; \
+               *yy_c_buf_p = '\0'; \
+               yyleng = n; \
+               } \
+       while ( 0 )
+
+
+/* Internal utility routines. */
+
+#ifndef yytext_ptr
+#ifdef YY_USE_PROTOS
+static void yy_flex_strncpy( char *s1, yyconst char *s2, int n )
+#else
+static void yy_flex_strncpy( s1, s2, n )
+char *s1;
+yyconst char *s2;
+int n;
+#endif
+       {
+       register int i;
+       for ( i = 0; i < n; ++i )
+               s1[i] = s2[i];
+       }
+#endif
+
+#ifdef YY_NEED_STRLEN
+#ifdef YY_USE_PROTOS
+static int yy_flex_strlen( yyconst char *s )
+#else
+static int yy_flex_strlen( s )
+yyconst char *s;
+#endif
+       {
+       register int n;
+       for ( n = 0; s[n]; ++n )
+               ;
+
+       return n;
+       }
+#endif
+
+
+#ifdef YY_USE_PROTOS
+static void *yy_flex_alloc( yy_size_t size )
+#else
+static void *yy_flex_alloc( size )
+yy_size_t size;
+#endif
+       {
+       return (void *) malloc( size );
+       }
+
+#ifdef YY_USE_PROTOS
+static void *yy_flex_realloc( void *ptr, yy_size_t size )
+#else
+static void *yy_flex_realloc( ptr, size )
+void *ptr;
+yy_size_t size;
+#endif
+       {
+       /* The cast to (char *) in the following accommodates both
+        * implementations that use char* generic pointers, and those
+        * that use void* generic pointers.  It works with the latter
+        * because both ANSI C and C++ allow castless assignment from
+        * any pointer type to void*, and deal with argument conversions
+        * as though doing an assignment.
+        */
+       return (void *) realloc( (char *) ptr, size );
+       }
+
+#ifdef YY_USE_PROTOS
+static void yy_flex_free( void *ptr )
+#else
+static void yy_flex_free( ptr )
+void *ptr;
+#endif
+       {
+       free( ptr );
+       }
+
+#if YY_MAIN
+int main()
+       {
+       yylex();
+       return 0;
+       }
+#endif
+#line 46 "wktparse.lex"
+
+
index b636e91adf6fb98252692fef0b4eede5f3080a55..273a03c1ec804777800589c2924515304be0ffbb 100644 (file)
@@ -7,6 +7,7 @@
 #include "utils/geo_decls.h"
 
 
+/*
 
 typedef struct
 {
@@ -14,8 +15,38 @@ typedef struct
        float ymin;
        float xmax;
        float ymax;
+
 } BOX2DFLOAT4;
 
+typedef struct
+{
+       float xmin;
+       float ymin;
+       float xmax;
+       float ymax;
+       char  junk[16];
+} BOX2DFLOAT4;
+typedef struct
+{
+       double xmin;
+       double ymin;
+       double xmax;
+       double ymax;
+} BOX2DFLOAT4;
+
+*/
+
+typedef struct
+{
+       float xmin;
+       float ymin;
+       float xmax;
+       float ymax;
+
+} BOX2DFLOAT4;
+
+
+
 
 typedef struct
 {
@@ -124,10 +155,10 @@ extern int pointArray_ptsize(POINTARRAY *pa);
 
  LWGEOM types are an 8-bit char in this format:
 
-xSDDtttt
+BSDDtttt
 
 WHERE
-    x = unused
+    B = 16 byte BOX2DFLOAT4 follows (probably not aligned) [before SRID]
     S = 4 byte SRID attached (0= not attached (-1), 1= attached)
     DD = dimentionality (0=2d, 1=3d, 2= 4d)
     tttt = actual type (as per the WKB type):
@@ -158,11 +189,12 @@ WHERE
 
 
 
-extern bool lwgeom_hasSRID(char type); // true iff S bit is set
-extern int  lwgeom_ndims(char type);    // true iff D bit is set
-extern int  lwgeom_getType(char type); // returns the tttt value
-extern char lwgeom_makeType(int ndims, char hasSRID, int type);
-
+extern bool lwgeom_hasSRID(unsigned char type); // true iff S bit is set
+extern int  lwgeom_ndims(unsigned char type);    // true iff D bit is set
+extern int  lwgeom_getType(unsigned char type); // returns the tttt value
+extern unsigned char lwgeom_makeType(int ndims, char hasSRID, int type);
+extern unsigned char lwgeom_makeType_full(int ndims, char hasSRID, int type, bool hasBBOX);
+extern bool lwgeom_hasBBOX(unsigned char type); // true iff B bit set
 
 // all the base types (point/line/polygon) will have a
 // basic constructor, basic de-serializer, basic serializer, and
@@ -292,7 +324,7 @@ typedef struct
 {
        int   SRID;
        char  *serialized_form; // orginal structure
-       char  type;            // 8-bit type for the LWGEOM
+       unsigned char  type;            // 8-bit type for the LWGEOM
        int   ngeometries;     // number of sub-geometries
        char  **sub_geoms;    // list of pointers (into serialized_form) of the sub-geoms
 } LWGEOM_INSPECTED;
@@ -382,6 +414,9 @@ extern int lwgeom_seralizedformlength_simple(char *serialized_form);
 extern int lwgeom_seralizedformlength(char *serialized_form, int geom_number);
 extern int lwgeom_seralizedformlength_inspected(LWGEOM_INSPECTED *inspected, int geom_number);
 
+//get the SRID from the LWGEOM
+// none present => -1
+extern int lwgeom_getSRID(char *serialized_form);
 
 //get bounding box of LWGEOM (automatically calls the sub-geometries bbox generators)
 extern BOX3D *lw_geom_getBB(char *serialized_form);
@@ -395,14 +430,20 @@ extern BOX3D *lw_geom_getBB_inspected(LWGEOM_INSPECTED *inspected);
 // will usually be a slightly bigger box because of the difference
 // between float8 and float4 representations.
 
-extern BOX2DFLOAT4 box3d_to_box2df(BOX3D *box);
-extern BOX2DFLOAT4 box_to_box2df(BOX *box);  // postgresql standard type
+extern BOX2DFLOAT4 *box3d_to_box2df(BOX3D *box);
+extern BOX2DFLOAT4 *box_to_box2df(BOX *box);  // postgresql standard type
 
 extern BOX3D box2df_to_box3d(BOX2DFLOAT4 *box);
 extern BOX   box2df_to_box(BOX2DFLOAT4 *box);  // postgresql standard type
 extern BOX3D *combine_boxes(BOX3D *b1, BOX3D *b2);
 
 
+// returns a real entity so it doesnt leak
+// if this has a pre-built BOX2d, then we use it,
+// otherwise we need to compute it.
+extern BOX2DFLOAT4 getbox2d(char *serialized_form);
+extern void getbox2d_p(char *serialized_form, BOX2DFLOAT4 *box);
+
 //****************************************************************
 // memory management -- these only delete the memory associated
 //  directly with the structure - NOT the stuff pointing into
@@ -421,11 +462,13 @@ extern void pfree_POINTARRAY(POINTARRAY *pa);
 extern uint32 get_uint32(char *loc);
 extern int32 get_int32(char *loc);
 extern void printPA(POINTARRAY *pa);
+extern void printLWPOINT(LWPOINT *point);
 extern void printLWLINE(LWLINE *line);
 extern void printLWPOLY(LWPOLY *poly);
 extern void printBYTES(unsigned char *a, int n);
 extern void printMULTI(char *serialized);
 extern void deparse_hex(unsigned char str, unsigned char *result);
+extern void printType(unsigned char str);
 
 
 //------------------------------------------------------------
@@ -548,3 +591,37 @@ extern void deparse_hex(unsigned char str, unsigned char *result);
 
 
 
+
+// other forwards (for indirect function calls)
+
+Datum box2d_same(PG_FUNCTION_ARGS);
+Datum box2d_overlap(PG_FUNCTION_ARGS);
+Datum box2d_overleft(PG_FUNCTION_ARGS);
+Datum box2d_left(PG_FUNCTION_ARGS);
+Datum box2d_right(PG_FUNCTION_ARGS);
+Datum box2d_overright(PG_FUNCTION_ARGS);
+Datum box2d_contained(PG_FUNCTION_ARGS);
+Datum box2d_contain(PG_FUNCTION_ARGS);
+Datum box2d_inter(PG_FUNCTION_ARGS);
+Datum box2d_union(PG_FUNCTION_ARGS);
+
+
+Datum gist_lwgeom_compress(PG_FUNCTION_ARGS);
+Datum gist_lwgeom_consistent(PG_FUNCTION_ARGS);
+Datum gist_rtree_decompress(PG_FUNCTION_ARGS);
+Datum lwgeom_box_union(PG_FUNCTION_ARGS);
+Datum lwgeom_box_penalty(PG_FUNCTION_ARGS);
+Datum lwgeom_gbox_same(PG_FUNCTION_ARGS);
+Datum lwgeom_gbox_picksplit(PG_FUNCTION_ARGS);
+
+
+extern float LWGEOM_Minf(float a, float b);
+extern float LWGEOM_Maxf(float a, float b);
+
+
+
+extern BOX3D *lw_geom_getBB_simple(char *serialized_form);
+
+
+
+
index 97a8baf122b5059cecaec66ff3fbc013acc8a0a4..8e3351b77dc1a60857c3ac84c24728c09623a5a0 100644 (file)
@@ -2,12 +2,12 @@ BEGIN;
 
 CREATE OR REPLACE FUNCTION lwgeom_in(cstring)
         RETURNS lwgeom
-        AS '/net/lion/raid/share/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','LWGEOM_in'
+        AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','LWGEOM_in'
         LANGUAGE 'C' WITH (isstrict,iscachable);
 
 CREATE OR REPLACE FUNCTION lwgeom_out(lwgeom)
         RETURNS cstring
-        AS '/net/lion/raid/share/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','LWGEOM_out'
+        AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','LWGEOM_out'
         LANGUAGE 'C' WITH (isstrict,iscachable);
 
 CREATE TYPE lwgeom (
@@ -17,39 +17,365 @@ CREATE TYPE lwgeom (
         storage = main
 );
 
+CREATE OR REPLACE FUNCTION box2d_in(cstring)
+        RETURNS box2d
+        AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','BOX2DFLOAT4_in'
+        LANGUAGE 'C' WITH (isstrict,iscachable);
 
-CREATE FUNCTION lwgeom(wkb)
-        RETURNS lwgeom
-        AS '/net/lion/raid/share/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','LWGEOMFromWKB'
+CREATE OR REPLACE FUNCTION box2d_out(box2d)
+        RETURNS cstring
+        AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','BOX2DFLOAT4_out'
         LANGUAGE 'C' WITH (isstrict,iscachable);
 
+CREATE TYPE box2d (
+        internallength = 16,
+        input = box2d_in,
+        output = box2d_out,
+        storage = plain
+);
 
-CREATE FUNCTION wkb(lwgeom)
-        RETURNS wkb
-        AS '/net/lion/raid/share/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','WKBFromLWGEOM'
+CREATE OR REPLACE FUNCTION box2d(LWGEOM)
+        RETURNS box2d
+        AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','LWGEOM_to_BOX2DFLOAT4'
         LANGUAGE 'C' WITH (isstrict,iscachable);
+
+
+
         
-CREATE FUNCTION cstring(lwgeom)
-       RETURNS CSTRING
-       AS '/net/lion/raid/share/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1',
-       'LWGEOM_asText'
+CREATE OR REPLACE FUNCTION lwgeom(text)
+        RETURNS lwgeom
+        AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','parse_WKT_lwgeom'
         LANGUAGE 'C' WITH (isstrict,iscachable);
 
-COMMIT;
+---- BOX2D  support functions
+
+
+CREATE FUNCTION box2d_overleft(box2d, box2d) 
+       RETURNS bool
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1'
+       LANGUAGE 'C' WITH (isstrict,iscachable);
+
+CREATE FUNCTION box2d_overright(box2d, box2d) 
+       RETURNS bool
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1' 
+       LANGUAGE 'C' WITH (isstrict,iscachable);
+
+CREATE FUNCTION box2d_left(box2d, box2d) 
+       RETURNS bool
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1' 
+       LANGUAGE 'C' WITH (isstrict,iscachable);
+
+CREATE FUNCTION box2d_right(box2d, box2d) 
+       RETURNS bool
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1' 
+       LANGUAGE 'C' WITH (isstrict,iscachable);
+
+CREATE FUNCTION box2d_contain(box2d, box2d) 
+       RETURNS bool
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1'
+       LANGUAGE 'C' WITH (isstrict,iscachable);
+
+CREATE FUNCTION box2d_contained(box2d, box2d) 
+       RETURNS bool
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1'
+       LANGUAGE 'C' WITH (isstrict,iscachable);
+
+CREATE FUNCTION box2d_overlap(box2d, box2d) 
+       RETURNS bool
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1'
+       LANGUAGE 'C' WITH (isstrict,iscachable);
+
+CREATE FUNCTION box2d_same(box2d, box2d) 
+       RETURNS bool
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1' 
+       LANGUAGE 'C' WITH (isstrict,iscachable);
+
+CREATE OPERATOR << (
+   LEFTARG = box2d, RIGHTARG = box2d, PROCEDURE = box2d_left,
+   COMMUTATOR = '>>',
+   RESTRICT = positionsel, JOIN = positionjoinsel
+);
+
+CREATE OPERATOR &< (
+   LEFTARG = box2d, RIGHTARG = box2d, PROCEDURE = box2d_overleft,
+   COMMUTATOR = '&>',
+   RESTRICT = positionsel, JOIN = positionjoinsel
+);
 
+CREATE OPERATOR && (
+   LEFTARG = box2d, RIGHTARG = box2d, PROCEDURE = box2d_overlap,
+   COMMUTATOR = '&&',
+   RESTRICT = areasel, JOIN = positionjoinsel
+);
+
+CREATE OPERATOR &> (
+   LEFTARG = box2d, RIGHTARG = box2d, PROCEDURE = box2d_overright,
+   COMMUTATOR = '&<',
+   RESTRICT = positionsel, JOIN = positionjoinsel
+);
+
+CREATE OPERATOR >> (
+   LEFTARG = box2d, RIGHTARG = box2d, PROCEDURE = box2d_right,
+   COMMUTATOR = '<<',
+   RESTRICT = positionsel, JOIN = positionjoinsel
+);
+
+CREATE OPERATOR ~= (
+   LEFTARG = box2d, RIGHTARG = box2d, PROCEDURE = box2d_same,
+   COMMUTATOR = '~=', 
+   RESTRICT = eqsel, JOIN = eqjoinsel
+);
+
+CREATE OPERATOR @ (
+   LEFTARG = box2d, RIGHTARG = box2d, PROCEDURE = box2d_contained,
+   COMMUTATOR = '~',
+   RESTRICT = contsel, JOIN = contjoinsel
+);
+
+CREATE OPERATOR ~ (
+   LEFTARG = box2d, RIGHTARG = box2d, PROCEDURE = box2d_contain,
+   COMMUTATOR = '@',
+   RESTRICT = contsel, JOIN = contjoinsel
+);
+
+
+-- lwgeom  operator support functions
 
----- test stuff
 
-select geometry(wkb(lwgeom(asbinary('POINT(1 2)'::geometry))));
-select geometry(wkb(lwgeom(asbinary('POINT(1 2 3)'::geometry))));
+CREATE FUNCTION lwgeom_overleft(lwgeom, lwgeom) 
+       RETURNS bool
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1'
+       LANGUAGE 'C' WITH (isstrict,iscachable);
 
-select geometry(wkb(lwgeom(asbinary('LINESTRING(1 2, 3 4)'::geometry))));
-select geometry(wkb(lwgeom(asbinary('LINESTRING(1 2 -999, 3 4 -999, 5 6 -999)'::geometry))));
+CREATE FUNCTION lwgeom_overright(lwgeom, lwgeom) 
+       RETURNS bool
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1' 
+       LANGUAGE 'C' WITH (isstrict,iscachable);
 
+CREATE FUNCTION lwgeom_left(lwgeom, lwgeom) 
+       RETURNS bool
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1' 
+       LANGUAGE 'C' WITH (isstrict,iscachable);
 
-select geometry(wkb(lwgeom(asbinary('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))'::geometry))));
-select geometry(wkb(lwgeom(asbinary('POLYGON((0 0 -99, 10 0 -99, 10 10 -99, 0 10 -99, 0 0 -99))'::geometry))));
+CREATE FUNCTION lwgeom_right(lwgeom, lwgeom) 
+       RETURNS bool
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1' 
+       LANGUAGE 'C' WITH (isstrict,iscachable);
+
+CREATE FUNCTION lwgeom_contain(lwgeom, lwgeom) 
+       RETURNS bool
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1'
+       LANGUAGE 'C' WITH (isstrict,iscachable);
+
+CREATE FUNCTION lwgeom_contained(lwgeom, lwgeom) 
+       RETURNS bool
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1'
+       LANGUAGE 'C' WITH (isstrict,iscachable);
+
+CREATE FUNCTION lwgeom_overlap(lwgeom, lwgeom) 
+       RETURNS bool
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1'
+       LANGUAGE 'C' WITH (isstrict,iscachable);
+
+CREATE FUNCTION lwgeom_same(lwgeom, lwgeom) 
+       RETURNS bool
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1' 
+       LANGUAGE 'C' WITH (isstrict,iscachable);
+
+CREATE OPERATOR << (
+   LEFTARG = lwgeom, RIGHTARG = lwgeom, PROCEDURE = lwgeom_left,
+   COMMUTATOR = '>>',
+   RESTRICT = positionsel, JOIN = positionjoinsel
+);
+
+CREATE OPERATOR &< (
+   LEFTARG = lwgeom, RIGHTARG = lwgeom, PROCEDURE = lwgeom_overleft,
+   COMMUTATOR = '&>',
+   RESTRICT = positionsel, JOIN = positionjoinsel
+);
+
+CREATE OPERATOR && (
+   LEFTARG = lwgeom, RIGHTARG = lwgeom, PROCEDURE = lwgeom_overlap,
+   COMMUTATOR = '&&',
+   RESTRICT = areasel, JOIN = positionjoinsel
+);
+
+CREATE OPERATOR &> (
+   LEFTARG = lwgeom, RIGHTARG = lwgeom, PROCEDURE = lwgeom_overright,
+   COMMUTATOR = '&<',
+   RESTRICT = positionsel, JOIN = positionjoinsel
+);
+
+CREATE OPERATOR >> (
+   LEFTARG = lwgeom, RIGHTARG = lwgeom, PROCEDURE = lwgeom_right,
+   COMMUTATOR = '<<',
+   RESTRICT = positionsel, JOIN = positionjoinsel
+);
+
+CREATE OPERATOR ~= (
+   LEFTARG = lwgeom, RIGHTARG = lwgeom, PROCEDURE = lwgeom_same,
+   COMMUTATOR = '~=', 
+   RESTRICT = eqsel, JOIN = eqjoinsel
+);
+
+CREATE OPERATOR @ (
+   LEFTARG = lwgeom, RIGHTARG = lwgeom, PROCEDURE = lwgeom_contained,
+   COMMUTATOR = '~',
+   RESTRICT = contsel, JOIN = contjoinsel
+);
+
+CREATE OPERATOR ~ (
+   LEFTARG = lwgeom, RIGHTARG = lwgeom, PROCEDURE = lwgeom_contain,
+   COMMUTATOR = '@',
+   RESTRICT = contsel, JOIN = contjoinsel
+);
+
+
+-- gist support functions
+
+
+CREATE FUNCTION gist_lwgeom_consistent(internal,lwgeom,int4) 
+       RETURNS bool 
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1' ,'gist_lwgeom_consistent'
+       LANGUAGE 'C';
+
+CREATE FUNCTION gist_lwgeom_compress(internal) 
+       RETURNS internal 
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','gist_lwgeom_compress'
+       LANGUAGE 'C';
+
+CREATE FUNCTION gist_lwgeom_penalty(internal,internal,internal) 
+       RETURNS internal 
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1' ,'lwgeom_box_penalty'
+       LANGUAGE 'C';
+
+CREATE FUNCTION gist_lwgeom_picksplit(internal, internal) 
+       RETURNS internal 
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1' ,'lwgeom_gbox_picksplit'
+       LANGUAGE 'C';
+
+CREATE FUNCTION gist_lwgeom_union(bytea, internal) 
+       RETURNS internal 
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1' ,'lwgeom_box_union'
+       LANGUAGE 'C';
+
+CREATE FUNCTION gist_lwgeom_same(box2d, box2d, internal) 
+       RETURNS internal 
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1' ,'lwgeom_gbox_same'
+       LANGUAGE 'C';
+
+CREATE FUNCTION gist_lwgeom_decompress(internal) 
+       RETURNS internal
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1' ,'gist_rtree_decompress'
+       LANGUAGE 'C';
+
+
+-- gist opclass
+CREATE OPERATOR CLASS gist_lwgeom_ops
+        DEFAULT FOR TYPE lwgeom USING gist AS
+        OPERATOR        1        << ,
+        OPERATOR        2        &< ,
+        OPERATOR        3        && ,
+        OPERATOR        4        &> ,
+        OPERATOR        5        >> ,
+        OPERATOR        6        ~= ,
+        OPERATOR        7        ~ ,
+        OPERATOR        8        @ ,
+        FUNCTION        1        gist_lwgeom_consistent (internal, lwgeom, int4),
+        FUNCTION        2        gist_lwgeom_union (bytea, internal),
+        FUNCTION        3        gist_lwgeom_compress (internal),
+        FUNCTION        4        gist_lwgeom_decompress (internal),
+        FUNCTION        5        gist_lwgeom_penalty (internal, internal, internal),
+        FUNCTION        6        gist_lwgeom_picksplit (internal, internal),
+        FUNCTION        7        gist_lwgeom_same (box2d, box2d, internal);
+
+UPDATE pg_opclass 
+       SET opckeytype = (select oid from pg_type where typname = 'box2d') 
+       WHERE opcname = 'gist_lwgeom_ops';
+       
+       
+       
+-- other lwgeom functions
+
+CREATE FUNCTION addBBOX(lwgeom) 
+       RETURNS lwgeom
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','LWGEOM_addBBOX'
+       LANGUAGE 'C' WITH (isstrict,iscachable);
+
+       
+CREATE FUNCTION getSRID(lwgeom) 
+       RETURNS int4
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','LWGEOM_getSRID'
+       LANGUAGE 'C' WITH (isstrict,iscachable);
+       
+CREATE FUNCTION setSRID(lwgeom,int4) 
+       RETURNS lwgeom
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','LWGEOM_setSRID'
+       LANGUAGE 'C' WITH (isstrict,iscachable);        
+       
+
+CREATE FUNCTION asText(lwgeom)
+       RETURNS TEXT
+       AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','LWGEOM_asText'
+       LANGUAGE 'C' WITH (isstrict,iscachable);
+
+CREATE OR REPLACE FUNCTION getbbox(LWGEOM)
+        RETURNS box2d
+        AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','LWGEOM_to_BOX2DFLOAT4'
+        LANGUAGE 'C' WITH (isstrict,iscachable);
+
+
+commit;
+
+begin;
+-- extra conversions (might not work if there's no postgis install)
+
+CREATE FUNCTION geometry(lwgeom)
+       RETURNS geometry
+       AS 
+'BEGIN
+ RETURN GeomFromWKB(asBinary($1),getSRID($1) );
+ END;'
+LANGUAGE 'plpgsql' WITH (iscachable,isstrict);
+
+
+CREATE OR REPLACE FUNCTION lwgeom(geometry)
+       RETURNS lwgeom
+       AS 
+'BEGIN
+ RETURN setSRID(lwgeom(asBinary($1)),SRID($1) );
+ END;'
+LANGUAGE 'plpgsql' WITH (iscachable,isstrict);
+
+CREATE CAST (lwgeom as geometry) WITH FUNCTION  geometry(lwgeom) AS IMPLICIT ;
+CREATE CAST (geometry as lwgeom) WITH FUNCTION lwgeom(geometry) AS IMPLICIT ;
+CREATE CAST (lwgeom as box2d) WITH FUNCTION box2d(lwgeom)  AS IMPLICIT ;
+       
+CREATE CAST (lwgeom as wkb) WITH FUNCTION wkb(lwgeom)  AS IMPLICIT ;
+CREATE CAST (wkb as lwgeom) WITH FUNCTION lwgeom(wkb)  AS IMPLICIT ;
+       
+CREATE FUNCTION lwgeom(wkb)
+        RETURNS lwgeom
+        AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','LWGEOMFromWKB'
+        LANGUAGE 'C' WITH (isstrict,iscachable);
+
+CREATE FUNCTION lwgeom(wkb,int4)
+        RETURNS lwgeom
+        AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','LWGEOMFromWKB'
+        LANGUAGE 'C' WITH (isstrict,iscachable);
+
+
+
+CREATE FUNCTION wkb(lwgeom)
+        RETURNS wkb
+        AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','WKBFromLWGEOM'
+        LANGUAGE 'C' WITH (isstrict,iscachable);
+        
+CREATE FUNCTION asbinary(lwgeom)
+        RETURNS wkb
+        AS '/data/Refractions/Projects/PostGIS/work_dave2/postgis/lwgeom/liblwgeom.so.0.1','WKBFromLWGEOM'
+        LANGUAGE 'C' WITH (isstrict,iscachable);
+       
+COMMIT;
 
 
-select geometry(wkb(lwgeom(asbinary('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0),(5 5,7 5, 7 7, 5 7, 5 5))'::geometry))));
-select geometry(wkb(lwgeom(asbinary('POLYGON((0 0 -99, 10 0 -99, 10 10 -99, 0 10 -99, 0 0 -99),(5 5 -99,7 5 -99, 7 7 -99, 5 7 -99, 5 5 -99))'::geometry))));
index 6a51ad3e71cf4542ae5af2136aeee48b9f1d3eee..59c292f5db5e59f35826cc03a124259c1266d7e7 100644 (file)
@@ -46,6 +46,7 @@ float nextDown_f(double d)
                return result;
 
        return nextafterf(result, result - 1000000);
+
 }
 
 // returns the float thats very close to the input, but >=
@@ -86,35 +87,35 @@ double nextUp_d(float d)
 }
 
 
-BOX2DFLOAT4 box3d_to_box2df(BOX3D *box)
+BOX2DFLOAT4 *box3d_to_box2df(BOX3D *box)
 {
-       BOX2DFLOAT4 result;
+       BOX2DFLOAT4 *result = (BOX2DFLOAT4*) palloc(sizeof(BOX2DFLOAT4));
 
        if (box == NULL)
                return result;
 
-       result.xmin = nextDown_f(box->xmin);
-       result.ymin = nextDown_f(box->ymin);
+       result->xmin = nextDown_f(box->xmin);
+       result->ymin = nextDown_f(box->ymin);
 
-       result.xmax = nextUp_f(box->xmax);
-       result.ymax = nextUp_f(box->ymax);
+       result->xmax = nextUp_f(box->xmax);
+       result->ymax = nextUp_f(box->ymax);
 
        return result;
 }
 
 
-BOX2DFLOAT4 box_to_box2df(BOX *box)
+BOX2DFLOAT4 *box_to_box2df(BOX *box)
 {
-       BOX2DFLOAT4 result;
+       BOX2DFLOAT4 *result = (BOX2DFLOAT4*) palloc(sizeof(BOX2DFLOAT4));
 
        if (box == NULL)
                return result;
 
-       result.xmin = nextDown_f(box->low.x);
-       result.ymin = nextDown_f(box->low.y);
+       result->xmin = nextDown_f(box->low.x);
+       result->ymin = nextDown_f(box->low.y);
 
-       result.xmax = nextUp_f(box->high.x);
-       result.ymax = nextUp_f(box->high.x);
+       result->xmax = nextUp_f(box->high.x);
+       result->ymax = nextUp_f(box->high.x);
 
        return result;
 }
@@ -187,9 +188,10 @@ BOX3D *combine_boxes(BOX3D *b1, BOX3D *b2)
        if (b1->xmax > b2->xmax)
                result->xmax = b1->xmax;
        else
-               result->ymax = b2->ymax;
+               result->xmax = b2->xmax;
+
        if (b1->ymax > b2->ymax)
-                               result->ymax = b1->ymax;
+               result->ymax = b1->ymax;
        else
                result->ymax = b2->ymax;
 
@@ -197,14 +199,74 @@ BOX3D *combine_boxes(BOX3D *b1, BOX3D *b2)
                        result->zmax = b1->zmax;
        else
                        result->zmax = b2->zmax;
+
        if (b1->zmin > b2->zmin)
-                       result->zmin = b1->zmin;
+               result->zmin = b1->zmin;
        else
                result->zmin = b2->zmin;
 
        return result;
 }
 
+// returns a real entity so it doesnt leak
+// if this has a pre-built BOX2d, then we use it,
+// otherwise we need to compute it.
+BOX2DFLOAT4 getbox2d(char *serialized_form)
+{
+               int     type = (unsigned char) serialized_form[0];
+               char    *loc;
+               BOX2DFLOAT4  result;
+               BOX3D   *box3d;
+               BOX2DFLOAT4     *box;
+
+               loc = serialized_form+1;
+
+               if (lwgeom_hasBBOX(type))
+               {
+                       //woot - this is easy
+//elog(NOTICE,"getbox2d has box");
+                       memcpy(&result,loc, sizeof(BOX2DFLOAT4));
+                       return result;
+               }
+
+               //we have to actually compute it!
+//elog(NOTICE,"getbox2d -- computing bbox");
+               box3d = lw_geom_getBB_simple(serialized_form);
+//elog(NOTICE,"getbox2d got bbox3d(%.15g %.15g,%.15g %.15g)",box3d->xmin,box3d->ymin,box3d->xmax,box3d->ymax);
+               box = box3d_to_box2df(box3d);
+//elog(NOTICE,"getbox2d made box2d(%.15g %.15g,%.15g %.15g)",box->xmin,box->ymin,box->xmax,box->ymax);
+               memcpy(&result,box, sizeof(BOX2DFLOAT4));
+               pfree(box3d);
+               pfree(box);
+               return result;
+}
+
+void getbox2d_p(char *serialized_form, BOX2DFLOAT4 *box)
+{
+                       int     type = (unsigned char) serialized_form[0];
+                       char    *loc;
+                       BOX3D   *box3d;
+                       BOX2DFLOAT4     *box2;
+
+                       loc = serialized_form+1;
+
+                       if (lwgeom_hasBBOX(type))
+                       {
+                               //woot - this is easy
+       //elog(NOTICE,"getbox2d has box");
+                               memcpy(box,loc, sizeof(BOX2DFLOAT4));
+                               return ;
+                       }
+
+                       //we have to actually compute it!
+//elog(NOTICE,"getbox2d_p:: computing box");
+                       box3d = lw_geom_getBB_simple(serialized_form);
+                       box2 = box3d_to_box2df(box3d);
+
+                       memcpy(box,box2, sizeof(BOX2DFLOAT4));
+                       pfree(box3d);
+                       pfree(box2);
+}
 
 //************************************************************************
 // POINTARRAY support functions
@@ -267,7 +329,7 @@ void getPoint4d_p(POINTARRAY *pa, int n, char *point)
                 }
 
                 if (pa->ndims >3)
-                       memcpy(point+24, &pa->serialized_pointlist[size*n + sizeof(double)*2],sizeof(double) );
+                       memcpy(point+24, &pa->serialized_pointlist[size*n + sizeof(double)*3],sizeof(double) );
                 else
                 {
                         double bad=NO_Z_VALUE;
@@ -413,7 +475,7 @@ BOX3D *pointArray_bbox(POINTARRAY *pa)
        result->ymax = pt.y;
        result->zmax = pt.z;
 
-       for (t=0;t<pa->npoints;t++)
+       for (t=1;t<pa->npoints;t++)
        {
                getPoint3d_p(pa,t,(char*)&pt);
                if (pt.x < result->xmin)
@@ -452,24 +514,24 @@ int pointArray_ptsize(POINTARRAY *pa)
 // basic type handling
 
 
-bool lwgeom_hasSRID(char type)
+bool lwgeom_hasSRID(unsigned char type)
 {
        return (type & 0x40);
 }
 
-int lwgeom_ndims(char type)
+int lwgeom_ndims(unsigned char type)
 {
        return  ( (type & 0x30) >>4) +2;
 }
 
-int  lwgeom_getType(char type)
+int  lwgeom_getType(unsigned char type)
 {
        return (type & 0x0F);
 }
 
-char lwgeom_makeType(int ndims, char hasSRID, int type)
+unsigned char lwgeom_makeType(int ndims, char hasSRID, int type)
 {
-       char result = type;
+       unsigned char result = type;
 
        if (ndims == 3)
                result = result | 0x10;
@@ -481,6 +543,27 @@ char lwgeom_makeType(int ndims, char hasSRID, int type)
        return result;
 }
 
+unsigned char lwgeom_makeType_full(int ndims, char hasSRID, int type, bool hasBBOX)
+{
+               unsigned char result = type;
+
+               if (ndims == 3)
+                       result = result | 0x10;
+               if (ndims == 4)
+                       result = result | 0x20;
+               if (hasSRID)
+                       result = result | 0x40;
+               if (hasBBOX)
+                       result = result | 0x80;
+
+       return result;
+}
+
+bool lwgeom_hasBBOX(unsigned char type)
+{
+       return (type & 0x80);
+}
+
 //*****************************************************************************
 // basic sub-geometry types
 
@@ -528,7 +611,7 @@ LWLINE *lwline_construct(int ndims, int SRID,  POINTARRAY *points)
 // See serialized form doc
 LWLINE *lwline_deserialize(char *serialized_form)
 {
-       char type;
+       unsigned char type;
        LWLINE *result;
        char *loc =NULL;
        uint32 npoints;
@@ -536,7 +619,7 @@ LWLINE *lwline_deserialize(char *serialized_form)
 
        result = (LWLINE*) palloc( sizeof(LWLINE)) ;
 
-       type = serialized_form[0];
+       type = (unsigned char) serialized_form[0];
 
 
        if ( lwgeom_getType(type) != LINETYPE)
@@ -545,15 +628,19 @@ LWLINE *lwline_deserialize(char *serialized_form)
                return NULL;
        }
 
+       loc = serialized_form+1;
+
+       if (lwgeom_hasBBOX(type))
+               loc += sizeof(BOX2DFLOAT4);
+
        if ( lwgeom_hasSRID(type))
        {
-               result->SRID = get_int32(loc+1);
-               loc = serialized_form+ 5; // type + SRID
+               result->SRID = get_int32(loc);
+               loc +=4; // type + SRID
        }
        else
        {
                result->SRID = -1;
-               loc = serialized_form +1;
        }
 
        // we've read the type (1 byte) and SRID (4 bytes, if present)
@@ -574,7 +661,7 @@ char  *lwline_serialize(LWLINE *line)
 {
        int size=1;  // type byte
        char hasSRID;
-       char * result;
+       unsigned char * result;
        int t;
        char *loc;
 
@@ -604,7 +691,7 @@ if (line == NULL)
 
        result = palloc(size);
 
-       result[0] = lwgeom_makeType(line->ndims,hasSRID, LINETYPE);
+       result[0] = (unsigned char) lwgeom_makeType(line->ndims,hasSRID, LINETYPE);
        loc = result+1;
 
        if (hasSRID)
@@ -659,7 +746,7 @@ BOX3D *lwline_findbbox(LWLINE *line)
 //find length of this serialized line
 uint32 lwline_findlength(char *serialized_line)
 {
-               int type = serialized_line[0];
+               int type = (unsigned char) serialized_line[0];
                uint32 result =1;  //type
                char *loc;
                uint32 npoints;
@@ -667,15 +754,20 @@ uint32 lwline_findlength(char *serialized_line)
                if ( lwgeom_getType(type) != LINETYPE)
                        elog(ERROR,"lwline_findlength::attempt to find the length of a non-line");
 
+
+       loc = serialized_line+1;
+
+       if (lwgeom_hasBBOX(type))
+       {
+               loc += sizeof(BOX2DFLOAT4);
+               result +=sizeof(BOX2DFLOAT4);
+       }
+
                if ( lwgeom_hasSRID(type))
                {
-                       loc = serialized_line+ 5; // type + SRID
+                       loc += 4; // type + SRID
                        result +=4;
                }
-               else
-               {
-                       loc = serialized_line +1;
-               }
 
                // we've read the type (1 byte) and SRID (4 bytes, if present)
 
@@ -725,28 +817,36 @@ LWPOINT  *lwpoint_construct(int ndims, int SRID, POINTARRAY *point)
 // See serialized form doc
 LWPOINT *lwpoint_deserialize(char *serialized_form)
 {
-               char type;
+               unsigned char type;
                LWPOINT *result;
                char *loc = NULL;
                POINTARRAY *pa;
 
                result = (LWPOINT*) palloc( sizeof(LWPOINT)) ;
 
-               type = serialized_form[0];
+               type = (unsigned char) serialized_form[0];
 
 
                if ( lwgeom_getType(type) != POINTTYPE)
                        return NULL;
 
+
+
+               loc = serialized_form+1;
+
+               if (lwgeom_hasBBOX(type))
+               {
+                       loc += sizeof(BOX2DFLOAT4);
+               }
+
                if ( lwgeom_hasSRID(type))
                {
-                       result->SRID = get_int32(loc+1);
-                       loc = serialized_form+ 5; // type + SRID
+                       result->SRID = get_int32(loc);
+                       loc +=4; // type + SRID
                }
                else
                {
                        result->SRID = -1;
-                       loc = serialized_form +1;
                }
 
                // we've read the type (1 byte) and SRID (4 bytes, if present)
@@ -764,7 +864,7 @@ LWPOINT *lwpoint_deserialize(char *serialized_form)
 // result's first char will be the 8bit type.  See serialized form doc
 char  *lwpoint_serialize(LWPOINT *point)
 {
-               int size=1;  // type byte
+               int size=1;
                char hasSRID;
                char * result;
                char *loc;
@@ -789,7 +889,7 @@ char  *lwpoint_serialize(LWPOINT *point)
 
                result = palloc(size);
 
-               result[0] = lwgeom_makeType(point->ndims,hasSRID, POINTTYPE);
+               result[0] = (unsigned char) lwgeom_makeType(point->ndims,hasSRID, POINTTYPE);
                loc = result+1;
 
                if (hasSRID)
@@ -851,25 +951,30 @@ POINT3D lwpoint_getPoint3d(LWPOINT *point)
 //find length of this serialized point
 uint32 lwpoint_findlength(char *serialized_point)
 {
-               uint  result = 1; // type char
-               char type;
+               uint  result = 1;
+               unsigned char type;
                char *loc;
 
 
-               type = serialized_point[0];
+               type = (unsigned char) serialized_point[0];
 
 
                if ( lwgeom_getType(type) != POINTTYPE)
                        return -9999;
 
-               if ( lwgeom_hasSRID(type))
+
+               loc = serialized_point+1;
+
+               if (lwgeom_hasBBOX(type))
                {
-                       loc = serialized_point+ 5; // type + SRID
-                       result +=4;
+                       loc += sizeof(BOX2DFLOAT4);
+                       result +=sizeof(BOX2DFLOAT4);
                }
-               else
+
+               if ( lwgeom_hasSRID(type))
                {
-                       loc = serialized_point +1;
+                       loc +=4; // type + SRID
+                       result +=4;
                }
 
                if (lwgeom_ndims(type) == 3)
@@ -919,7 +1024,7 @@ LWPOLY *lwpoly_deserialize(char *serialized_form)
        uint32 nrings;
        int ndims;
        uint32 npoints;
-       char type;
+       unsigned char type;
        char  *loc;
        int t;
 
@@ -929,22 +1034,29 @@ LWPOLY *lwpoly_deserialize(char *serialized_form)
        result = (LWPOLY*) palloc(sizeof(LWPOLY));
 
 
-       type = serialized_form[0];
+       type = (unsigned  char) serialized_form[0];
        ndims = lwgeom_ndims(type);
        loc = serialized_form;
 
        if ( lwgeom_getType(type) != POLYGONTYPE)
                return NULL;
 
+
+       loc = serialized_form+1;
+
+       if (lwgeom_hasBBOX(type))
+       {
+               loc += sizeof(BOX2DFLOAT4);
+       }
+
        if ( lwgeom_hasSRID(type))
        {
-               result->SRID = get_int32(loc+1);
-               loc = serialized_form+ 5; // type + SRID
+               result->SRID = get_int32(loc);
+               loc +=4; // type + SRID
        }
        else
        {
                result->SRID = -1;
-               loc = serialized_form +1;
        }
 
        nrings = get_uint32(loc);
@@ -1006,7 +1118,7 @@ char *lwpoly_serialize(LWPOLY *poly)
 
                result = palloc(size);
 
-               result[0] = lwgeom_makeType(poly->ndims,hasSRID, POLYGONTYPE);
+               result[0] = (unsigned char) lwgeom_makeType(poly->ndims,hasSRID, POLYGONTYPE);
                loc = result+1;
 
                if (hasSRID)
@@ -1059,22 +1171,23 @@ char *lwpoly_serialize(LWPOLY *poly)
 // find bounding box (standard one)  zmin=zmax=0 if 2d (might change to NaN)
 BOX3D *lwpoly_findbbox(LWPOLY *poly)
 {
-       int t;
+//     int t;
 
-       BOX3D *result, *abox,*abox2;
+       BOX3D *result;
+//     BOX3D *abox,*abox2;
 
-       POINTARRAY *pa = poly->rings[0];
+       POINTARRAY *pa = poly->rings[0];   // just need to check outer ring -- interior rings are inside
        result  = pointArray_bbox(pa);
 
-       for (t=1;t<poly->nrings;t++)
-       {
-               pa = poly->rings[t];
-               abox  = pointArray_bbox(pa);
-               abox2 = result;
-               result = combine_boxes( abox, abox2);
-               pfree(abox);
-               pfree(abox2);
-    }
+//     for (t=1;t<poly->nrings;t++)
+       //{
+//             pa = poly->rings[t];
+//             abox  = pointArray_bbox(pa);
+//             abox2 = result;
+//             result = combine_boxes( abox, abox2);
+//             pfree(abox);
+//             pfree(abox2);
+  //  }
 
     return result;
 }
@@ -1086,7 +1199,7 @@ uint32 lwpoly_findlength(char *serialized_poly)
                uint32 nrings;
                int   ndims;
                int t;
-               char type;
+               unsigned char type;
                uint32 npoints;
                char *loc;
 
@@ -1094,21 +1207,28 @@ uint32 lwpoly_findlength(char *serialized_poly)
                        return -9999;
 
 
-               type = serialized_poly[0];
+               type = (unsigned char) serialized_poly[0];
                ndims = lwgeom_ndims(type);
 
                if ( lwgeom_getType(type) != POLYGONTYPE)
                        return -9999;
 
+
+       loc = serialized_poly+1;
+
+       if (lwgeom_hasBBOX(type))
+       {
+               loc += sizeof(BOX2DFLOAT4);
+               result +=sizeof(BOX2DFLOAT4);
+       }
+
+
                if ( lwgeom_hasSRID(type))
                {
-                       loc = serialized_poly+ 5; // type + SRID
+                       loc +=4; // type + SRID
                        result += 4;
                }
-               else
-               {
-                       loc = serialized_poly +1;
-               }
+
 
                nrings = get_uint32(loc);
                loc +=4;
@@ -1134,8 +1254,8 @@ uint32 lwpoly_findlength(char *serialized_poly)
                        }
                        else if (ndims == 4)
                        {
-                               loc += 16*npoints;
-                               result += 16*npoints;
+                               loc += 32*npoints;
+                               result += 32*npoints;
                        }
                }
        return result;
@@ -1155,7 +1275,7 @@ uint32 lwpoly_findlength(char *serialized_poly)
 LWGEOM_INSPECTED *lwgeom_inspect(char *serialized_form)
 {
        LWGEOM_INSPECTED *result = palloc(sizeof(LWGEOM_INSPECTED));
-       char type;
+       unsigned char type;
        char **sub_geoms;
        char *loc;
        int     t;
@@ -1164,16 +1284,24 @@ LWGEOM_INSPECTED *lwgeom_inspect(char *serialized_form)
                return NULL;
 
        result->serialized_form = serialized_form;
-       result->type = serialized_form[0];
+       result->type = (unsigned char) serialized_form[0];
     result->SRID = -1; // assume
 
-       type = lwgeom_getType(serialized_form[0]);
+       type = lwgeom_getType( (unsigned char) serialized_form[0]);
+
+
+       loc = serialized_form+1;
+
+       if (lwgeom_hasBBOX((unsigned char) serialized_form[0]))
+       {
+               loc += sizeof(BOX2DFLOAT4);
+       }
 
        if ( (type==POINTTYPE) || (type==LINETYPE) || (type==POLYGONTYPE) )
        {
-               if (lwgeom_hasSRID(serialized_form[0]) )
+               if (lwgeom_hasSRID((unsigned char) serialized_form[0]) )
                {
-                       result->SRID=  get_int32(serialized_form+1);
+                       result->SRID=  get_int32(loc);
                }
                //simple geometry (point/line/polygon)-- not multi!
                result->ngeometries = 1;
@@ -1184,8 +1312,7 @@ LWGEOM_INSPECTED *lwgeom_inspect(char *serialized_form)
        }
        else
        {
-                       loc =serialized_form + 1;
-                       if (lwgeom_hasSRID(serialized_form[0]) )
+                       if (lwgeom_hasSRID((unsigned char) serialized_form[0]) )
                        {
                                result->SRID=  get_int32(loc);
                                loc += 4;
@@ -1214,7 +1341,7 @@ LWGEOM_INSPECTED *lwgeom_inspect(char *serialized_form)
 // this is fine to call on a point (with geom_num=0), multipoint or geometrycollection
 LWPOINT *lwgeom_getpoint(char *serialized_form, int geom_number)
 {
-       char type = lwgeom_getType(serialized_form[0]);
+       char type = lwgeom_getType((unsigned char)serialized_form[0]);
        char *sub_geom;
 
        if ((type == POINTTYPE)  && (geom_number == 0))
@@ -1230,7 +1357,7 @@ LWPOINT *lwgeom_getpoint(char *serialized_form, int geom_number)
        if (sub_geom == NULL)
                return NULL;
 
-       type = lwgeom_getType(sub_geom[0]);
+       type = lwgeom_getType( (unsigned char) sub_geom[0]);
        if (type != POINTTYPE)
                return NULL;
 
@@ -1240,14 +1367,14 @@ LWPOINT *lwgeom_getpoint(char *serialized_form, int geom_number)
 LWPOINT *lwgeom_getpoint_inspected(LWGEOM_INSPECTED *inspected, int geom_number)
 {
                char *sub_geom;
-               char type;
+               unsigned char type;
 
                sub_geom = lwgeom_getsubgeometry_inspected(inspected, geom_number);
 
                if (sub_geom == NULL)
                        return NULL;
 
-               type = lwgeom_getType(sub_geom[0]);
+               type = lwgeom_getType( (unsigned char) sub_geom[0]);
                if (type != POINTTYPE)
                        return NULL;
 
@@ -1261,7 +1388,7 @@ LWPOINT *lwgeom_getpoint_inspected(LWGEOM_INSPECTED *inspected, int geom_number)
 // this is fine to call on a line, multiline or geometrycollection
 LWLINE *lwgeom_getline(char *serialized_form, int geom_number)
 {
-               char type = lwgeom_getType(serialized_form[0]);
+               unsigned char type = lwgeom_getType( (unsigned char) serialized_form[0]);
                char *sub_geom;
 
                if ((type == LINETYPE)  && (geom_number == 0))
@@ -1277,7 +1404,7 @@ LWLINE *lwgeom_getline(char *serialized_form, int geom_number)
                if (sub_geom == NULL)
                        return NULL;
 
-               type = lwgeom_getType(sub_geom[0]);
+               type = lwgeom_getType((unsigned char) sub_geom[0]);
                if (type != LINETYPE)
                        return NULL;
 
@@ -1287,7 +1414,7 @@ LWLINE *lwgeom_getline(char *serialized_form, int geom_number)
 LWLINE *lwgeom_getline_inspected(LWGEOM_INSPECTED *inspected, int geom_number)
 {
                char *sub_geom;
-               char type;
+               unsigned char type;
 
 
                sub_geom = lwgeom_getsubgeometry_inspected(inspected, geom_number);
@@ -1296,7 +1423,7 @@ LWLINE *lwgeom_getline_inspected(LWGEOM_INSPECTED *inspected, int geom_number)
                        return NULL;
 
 
-               type = lwgeom_getType(sub_geom[0]);
+               type = lwgeom_getType((unsigned char) sub_geom[0]);
                if (type != LINETYPE)
                        return NULL;
 
@@ -1310,7 +1437,7 @@ LWLINE *lwgeom_getline_inspected(LWGEOM_INSPECTED *inspected, int geom_number)
 // this is fine to call on a polygon, multipolygon or geometrycollection
 LWPOLY *lwgeom_getpoly(char *serialized_form, int geom_number)
 {
-               char type = lwgeom_getType(serialized_form[0]);
+               unsigned char type = lwgeom_getType( (unsigned char) serialized_form[0]);
                char *sub_geom;
 
                if ((type == POLYGONTYPE)  && (geom_number == 0))
@@ -1326,7 +1453,7 @@ LWPOLY *lwgeom_getpoly(char *serialized_form, int geom_number)
                if (sub_geom == NULL)
                        return NULL;
 
-               type = lwgeom_getType(sub_geom[0]);
+               type = lwgeom_getType((unsigned char) sub_geom[0]);
                if (type != POLYGONTYPE)
                        return NULL;
 
@@ -1336,14 +1463,14 @@ LWPOLY *lwgeom_getpoly(char *serialized_form, int geom_number)
 LWPOLY *lwgeom_getpoly_inspected(LWGEOM_INSPECTED *inspected, int geom_number)
 {
                char *sub_geom;
-               char type;
+               unsigned char type;
 
                sub_geom = lwgeom_getsubgeometry_inspected(inspected, geom_number);
 
                if (sub_geom == NULL)
                        return NULL;
 
-               type = lwgeom_getType(sub_geom[0]);
+               type = lwgeom_getType((unsigned char) sub_geom[0]);
                if (type != POLYGONTYPE)
                        return NULL;
 
@@ -1412,7 +1539,7 @@ char lwgeom_getsubtype_inspected(LWGEOM_INSPECTED *inspected, int geom_number)
 //  for point,line,polygon will return 1.
 int lwgeom_getnumgeometries(char *serialized_form)
 {
-               char type = lwgeom_getType(serialized_form[0]);
+               unsigned char type = lwgeom_getType((unsigned char)serialized_form[0]);
                char *loc;
 
                if ( (type==POINTTYPE) || (type==LINETYPE) || (type==POLYGONTYPE) )
@@ -1420,8 +1547,15 @@ int lwgeom_getnumgeometries(char *serialized_form)
                        return 1;
                }
 
-               loc =serialized_form + 1;
-               if (lwgeom_hasSRID(serialized_form[0]) )
+
+       loc = serialized_form+1;
+
+       if (lwgeom_hasBBOX((unsigned char) serialized_form[0]))
+       {
+               loc += sizeof(BOX2DFLOAT4);
+       }
+
+               if (lwgeom_hasSRID((unsigned char) serialized_form[0]) )
                {
                        loc += 4;
                }
@@ -1459,7 +1593,7 @@ extern char *lwgeom_construct(int SRID,int finalType,int ndims, int nsubgeometri
        {
                lengths[t] = lwgeom_seralizedformlength(serialized_subs[t],-1);
                total_length += lengths[t];
-               this_type = lwgeom_getType(serialized_subs[t][0]);
+               this_type = lwgeom_getType((unsigned char) (serialized_subs[t][0]));
                if (type == -1)
                {
                        type = this_type;
@@ -1513,7 +1647,7 @@ extern char *lwgeom_construct(int SRID,int finalType,int ndims, int nsubgeometri
        total_length +=4 ;   // nsubgeometries
 
        result = palloc(total_length);
-       result[0] = lwgeom_makeType( ndims, SRID != -1,  type);
+       result[0] = (unsigned char) lwgeom_makeType( ndims, SRID != -1,  type);
        if (SRID != -1)
        {
                memcpy(&result[1],&SRID,4);
@@ -1552,7 +1686,7 @@ char *lwgeom_constructempty(int SRID,int ndims)
 
        result = palloc(size);
 
-       result[0] = lwgeom_makeType( ndims, SRID != -1,  COLLECTIONTYPE);
+       result[0] =(unsigned char) lwgeom_makeType( ndims, SRID != -1,  COLLECTIONTYPE);
        if (SRID != -1)
        {
                memcpy(&result[1],&SRID,4);
@@ -1577,7 +1711,7 @@ char *lwgeom_constructempty(int SRID,int ndims)
 // take a geometry, and find its length
 int lwgeom_seralizedformlength_simple(char *serialized_form)
 {
-       char type = lwgeom_getType( serialized_form[0]);
+       unsigned char type = lwgeom_getType((unsigned char) serialized_form[0]);
        int t;
        char *loc;
        uint32 ngeoms;
@@ -1594,16 +1728,23 @@ int lwgeom_seralizedformlength_simple(char *serialized_form)
        //handle all the multi* and geometrycollections the same
            //NOTE: for a geometry collection of GC of GC of GC we will be recursing...
 
-       if (lwgeom_hasSRID( serialized_form[0]) )
+
+
+       loc = serialized_form+1;
+
+       if (lwgeom_hasBBOX((unsigned char) serialized_form[0]))
        {
-               result +=4;
-               loc = serialized_form+5;
+               loc += sizeof(BOX2DFLOAT4);
+               result +=sizeof(BOX2DFLOAT4);
        }
-       else
+
+       if (lwgeom_hasSRID( (unsigned char) serialized_form[0]) )
        {
-               loc =serialized_form+1;
+               result +=4;
+               loc +=4;
        }
 
+
        ngeoms =  get_uint32(loc);
        loc +=4;
 
@@ -1648,7 +1789,7 @@ BOX3D *lw_geom_getBB(char *serialized_form)
 
 BOX3D *lw_geom_getBB_simple(char *serialized_form)
 {
-               char type = lwgeom_getType( serialized_form[0]);
+               char type = lwgeom_getType((unsigned char) serialized_form[0]);
                int t;
                char *loc;
                uint32 ngeoms;
@@ -1659,10 +1800,17 @@ BOX3D *lw_geom_getBB_simple(char *serialized_form)
 
                if (type == POINTTYPE)
                {
+
                        LWPOINT *pt = lwpoint_deserialize(serialized_form);
                        result = lwpoint_findbbox(pt);
                        pfree_point(pt);
                        return result;
+               /*
+                       result = palloc(sizeof(BOX3D));
+                       memcpy(result, serialized_form+1, sizeof(BOX2DFLOAT4));
+                       memcpy(( (char *)result)+24, serialized_form+1, sizeof(BOX2DFLOAT4));
+                       return result;
+               */
                }
 
                else if (type == LINETYPE)
@@ -1671,6 +1819,7 @@ BOX3D *lw_geom_getBB_simple(char *serialized_form)
                        result = lwline_findbbox(line);
                        pfree_line(line);
                        return result;
+
                }
                else if (type == POLYGONTYPE)
                {
@@ -1680,13 +1829,16 @@ BOX3D *lw_geom_getBB_simple(char *serialized_form)
                        return result;
                }
 
-               if (lwgeom_hasSRID( serialized_form[0]) )
-               {
-                       loc = serialized_form+5;
-               }
-               else
+                  loc = serialized_form+1;
+
+                       if (lwgeom_hasBBOX((unsigned char) serialized_form[0]))
+                       {
+                               loc += sizeof(BOX2DFLOAT4);
+                       }
+
+               if (lwgeom_hasSRID((unsigned char) serialized_form[0]) )
                {
-                       loc =serialized_form+1;
+                       loc +=4;
                }
 
                ngeoms =  get_uint32(loc);
@@ -1730,10 +1882,15 @@ BOX3D *lw_geom_getBB_inspected(LWGEOM_INSPECTED *inspected)
                for (t=0;t<inspected->ngeometries;t++)
                {
                        b1 = lw_geom_getBB_simple( inspected->sub_geoms[t] );
+
+//     elog(NOTICE,"%i has box :: BBOX3D(%g %g, %g %g)",t,b1->xmin, b1->ymin,b1->xmax, b1->ymax);
+
                        if (result != NULL)
                        {
                                b2= result;
                                result = combine_boxes(b2, b1);
+//     elog(NOTICE,"combined has :: BBOX3D(%g %g, %g %g)",result->xmin, result->ymin,result->xmax, result->ymax);
+
                                pfree(b1);
                                pfree(b2);
                        }
@@ -1808,6 +1965,14 @@ void printLWLINE(LWLINE *line)
        elog(NOTICE,"}");
 }
 
+void printLWPOINT(LWPOINT *point)
+{
+       elog(NOTICE,"LWPOINT {");
+       elog(NOTICE,"    ndims = %i", (int)point->ndims);
+       elog(NOTICE,"    SRID = %i", (int)point->SRID);
+       printPA(point->point);
+       elog(NOTICE,"}");
+}
 
 void printPA(POINTARRAY *pa)
 {
@@ -1849,7 +2014,7 @@ void printBYTES(unsigned char *a, int n)
 
        buff[2] = 0; //null terminate
 
-       elog(NOTICE," BYTE ARRAY (n=%i) {", n);
+       elog(NOTICE," BYTE ARRAY (n=%i) IN HEX: {", n);
        for (t=0;t<n;t++)
        {
                deparse_hex(a[t], buff);
@@ -1882,7 +2047,7 @@ void printMULTI(char *serialized)
        LWPOLY  *poly;
        int t;
 
-       elog(NOTICE,"MULTI* geometry (type = %i), with %i sub-geoms",lwgeom_getType(serialized[0]), inspected->ngeometries);
+       elog(NOTICE,"MULTI* geometry (type = %i), with %i sub-geoms",lwgeom_getType((unsigned char)serialized[0]), inspected->ngeometries);
 
        for (t=0;t<inspected->ngeometries;t++)
        {
@@ -1911,3 +2076,32 @@ void printMULTI(char *serialized)
        pfree_inspected(inspected);
 }
 
+void printType(unsigned char type)
+{
+       elog(NOTICE,"type 0x%x ==> hasBBOX=%i, hasSRID=%i, ndims=%i, type=%i",(unsigned int) type, lwgeom_hasBBOX(type), lwgeom_hasSRID(type),lwgeom_ndims(type), lwgeom_getType(type));
+}
+
+
+
+
+//get the SRID from the LWGEOM
+// none present => -1
+int lwgeom_getSRID(char *serialized_form)
+{
+               unsigned char type = (unsigned char) serialized_form[0];
+               char *loc;
+
+
+       loc = serialized_form+1;
+
+       if (lwgeom_hasBBOX((unsigned char) serialized_form[0]))
+       {
+               loc += sizeof(BOX2DFLOAT4);
+       }
+
+               if ( lwgeom_hasSRID(type))
+               {
+                       return get_int32(loc);
+               }
+               return -1;
+}
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..e535ae73ed781cb59755b06e69c391966ec9b253 100644 (file)
@@ -0,0 +1,103 @@
+#include "postgres.h"
+
+#include <math.h>
+#include <float.h>
+#include <string.h>
+#include <stdio.h>
+#include <errno.h>
+
+#include "access/gist.h"
+#include "access/itup.h"
+#include "access/rtree.h"
+
+#include "fmgr.h"
+#include "utils/elog.h"
+
+
+#include "lwgeom.h"
+
+
+
+
+
+#define DEBUG
+
+#include "wktparse.h"
+
+Datum LWGEOM_getSRID(PG_FUNCTION_ARGS);
+Datum LWGEOM_setSRID(PG_FUNCTION_ARGS);
+
+
+// getSRID(lwgeom) :: int4
+PG_FUNCTION_INFO_V1(LWGEOM_getSRID);
+Datum LWGEOM_getSRID(PG_FUNCTION_ARGS)
+{
+               char                    *lwgeom = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+               int                                     srid = lwgeom_getSRID (lwgeom+4);
+
+       //      printBYTES(lwgeom, *((int*)lwgeom) );
+
+               PG_RETURN_INT32(srid);
+
+}
+
+//setSRID(lwgeom, int4) :: lwgeom
+PG_FUNCTION_INFO_V1(LWGEOM_setSRID);
+Datum LWGEOM_setSRID(PG_FUNCTION_ARGS)
+{
+               char                    *lwgeom = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+               int                                     newSRID = PG_GETARG_INT32(1);
+               char                            type = lwgeom[4];
+               int                                     bbox_offset =0; //0=no bbox, otherwise sizeof(BOX2DFLOAT4)
+               int                                     len,len_new,len_left;
+               char                            *result;
+               char                            *loc_new, *loc_old;
+
+       // I'm totally cheating here
+       //    If it already has an SRID, just overwrite it.
+       //    If it doesnt have an SRID, we'll create it
+
+       if (lwgeom_hasBBOX(type))
+               bbox_offset =  sizeof(BOX2DFLOAT4);
+
+       len = *((int *)lwgeom);
+
+       if (lwgeom_hasSRID(type))
+       {
+               //we create a new one and copy the SRID in
+               result = palloc(len);
+               memcpy(result, lwgeom, len);
+               memcpy(result+5+bbox_offset, &newSRID,4);
+               PG_RETURN_POINTER(result);
+       }
+       else  // need to add one
+       {
+               len_new = len + 4;//+4 for SRID
+               result = palloc(len_new);
+               memcpy(result, &len_new, 4); // size copy in
+               result[4] = lwgeom_makeType_full(lwgeom_ndims(type), true, lwgeom_getType(type),lwgeom_hasBBOX(type));
+
+
+               loc_new = result+5;
+               loc_old = lwgeom+5;
+
+               len_left = len -4-1;// old length - size - type
+
+                       // handle bbox (if there)
+
+               if (lwgeom_hasBBOX(type))
+               {
+                       memcpy(loc_new, loc_old, sizeof(BOX2DFLOAT4)) ;//copy in bbox
+                       loc_new+=       sizeof(BOX2DFLOAT4);
+                       loc_old+=       sizeof(BOX2DFLOAT4);
+                       len_left -= sizeof(BOX2DFLOAT4);
+               }
+
+               //put in SRID
+
+               memcpy(loc_new, &newSRID,4);
+               loc_new +=4;
+               memcpy(loc_new, loc_old, len_left);
+           PG_RETURN_POINTER(result);
+       }
+}
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..f3bf62a803ddf2b8068f46ca6e1e137f7cd6faa4 100644 (file)
+#include "postgres.h"
+
+#include <math.h>
+#include <float.h>
+#include <string.h>
+#include <stdio.h>
+#include <errno.h>
+
+#include "access/gist.h"
+#include "access/itup.h"
+#include "access/rtree.h"
+
+#include "fmgr.h"
+#include "utils/elog.h"
+
+
+#include "lwgeom.h"
+#include "stringBuffer.h"
+
+
+//#define DEBUG
+//#define DEBUG_GIST
+//#define DEBUG_GIST2
+//#define DEBUG_GIST3
+//#define DEBUG_GIST4
+//#define DEBUG_GIST5
+//#define DEBUG_GIST6
+
+Datum lwgeom_same(PG_FUNCTION_ARGS);
+Datum lwgeom_overlap(PG_FUNCTION_ARGS);
+Datum lwgeom_overleft(PG_FUNCTION_ARGS);
+Datum lwgeom_left(PG_FUNCTION_ARGS);
+Datum lwgeom_right(PG_FUNCTION_ARGS);
+Datum lwgeom_overright(PG_FUNCTION_ARGS);
+Datum lwgeom_contained(PG_FUNCTION_ARGS);
+Datum lwgeom_contain(PG_FUNCTION_ARGS);
+
+
+
+static float size_box2d(Datum box);
+
+static bool lwgeom_rtree_internal_consistent(BOX2DFLOAT4 *key,BOX2DFLOAT4 *query,StrategyNumber strategy);
+static bool lwgeom_rtree_leaf_consistent(BOX2DFLOAT4 *key,BOX2DFLOAT4 *query,  StrategyNumber strategy);
+
+
+static char *bbox_cache_index =NULL;
+static BOX2DFLOAT4 *bbox_cache_value = NULL;
+static BOX2DFLOAT4 *getBOX2D_cache(char *lwgeom);
+static int counter = 0;
+static int counter2 = 0;
+static int counter1 = 0;
+
+int counter_leaf = 0;
+int counter_intern = 0;
+
+static BOX2DFLOAT4 *getBOX2D_cache(char *lwgeom)
+{
+       if (bbox_cache_index != lwgeom)
+       {
+                       BOX2DFLOAT4 box;
+
+#ifdef DEBUG_GIST4
+               elog(NOTICE,"GIST: getBOX2D_cache -- cache miss %i times",counter);
+               counter++;
+#endif
+
+                       if (bbox_cache_value == NULL)
+                               bbox_cache_value = malloc(sizeof(BOX2DFLOAT4)); // initial value
+
+                       box = getbox2d(lwgeom+4);
+
+                       memcpy(bbox_cache_value, &box, sizeof(BOX2DFLOAT4));
+                       bbox_cache_index = lwgeom;
+                       return bbox_cache_value;
+       }
+       else
+       {
+               return bbox_cache_value;
+       }
+}
+
+// all the lwgeom_<same,overlpa,overleft,left,right,overright,contained,contain>
+//  work the same.
+//  1. get lwgeom1
+//  2. get lwgeom2
+//  3. get box3d for lwgeom1
+//  4. get box3d for lwgeom2
+//  5. convert box3d (for lwgeom1) to BOX2DFLOAT4
+//  6. convert box3d (for lwgeom2) to BOX2DFLOAT4
+//  7. call the appropriate BOX2DFLOAT4 function
+//  8. return result;
+
+// lwgeom_same(lwgeom1, lwgeom2)
+PG_FUNCTION_INFO_V1(lwgeom_same);
+Datum lwgeom_same(PG_FUNCTION_ARGS)
+{
+                       char                    *lwgeom1 = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+                       char                    *lwgeom2 = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
+                       bool                            result;
+
+
+                               BOX2DFLOAT4                             box1 = getbox2d(lwgeom1+4);
+                               BOX2DFLOAT4                             box2 = getbox2d(lwgeom2+4);
+
+
+#ifdef DEBUG_GIST
+               elog(NOTICE,"GIST: lwgeom_same --entry");
+#endif
+
+
+                       result = DatumGetBool(DirectFunctionCall2(box2d_same,
+                                                                                                 PointerGetDatum(&box1),
+                                                                                     PointerGetDatum(&box2)   )     );
+
+               PG_FREE_IF_COPY(lwgeom1, 0);
+        PG_FREE_IF_COPY(lwgeom2, 1);
+
+        PG_RETURN_BOOL(result);
+}
+
+PG_FUNCTION_INFO_V1(lwgeom_overlap);
+Datum lwgeom_overlap(PG_FUNCTION_ARGS)
+{
+                       char                    *lwgeom1 = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+                       char                    *lwgeom2 = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
+                       bool                            result;
+
+
+                               BOX2DFLOAT4                             box1 = getbox2d(lwgeom1+4);
+                               BOX2DFLOAT4                             box2 = getbox2d(lwgeom2+4);
+
+
+#ifdef DEBUG_GIST
+               elog(NOTICE,"GIST: lwgeom_overlap --entry");
+#endif
+
+                       result = DatumGetBool(DirectFunctionCall2(box2d_overlap,
+                                                                                                 PointerGetDatum(&box1),
+                                                                                     PointerGetDatum(&box2)   )     );
+
+               PG_FREE_IF_COPY(lwgeom1, 0);
+        PG_FREE_IF_COPY(lwgeom2, 1);
+
+        PG_RETURN_BOOL(result);
+}
+
+
+PG_FUNCTION_INFO_V1(lwgeom_overleft);
+Datum lwgeom_overleft(PG_FUNCTION_ARGS)
+{
+                       char                    *lwgeom1 = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+                       char                    *lwgeom2 = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
+                       bool                            result;
+
+
+                               BOX2DFLOAT4                             box1 = getbox2d(lwgeom1+4);
+                               BOX2DFLOAT4                             box2 = getbox2d(lwgeom2+4);
+#ifdef DEBUG_GIST
+               elog(NOTICE,"GIST: lwgeom_overleft --entry");
+#endif
+
+                       result = DatumGetBool(DirectFunctionCall2(box2d_overleft,
+                                                                                                 PointerGetDatum(&box1),
+                                                                                     PointerGetDatum(&box2)   )     );
+
+               PG_FREE_IF_COPY(lwgeom1, 0);
+        PG_FREE_IF_COPY(lwgeom2, 1);
+
+        PG_RETURN_BOOL(result);
+}
+
+PG_FUNCTION_INFO_V1(lwgeom_left);
+Datum lwgeom_left(PG_FUNCTION_ARGS)
+{
+                       char                    *lwgeom1 = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+                       char                    *lwgeom2 = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
+                       bool                            result;
+
+                               BOX2DFLOAT4                             box1 = getbox2d(lwgeom1+4);
+                               BOX2DFLOAT4                             box2 = getbox2d(lwgeom2+4);
+
+#ifdef DEBUG_GIST
+               elog(NOTICE,"GIST: lwgeom_left --entry");
+#endif
+
+                       result = DatumGetBool(DirectFunctionCall2(box2d_left,
+                                                                                                 PointerGetDatum(&box1),
+                                                                                     PointerGetDatum(&box2)   )     );
+
+               PG_FREE_IF_COPY(lwgeom1, 0);
+        PG_FREE_IF_COPY(lwgeom2, 1);
+
+        PG_RETURN_BOOL(result);
+}
+
+
+PG_FUNCTION_INFO_V1(lwgeom_right);
+Datum lwgeom_right(PG_FUNCTION_ARGS)
+{
+                       char                    *lwgeom1 = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+                       char                    *lwgeom2 = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
+                       bool                            result;
+
+
+                               BOX2DFLOAT4                             box1 = getbox2d(lwgeom1+4);
+                               BOX2DFLOAT4                             box2 = getbox2d(lwgeom2+4);
+#ifdef DEBUG_GIST
+               elog(NOTICE,"GIST: lwgeom_right --entry");
+#endif
+                       result = DatumGetBool(DirectFunctionCall2(box2d_right,
+                                                                                                 PointerGetDatum(&box1),
+                                                                                     PointerGetDatum(&box2)   )     );
+
+               PG_FREE_IF_COPY(lwgeom1, 0);
+        PG_FREE_IF_COPY(lwgeom2, 1);
+
+        PG_RETURN_BOOL(result);
+}
+
+
+PG_FUNCTION_INFO_V1(lwgeom_overright);
+Datum lwgeom_overright(PG_FUNCTION_ARGS)
+{
+                       char                    *lwgeom1 = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+                       char                    *lwgeom2 = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
+                       bool                            result;
+
+
+                               BOX2DFLOAT4                             box1 = getbox2d(lwgeom1+4);
+                               BOX2DFLOAT4                             box2 = getbox2d(lwgeom2+4);
+
+#ifdef DEBUG_GIST
+               elog(NOTICE,"GIST: lwgeom_overright --entry");
+#endif
+
+                       result = DatumGetBool(DirectFunctionCall2(box2d_overright,
+                                                                                                 PointerGetDatum(&box1),
+                                                                                     PointerGetDatum(&box2)   )     );
+
+               PG_FREE_IF_COPY(lwgeom1, 0);
+        PG_FREE_IF_COPY(lwgeom2, 1);
+
+        PG_RETURN_BOOL(result);
+}
+
+
+PG_FUNCTION_INFO_V1(lwgeom_contained);
+Datum lwgeom_contained(PG_FUNCTION_ARGS)
+{
+                       char                    *lwgeom1 = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+                       char                    *lwgeom2 = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
+                       bool                            result;
+
+
+                               BOX2DFLOAT4                             box1 = getbox2d(lwgeom1+4);
+                               BOX2DFLOAT4                             box2 = getbox2d(lwgeom2+4);
+
+#ifdef DEBUG_GIST
+               elog(NOTICE,"GIST: lwgeom_contained --entry");
+#endif
+                       result = DatumGetBool(DirectFunctionCall2(box2d_contained,
+                                                                                                 PointerGetDatum(&box1),
+                                                                                     PointerGetDatum(&box2)   )     );
+
+               PG_FREE_IF_COPY(lwgeom1, 0);
+        PG_FREE_IF_COPY(lwgeom2, 1);
+
+        PG_RETURN_BOOL(result);
+}
+
+
+PG_FUNCTION_INFO_V1(lwgeom_contain);
+Datum lwgeom_contain(PG_FUNCTION_ARGS)
+{
+                       char                    *lwgeom1 = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+                       char                    *lwgeom2 = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
+                       bool                            result;
+
+
+                               BOX2DFLOAT4                             box1 = getbox2d(lwgeom1+4);
+                               BOX2DFLOAT4                             box2 = getbox2d(lwgeom2+4);
+
+#ifdef DEBUG_GIST
+               elog(NOTICE,"GIST: lwgeom_contain --entry");
+#endif
+
+                       result = DatumGetBool(DirectFunctionCall2(box2d_contain,
+                                                                                                 PointerGetDatum(&box1),
+                                                                                     PointerGetDatum(&box2)   )     );
+
+               PG_FREE_IF_COPY(lwgeom1, 0);
+        PG_FREE_IF_COPY(lwgeom2, 1);
+
+        PG_RETURN_BOOL(result);
+}
+
+
+// These functions are taken from the postgis_gist_72.c file
+
+
+
+PG_FUNCTION_INFO_V1(gist_lwgeom_compress);
+Datum gist_lwgeom_compress(PG_FUNCTION_ARGS)
+{
+       GISTENTRY *entry=(GISTENTRY*)PG_GETARG_POINTER(0);
+       GISTENTRY *retval;
+
+
+#ifdef DEBUG_GIST4
+       elog(NOTICE,"GIST: gist_lwgeom_compress called on lwgeom");
+#endif
+
+       if ( entry->leafkey)
+       {
+               retval = palloc(sizeof(GISTENTRY));
+               if ( DatumGetPointer(entry->key) != NULL )
+               {
+
+                       char        *in; // lwgeom serialized
+                       BOX2DFLOAT4     r,*rr;
+
+                       in = (char*)PG_DETOAST_DATUM( entry->key );  // lwgeom serialized form
+
+                       if (in == NULL)
+                               PG_RETURN_POINTER(entry);
+
+                       if (lwgeom_getnumgeometries(in+4) ==0)  // this is the EMPTY geometry
+                       {
+elog(NOTICE,"found an empty geometry");
+                               // dont bother adding this to the index
+                               PG_RETURN_POINTER(entry);
+                       }
+                       else
+                       {
+                               r = getbox2d(in+4);
+                               rr = (BOX2DFLOAT4*) palloc(sizeof(BOX2DFLOAT4));
+                               memcpy(rr,&r,sizeof(BOX2DFLOAT4));
+
+
+#ifdef DEBUG_GIST2
+       elog(NOTICE,"GIST: gist_lwgeom_compress -- got box2d BOX(%.15g %.15g,%.15g %.15g)", r.xmin, r.ymin, r.xmax, r.ymax);
+#endif
+                               //r = convert_box3d_to_box(&in->bvol);
+
+                               if ( in != (char*)DatumGetPointer(entry->key) )
+                               {
+                                       pfree( in );  // PG_FREE_IF_COPY
+                               }
+
+                               gistentryinit(*retval, PointerGetDatum(rr), entry->rel, entry->page,
+                                       entry->offset, sizeof(BOX2DFLOAT4), FALSE);
+                       }
+
+               }
+               else
+               {
+                       gistentryinit(*retval, (Datum) 0, entry->rel, entry->page,
+                               entry->offset, 0,FALSE);
+               }
+       }
+       else
+       {
+               retval = entry;
+       }
+
+       PG_RETURN_POINTER(retval);
+}
+
+
+PG_FUNCTION_INFO_V1(gist_lwgeom_consistent);
+Datum gist_lwgeom_consistent(PG_FUNCTION_ARGS)
+{
+    GISTENTRY *entry = (GISTENTRY*) PG_GETARG_POINTER(0);
+    char *query ; // lwgeom serialized form
+    StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
+   // BOX              thebox;
+       bool result;
+       BOX2DFLOAT4 *thebox;
+       BOX2DFLOAT4  box;
+
+       /*
+    ** if entry is not leaf, use rtree_internal_consistent,
+    ** else use rtree_leaf_consistent
+    */
+#ifdef DEBUG_GIST
+       elog(NOTICE,"GIST: gist_lwgeom_consistent called");
+#endif
+
+   if (   ( (Pointer *) PG_GETARG_DATUM(1) ) == NULL)
+    {
+               //elog(NOTICE,"gist_lwgeom_consistent:: got null query!");
+               PG_RETURN_BOOL(false); // null query - this is screwy!
+       }
+
+       query = (char*) PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
+
+
+    if ( ! (DatumGetPointer(entry->key) != NULL && query) )
+               PG_RETURN_BOOL(FALSE);
+
+
+       //box3d = lw_geom_getBB(query+4);
+       //thebox = box3d_to_box2df(box3d);
+       //pfree(box3d);
+               //convert_box3d_to_box_p( &(query->bvol) , &thebox);
+       //thebox = getBOX2D_cache(query);
+  getbox2d_p(query+4, &box);
+
+       if (GIST_LEAF(entry))
+               result = lwgeom_rtree_leaf_consistent((BOX2DFLOAT4 *) DatumGetPointer(entry->key), &box, strategy );
+       else
+               result = lwgeom_rtree_internal_consistent((BOX2DFLOAT4 *) DatumGetPointer(entry->key), &box, strategy );
+
+       //pfree(thebox);
+       PG_FREE_IF_COPY(query, 1);
+       PG_RETURN_BOOL(result);
+}
+
+
+static bool lwgeom_rtree_internal_consistent(BOX2DFLOAT4 *key,
+                       BOX2DFLOAT4 *query,
+                       StrategyNumber strategy)
+{
+    bool retval;
+
+#ifdef DEBUG_GIST
+       elog(NOTICE,"GIST: lwgeom_rtree_internal_consistent called with strategy=%i",strategy);
+#endif
+
+    switch(strategy) {
+    case RTLeftStrategyNumber:
+    case RTOverLeftStrategyNumber:
+      retval = DatumGetBool( DirectFunctionCall2( box2d_overleft, PointerGetDatum(key), PointerGetDatum(query) ) );
+      break;
+    case RTOverlapStrategyNumber:  //optimized for speed
+
+        retval = (((key->xmax>= query->xmax) &&
+                        (key->xmin <= query->xmax)) ||
+                       ((query->xmax>= key->xmax) &&
+                        (query->xmin<= key->xmax)))
+               &&
+               (((key->ymax>= query->ymax) &&
+                 (key->ymin<= query->ymax)) ||
+                ((query->ymax>= key->ymax) &&
+                 (query->ymin<= key->ymax)));
+
+
+
+#ifdef DEBUG_GIST5
+                                       if (counter_intern == 0)
+                                       {
+                                                                        elog(NOTICE,"search bounding box is: <%.16g %.16g,%.16g %.16g> - size box2d= %i",
+                                                                               query->xmin,query->ymin,query->xmax,query->ymax,sizeof(BOX2DFLOAT4));
+
+                                       }
+
+
+
+elog(NOTICE,"%i:(int)<%.8g %.8g,%.8g %.8g>&&<%.8g %.8g,%.8g %.8g> %i",counter_intern,key->xmin,key->ymin,key->xmax,key->ymax,
+                                                               query->xmin,query->ymin,query->xmax,query->ymax,   (int) retval);
+#endif
+                       counter_intern++;
+                 return(retval);
+      break;
+    case RTOverRightStrategyNumber:
+    case RTRightStrategyNumber:
+      retval = DatumGetBool( DirectFunctionCall2( box2d_overright, PointerGetDatum(key), PointerGetDatum(query) ) );
+      break;
+    case RTSameStrategyNumber:
+    case RTContainsStrategyNumber:
+      retval = DatumGetBool( DirectFunctionCall2( box2d_contain, PointerGetDatum(key), PointerGetDatum(query) ) );
+      break;
+    case RTContainedByStrategyNumber:
+      retval = DatumGetBool( DirectFunctionCall2( box2d_overlap, PointerGetDatum(key), PointerGetDatum(query) ) );
+      break;
+    default:
+      retval = FALSE;
+    }
+    return(retval);
+}
+
+
+static bool lwgeom_rtree_leaf_consistent(BOX2DFLOAT4 *key,
+                       BOX2DFLOAT4 *query,
+                       StrategyNumber strategy)
+{
+    bool retval;
+
+#ifdef DEBUG_GIST
+       elog(NOTICE,"GIST: rtree_leaf_consist called with strategy=%i",strategy);
+#endif
+
+       switch (strategy)
+       {
+               case RTLeftStrategyNumber:
+                       retval = DatumGetBool(DirectFunctionCall2(box2d_left, PointerGetDatum(key), PointerGetDatum(query)));
+                       break;
+               case RTOverLeftStrategyNumber:
+                       retval = DatumGetBool(DirectFunctionCall2(box2d_overleft, PointerGetDatum(key), PointerGetDatum(query)));
+                       break;
+               case RTOverlapStrategyNumber://optimized for speed
+                                  retval = (((key->xmax>= query->xmax) &&
+                                                (key->xmin <= query->xmax)) ||
+                                               ((query->xmax>= key->xmax) &&
+                                                (query->xmin<= key->xmax)))
+                                       &&
+                                       (((key->ymax>= query->ymax) &&
+                                         (key->ymin<= query->ymax)) ||
+                                        ((query->ymax>= key->ymax) &&
+                                         (query->ymin<= key->ymax)));
+#ifdef DEBUG_GIST5
+                          elog(NOTICE,"%i:gist test (leaf) <%.6g %.6g,%.6g %.6g> &&  <%.6g %.6g,%.6g %.6g> --> %i",counter_leaf,key->xmin,key->ymin,key->xmax,key->ymax,
+                                                                                       query->xmin,query->ymin,query->xmax,query->ymax,   (int) retval);
+#endif
+                          counter_leaf++;
+                 return(retval);
+                       break;
+               case RTOverRightStrategyNumber:
+                       retval = DatumGetBool(DirectFunctionCall2(box2d_overright, PointerGetDatum(key), PointerGetDatum(query)));
+                       break;
+               case RTRightStrategyNumber:
+                       retval = DatumGetBool(DirectFunctionCall2(box2d_right, PointerGetDatum(key), PointerGetDatum(query)));
+                       break;
+               case RTSameStrategyNumber:
+                       retval = DatumGetBool(DirectFunctionCall2(box2d_same, PointerGetDatum(key), PointerGetDatum(query)));
+                       break;
+               case RTContainsStrategyNumber:
+                       retval = DatumGetBool(DirectFunctionCall2(box2d_contain, PointerGetDatum(key), PointerGetDatum(query)));
+                       break;
+               case RTContainedByStrategyNumber:
+                       retval = DatumGetBool(DirectFunctionCall2(box2d_contained, PointerGetDatum(key), PointerGetDatum(query)));
+                       break;
+               default:
+                       retval = FALSE;
+       }
+       return (retval);
+}
+
+
+
+PG_FUNCTION_INFO_V1(gist_rtree_decompress);
+Datum gist_rtree_decompress(PG_FUNCTION_ARGS)
+{
+#ifdef DEBUG_GIST
+               elog(NOTICE,"GIST: gist_rtree_decompress called %i",counter2);
+               counter2++;
+#endif
+
+    PG_RETURN_POINTER(PG_GETARG_POINTER(0));
+}
+
+
+
+/*
+** The GiST Union method for boxes
+** returns the minimal bounding box that encloses all the entries in entryvec
+*/
+PG_FUNCTION_INFO_V1(lwgeom_box_union);
+Datum lwgeom_box_union(PG_FUNCTION_ARGS)
+{
+       bytea      *entryvec = (bytea *) PG_GETARG_POINTER(0);
+       int                *sizep = (int *) PG_GETARG_POINTER(1);
+       int                     numranges,
+                               i;
+       BOX2DFLOAT4                *cur,
+                          *pageunion;
+
+
+       numranges = (VARSIZE(entryvec) - VARHDRSZ) / sizeof(GISTENTRY);
+       pageunion = (BOX2DFLOAT4 *) palloc(sizeof(BOX2DFLOAT4));
+       cur = (BOX2DFLOAT4 *) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[0].key);
+       memcpy((void *) pageunion, (void *) cur, sizeof(BOX2DFLOAT4));
+
+
+
+
+       for (i = 1; i < numranges; i++)
+       {
+               cur = (BOX2DFLOAT4*) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[i].key);
+
+               if (pageunion->xmax < cur->xmax)
+                       pageunion->xmax = cur->xmax;
+               if (pageunion->xmin > cur->xmin)
+                       pageunion->xmin = cur->xmin;
+               if (pageunion->ymax < cur->ymax)
+                       pageunion->ymax = cur->ymax;
+               if (pageunion->ymin > cur->ymin)
+                       pageunion->ymin = cur->ymin;
+       }
+
+       *sizep = sizeof(BOX2DFLOAT4);
+
+#ifdef DEBUG_GIST
+       elog(NOTICE,"GIST: gbox_union called with numranges=%i pageunion is: <%.16g %.16g,%.16g %.16g>", numranges,pageunion->xmin, pageunion->ymin, pageunion->xmax, pageunion->ymax);
+#endif
+
+
+       PG_RETURN_POINTER(pageunion);
+}
+
+static float size_box2d(Datum box2d)
+{
+
+       float result;
+
+
+       if (DatumGetPointer(box2d) != NULL)
+       {
+               BOX2DFLOAT4 *a = (BOX2DFLOAT4*) DatumGetPointer(box2d);
+
+                   if (a == (BOX2DFLOAT4 *) NULL || a->xmax <= a->xmin || a->ymax <= a->ymin)
+                       result =  (float) 0.0;
+                   else
+                   {
+                               result = (((double) a->xmax)-((double) a->xmin)) * (((double) a->ymax)-((double) a->ymin));
+                       }
+                     //  result= (float) ((a->xmax - a->xmin) * (a->ymax - a->ymin));
+       }
+       else
+               result = (float) 0.0;
+
+#ifdef DEBUG_GIST
+       elog(NOTICE,"GIST: size_box2d called - returning %.15g",result);
+#endif
+       return result;
+}
+
+static double size_box2d_double(Datum box2d);
+static double size_box2d_double(Datum box2d)
+{
+
+       double result;
+
+
+       if (DatumGetPointer(box2d) != NULL)
+       {
+               BOX2DFLOAT4 *a = (BOX2DFLOAT4*) DatumGetPointer(box2d);
+
+                   if (a == (BOX2DFLOAT4 *) NULL || a->xmax <= a->xmin || a->ymax <= a->ymin)
+                       result =  (double) 0.0;
+                   else
+                   {
+                               result = (((double) a->xmax)-((double) a->xmin)) * (((double) a->ymax)-((double) a->ymin));
+                       }
+                     //  result= (float) ((a->xmax - a->xmin) * (a->ymax - a->ymin));
+       }
+       else
+               result = (double) 0.0;
+
+#ifdef DEBUG_GIST
+       elog(NOTICE,"GIST: size_box2d_double called - returning %.15g",result);
+#endif
+       return result;
+}
+
+
+
+/*
+** The GiST Penalty method for boxes
+** As in the R-tree paper, we use change in area as our penalty metric
+*/
+PG_FUNCTION_INFO_V1(lwgeom_box_penalty);
+Datum lwgeom_box_penalty(PG_FUNCTION_ARGS)
+{
+       GISTENTRY  *origentry = (GISTENTRY *) PG_GETARG_POINTER(0);
+       GISTENTRY  *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
+       float      *result = (float *) PG_GETARG_POINTER(2);
+       Datum           ud;
+       BOX2DFLOAT4     *b1, *b2;
+       double          tmp1;
+
+
+
+       ud = DirectFunctionCall2(box2d_union, origentry->key, newentry->key);
+       tmp1 = size_box2d_double(ud);
+       if (DatumGetPointer(ud) != NULL)
+               pfree(DatumGetPointer(ud));
+
+       *result = tmp1 - size_box2d_double(origentry->key);
+/*
+       if ( (*result <0) && (*result > -0.00001))
+               *result = 0;
+       if ( (*result >0) && (*result < 0.00001))
+               *result = 0;
+
+*/
+
+#ifdef DEBUG_GIST6
+       elog(NOTICE,"GIST: lwgeom_box_penalty called and returning %.15g", *result);
+       if (*result<0)
+       {
+               BOX2DFLOAT4 *a, *b,*c;
+               a = (BOX2DFLOAT4*) DatumGetPointer(origentry->key);
+               b = (BOX2DFLOAT4*) DatumGetPointer(newentry->key);
+               c = (BOX2DFLOAT4*) DatumGetPointer(DirectFunctionCall2(box2d_union, origentry->key, newentry->key));
+               //elog(NOTICE,"lwgeom_box_penalty -- a = <%.16g %.16g,%.16g %.16g>", a->xmin, a->ymin, a->xmax, a->ymax);
+               //elog(NOTICE,"lwgeom_box_penalty -- b = <%.16g %.16g,%.16g %.16g>", b->xmin, b->ymin, b->xmax, b->ymax);
+               //elog(NOTICE,"lwgeom_box_penalty -- c = <%.16g %.16g,%.16g %.16g>", c->xmin, c->ymin, c->xmax, c->ymax);
+       }
+#endif
+
+       PG_RETURN_POINTER(result);
+}
+
+typedef struct {
+       BOX2DFLOAT4     *key;
+       int     pos;
+} KBsort;
+
+static int
+compare_KB(const void* a, const void* b) {
+       BOX2DFLOAT4 *abox = ((KBsort*)a)->key;
+       BOX2DFLOAT4 *bbox = ((KBsort*)b)->key;
+       float sa = (abox->xmax - abox->xmin) * (abox->ymax - abox->ymin);
+       float sb = (bbox->xmax - bbox->xmin) * (bbox->ymax - bbox->ymin);
+
+       if ( sa==sb ) return 0;
+       return ( sa>sb ) ? 1 : -1;
+}
+
+/*
+** Equality method
+*/
+PG_FUNCTION_INFO_V1(lwgeom_gbox_same);
+Datum lwgeom_gbox_same(PG_FUNCTION_ARGS)
+{
+       BOX2DFLOAT4                *b1 = (BOX2DFLOAT4 *) PG_GETARG_POINTER(0);
+       BOX2DFLOAT4                *b2 = (BOX2DFLOAT4 *) PG_GETARG_POINTER(1);
+       bool       *result = (bool *) PG_GETARG_POINTER(2);
+
+#ifdef DEBUG_GIST
+       elog(NOTICE,"GIST: lwgeom_gbox_same called");
+#endif
+
+       if (b1 && b2)
+               *result = DatumGetBool(DirectFunctionCall2(box2d_same, PointerGetDatum(b1), PointerGetDatum(b2)));
+       else
+               *result = (b1 == NULL && b2 == NULL) ? TRUE : FALSE;
+       PG_RETURN_POINTER(result);
+}
+
+
+
+
+/*
+** The GiST PickSplit method
+** New linear algorithm, see 'New Linear Node Splitting Algorithm for R-tree',
+** C.H.Ang and T.C.Tan
+*/
+PG_FUNCTION_INFO_V1(lwgeom_gbox_picksplit);
+Datum lwgeom_gbox_picksplit(PG_FUNCTION_ARGS)
+{
+       bytea      *entryvec = (bytea *) PG_GETARG_POINTER(0);
+       GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
+       OffsetNumber i;
+       OffsetNumber *listL,
+                          *listR,
+                          *listB,
+                          *listT;
+       BOX2DFLOAT4                *unionL,
+                          *unionR,
+                          *unionB,
+                          *unionT;
+       int                     posL,
+                               posR,
+                               posB,
+                               posT;
+       BOX2DFLOAT4                     pageunion;
+       BOX2DFLOAT4                *cur;
+       char            direction = ' ';
+       bool            allisequal = true;
+       OffsetNumber maxoff;
+       int                     nbytes;
+
+#ifdef DEBUG_GIST
+       elog(NOTICE,"GIST: lwgeom_gbox_picksplit called");
+#endif
+
+#ifdef DEBUG_GIST6
+       elog(NOTICE,"GIST: lwgeom_gbox_picksplit called -- analysing");
+#endif
+
+
+       posL = posR = posB = posT = 0;
+       maxoff = ((VARSIZE(entryvec) - VARHDRSZ) / sizeof(GISTENTRY)) - 1;
+
+
+
+       cur = (BOX2DFLOAT4*) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[FirstOffsetNumber].key);
+       memcpy((void *) &pageunion, (void *) cur, sizeof(BOX2DFLOAT4));
+
+#ifdef DEBUG_GIST6
+elog(NOTICE,"   cur is: <%.16g %.16g,%.16g %.16g>", cur->xmin, cur->ymin, cur->xmax, cur->ymax);
+#endif
+
+
+       /* find MBR */
+       for (i = OffsetNumberNext(FirstOffsetNumber); i <= maxoff; i = OffsetNumberNext(i))
+       {
+               cur =  (BOX2DFLOAT4*) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[i].key);
+               if ( allisequal == true &&  (
+                               pageunion.xmax != cur->xmax ||
+                               pageunion.ymax != cur->ymax ||
+                               pageunion.xmin != cur->xmin ||
+                               pageunion.ymin != cur->ymin
+                       ) )
+                       allisequal = false;
+
+               if (pageunion.xmax < cur->xmax)
+                       pageunion.xmax = cur->xmax;
+               if (pageunion.xmin > cur->xmin)
+                       pageunion.xmin = cur->xmin;
+               if (pageunion.ymax < cur->ymax)
+                       pageunion.ymax = cur->ymax;
+               if (pageunion.ymin> cur->ymin)
+                       pageunion.ymin = cur->ymin;
+       }
+
+#ifdef DEBUG_GIST6
+elog(NOTICE,"   pageunion is: <%.16g %.16g,%.16g %.16g>", pageunion.xmin, pageunion.ymin, pageunion.xmax, pageunion.ymax);
+#endif
+
+
+       nbytes = (maxoff + 2) * sizeof(OffsetNumber);
+       listL = (OffsetNumber *) palloc(nbytes);
+       listR = (OffsetNumber *) palloc(nbytes);
+       unionL = (BOX2DFLOAT4 *) palloc(sizeof(BOX2DFLOAT4));
+       unionR = (BOX2DFLOAT4 *) palloc(sizeof(BOX2DFLOAT4));
+
+       if (allisequal)
+       {
+#ifdef DEBUG_GIST6
+elog(NOTICE," AllIsEqual!");
+#endif
+               cur = (BOX2DFLOAT4*) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[OffsetNumberNext(FirstOffsetNumber)].key);
+               if (memcmp((void *) cur, (void *) &pageunion, sizeof(BOX2DFLOAT4)) == 0)
+               {
+                       v->spl_left = listL;
+                       v->spl_right = listR;
+                       v->spl_nleft = v->spl_nright = 0;
+                       memcpy((void *) unionL, (void *) &pageunion, sizeof(BOX2DFLOAT4));
+                       memcpy((void *) unionR, (void *) &pageunion, sizeof(BOX2DFLOAT4));
+
+                       for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
+                       {
+                               if (i <= (maxoff - FirstOffsetNumber + 1) / 2)
+                               {
+                                       v->spl_left[v->spl_nleft] = i;
+                                       v->spl_nleft++;
+                               }
+                               else
+                               {
+                                       v->spl_right[v->spl_nright] = i;
+                                       v->spl_nright++;
+                               }
+                       }
+                       v->spl_ldatum = PointerGetDatum(unionL);
+                       v->spl_rdatum = PointerGetDatum(unionR);
+
+                       PG_RETURN_POINTER(v);
+               }
+       }
+
+
+
+
+       listB = (OffsetNumber *) palloc(nbytes);
+       listT = (OffsetNumber *) palloc(nbytes);
+       unionB = (BOX2DFLOAT4 *) palloc(sizeof(BOX2DFLOAT4));
+       unionT = (BOX2DFLOAT4 *) palloc(sizeof(BOX2DFLOAT4));
+
+#define ADDLIST( list, unionD, pos, num ) do { \
+       if ( pos ) { \
+               if ( unionD->xmax < cur->xmax )    unionD->xmax = cur->xmax; \
+               if ( unionD->xmin       > cur->xmin  ) unionD->xmin     = cur->xmin; \
+               if ( unionD->ymax < cur->ymax )    unionD->ymax = cur->ymax; \
+               if ( unionD->ymin       > cur->ymin  ) unionD->ymin     = cur->ymin; \
+       } else { \
+                       memcpy( (void*)unionD, (void*) cur, sizeof( BOX2DFLOAT4 ) );  \
+       } \
+       list[pos] = num; \
+       (pos)++; \
+} while(0)
+
+       for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i))
+       {
+               cur = (BOX2DFLOAT4*) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[i].key);
+               if (cur->xmin - pageunion.xmin < pageunion.xmax - cur->xmax)
+                       ADDLIST(listL, unionL, posL,i);
+               else
+                       ADDLIST(listR, unionR, posR,i);
+               if (cur->ymin - pageunion.ymin < pageunion.ymax - cur->ymax)
+                       ADDLIST(listB, unionB, posB,i);
+               else
+                       ADDLIST(listT, unionT, posT,i);
+       }
+
+#ifdef DEBUG_GIST6
+elog(NOTICE,"   unionL is: <%.16g %.16g,%.16g %.16g>", unionL->xmin, unionL->ymin, unionL->xmax, unionL->ymax);
+elog(NOTICE,"   unionR is: <%.16g %.16g,%.16g %.16g>", unionR->xmin, unionR->ymin, unionR->xmax, unionR->ymax);
+elog(NOTICE,"   unionT is: <%.16g %.16g,%.16g %.16g>", unionT->xmin, unionT->ymin, unionT->xmax, unionT->ymax);
+elog(NOTICE,"   unionB is: <%.16g %.16g,%.16g %.16g>", unionB->xmin, unionB->ymin, unionB->xmax, unionB->ymax);
+#endif
+
+
+
+       /* bad disposition, sort by ascending and resplit */
+       if ( (posR==0 || posL==0) && (posT==0 || posB==0) ) {
+               KBsort *arr = (KBsort*)palloc( sizeof(KBsort) * maxoff );
+               posL = posR = posB = posT = 0;
+               for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i)) {
+                       arr[i-1].key = (BOX2DFLOAT4*) DatumGetPointer(((GISTENTRY *) VARDATA(entryvec))[i].key);
+                       arr[i-1].pos = i;
+               }
+               qsort( arr, maxoff, sizeof(KBsort), compare_KB );
+               for (i = FirstOffsetNumber; i <= maxoff; i = OffsetNumberNext(i)) {
+                       cur = arr[i-1].key;
+                       if (cur->xmin - pageunion.xmin < pageunion.xmax - cur->xmax)
+                               ADDLIST(listL, unionL, posL,arr[i-1].pos);
+                       else if ( cur->xmin - pageunion.xmin == pageunion.xmax - cur->xmax ) {
+                               if ( posL>posR )
+                                       ADDLIST(listR, unionR, posR,arr[i-1].pos);
+                               else
+                                       ADDLIST(listL, unionL, posL,arr[i-1].pos);
+                       } else
+                               ADDLIST(listR, unionR, posR,arr[i-1].pos);
+
+                       if (cur->ymin - pageunion.ymin < pageunion.ymax - cur->ymax)
+                               ADDLIST(listB, unionB, posB,arr[i-1].pos);
+                       else if ( cur->ymin - pageunion.ymin == pageunion.ymax - cur->ymax ) {
+                               if ( posB>posT )
+                                       ADDLIST(listT, unionT, posT,arr[i-1].pos);
+                               else
+                                       ADDLIST(listB, unionB, posB,arr[i-1].pos);
+                       } else
+                               ADDLIST(listT, unionT, posT,arr[i-1].pos);
+               }
+               pfree(arr);
+       }
+
+       /* which split more optimal? */
+       if (Max(posL, posR) < Max(posB, posT))
+               direction = 'x';
+       else if (Max(posL, posR) > Max(posB, posT))
+               direction = 'y';
+       else
+       {
+               Datum           interLR = DirectFunctionCall2(box2d_inter,
+                                                                                                 PointerGetDatum(unionL),
+                                                                                                 PointerGetDatum(unionR));
+               Datum           interBT = DirectFunctionCall2(box2d_inter,
+                                                                                                 PointerGetDatum(unionB),
+                                                                                                 PointerGetDatum(unionT));
+               float           sizeLR,
+                                       sizeBT;
+
+//elog(NOTICE,"direction is abigeous");
+
+               sizeLR = size_box2d(interLR);
+               sizeBT = size_box2d(interBT);
+
+               if (sizeLR < sizeBT)
+                       direction = 'x';
+               else
+                       direction = 'y';
+       }
+
+       if (direction == 'x')
+       {
+               pfree(unionB);
+               pfree(listB);
+               pfree(unionT);
+               pfree(listT);
+
+               v->spl_left = listL;
+               v->spl_right = listR;
+               v->spl_nleft = posL;
+               v->spl_nright = posR;
+               v->spl_ldatum = PointerGetDatum(unionL);
+               v->spl_rdatum = PointerGetDatum(unionR);
+
+#ifdef DEBUG_GIST6
+elog(NOTICE,"   split direction was '%c'", direction);
+elog(NOTICE,"   posL = %i, posR=%i", posL,posR);
+elog(NOTICE,"   posL's (nleft) offset numbers:");
+{
+               char            aaa[5000],bbb[100];
+               aaa[0] =0;
+
+for (i=0;i<posL;i++)
+{
+       sprintf(bbb," %i", listL[i]);
+       strcat(aaa,bbb);
+}
+elog(NOTICE,aaa);
+aaa[0]=0;
+elog(NOTICE,"   posR's (nright) offset numbers:");
+for (i=0;i<posR;i++)
+{
+       sprintf(bbb," %i", listR[i]);
+       strcat(aaa,bbb);
+}
+elog(NOTICE,aaa);
+}
+
+#endif
+
+
+       }
+       else
+       {
+               pfree(unionR);
+               pfree(listR);
+               pfree(unionL);
+               pfree(listL);
+
+               v->spl_left = listB;
+               v->spl_right = listT;
+               v->spl_nleft = posB;
+               v->spl_nright = posT;
+               v->spl_ldatum = PointerGetDatum(unionB);
+               v->spl_rdatum = PointerGetDatum(unionT);
+
+#ifdef DEBUG_GIST6
+elog(NOTICE,"   split direction was '%c'", direction);
+elog(NOTICE,"   posB = %i, posT=%i", posB,posT);
+elog(NOTICE,"   posB's (nleft) offset numbers:");
+{
+       char            aaa[5000],bbb[100];
+       aaa[0] =0;
+for (i=0;i<posB;i++)
+{
+       sprintf(bbb," %i", listB[i]);
+       strcat(aaa,bbb);
+}
+elog(NOTICE,aaa);
+aaa[0]=0;
+elog(NOTICE,"   posT's (nright) offset numbers:");
+for (i=0;i<posT;i++)
+{
+       sprintf(bbb," %i", listT[i]);
+       strcat(aaa,bbb);
+}
+elog(NOTICE,aaa);
+}
+#endif
+
+
+       }
+       PG_RETURN_POINTER(v);
+}
+
+Datum report_lwgeom_gist_activity(PG_FUNCTION_ARGS);
+
+PG_FUNCTION_INFO_V1(report_lwgeom_gist_activity);
+Datum report_lwgeom_gist_activity(PG_FUNCTION_ARGS)
+{
+       elog(NOTICE,"lwgeom gist activity - internal consistency= %i, leaf consistency = %i",counter_intern,counter_leaf);
+       PG_RETURN_NULL();
+}
+
+
+
index 78bca3915bac4b681c51a2e92b425e2884bac63e..7a975ccfede72db0956e8c5267ccd68a474c1856 100644 (file)
 
 #define DEBUG
 
+#include "wktparse.h"
 
+void elog_ERROR(const char* string);
+void *palloc_fn(size_t size);
 
 
+extern unsigned char   parse_hex(char *str);
+extern void deparse_hex(unsigned char str, unsigned char *result);
 
-extern char *wkb_to_lwgeom(char *wkb, int SRID,int *sizeLWGEOM, int *sizeWKB);
-extern char *lwgeom_to_wkb(char *serialized_form,int *wkblength,char desiredWKBEndian);
-extern void swap_char(char     *a,char *b);
-extern void    flip_endian_double(char         *d);
-extern void            flip_endian_int32(char          *i);
-extern char wkb_dims(uint32 type);
-extern char wkb_simpletype (uint32 type);
-extern uint32 constructWKBType(int simple_type, char dims);
-extern bool requiresflip(char WKBendianflag);
-extern void flipPoints(char *pts, int npoints, char dims);
-extern uint32 constructWKBType(int simple_type, char dims);
-
-extern LWPOINT *wkb_point_to_lwpoint(char *wkb,int SRID, int *sizeWKB);
-extern LWLINE *wkb_line_to_lwline(char *wkb,int SRID, int *sizeWKB);
-extern LWPOLY *wkb_poly_to_lwpoly(char *wkb,int SRID, int *sizeWKB);
 
-extern char *lwline_to_wkb(LWLINE *line, char desiredWKBEndian, int *wkbsize);
-extern char *lwpoint_to_wkb(LWPOINT *point, char desiredWKBEndian, int *wkbsize);
-extern char *lwpoly_to_wkb(LWPOLY *poly, char desiredWKBEndian, int *wkbsize);
+extern char *parse_lwgeom_wkt(char *wkt_input);
 
-extern char *wkb_multi_to_lwgeom_serialized(char *wkb,int type, int *serialized_size, int SRID, int *sizeWKB);
-extern char *serialized_multi_to_wkb(char *serialized_form,char desiredWKBEndian,int *wkblength);
+void *palloc_fn2(size_t size);
+void free_fn(void *ptr);
 
-extern unsigned char   parse_hex(char *str);
-extern void deparse_hex(unsigned char str, unsigned char *result);
 
-extern char *renderPOINTARRAY(POINTARRAY *array, int *slen,char *header);
-extern char *renderLWLINE(LWLINE *line, bool wantHeader, int *slen);
-extern char *renderLWPOINT(LWPOINT *pt, bool wantHeader, int *slen);
-extern char *renderLWPOLY(LWPOLY  *poly, bool wantHeader, int *slen);
-extern char *renderMULTI(LWGEOM_INSPECTED *inspected, int *slen);
-extern char *renderMULTIPOINT(LWGEOM_INSPECTED *inspected, int *slen);
-extern char *renderMULTILINE(LWGEOM_INSPECTED *inspected, int *slen);
-extern char *renderMULTIPOLY(LWGEOM_INSPECTED *inspected, int *slen);
-extern char *renderGC(LWGEOM_INSPECTED *inspected, int *slen);
 
 // 3d or 4d.  There is NOT a (x,y,m) point type!!!
 #define WKB3DOFFSET 0x80000000
@@ -73,87 +50,82 @@ Datum LWGEOM_out(PG_FUNCTION_ARGS);
 
 Datum LWGEOM_asText(PG_FUNCTION_ARGS);
 
+Datum LWGEOM_addBBOX(PG_FUNCTION_ARGS);
+Datum LWGEOM_getBBOX(PG_FUNCTION_ARGS);
+
+
+Datum parse_WKT_lwgeom(PG_FUNCTION_ARGS);
 
 // WKB structure  -- exactly the same as TEXT
 typedef struct Well_known_bin {
-    int32 size;    // total size of this structure
+    int32 size;             // total size of this structure
     unsigned char  data[1]; //THIS HOLD VARIABLE LENGTH DATA
 } WellKnownBinary;
 
 
 
+
+
+// LWGEOM_in(cstring)
+// format is '[SRID=#;]wkt|wkb'
+//  LWGEOM_in( 'SRID=99;POINT(0 0)')
+//  LWGEOM_in( 'POINT(0 0)')            --> assumes SRID=-1
+//  LWGEOM_in( 'SRID=99;0101000000000000000000F03F000000000000004')
+//  returns a LWGEOM object
 PG_FUNCTION_INFO_V1(LWGEOM_in);
 Datum LWGEOM_in(PG_FUNCTION_ARGS)
 {
            char                    *str = PG_GETARG_CSTRING(0);
-           WellKnownBinary         *wkb;
-           char                                        *result,*lwgeom;
-        int                     size;
-        int                     t;
-        int                     input_str_len;
-        int                                            sizeWKB;
-
-        int SRID = -1; //default (change)
-
-       input_str_len = strlen(str);
-
-       if  ( ( ( (int)(input_str_len/2.0) ) *2.0) != input_str_len)
-       {
-                       elog(ERROR,"LWGEOM_in parser - should be even number of characters!");
-                       PG_RETURN_NULL();
-       }
-
-       if (strspn(str,"0123456789ABCDEF") != strlen(str) )
-       {
-                       elog(ERROR,"WKB_in parser - input contains bad characters.  Should only have '0123456789ABCDEF'!");
-                       PG_RETURN_NULL();
-       }
-       size = (input_str_len/2) + 4;
-       wkb = (WellKnownBinary *) palloc(size);
-       wkb->size = size;
-
-       for (t=0;t<input_str_len/2;t++)
-       {
-                       ((unsigned char *)wkb)[t+4] = parse_hex( &str[t*2]) ;
-       }
 
-       //have WKB string (and we can safely modify it)
-                       lwgeom = wkb_to_lwgeom(((char *) wkb)+4, SRID,&size,&sizeWKB);
+               char  *semicolonLoc,start;
 
-                       pfree(wkb); // no longer referenced!
 
-                       result = palloc(size+4);
+               //determine if its WKB or WKT
 
-                       memcpy(result+4, lwgeom, size);
-                       size+=4;
-                       memcpy(result, &size, 4);
+               semicolonLoc = strchr(str,';');
+               if (semicolonLoc == NULL)
+               {
+                       start = str[0];
+               }
+               else
+               {
+                       start =semicolonLoc[1]; // one in
+               }
 
-                       pfree(lwgeom);
+               if (
+                               ( (start >= '0') &&  (start <= '9') ) ||
+                               ( (start >= 'A') &&  (start <= 'F') )
+                       )
+               {
+                               //its WKB
+                               //PG_RETURN_POINTER(parse_lwgeom_serialized_form(str));
+                               PG_RETURN_POINTER( parse_lwgeom_wkt(str) );   // this function handles wkt and wkb (in hex-form)
+               }
+               else if ( (start == 'P') || (start == 'L') || (start == 'M') || (start == 'G'))
+               {
+                               // its WKT
+                               PG_RETURN_POINTER( parse_lwgeom_wkt(str) );  // this function handles wkt and wkb (in hex-form)
 
-               PG_RETURN_POINTER(result);
+               }
+               else
+               {
+                       elog(ERROR,"couldnt determine if input lwgeom is WKB or WKT");
+                       PG_RETURN_NULL();
+               }
 }
 
+
+// LWGEOM_out(lwgeom) --> cstring
+// output is 'SRID=#;<wkb in hex form>'
+// ie. 'SRID=-99;0101000000000000000000F03F0000000000000040'
+// WKB is machine endian
+// if SRID=-1, the 'SRID=-1;' will probably not be present.
 PG_FUNCTION_INFO_V1(LWGEOM_out);
 Datum LWGEOM_out(PG_FUNCTION_ARGS)
 {
                char                    *lwgeom = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-               char                            *result;
-               int                                     size_result;
-               int                                     t;
-               int                                     size;
-
-               char *wkb = lwgeom_to_wkb(lwgeom+4,&size,1); // 0=XDR, 1=NDR
-
-               size_result = size *2 +1; //+1 for null char
-               result = palloc (size_result);
-               result[size_result-1] = 0; //null terminate
+               char                            *result =unparse_WKB(lwgeom,palloc_fn2,free_fn);
 
-               for (t=0; t< size; t++)
-               {
-                       deparse_hex( ((unsigned char *) wkb)[t], &result[t*2]);
-               }
-
-               pfree(wkb);
 
                PG_RETURN_CSTRING(result);
 }
@@ -161,16 +133,18 @@ Datum LWGEOM_out(PG_FUNCTION_ARGS)
 
 
 // LWGEOMFromWKB(wkb,  [SRID] )
+// NOTE: wkb is in *binary* not hex form.
 PG_FUNCTION_INFO_V1(LWGEOMFromWKB);
 Datum LWGEOMFromWKB(PG_FUNCTION_ARGS)
 {
-               char   *wkb_input = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+               WellKnownBinary   *wkb_input = (WellKnownBinary *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+               char   *wkb_srid_hexized;
+               int    size_result,size_header;
                int    SRID = -1;
-               int    size;
-               char *lwgeom;
-               char * result;
-               char    *wkb_copy;
-               int             sizeWKB;
+               char    sridText[100];
+               char    *loc;
+               char    *lwgeom;
+               int t;
 
 
                                if (  ( PG_NARGS()>1) && ( ! PG_ARGISNULL(1) ))
@@ -181,702 +155,90 @@ Datum LWGEOMFromWKB(PG_FUNCTION_ARGS)
  //       elog(NOTICE,"LWGEOMFromWKB: entry with SRID=%i",SRID);
 
 
-                                       // need to do this because there might be a bunch of endian flips!
-               wkb_copy = palloc( *((int32 *) wkb_input));
-               memcpy(wkb_copy, wkb_input+4, *((int32 *) wkb_input) -4);
-
-               lwgeom = wkb_to_lwgeom(wkb_copy, SRID,&size,&sizeWKB);
-
-               pfree(wkb_copy); // no longer referenced!
-
-               result = palloc(size+4);
-
-               memcpy(result+4, lwgeom, size);
-               size+=4;
-               memcpy(result, &size, 4);
-
-               pfree(lwgeom);
-
-               PG_RETURN_POINTER(result);
-}
-
-// WKBFromLWGEOM(wkb)
-PG_FUNCTION_INFO_V1(WKBFromLWGEOM);
-Datum WKBFromLWGEOM(PG_FUNCTION_ARGS)
-{
-               char   *lwgeom_input = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-               int    size;
-               char *wkb = lwgeom_to_wkb(lwgeom_input+4,&size,1); // 0=XDR, 1=NDR
-
-               char *result = palloc(size+4);
-
-               memcpy(result+4, wkb, size);
-               size+=4;
-               memcpy(result, &size, 4);
-               pfree(wkb);
-
-               PG_RETURN_POINTER(result);
-}
-
-
-//given a wkb (with 3d and 4d extension)
-// create the serialized form of an equivelent LWGEOM
-// size of the lwgeom form is also returned
-//
-// note - to actually serialize, you should put the first 4
-// bytes as size (size +4), then the results of this function.
-//
-// because this function modifies the wkb (to make it the
-// server's endian), you should give it a copy of the wkb.
-// The underlying LWGEOM will point into this WKB, so
-// be extreamly careful when you free it!
-//
-// also, wkb should point to the 1st wkb character; NOT
-// the postgresql length int32.
-char *wkb_to_lwgeom(char *wkb, int SRID,int *sizeLWGEOM, int *sizeWKB)
-{
-       uint32 wkbtype;
-       char   dims;
-       bool need_flip =  requiresflip( wkb[0] );
-
-       LWPOINT *pt;
-       LWLINE  *line;
-       LWPOLY  *poly;
-       char    *result = NULL;
-       int simpletype;
-
-       if (need_flip)
-               flip_endian_int32(wkb+1);
-
-       wkbtype = get_uint32(wkb+1);
-       dims = wkb_dims(wkbtype);
-       simpletype = wkb_simpletype(wkbtype);
-
-
-//        elog(NOTICE,"wkb_to_lwgeom: entry with SRID=%i, dims=%i, simpletype=%i",SRID,(int) dims, (int) simpletype);
-
-
-
-       switch (simpletype)
-       {
-               case POINTTYPE:
-                                               pt = wkb_point_to_lwpoint(wkb, SRID,sizeWKB);
-                                               result  = lwpoint_serialize(pt);
-                                               *sizeLWGEOM = lwpoint_findlength(result);
-                                               pfree_point(pt);
-                                               break;
-               case LINETYPE:
-                                               line = wkb_line_to_lwline(wkb, SRID,sizeWKB);
-       //printLWLINE(line);
-                                               result  = lwline_serialize(line);
-//     elog(NOTICE,"line serialized");
-                                               *sizeLWGEOM = lwline_findlength(result);
-                                               pfree_line(line);
-                                               break;
-               case POLYGONTYPE:
-                                               poly = wkb_poly_to_lwpoly(wkb, SRID,sizeWKB);
-//     printLWPOLY(poly);
-                                               result  = lwpoly_serialize(poly);
-//     {
-//             LWPOLY *poly2 = lwpoly_deserialize(result);
-//             printLWPOLY(poly2);
-//     }
-                                               *sizeLWGEOM = lwpoly_findlength(result);
-                                               pfree_polygon(poly);
-                                               break;
-               case MULTIPOINTTYPE:
-               case MULTILINETYPE:
-               case MULTIPOLYGONTYPE:
-               case COLLECTIONTYPE:
-                                               result = wkb_multi_to_lwgeom_serialized(wkb,simpletype, sizeLWGEOM,  SRID,sizeWKB);
-                                               //printBYTES(result,*size);
-                                               //printMULTI(result);
-                                               break;
-       }
-
-//        elog(NOTICE,"wkb_to_lwgeom:returning");
-
-
-       return result;
-}
-
-//NOTE: THIS CHANGES THE ENDIAN OF THE WKB!!
-// we make sure the point is correctly endianed
-// and make a LWPOINT that points into it.
-// wkb --> point to the endian definition of the wkb point
-LWPOINT *wkb_point_to_lwpoint(char *wkb,int SRID, int *sizeWKB)
-{
-       uint32 wkbtype;
-       char   dims;
-       char   simpletype;
-       POINTARRAY *pa;
-
-       bool need_flip =  requiresflip( wkb[0] );
-       if (need_flip)
-               flip_endian_int32(wkb+1);
-
-       wkbtype = get_uint32(wkb+1);
-       dims = wkb_dims(wkbtype);
-       simpletype = wkb_simpletype(wkbtype);
-
-
-      //  elog(NOTICE,"wkb_point_to_lwpoint: entry with SRID=%i, dims=%i, simpletype=%i",SRID,(int) dims, (int) simpletype);
-
+                       // convert WKB to hexized WKB string
 
 
 
 
-       if (simpletype != POINTTYPE)
-               elog(ERROR,"WKB::point parser - go wrong type");
+           size_header = sprintf(sridText,"SRID=%i;",SRID);
+           size_result = size_header +  2*(wkb_input->size -4) + 1;  //SRID text size + wkb size (+1 = NULL term)
 
-       pa = pointArray_construct(wkb+5, dims, 1);
+           wkb_srid_hexized = palloc(size_result);
+           wkb_srid_hexized[0] = 0; // empty
+           strcpy(wkb_srid_hexized, sridText);
+           loc = wkb_srid_hexized + size_header; // points to null in "SRID=#;"
 
-
-       *sizeWKB = dims * 8 + 5;
-       return lwpoint_construct(dims, SRID, pa);
-}
-
-LWLINE *wkb_line_to_lwline(char *wkb,int SRID, int *sizeWKB)
-{
-               uint32 wkbtype;
-               char   dims;
-               char   simpletype;
-               POINTARRAY *pa;
-               int npoints;
-
-       bool need_flip =  requiresflip( wkb[0] );
-       if (need_flip)
-               flip_endian_int32(wkb+1); // type
-
-       wkbtype = get_uint32(wkb+1);
-       dims = wkb_dims(wkbtype);
-       simpletype = wkb_simpletype(wkbtype);
-
-       if (simpletype != LINETYPE)
-               elog(ERROR,"WKB::line parser - go wrong type");
-
-       if (need_flip)
-               flip_endian_int32(wkb+5); // npoints
-
-       npoints = get_uint32(wkb+5);
-
-       if (need_flip)
-               flipPoints(wkb+9,npoints,dims);
-
-       pa = pointArray_construct(wkb+9, dims, npoints);
-
-       *sizeWKB = dims * 8*npoints + 5 +4;
-
-       return lwline_construct(dims, SRID, pa);
-}
-
-LWPOLY *wkb_poly_to_lwpoly(char *wkb,int SRID, int *sizeWKB)
-{
-               uint32 wkbtype;
-               char   dims;
-               char   simpletype;
-               POINTARRAY *pa;
-               int npoints =0;
-               POINTARRAY **rings;
-               int nrings;
-               int t;
-               char *loc;
-               int ptsize =16;
-               int total_points=0;
-
-               bool need_flip =  requiresflip( wkb[0] );
-               if (need_flip)
-                       flip_endian_int32(wkb+1); // type
-
-               wkbtype = get_uint32(wkb+1);
-               dims = wkb_dims(wkbtype);
-               simpletype = wkb_simpletype(wkbtype);
-
-               if (simpletype != POLYGONTYPE)
-                       elog(ERROR,"WKB::polygon parser - go wrong type");
-
-               if (need_flip)
-                       flip_endian_int32(wkb+5); // nrings
-
-               nrings = get_uint32(wkb+5);
-
-               loc = wkb+9;
-
-                       //point size
-
-               ptsize =16;
-               if (dims == 4)
+           for (t=0; t< (wkb_input->size -4); t++)
                {
-                       ptsize = 32;
+                               deparse_hex( ((unsigned char *) wkb_input)[4 + t], &loc[t*2]);
                }
-               if (dims == 3)
-               {
-                       ptsize = 24;
-        }
-
-
-               rings = (POINTARRAY **) palloc(sizeof(POINTARRAY*) * nrings);
-
-               for (t=0;t<nrings;t++)
-               {
-
-                       if (need_flip)
-                               flip_endian_int32(loc); // npoints
-
-                       npoints = get_uint32(loc);
-
-                                       // read a ring
-                       if (need_flip)
-                               flipPoints(loc+4,npoints,dims);
-                       pa = pointArray_construct(loc+4, dims, npoints);
-
-                       loc += 4;
-                       loc += npoints * ptsize;
-                       rings[t] = pa;
-                       total_points += npoints;
-               }
-               *sizeWKB = dims * 8 *total_points + 4*nrings + 9;
-//elog(NOTICE,"polygon: size wkb = %i; npoints = %i", *sizeWKB, total_points);
-               return lwpoly_construct(dims, SRID, nrings,rings);
-}
-
-
-// Takes a wkb thats a multi* (or GC) and returns it in the serialized form.
-//  serialized_size is filled in.
-//  resulting serialized form will have type 'type' (MULTI* or GC).
-//
-//  Basically, all multi* geometries look the same - you just need to set
-//  the "main" type.  For example, a multipoint and GC are the same except
-//  the type is "MULTIPOINT" or "GEOMETRYCOLLECTION".
-
-char *wkb_multi_to_lwgeom_serialized(char *wkb,int type, int *serialized_size, int SRID, int *sizeWKB)
-{
-       int   *sub_obj_sizes;
-       char **sub_objs;
-
-
-       bool need_flip =  requiresflip( wkb[0] );
-       uint32 wkbtype;
-       char   dims;
-       int    ngeoms;
-       char   simpletype;
-       char   *loc;
-       int    sum_size_sub_objs = 0;
-       int    total_size = 0;
-       char   *result;
-       int    ndims = 2;
-       char   sub_type;
-       int t;
-       int   sizeWKB_sub;
-
-       sizeWKB = 0;
-
-       //make a list of all the serialized forms of the sub-objects
-       // NOTE: the sub-objects could possibly be MULTI* (gasp!).
-
-               wkbtype = get_uint32(wkb+1);
-               dims = wkb_dims(wkbtype);
-               simpletype = wkb_simpletype(wkbtype);
-
-       if ( (simpletype != MULTIPOINTTYPE) && (simpletype != MULTILINETYPE) && (simpletype != MULTIPOLYGONTYPE) && (simpletype != COLLECTIONTYPE))
-       {
-               elog(ERROR,"wkb_multi_to_lwgeom_serialized:: got a non-multi* type");
-               return NULL;
-       }
-       if (need_flip)
-               flip_endian_int32(wkb+5); // ngeoms
 
-       ngeoms = get_uint32(wkb+5);   // ngeoms
+           wkb_srid_hexized[size_result-1] = 0; // null term
 
-       sub_obj_sizes = (int *) palloc(sizeof(int  *) * ngeoms);
-       sub_objs      =         palloc(sizeof(char *) * ngeoms);
+//elog(NOTICE,"size_header = %i",size_header);
+//elog(NOTICE,"size_result = %i", size_result);
+//elog(NOTICE,"LWGEOMFromWKB :: '%s'", wkb_srid_hexized);
 
-       loc = wkb+9; // start of 1st geometry
-
-       for (t=0;t<ngeoms;t++)
-       {
-                               // this could recurse for GC's with multi* as sub-geometries!
-               sub_objs[t] = wkb_to_lwgeom(loc,SRID, &sub_obj_sizes[t],&sizeWKB_sub);
-               loc += sizeWKB_sub;
-               sum_size_sub_objs += sub_obj_sizes[t];
-               sub_type = sub_objs[t][0];
-               if (ndims < lwgeom_ndims(sub_type))
-                       ndims =  lwgeom_ndims(sub_type);
-               sizeWKB += sizeWKB_sub;
-               //elog(NOTICE,"wkb_multi_to_lwgeom_serialized:: sub object %i has LWGEOM size %i, WKB size = %i", t,sub_obj_sizes[t],sizeWKB_sub );
-       }
-
-       if (SRID != -1)
-               total_size +=4; //SRID byte
-
-       total_size += 5; // 1 for type (multi*) + 4 (numb geometries);
-       total_size += sum_size_sub_objs; // actual geometries
-
-       result = palloc(total_size);
-
-//elog(NOTICE,"wkb_multi_to_lwgeom_serialized:: total wkb size = %i", total_size);
-
-       result[0] = lwgeom_makeType( ndims,  SRID != -1,  type);//type
-       loc  = result+1;
-       if (SRID != -1)
-       {
-                       memcpy(loc, &SRID,4);
-                       loc +=4;
-       }
-
-       memcpy(loc, &ngeoms,4);
-       loc +=4;
-
-       for (t=0;t<ngeoms;t++)
-       {
-               memcpy(loc, sub_objs[t], sub_obj_sizes[t]);
-               loc += sub_obj_sizes[t];
-       }
-
-               // clean up memory
-       for (t=0;t<ngeoms;t++)
-       {
-               pfree(sub_objs[t]);
-       }
-       pfree(sub_obj_sizes);
-       pfree(sub_objs);
-
-       *serialized_size = total_size;
-       sizeWKB  += 9; // already has all the components in it
-       return result;
-
-}
+               lwgeom =  parse_lwgeom_wkt(wkb_srid_hexized);
 
+               pfree(wkb_srid_hexized);
 
-// takes a lwgeom and converts it to an appropriate wkb
-//
-// length of the wkb is returned in wkblength (this doesnt
-//  include the 4 bytes needed for the postgresql int32 length)
-//
-char *lwgeom_to_wkb(char *serialized_form,int *wkblength,char desiredWKBEndian)
-{
-       char simple_type = lwgeom_getType(serialized_form[0]);
-       char *result = NULL;
-       LWPOINT *pt;
-       LWLINE *line;
-       LWPOLY *poly;
-
-
-
-//        elog(NOTICE,"lwgeom_to_wkb: entry with  simpletype=%i, dims=%i",(int) simple_type,  lwgeom_ndims( serialized_form[0]) );
-
-
-       switch (simple_type)
-       {
-               case POINTTYPE:
-                                               pt = lwpoint_deserialize(serialized_form);
-                                               result = lwpoint_to_wkb(pt, desiredWKBEndian, wkblength);
-                                               pfree_point(pt);
-                                               break;
-               case LINETYPE:
-                                               line = lwline_deserialize(serialized_form);
-                                               result = lwline_to_wkb(line, desiredWKBEndian, wkblength);
-                                               pfree_line(line);
-                                               break;
-               case POLYGONTYPE:
-                                               poly = lwpoly_deserialize(serialized_form);
-       //printLWPOLY(poly);
-                                               result = lwpoly_to_wkb(poly, desiredWKBEndian, wkblength);
-                                               pfree_polygon(poly );
-                                               break;
-               case MULTIPOINTTYPE:
-               case MULTILINETYPE:
-               case MULTIPOLYGONTYPE:
-               case COLLECTIONTYPE:
-                                       //      elog(NOTICE,"lwgeom_to_wkb:: got a multi* lwgeom");
-                                               result = serialized_multi_to_wkb(serialized_form,desiredWKBEndian,wkblength);
-                                               //printBYTES(result,*wkblength);
-                                               break;
-       }
-       return result;
+               PG_RETURN_POINTER(lwgeom);
 }
 
-char *lwpoint_to_wkb(LWPOINT *pt, char desiredWKBEndian, int *wkbsize)
-{
-       int ptsize = pointArray_ptsize(pt->point);
-       char * result;
-       uint32 wkbtype ;
-       bool need_flip =  requiresflip( desiredWKBEndian );
-
-
-     //   elog(NOTICE,"lwpoint_to_wkb:  pa dims = %i", (int)pt->point->ndims );
-
-
-
-       *wkbsize = 1+ 4+ ptsize; //endian, type, point
-
-       result = palloc(*wkbsize);
-
-       result[0] = desiredWKBEndian; //endian flag
-
-       wkbtype = constructWKBType(POINTTYPE, pt->point->ndims);
-
-
-      //  elog(NOTICE,"lwpoint_to_wkb: entry with wkbtype=%i, pa dims = %i",wkbtype, (int)pt->point->ndims );
-
-
-
-
-       memcpy(result+1, &wkbtype, 4);
-       if (need_flip)
-               flip_endian_int32(result+1);
-
-       memcpy(result+5, pt->point->serialized_pointlist, pointArray_ptsize(pt->point) );
-       if (need_flip)
-               flipPoints(result+5, 1, pt->point->ndims);
-
-       return result;
-}
-
-char *lwline_to_wkb(LWLINE *line, char desiredWKBEndian, int *wkbsize)
+// WKBFromLWGEOM(lwgeom) --> wkb
+// this will have no 'SRID=#;'
+PG_FUNCTION_INFO_V1(WKBFromLWGEOM);
+Datum WKBFromLWGEOM(PG_FUNCTION_ARGS)
 {
-               int ptsize = pointArray_ptsize(line->points);
-               char * result;
-               uint32 wkbtype ;
-               bool need_flip =  requiresflip( desiredWKBEndian );
-
-               *wkbsize = 1+ 4+4+ line->points->npoints * ptsize; //endian, type, npoints, points
-
-               result = palloc(*wkbsize);
-
-               result[0] = desiredWKBEndian; //endian flag
-
-               wkbtype = constructWKBType(LINETYPE, line->points->ndims);
-               memcpy(result+1, &wkbtype, 4);
-               if (need_flip)
-                       flip_endian_int32(result+1);
-
-               memcpy(result+5, &line->points->npoints, 4);
-               if (need_flip)
-                       flip_endian_int32(result+5);
+               char   *lwgeom_input = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
 
-               memcpy(result+9, line->points->serialized_pointlist, ptsize * line->points->npoints);
-               if (need_flip)
-                       flipPoints(result+9, line->points->npoints, line->points->ndims);
-               return result;
-}
+               char                            *hexized_wkb_srid =unparse_WKB(lwgeom_input,palloc_fn2,free_fn); // SRID=#;<hexized wkb>
+               char                            *hexized_wkb; // hexized_wkb_srid w/o srid
+               char            *result; //wkb
 
-char *lwpoly_to_wkb(LWPOLY *poly, char desiredWKBEndian, int *wkbsize)
-{
-               int ptsize = pointArray_ptsize(poly->rings[0]);
-               char * result;
-               uint32 wkbtype ;
-               bool need_flip =  requiresflip( desiredWKBEndian );
-               int total_points =0;
-               char *loc;
+               int                     len_hexized_wkb;
+               int                     size_result;
+               char            *semicolonLoc;
                int t;
 
+//elog(NOTICE, "in WKBFromLWGEOM with WKB = '%s'", hexized_wkb_srid);
 
-               for (t=0;t<poly->nrings;t++)
-               {
-                       total_points += poly->rings[t]->npoints;
-               }
-
-               *wkbsize = 1+ 4+ 4+total_points * ptsize + 4* poly->nrings; //endian, type, npoints, all points, ring lengths
-
-//elog(NOTICE,"lwpoly_to_wkb:: a polygon with %i rings, total of %i points, wkbsize =%i",poly->nrings, total_points, *wkbsize);
-
-
-               result = palloc(*wkbsize);
+               hexized_wkb = hexized_wkb_srid;
+               semicolonLoc = strchr(hexized_wkb_srid,';');
 
-               result[0] = desiredWKBEndian; //endian flag
 
-               wkbtype = constructWKBType(POLYGONTYPE, poly->ndims);
+                               if (semicolonLoc != NULL)
+                               {
+                                       hexized_wkb = (semicolonLoc+1);
+                               }
 
-               memcpy(result+1, &wkbtype, 4);  // type
-               if (need_flip)
-                       flip_endian_int32(result+1);
+//elog(NOTICE, "in WKBFromLWGEOM with WKB (with no 'SRID=#;' = '%s'", hexized_wkb);
 
-               memcpy(result+5, &poly->nrings, 4);     // nrings
-               if (need_flip)
-                       flip_endian_int32(result+5);
-
-               loc = result+9;
-
-               for (t=0;t<poly->nrings;t++)
-               {
-                       int npoints =poly->rings[t]->npoints;
-                       memcpy( loc, &npoints, 4); // npoints
-                       if (need_flip)
-                               flip_endian_int32(loc);
-                       memcpy(loc+4, poly->rings[t]->serialized_pointlist, ptsize * npoints);
-                       if (need_flip)
-                               flipPoints(loc+4, npoints, poly->ndims);
-                       loc += 4+ ptsize * npoints;
-               }
-//printBYTES(result, *wkbsize);
-               return result;
-}
-
-
-//given an LWGEOM thats a GC or MULTI*, return the corresponding WKB
-char *serialized_multi_to_wkb(char *serialized_form,char desiredWKBEndian,int *wkblength)
-{
-       char *result;
-       uint32 wkbtype;
-       int t;
-       LWGEOM_INSPECTED *inspected;
-       int total_size_sub_objs = 0; //wkb
-       int total_size;
 
-       int   *sub_obj_sizes;  //wkb
-       char **sub_objs;       //wkb
-       bool need_flip =  requiresflip( desiredWKBEndian );
-       char *loc;
+               len_hexized_wkb = strlen(hexized_wkb);
+               size_result = len_hexized_wkb/2 + 4;
+               result = palloc(size_result);
 
-       wkbtype = constructWKBType(lwgeom_getType(serialized_form[0]), lwgeom_ndims(serialized_form[0]) );
+               memcpy(result, &size_result,4); // size header
 
-//elog(NOTICE,"serialized_multi_to_wkb::wkbtype = %i", wkbtype);
 
-       inspected = lwgeom_inspect(serialized_form);
-       sub_obj_sizes = (int *) palloc(sizeof(int  *) * inspected->ngeometries);
-       sub_objs      =         palloc(sizeof(char *) * inspected->ngeometries);
-
-
-       for ( t=0;t<inspected->ngeometries;t++)
-       {
-               char *serial_sub = inspected->sub_geoms[t];
-               sub_objs[t] = lwgeom_to_wkb(serial_sub,&sub_obj_sizes[t], desiredWKBEndian);
-               total_size_sub_objs += sub_obj_sizes[t];
-               //elog(NOTICE,"serialized_multi_to_wkb:: sub geom %i has size %i (wkb)",t,sub_obj_sizes[t]);
-       }
-
-       total_size = 1 + 4+ 4 + total_size_sub_objs;// endian + wkbtype + ngeoms + geoms
-       result = palloc(total_size);
-
-//elog(NOTICE,"serialized_multi_to_wkb::total size of wkb = %i", total_size);
-
-       result[0] = desiredWKBEndian;
-       memcpy(result+1, &wkbtype, 4);
-       if (need_flip)
-               flip_endian_int32(result+1);
-
-       memcpy(result+5, &inspected->ngeometries, 4);
-       if (need_flip)
-               flip_endian_int32(result+5);
-
-       loc = result+9;
-       for ( t=0;t<inspected->ngeometries;t++)
-       {
-               memcpy(loc, sub_objs[t], sub_obj_sizes[t]);
-               loc += sub_obj_sizes[t];
-       }
 
+                       // have a hexized string, want to make it binary
 
-               // clean up memory
-               for ( t=0;t<inspected->ngeometries;t++)
+               for (t=0; t< (len_hexized_wkb/2); t++)
                {
-                       pfree(sub_objs[t]);
+                       ((unsigned char *) result +4)[t] =      parse_hex(  hexized_wkb + (t*2) );
                }
-               pfree(sub_obj_sizes);
-               pfree(sub_objs);
-
-               pfree_inspected(inspected);
-
-       *wkblength = total_size;
-
-//     elog(NOTICE,"serialized_multi_to_wkb::returning");
-       return result;
-}
-
-
-
-bool requiresflip(char WKBendianflag)
-{
-       if (WKBendianflag == 1) // NDR
-               return ( BYTE_ORDER != LITTLE_ENDIAN );
-       if (WKBendianflag == 0) // xDR
-               return ( BYTE_ORDER != BIG_ENDIAN );
-       elog(ERROR,"WKB: endian flag isnt a 0 or 1.  WKB screwed?");
-       return 0; //shouldnt get here
-}
-
-void swap_char(char    *a,char *b)
-{
-       char c;
-
-       c = *a;
-       *a=*b;
-       *b=c;
-}
-
-
-void   flip_endian_double(char         *d)
-{
-       swap_char(d+7, d);
-       swap_char(d+6, d+1);
-       swap_char(d+5, d+2);
-       swap_char(d+4, d+3);
-}
-
-void           flip_endian_int32(char          *i)
-{
-       swap_char (i+3,i);
-       swap_char (i+2,i+1);
-}
-
-// given a wkb type
-// return twoDims, threeDims, or fourDims
-char wkb_dims(uint32 type)
-{
-       if (type & 0x80000000)
-               return 3;
-       if (type & 0x40000000)
-               return 4;
-       return 2;
-}
 
+               pfree(hexized_wkb_srid);
 
-char wkb_simpletype (uint32 type)
-{
-       return type & 0x0F;
+               PG_RETURN_POINTER(result);
 }
 
-void flipPoints(char *pts, int npoints, char dims)
-{
-       int t;
-       char *loc = pts;
-       int size =16;
 
-       if (dims == 4)
-       {
-               size = 32;
-       }
-       if (dims == 3)
-       {
-               size = 24;
-       }
 
-       for (t=0;t<npoints;t++)
-       {
-               flip_endian_double(loc);
-               flip_endian_double(loc+8);
-               if ( (dims == 3)  || (dims == 4) )
-               {
-                       flip_endian_double(loc+16);
-               }
-               if (dims == 4)
-               {
-                       flip_endian_double(loc+24);
-               }
-               loc += size;
-       }
-}
 
-uint32 constructWKBType(int simple_type, char dims)
-{
-       if (dims == 2)
-               return simple_type;
-       if (dims == 3)
-               return simple_type | 0x80000000;
-
-       return simple_type | 0x40000000;
-}
 
 
 //given one byte, populate result with two byte representing
@@ -997,7 +359,6 @@ void deparse_hex(unsigned char str, unsigned char *result)
        }
 }
 
-
 //given a string with at least 2 chars in it, convert them to
 // a byte value.  No error checking done!
 unsigned char  parse_hex(char *str)
@@ -1113,420 +474,178 @@ unsigned char  parse_hex(char *str)
 }
 
 
-// takes a point list and makes a '(1 2,3 4)'-like string
-// handles 2d, 3d, and 4d entities
-//
-// zero-length list looks like '()'
-//
-// final length of the string (excluding null-termination)
-// is returned in 'slen' for easy manipulation (strlen(result) == *slen)
-//
-// '(1 2,3 4)'         -- 2d example
-// '(1 2 3,4 5 6)'     -- 3d example
-// '(1 2 3 4,5 6 7 8)' -- 4d example
-//
-// header is something you put at the beginning of the result
-// ie renderPOINTARRAY(<array>, <len return>, "LINESTRING")
-// --> 'LINESTRING(1 2 3,4 5 6)'
-// if you dont want a header, then send in null or an empty string
-char *renderPOINTARRAY(POINTARRAY *array, int *slen, char *header)
-{
-       STRBUFF *buff ;
-       char    aPointStr[400]; // big enough for a 4d point in all representations!
-       int             t;
-       POINT2D pt2d;
-       POINT3D pt3d;
-       POINT4D pt4d;
-       char    *result;
-       int     len;
-       int     head_length=0;
-
-       if (header != NULL)
-       {
-               head_length = strlen(header);
-                                       // guess size a 10 chars/ordinate
-               buff = new_strBUFF(array->ndims * array->npoints * 10 +  head_length);
-               catenate(buff, header, head_length);
-       }
-       else  // no header
-       {
-                                       // guess size a 10 chars/ordinate
-               buff = new_strBUFF(array->ndims * array->npoints * 10);
-       }
-
-       catenate(buff,"(",1);
-
-                       //optimize loop for speed
-               if (array->ndims == 4)
-               {
-                       for (t=0;t<array->npoints;t++)
-                       {
-                                getPoint4d_p(array, t,(char *) &pt4d);
-                                len = sprintf(aPointStr,"%.15g %.15g %.15g %.15g",pt4d.x,pt4d.y,pt4d.z,pt4d.m);
-                                catenate(buff,aPointStr,len);
-                                if (t!= (array->npoints-1)) // not the last one in the list
-                                       catenate(buff,",",1);
-                       }
-               }
-               if (array->ndims == 3)
-               {
-                       for (t=0;t<array->npoints;t++)
-                       {
-                                getPoint3d_p(array, t,(char *) &pt3d);
-                                len = sprintf(aPointStr,"%.15g %.15g %.15g",pt3d.x,pt3d.y,pt3d.z);
-                                catenate(buff,aPointStr,len);
-                                if (t!= (array->npoints-1)) // not the last one in the list
-                                       catenate(buff,",",1);
-                       }
-               }
-               if (array->ndims == 2)
-               {
-                       for (t=0;t<array->npoints;t++)
-                       {
-                                getPoint2d_p(array, t,(char *) &pt2d);
-                                len = sprintf(aPointStr,"%.15g %.15g",pt2d.x,pt2d.y);
-                                catenate(buff,aPointStr,len);
-                                if (t!= (array->npoints-1)) // not the last one in the list
-                                       catenate(buff,",",1);
-                       }
-               }
 
+void *palloc_fn2(size_t size)
+{
+       void * result;
 
-       catenate(buff,")",1);
-       *slen =  buff->length;
-       result = to_CString(buff);
-       delete_StrBUFF(buff);
+       result = palloc(size);
+//     elog(NOTICE,"  palloc(%i) at %p",size,result);
        return result;
 }
 
-
-// makes a 'LINESTRING(1 2, 3 4)' (wantHeader = true)
-// makes a '(1 2, 3 4)'           (wantHeader = false)
-// final length of the string (excluding null-termination)
-// is returned in 'slen' for easy manipulation (strlen(result) == *slen)
-char *renderLWLINE(LWLINE *line, bool wantHeader, int *slen)
+void free_fn(void *ptr)
 {
-       if (wantHeader)
-       {
-               return renderPOINTARRAY( line->points, slen, "LINESTRING");
-       }
-       else
-       {
-               return renderPOINTARRAY( line->points, slen, NULL);
-       }
-}
 
-// makes a 'POINT(1 2)' (wantHeader = true)
-// makes a '(1 2)'      (wantHeader = false)
-// final length of the string (excluding null-termination)
-// is returned in 'slen' for easy manipulation (strlen(result) == *slen)
-char *renderLWPOINT(LWPOINT *pt, bool wantHeader, int *slen)
-{
-       if (wantHeader)
-       {
-               return renderPOINTARRAY( pt->point, slen, "POINT");
-       }
-       else
-       {
-               return renderPOINTARRAY( pt->point, slen, NULL);
-       }
+//     elog(NOTICE,"  pfree(%p)", ptr);
+       pfree(ptr);
 }
 
-// makes a 'POLYGON((<ring 0>),(<ring 1>))' (wantHeader = true)
-// makes a '((<ring 0>),(<ring 1>))'      (wantHeader = false)
-// example::
-//  'POLYGON((0 0.1,0.2 10.3,10.4 10.5,10.7 0.6,0 0.1))'     (1 ring)
-//  'POLYGON((0 0.1,0.2 10.3,10.4 10.5,10.7 0.6,0 0.1),(2 2,2 3,3 3,3 2,2 2))'   (2 rings)
-// final length of the string (excluding null-termination)
-// is returned in 'slen' for easy manipulation (strlen(result) == *slen)
-char *renderLWPOLY(LWPOLY *poly, bool wantHeader, int *slen)
+
+
+
+
+PG_FUNCTION_INFO_V1(LWGEOM_asText);
+Datum LWGEOM_asText(PG_FUNCTION_ARGS)
 {
-       STRBUFF *result_buff;
-       char    *result;
-       int total_points =0;
-       int t;
+               char                    *lwgeom = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+               char                            *result_cstring =  unparse_WKT(lwgeom,palloc_fn2,free_fn);
+               int                                     len;
 
 
+        char                *result,*loc_wkt;
+               char            *semicolonLoc;
 
-       for (t=0;t<poly->nrings;t++)
-       {
-               total_points += poly->rings[t]->npoints;
-       }
-               // guess at size
-       result_buff = new_strBUFF(poly->ndims * total_points * 10 );
+               semicolonLoc = strchr(result_cstring,';');
 
+                               //loc points to start of wkt
+               if (semicolonLoc == NULL)
+                       loc_wkt = result_cstring;
+               else
+                       loc_wkt = semicolonLoc +1;
 
-       if (wantHeader)
-               catenate(result_buff, "POLYGON(",8);
-       else
-               catenate(result_buff, "(",1);
 
-       for (t=0;t<poly->nrings;t++)
-       {
-               char *aRingStr = renderPOINTARRAY( poly->rings[t], slen, NULL);
-               catenate(result_buff,aRingStr,*slen);
-               pfree(aRingStr);
-               if (t!= (poly->nrings-1) )      // not the last one
-                       catenate(result_buff,",",1);
-       }
+               len = strlen(loc_wkt)+4;
+               result = palloc(len);
+               memcpy(result, &len, 4);
 
-       catenate(result_buff, ")",1);
+               memcpy(result+4,loc_wkt, len-4);
 
-       *slen =  result_buff->length;
-       result = to_CString(result_buff);
-       delete_StrBUFF(result_buff);
-       return result;
+               pfree(result_cstring);
+               PG_RETURN_POINTER(result);
 }
 
-// NOTE:  (!! LOOK !!)
-//  'MULTIPOINT(0 0,1 1,2 2)' --NOT--- 'MULTIPOINT((0 0),(1 1),(2 2))'
-char *renderMULTIPOINT(LWGEOM_INSPECTED *inspected, int *slen)
-{
-               STRBUFF *buff ;
-               char    aPointStr[400]; // big enough for a 4d point in all representations!
-               int             t;
-               POINT2D pt2d;
-               POINT3D pt3d;
-               POINT4D pt4d;
-               char    *result;
-               int     len;
-
 
-                                               // guess size a 10 chars/ordinate
-                       buff = new_strBUFF(4 * inspected->ngeometries * 10);
+// puts a bbox inside the geometry
+PG_FUNCTION_INFO_V1(LWGEOM_addBBOX);
+Datum LWGEOM_addBBOX(PG_FUNCTION_ARGS)
+{
+                       char                    *lwgeom = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+                       char                            *result;
+                       BOX2DFLOAT4                     box;
+                       unsigned char           old_type;
+                       int                                     size;
 
 
-               catenate(buff,"MULTIPOINT(",11);
+//elog(NOTICE,"in LWGEOM_addBBOX");
 
-               for (t=0;t<inspected->ngeometries;t++)
-               {
-                       LWPOINT *pt = lwgeom_getpoint_inspected(inspected, t);
-                       if (pt->ndims == 4)
-                       {
-                                getPoint4d_p(pt->point,0,(char *) &pt4d);
-                                len = sprintf(aPointStr,"%.15g %.15g %.15g %.15g",pt4d.x,pt4d.y,pt4d.z,pt4d.m);
-                                catenate(buff,aPointStr,len);
-                                if (t!= (inspected->ngeometries-1)) // not the last one in the list
-                                               catenate(buff,",",1);
-                       }
-                       if (pt->ndims == 3)
-                       {
-                                getPoint3d_p(pt->point,0,(char *) &pt3d);
-                                len = sprintf(aPointStr,"%.15g %.15g %.15g",pt3d.x,pt3d.y,pt3d.z);
-                                catenate(buff,aPointStr,len);
-                                if (t!= (inspected->ngeometries-1)) // not the last one in the list
-                                catenate(buff,",",1);
-                       }
-                       if (pt->ndims == 2)
+                       if (lwgeom_hasBBOX( (unsigned char) lwgeom[0] ) )
                        {
-                                getPoint2d_p(pt->point,0,(char *) &pt2d);
-                                len = sprintf(aPointStr,"%.15g %.15g",pt2d.x,pt2d.y);
-                                catenate(buff,aPointStr,len);
-                                if (t!= (inspected->ngeometries-1)) // not the last one in the list
-                                        catenate(buff,",",1);
+//elog(NOTICE,"LWGEOM_addBBOX  -- already has bbox");
+                               // easy - already has one.  Just copy!
+                               result = palloc ( *((int *) lwgeom));
+                               memcpy(result,lwgeom, *((int *) lwgeom));
+                               PG_RETURN_POINTER(result);
                        }
-                       pfree_point(pt);
-               }
 
-               catenate(buff,")",1);
-               *slen =  buff->length;
-               result = to_CString(buff);
-               delete_StrBUFF(buff);
-       return result;
-}
+//elog(NOTICE,"LWGEOM_addBBOX  -- giving it a bbox");
 
-char *renderMULTILINE(LWGEOM_INSPECTED *inspected, int *slen)
-{
-                       STRBUFF *buff ;
-                       int             t;
-                       char    *result;
-                       int     len;
+                       //construct new one
+                       box = getbox2d(lwgeom+4);
+                       old_type = (unsigned char) lwgeom[4];
 
-                                                       // guess size a 10 chars/ordinate and 10 points/line -- total guess
-                       buff = new_strBUFF(4* inspected->ngeometries * 10 * 10);
+                       size = *((int *) lwgeom) +sizeof(BOX2DFLOAT4);
 
+                       result = palloc ( size );// 16 for bbox2d
 
-                       catenate(buff,"MULTILINESTRING(",16);
+                       memcpy(result,&size,4); // size
+                       result[4] = lwgeom_makeType_full( lwgeom_ndims(old_type), lwgeom_hasSRID(old_type), lwgeom_getType(old_type), 1);
+                       memcpy(result+5, &box, sizeof(BOX2DFLOAT4)); // copy in bbox
+                       memcpy(result+5+sizeof(BOX2DFLOAT4), lwgeom+5, *((int *) lwgeom) -5);  // everything but the type and length
 
-                       for (t=0;t<inspected->ngeometries;t++)
-                       {
-                               LWLINE *line = lwgeom_getline_inspected(inspected, t);
-                               char    *single_line = renderLWLINE(line,false, &len);
-                               catenate(buff, single_line, len);
-                               pfree(single_line);
-                               pfree_line(line);
-                               if (t != (inspected->ngeometries-1) )
-                                       catenate(buff,",",1);
-                       }
+                       PG_RETURN_POINTER(result);
+}
 
-                       catenate(buff,")",1);
-                       *slen =  buff->length;
-                       result = to_CString(buff);
-                       delete_StrBUFF(buff);
 
-                       return result;
-}
 
-char *renderMULTIPOLY(LWGEOM_INSPECTED *inspected, int *slen)
+void elog_ERROR(const char* string)
 {
-                       STRBUFF *buff ;
-                       int             t;
-                       char    *result;
-                       int     len;
+       elog(ERROR,string);
+}
 
+void *palloc_fn(size_t size)
+{
+       void * result;
 
+       result = palloc(size);
+       return result;
+}
 
-                                                       // guess size a 10 chars/ordinate and 20 points/polygon -- total guess
-                       buff = new_strBUFF(4 * inspected->ngeometries * 10 * 20);
+PG_FUNCTION_INFO_V1(parse_WKT_lwgeom);
+Datum parse_WKT_lwgeom(PG_FUNCTION_ARGS)
+{
+               const char   *wkt_input = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));  // text
+               char             *serialized_form;  //with length
+               char             *wkt;
+               int                       wkt_size ;
 
 
-                       catenate(buff,"MULTIPOLYGON(",13);
 
-                       for (t=0;t<inspected->ngeometries;t++)
-                       {
-                               LWPOLY  *poly = lwgeom_getpoly_inspected(inspected, t);
-                               char    *single_poly = renderLWPOLY(poly,false, &len);
-                               catenate(buff, single_poly, len);
-                               pfree(single_poly);
-                               pfree_polygon(poly);
-                               if (t != (inspected->ngeometries-1) )
-                                       catenate(buff,",",1);
-                       }
+               wkt_size = (*(int*) wkt_input) -4;  // actual letters
 
-                       catenate(buff,")",1);
-                       *slen =  buff->length;
-                       result = to_CString(buff);
-                       delete_StrBUFF(buff);
+               wkt = palloc( wkt_size+1); //+1 for null
+               memcpy(wkt, wkt_input+4, wkt_size );
+               wkt[wkt_size] = 0; // null term
 
-                       return result;
-}
 
+//elog(NOTICE,"in parse_WKT_lwgeom");
+//elog(NOTICE,"in parse_WKT_lwgeom with input: '%s'",wkt);
 
-char *renderGC(LWGEOM_INSPECTED *inspected, int *slen)
-{
-                       STRBUFF *buff ;
-                       int             t;
-                       char    *result;
-                       int     len;
-                                                       // guess size a 10 chars/ordinate and 20 points/object -- total guess
-                       buff = new_strBUFF(4 * inspected->ngeometries * 10 * 20);
+               //serialized_form = parse_WKT((const char *)wkt,(allocator) malloc, (report_error)elog_ERROR);
+               serialized_form = parse_lwg((const char *)wkt,(allocator) palloc_fn, (report_error)elog_ERROR);
+//elog(NOTICE,"parse_WKT_lwgeom:: finished parse");
+               pfree (wkt);
 
+               if (serialized_form == NULL)
+                       elog(ERROR,"parse_WKT:: couldnt parse!");
 
-                       catenate(buff,"GEOMETRYCOLLECTION(",19);
 
-                       for (t=0;t<inspected->ngeometries;t++)
-                       {
-                               char    *single_obj = lwgeom_getsubgeometry_inspected(inspected,t);
-                               LWGEOM_INSPECTED *inspected_sub = lwgeom_inspect(single_obj);
-                               char    *single_obj_str = renderMULTI(inspected_sub, &len);
-                               pfree_inspected(inspected_sub);
 
-                               catenate(buff, single_obj_str, len);
-                               pfree(single_obj_str);
+       //      printBYTES(serialized_form+4,*((int*) serialized_form)-4);
 
-                               if (t != (inspected->ngeometries-1) )
-                                       catenate(buff,",",1);
-                       }
+       //      result = palloc( (*((int*) serialized_form)));
+       //      memcpy(result, serialized_form, *((int*) serialized_form));
+       //      free(serialized_form);
 
-                       catenate(buff,")",1);
-                       *slen =  buff->length;
-                       result = to_CString(buff);
-                       delete_StrBUFF(buff);
+       //      PG_RETURN_POINTER(result);
+               //PG_RETURN_NULL();
 
-                       return result;
+               PG_RETURN_POINTER(serialized_form);
 }
 
-// makes things like:
-// 'MULTILINESTRING((1 2,3 4,5 6),(7 8,9 10))'
-// 'MULTIPOLYGON(((0 0.1,0.2 10.3,10.4 10.5,10.7 0.6,0 0.1)),((0 0.1,0.2 10.3,10.4 10.5,10.7 0.6,0 0.1)'
-// 'GEOMETRYCOLLECTION(LINESTRING(5 6 -55,7 8 -22),LINESTRING(1 2 -1,3 4 -2))'
-//
-// NOTE:  (!! LOOK !!)
-//  'MULTIPOINT(0 0,1 1,2 2)' --NOT--- 'MULTIPOINT((0 0),(1 1),(2 2))'
-//
-// handles MULTI* inside GCs
-//
-// final length of the string (excluding null-termination)
-// is returned in 'slen' for easy manipulation (strlen(result) == *slen)
-char *renderMULTI(LWGEOM_INSPECTED *inspected, int *slen)
-{
-       LWPOINT *pt;
-       LWLINE  *line;
-       LWPOLY *poly;
-       char    *result;
 
+char *parse_lwgeom_wkt(char *wkt_input)
+{
+                       char *serialized_form = parse_lwg((const char *)wkt_input,(allocator) palloc_fn, (report_error)elog_ERROR);
 
-       switch(lwgeom_getType(inspected->type))
-       {
-                               // shouldnt call this with a singular type, but we handle it here anyways
-               case POINTTYPE:
-                                       pt = lwgeom_getpoint_inspected(inspected,0);
-                                       result =  renderLWPOINT(pt, true,  slen);
-                                       pfree_point(pt);
-                                       return result;
-                                       break; //redundant
-               case LINETYPE:
-                                       line = lwgeom_getline_inspected(inspected,0);
-                                       result =   renderLWLINE(line, true,  slen);
-                                       pfree_line(line);
-                                       return result;
-                                       break; //redundant
-               case POLYGONTYPE:
-                                       poly = lwgeom_getpoly_inspected(inspected,0);
-                                       result =   renderLWPOLY(poly, true,  slen);
-                                       pfree_polygon(poly);
-                                       return result;
-                                       break; //redundant
-               case MULTIPOINTTYPE:
-                                       //this is the special case 'MULTIPOINT(0 0,1 1,2 2,3 3)'
-                                       return renderMULTIPOINT(inspected, slen);  // special case!!
-                                       break; //redundant
-               case MULTILINETYPE:
-                                       return renderMULTILINE(inspected, slen);
-                                       break; //redundant
-               case MULTIPOLYGONTYPE:
-                                       return renderMULTIPOLY(inspected, slen);
-                                       break; //redundant
-               case COLLECTIONTYPE:
-                               return renderGC(inspected, slen);  // special case -- could recurse
-                                       break; //redundant
-       }
-       elog(ERROR,"renderMULTI:: couldnt determine geometry type");
-       return NULL;
-}
 
+//elog(NOTICE,"parse_lwgeom_wkt with %s",wkt_input);
 
+                       if (serialized_form == NULL)
+                               elog(ERROR,"parse_WKT:: couldnt parse!");
 
+                       return serialized_form;
 
-PG_FUNCTION_INFO_V1(LWGEOM_asText);
-Datum LWGEOM_asText(PG_FUNCTION_ARGS)
-{
-               char                    *lwgeom = (char *)  PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-        char                *wkt;
-        char                *result;
-        int                 len,len_total;
-        int                            SRID;
-               LWGEOM_INSPECTED   *inspected = lwgeom_inspect(lwgeom+4);
-               char                            SRIDtext[100];
-               int                                     len_SRIDtext;
+               //      result = palloc( (*((int*) serialized_form)));
+               //      memcpy(result, serialized_form, *((int*) serialized_form));
+               //      free(serialized_form);
 
-        wkt = renderMULTI(inspected, &len);
-  elog(NOTICE," wkt = %s", wkt);
+               //      return result;
+}
 
-               SRID = inspected->SRID;
 
-               len_SRIDtext = sprintf(SRIDtext,"SRID=%i;",SRID);
- elog(NOTICE," srid = %i", SRID);
-        len_total = len + len_SRIDtext+ 1 ; // 1= null term
 
-        result = palloc(len_total);//extra space for SRID
 
-        memcpy(result, SRIDtext, len_SRIDtext);
-        memcpy(result+len_SRIDtext, wkt, len);
-               result[len_total-1] = 0;//ensure null-terminated
 
-        pfree(wkt);
 
-        PG_RETURN_CSTRING(result);
 
-}
diff --git a/lwgeom/lwgparse.c b/lwgeom/lwgparse.c
new file mode 100644 (file)
index 0000000..6bd27dc
--- /dev/null
@@ -0,0 +1,715 @@
+/*
+ * Written by Ralph Mason ralph.mason<at>telogis.com
+ *
+ * Copyright Telogis 2004
+ * www.telogis.com
+ *
+ */
+#include "wktparse.h"
+#include <string.h>
+
+//To get byte order
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <netinet/in_systm.h>
+#include <netinet/ip.h>
+
+typedef unsigned long int4;
+
+int srid=-1;
+
+static int ferror;
+static allocator local_malloc;
+static report_error error_func;
+
+void set_srid(double d_srid){
+       if ( d_srid >= 0 )
+               d_srid+=0.1;
+       else
+               d_srid-=0.1;
+
+
+       srid=(int)(d_srid+0.1);
+}
+
+typedef struct tag_tuple tuple;
+
+struct tag_outputstate{
+       byte*   pos;
+};
+
+typedef struct tag_outputstate output_state;
+
+typedef void (*output_func)(tuple* this,output_state* out);
+
+struct tag_tuple{
+       output_func   of;
+       union {
+               double points[4];
+               int4   pointsi[4];
+
+               struct{
+                       tuple*  stack_next;
+                       int     type;
+                       int     num;
+                       int     size_here;
+               };
+       };
+       tuple* next;
+};
+
+struct {
+       int     type;
+       int     flags;
+       int     srid;
+       int     ndims;
+       /* create integer version */
+       int lwgi;
+       /* input is integer (wkb only)*/
+       int from_lwgi;
+
+       int4 alloc_size;
+
+       /*
+               linked list of all tuples
+       */
+       tuple*  first;
+       tuple*  last;
+
+       /*
+               stack of open geometries
+       */
+       tuple*  stack;
+
+} the_geom;
+
+tuple* free_list=0;
+
+tuple* alloc_tuple(output_func of,size_t size){
+       tuple* ret = free_list;
+
+       if ( ! ret ){
+               int toalloc = (ALLOC_CHUNKS /sizeof(tuple));
+               ret = malloc( toalloc *sizeof(tuple) );
+
+               free_list = ret;
+
+               while(--toalloc){
+                       ret->next = ret+1;
+                       ret++;
+               }
+
+               ret->next = NULL;
+
+               return alloc_tuple(of,size);
+       }
+
+       free_list = ret->next;
+       ret->of = of;
+       ret->next = NULL;
+
+       if ( the_geom.last ) {
+               the_geom.last->next = ret;
+               the_geom.last = ret;
+       }
+       else {
+               the_geom.first = the_geom.last = ret;
+       }
+
+       the_geom.alloc_size += size;
+
+       return ret;
+}
+
+void error(const char* err){
+       error_func(err);
+       ferror=1;
+}
+
+void free_tuple(tuple* to_free){
+
+       tuple* list_end = to_free;
+
+       if( !to_free)
+               return;
+
+       while(list_end->next){
+               list_end=list_end->next;
+       }
+
+       list_end->next = free_list;
+       free_list = to_free;
+}
+
+void inc_num(void){
+       the_geom.stack->num++;
+}
+
+/*
+       Allocate a 'counting' tuple
+*/
+void alloc_stack_tuple(int type,output_func of,size_t size){
+       tuple*  p;
+       inc_num();
+
+       p = alloc_tuple(of,size);
+       p->stack_next = the_geom.stack;
+       p->type = type;
+       p->size_here = the_geom.alloc_size;
+       p->num = 0;
+       the_geom.stack = p;
+}
+
+void pop(void){
+       the_geom.stack = the_geom.stack->stack_next;
+}
+
+int minpoints;
+
+void popc(void){
+       if ( the_geom.stack->num < minpoints){
+               error("geometry requires more points");
+       }
+       the_geom.stack = the_geom.stack->stack_next;
+}
+
+
+void check_dims(int num){
+
+       if( the_geom.ndims != num){
+               if (the_geom.ndims) {
+                       error("Can not mix dimentionality in a geometry");
+               }
+               else{
+                       the_geom.ndims = num;
+               }
+       }
+}
+
+#define WRITE_INT4_REAL(x,y) { memcpy(x->pos,&y,4); x->pos+=4;}
+#define WRITE_INT4_REAL_MULTIPLE(x,y,z) { memcpy(x->pos,&y,z*4); x->pos+=(z*4);}
+
+/*
+       we can shrink ints to one byte if they are less than 127
+       by setting the extra bit.  Because if the different byte
+       ordering possibilities we need to set the lsb on little
+       endian to show a packed one and the msb on a big endian
+       machine
+*/
+#ifdef SHRINK_INTS
+void WRITE_INT4(output_state * out,int4 val){
+       if ( val <= 0x7f ){
+               if ( BYTE_ORDER == LITTLE_ENDIAN ){
+                       val = (val<<1) | 1;
+               }
+               else{
+                       val |=0x80;
+               }
+
+               *out->pos++ = (byte)val;
+               the_geom.alloc_size-=3;
+       }
+       else{
+               if ( BYTE_ORDER == LITTLE_ENDIAN ){
+                       val <<=1;
+               }
+               WRITE_INT4_REAL(out,val);
+       }
+}
+#else
+#define WRITE_INT4 WRITE_INT4_REAL
+#endif
+
+
+void WRITE_DOUBLES(output_state* out,double* points, int cnt){
+       if ( the_geom.lwgi){
+               int4 vals[4];
+               int i;
+
+               for(i=0; i<cnt;i++){
+                       vals[i] = (int4)(((points[i]+180.0)*0xB60B60)+.5);
+               }
+               memcpy(out->pos,vals,sizeof(int4)*cnt);
+               out->pos+=sizeof(int4)*cnt;
+       }
+       else{
+               memcpy(out->pos,points,sizeof(double)*cnt);
+               out->pos+=sizeof(double)*cnt;
+       }
+
+}
+
+void write_size(tuple* this,output_state* out){
+       WRITE_INT4_REAL(out,the_geom.alloc_size);
+}
+
+void alloc_lwgeom(int srid){
+       the_geom.srid=srid;
+       the_geom.alloc_size=0;
+       the_geom.stack=NULL;
+       the_geom.ndims=0;
+
+       //Free if used already
+       if ( the_geom.first ){
+               free_tuple(the_geom.first);
+               the_geom.first=the_geom.last=NULL;
+       }
+
+       if ( srid != -1 ){
+               the_geom.alloc_size+=4;
+       }
+
+       the_geom.stack = alloc_tuple(write_size,4);
+}
+
+
+void write_point_2(tuple* this,output_state* out){
+       WRITE_DOUBLES(out,this->points,2);
+}
+
+void write_point_3(tuple* this,output_state* out){
+       WRITE_DOUBLES(out,this->points,3);
+}
+void write_point_4(tuple* this,output_state* out){
+       WRITE_DOUBLES(out,this->points,4);
+}
+
+void write_point_2i(tuple* this,output_state* out){
+       WRITE_INT4_REAL_MULTIPLE(out,this->points,2);
+}
+
+void write_point_3i(tuple* this,output_state* out){
+       WRITE_INT4_REAL_MULTIPLE(out,this->points,3);
+}
+void write_point_4i(tuple* this,output_state* out){
+       WRITE_INT4_REAL_MULTIPLE(out,this->points,4);
+}
+
+void alloc_point_2d(double x,double y){
+       tuple* p = alloc_tuple(write_point_2,the_geom.lwgi?8:16);
+       p->points[0] = x;
+       p->points[1] = y;
+       inc_num();
+       check_dims(2);
+}
+
+void alloc_point_3d(double x,double y,double z){
+       tuple* p = alloc_tuple(write_point_3,the_geom.lwgi?12:24);
+       p->points[0] = x;
+       p->points[1] = y;
+       p->points[2] = z;
+       inc_num();
+       check_dims(3);
+}
+
+void alloc_point_4d(double x,double y,double z,double m){
+       tuple* p = alloc_tuple(write_point_4,the_geom.lwgi?16:32);
+       p->points[0] = x;
+       p->points[1] = y;
+       p->points[2] = z;
+       p->points[3] = m;
+       inc_num();
+       check_dims(4);
+}
+
+void write_type(tuple* this,output_state* out){
+       byte type=0;
+
+       //Empty handler - switch back
+       if ( this->type == 0xff )
+               this->type = COLLECTIONTYPE;
+
+       type |= this->type;
+
+       if (the_geom.ndims) //Support empty
+               type |= ((the_geom.ndims-2) << 4);
+
+       if ( the_geom.srid != -1 ){
+               type |= 0x40;
+       }
+
+       *(out->pos)=type;
+       out->pos++;
+
+       if ( the_geom.srid != -1 ){
+               //Only the first geometry will have a srid attached
+               WRITE_INT4(out,the_geom.srid);
+               the_geom.srid = -1;
+       }
+}
+
+void write_count(tuple* this,output_state* out){
+       int num = this->num;
+       WRITE_INT4(out,num);
+}
+
+void write_type_count(tuple* this,output_state* out){
+       write_type(this,out);
+       write_count(this,out);
+}
+
+void alloc_point(void){
+       if( the_geom.lwgi)
+               alloc_stack_tuple(POINTTYPEI,write_type,1);
+       else
+               alloc_stack_tuple(POINTTYPE,write_type,1);
+
+       minpoints=1;
+}
+
+void alloc_linestring(void){
+       if( the_geom.lwgi)
+               alloc_stack_tuple(LINETYPEI,write_type,1);
+       else
+               alloc_stack_tuple(LINETYPE,write_type,1);
+
+       minpoints=2;
+}
+
+void alloc_polygon(void){
+       if( the_geom.lwgi)
+               alloc_stack_tuple(POLYGONTYPEI, write_type,1);
+       else
+               alloc_stack_tuple(POLYGONTYPE, write_type,1);
+
+       minpoints=3;
+}
+
+void alloc_multipoint(void){
+       alloc_stack_tuple(MULTIPOINTTYPE,write_type,1);
+}
+
+void alloc_multilinestring(void){
+       alloc_stack_tuple(MULTILINETYPE,write_type,1);
+}
+
+void alloc_multipolygon(void){
+       alloc_stack_tuple(MULTIPOLYGONTYPE,write_type,1);
+}
+
+void alloc_geomertycollection(void){
+       alloc_stack_tuple(COLLECTIONTYPE,write_type,1);
+}
+
+void alloc_counter(void){
+       alloc_stack_tuple(0,write_count,4);
+}
+
+void alloc_empty(){
+       tuple* st = the_geom.stack;
+       //Find the last geometry
+       while(st->type == 0){
+               st =st->stack_next;
+       }
+
+       //Reclaim memory
+       free_tuple(st->next);
+
+       //Put an empty geometry collection on the top of the stack
+       st->next=NULL;
+       the_geom.stack=st;
+       the_geom.alloc_size=st->size_here;
+
+       //Mark as a empty stop
+       if (st->type != 0xFF){
+               st->type=0xFF;
+               st->of = write_type_count;
+               the_geom.alloc_size+=4;
+               st->size_here=the_geom.alloc_size;
+       }
+
+       st->num=0;
+
+}
+
+byte* make_lwgeom(){
+       byte* out_c;
+       output_state out;
+       tuple* cur;
+       out_c = (byte*)local_malloc(the_geom.alloc_size);
+       out.pos = out_c;
+       cur = the_geom.first ;
+
+       while(cur){
+               cur->of(cur,&out);
+               cur=cur->next;
+       }
+
+       //if ints shrink then we need to rewrite the size smaller
+       out.pos = out_c;
+       write_size(NULL,&out);
+
+       return out_c;
+}
+
+int lwg_parse_yyerror(char* s){
+       error("parse error - invalid geometry");
+       return 1;
+}
+
+/*
+ Table below generated using this ruby.
+
+ a=(0..0xff).to_a.collect{|x|0xff};('0'..'9').each{|x|a[x[0]]=x[0]-'0'[0]}
+ ('a'..'f').each{|x|v=x[0]-'a'[0]+10;a[x[0]]=a[x.upcase[0]]=v}
+ puts '{'+a.join(",")+'}'
+
+ */
+static const byte to_hex[]  = {
+       255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+       255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+       255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+       0,1,2,3,4,5,6,7,8,9,255,255,255,255,255,255,255,10,11,12,13,14,
+       15,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+       255,255,255,255,255,255,255,255,255,255,255,10,11,12,13,14,15,255,
+       255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+       255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+       255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+       255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+       255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+       255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+       255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+       255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+       255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+       255,255,255,255,255,255,255,255};
+
+byte strhex_readbyte(const char* in){
+       if ( *in == 0 ){
+               if ( ! ferror){
+                       error("invalid wkb");
+               }
+               return 0;
+       }
+       return to_hex[(int)*in]<<4 | to_hex[(int)*(in+1)];
+}
+
+byte read_wkb_byte(const char** in){
+       byte ret = strhex_readbyte(*in);
+       (*in)+=2;
+       return ret;
+}
+
+int swap_order;
+
+int4 read_wkb_int(const char** in){
+       int4 ret;
+       byte* pb = (byte*) &ret;
+       int i;
+
+       if( ! swap_order ){
+               for(i=0;i<4;i++){
+                       pb[i]=read_wkb_byte(in);
+               }
+       }
+       else{
+               for(i=3;i<=0;i--){
+                       pb[i]=read_wkb_byte(in);
+               }
+
+       }
+
+       return ret;
+}
+
+double read_wbk_double(const char** in,int convert_from_int){
+       double ret;
+       byte* pb = (byte*) &ret;
+       int i;
+
+       if ( ! convert_from_int){
+               if( !swap_order ){
+                       for(i=0;i<8;i++){
+                               pb[i]=read_wkb_byte(in);
+                       }
+               }
+               else{
+                       for(i=7;i<=0;i--){
+                               pb[i]=read_wkb_byte(in);
+                       }
+               }
+               return ret;
+       }else{
+               ret  = read_wkb_int(in);
+               ret /= 0xb60b60;
+               return ret-180.0;
+       }
+}
+
+void read_wkb_point(const char** b){
+       int i;
+       tuple* p;
+
+       if(the_geom.lwgi && the_geom.from_lwgi ){
+               //Special case - reading from lwgi to lwgi
+               //we don't want to go via doubles in the middle.
+               switch(the_geom.ndims){
+                       case 2: p=alloc_tuple(write_point_2i,8); break;
+                       case 3: p=alloc_tuple(write_point_3i,12); break;
+                       case 4: p=alloc_tuple(write_point_4i,16); break;
+               }
+
+               for(i=0;i<the_geom.ndims;i++){
+                       p->pointsi[i]=read_wkb_int(b);
+               }
+       }
+       else{
+               int mul = the_geom.lwgi ? 1 : 2;
+
+               switch(the_geom.ndims){
+                       case 2: p=alloc_tuple(write_point_2,8*mul); break;
+                       case 3: p=alloc_tuple(write_point_3,12*mul); break;
+                       case 4: p=alloc_tuple(write_point_4,16*mul); break;
+               }
+
+               for(i=0;i<the_geom.ndims;i++){
+                       p->points[i]=read_wbk_double(b,the_geom.from_lwgi);
+               }
+       }
+
+       inc_num();
+       check_dims(the_geom.ndims);
+}
+
+typedef void (*read_col_func)(const char**f);
+
+void read_collection(const char** b,read_col_func f){
+       int4 cnt=read_wkb_int(b);
+       alloc_counter();
+
+       while(cnt--){
+               if ( ferror )   return;
+               f(b);
+       }
+
+       pop();
+}
+
+void read_collection2(const char** b){
+       return read_collection(b,read_wkb_point);
+}
+
+void parse_wkb(const char** b){
+       int4 type;
+       byte xdr = read_wkb_byte(b);
+       swap_order=0;
+
+       if ( xdr == 0x01 ){
+               if ( BYTE_ORDER != LITTLE_ENDIAN )
+                       swap_order=1;
+       }
+       else if ( xdr == 0x00 ){
+               if ( BYTE_ORDER == LITTLE_ENDIAN )
+                       swap_order=1;
+       }
+
+       type = read_wkb_int(b);
+
+       //quick exit on error
+       if ( ferror ) return;
+
+       if (type & 0x80000000)
+               the_geom.ndims=3;
+       else if (type & 0x40000000)
+               the_geom.ndims=4;
+       else
+               the_geom.ndims=2;
+
+       type &=0x0f;
+
+       if ( the_geom.lwgi  ){
+
+               if ( type<= POLYGONTYPE )
+                       alloc_stack_tuple(type +9,write_type,1);
+               else
+                       alloc_stack_tuple(type,write_type,1);
+       }
+       else{
+               //If we are writing lwg and are reading wbki
+               int4 towrite=type;
+               if (towrite > COLLECTIONTYPE ){
+                       towrite-=9;
+               }
+               alloc_stack_tuple(towrite,write_type,1);
+       }
+
+       switch(type ){
+               case    POINTTYPE:
+                       read_wkb_point(b);
+                       break;
+
+               case    LINETYPE:
+                       read_collection(b,read_wkb_point);
+                       break;
+
+               case    POLYGONTYPE:
+                       read_collection(b,read_collection2);
+                       break;
+
+               case    MULTIPOINTTYPE:
+               case    MULTILINETYPE:
+               case    MULTIPOLYGONTYPE:
+               case    COLLECTIONTYPE:
+                       read_collection(b,parse_wkb);
+                       break;
+
+               case    POINTTYPEI:
+                       the_geom.from_lwgi=1;
+                       read_wkb_point(b);
+                       break;
+
+               case    LINETYPEI:
+                       the_geom.from_lwgi=1;
+                       read_collection(b,read_wkb_point);
+                       break;
+
+               case    POLYGONTYPEI:
+                       the_geom.from_lwgi=1;
+                       read_collection(b,read_collection2);
+                       break;
+
+               default:
+                       error("Invalid type in wbk");
+       }
+
+       the_geom.from_lwgi=0;
+
+       pop();
+}
+
+
+void alloc_wkb(const char* parser){
+       parse_wkb(&parser);
+}
+
+/*
+       Parse a string and return a LW_GEOM
+*/
+byte* parse_it(const char* geometry,allocator allocfunc,report_error errfunc){
+
+       local_malloc = allocfunc;
+       error_func=errfunc;
+
+       ferror = 0;
+
+       init_parser(geometry);
+       lwg_parse_yyparse();
+       close_parser();
+
+       if (ferror)
+               return NULL;
+
+       return make_lwgeom();
+}
+
+byte* parse_lwg(const char* geometry,allocator allocfunc,report_error errfunc){
+       the_geom.lwgi=0;
+       return parse_it(geometry,allocfunc,errfunc);
+}
+
+byte* parse_lwgi(const char* geometry,allocator allocfunc,report_error errfunc){
+       the_geom.lwgi=1;
+       return parse_it(geometry,allocfunc,errfunc);
+}
+
+
+
diff --git a/lwgeom/wktparse.h b/lwgeom/wktparse.h
new file mode 100644 (file)
index 0000000..e2535c1
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Written by Ralph Mason ralph.mason<at>telogis.com
+ *
+ * Copyright Telogis 2004
+ * www.telogis.com
+ *
+ */
+
+#include <stdlib.h>
+
+typedef unsigned char byte;
+typedef void* (*allocator)(size_t size);
+typedef void  (*freeor)(void* mem);
+typedef void  (*report_error)(const char* string);
+
+//typedef unsigned long int4;
+
+
+/* How much memory is allocated at a time(bytes) for tuples */
+#define ALLOC_CHUNKS 8192
+
+/* to shrink ints less than 0x7f to 1 byte */
+//#define SHRINK_INTS
+
+#define        POINTTYPE       1
+#define        LINETYPE        2
+#define        POLYGONTYPE     3
+#define        MULTIPOINTTYPE  4
+#define        MULTILINETYPE   5
+#define        MULTIPOLYGONTYPE        6
+#define        COLLECTIONTYPE  7
+
+/* Extended lwgeom integer types */
+#define POINTTYPEI    10
+#define LINETYPEI     11
+#define POLYGONTYPEI  12
+
+extern int srid;
+
+void set_srid(double srid);
+void alloc_lwgeom(int srid);
+
+void alloc_point_2d(double x,double y);
+void alloc_point_3d(double x,double y,double z);
+void alloc_point_4d(double x,double y,double z,double m);
+
+void alloc_point(void);
+void alloc_linestring(void);
+void alloc_polygon(void);
+void alloc_multipoint(void);
+void alloc_multilinestring(void);
+void alloc_multipolygon(void);
+void alloc_geomertycollection(void);
+void alloc_empty();
+
+void alloc_counter(void);
+
+
+void pop(void);
+void popc(void);
+
+void alloc_wkb(const char* parser);
+/*
+       Use these functions to parse and unparse lwgeoms
+       You are responsible for freeing the returned memory.
+*/
+
+byte* parse_lwg(const char* wkt,allocator allocfunc,report_error errfunc);
+byte* parse_lwgi(const char* wkt,allocator allocfunc,report_error errfunc);
+char* unparse_WKT(byte* lw_geom,allocator alloc,freeor free);
+char* unparse_WKB(byte* lw_geom,allocator alloc,freeor free);
+
+
diff --git a/lwgeom/wktparse.tab.c b/lwgeom/wktparse.tab.c
new file mode 100644 (file)
index 0000000..4e7f1e0
--- /dev/null
@@ -0,0 +1,1376 @@
+/* A Bison parser, made from wktparse.y
+   by GNU bison 1.32.  */
+
+#define YYBISON 1  /* Identify Bison output.  */
+
+#define yyparse lwg_parse_yyparse
+#define yylex lwg_parse_yylex
+#define yyerror lwg_parse_yyerror
+#define yylval lwg_parse_yylval
+#define yychar lwg_parse_yychar
+#define yydebug lwg_parse_yydebug
+#define yynerrs lwg_parse_yynerrs
+# define       POINT   257
+# define       LINESTRING      258
+# define       POLYGON 259
+# define       MULTIPOINT      260
+# define       MULTILINESTRING 261
+# define       MULTIPOLYGON    262
+# define       GEOMETRYCOLLECTION      263
+# define       SRID    264
+# define       EMPTY   265
+# define       VALUE   266
+# define       LPAREN  267
+# define       RPAREN  268
+# define       COMMA   269
+# define       EQUALS  270
+# define       SEMICOLON       271
+# define       WKB     272
+
+#line 9 "wktparse.y"
+
+#include "wktparse.h"
+
+
+#line 16 "wktparse.y"
+#ifndef YYSTYPE
+typedef union {
+       double value;
+       const char* wkb;
+} yystype;
+# define YYSTYPE yystype
+#endif
+#ifndef YYDEBUG
+# define YYDEBUG 0
+#endif
+
+
+
+#define        YYFINAL         108
+#define        YYFLAG          -32768
+#define        YYNTBASE        19
+
+/* YYTRANSLATE(YYLEX) -- Bison token number corresponding to YYLEX. */
+#define YYTRANSLATE(x) ((unsigned)(x) <= 272 ? yytranslate[x] : 68)
+
+/* YYTRANSLATE[YYLEX] -- Bison token number corresponding to YYLEX. */
+static const char yytranslate[] =
+{
+       0,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     1,     3,     4,     5,
+       6,     7,     8,     9,    10,    11,    12,    13,    14,    15,
+      16,    17,    18
+};
+
+#if YYDEBUG
+static const short yyprhs[] =
+{
+       0,     0,     1,     6,     7,    10,    12,    14,    16,    18,
+      20,    22,    24,    26,    30,    32,    35,    36,    39,    41,
+      45,    46,    50,    52,    53,    58,    60,    64,    66,    67,
+      70,    73,    74,    77,    79,    80,    85,    87,    91,    92,
+      96,    98,    99,   104,   106,   110,   113,   114,   117,   119,
+     120,   125,   127,   131,   132,   136,   138,   139,   144,   146,
+     150,   151,   155,   157,   158,   163,   165,   169,   171,   173,
+     175,   178,   182,   187
+};
+static const short yyrhs[] =
+{
+      -1,    23,    17,    20,    22,     0,     0,    21,    22,     0,
+      24,     0,    25,     0,    36,     0,    47,     0,    29,     0,
+      42,     0,    53,     0,    58,     0,    10,    16,    12,     0,
+      18,     0,     3,    26,     0,     0,    27,    28,     0,    67,
+       0,    13,    63,    14,     0,     0,     6,    30,    31,     0,
+      67,     0,     0,    32,    13,    33,    14,     0,    34,     0,
+      33,    15,    34,     0,    26,     0,     0,    35,    63,     0,
+       4,    37,     0,     0,    38,    39,     0,    67,     0,     0,
+      40,    13,    41,    14,     0,    63,     0,    41,    15,    63,
+       0,     0,     7,    43,    44,     0,    67,     0,     0,    45,
+      13,    46,    14,     0,    37,     0,    46,    15,    37,     0,
+       5,    48,     0,     0,    49,    50,     0,    67,     0,     0,
+      51,    13,    52,    14,     0,    39,     0,    52,    15,    39,
+       0,     0,     8,    54,    55,     0,    67,     0,     0,    56,
+      13,    57,    14,     0,    48,     0,    57,    15,    48,     0,
+       0,     9,    59,    60,     0,    67,     0,     0,    61,    13,
+      62,    14,     0,    22,     0,    62,    15,    22,     0,    64,
+       0,    65,     0,    66,     0,    12,    12,     0,    12,    12,
+      12,     0,    12,    12,    12,    12,     0,    11,     0
+};
+
+#endif
+
+#if YYDEBUG
+/* YYRLINE[YYN] -- source line where rule number YYN was defined. */
+static const short yyrline[] =
+{
+       0,    30,    30,    31,    31,    33,    33,    33,    33,    33,
+      34,    34,    34,    36,    38,    43,    45,    45,    47,    47,
+      51,    51,    53,    53,    53,    55,    55,    57,    57,    57,
+      62,    64,    64,    66,    66,    66,    68,    68,    72,    72,
+      74,    74,    74,    76,    76,    81,    83,    83,    85,    85,
+      85,    87,    87,    91,    91,    93,    93,    93,    95,    95,
+     101,   101,   103,   103,   103,   105,   105,   108,   108,   108,
+     110,   112,   114,   116
+};
+#endif
+
+
+#if (YYDEBUG) || defined YYERROR_VERBOSE
+
+/* YYTNAME[TOKEN_NUM] -- String name of the token TOKEN_NUM. */
+static const char *const yytname[] =
+{
+  "$", "error", "$undefined.", "POINT", "LINESTRING", "POLYGON", 
+  "MULTIPOINT", "MULTILINESTRING", "MULTIPOLYGON", "GEOMETRYCOLLECTION", 
+  "SRID", "EMPTY", "VALUE", "LPAREN", "RPAREN", "COMMA", "EQUALS", 
+  "SEMICOLON", "WKB", "geometry", "@1", "@2", "geometry_int", "srid", 
+  "geom_wkb", "geom_point", "point", "@3", "point_int", "geom_multipoint", 
+  "@4", "multipoint", "@5", "multipoint_int", "mpoint", "@6", 
+  "geom_linestring", "linestring", "@7", "linestring_1", "@8", 
+  "linestring_int", "geom_multilinestring", "@9", "multilinestring", 
+  "@10", "multilinestring_int", "geom_polygon", "polygon", "@11", 
+  "polygon_1", "@12", "polygon_int", "geom_multipolygon", "@13", 
+  "multipolygon", "@14", "multipolygon_int", "geom_geometrycollection", 
+  "@15", "geometrycollection", "@16", "geometrycollection_int", "a_point", 
+  "point_2d", "point_3d", "point_4d", "empty", NULL
+};
+#endif
+
+/* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
+static const short yyr1[] =
+{
+       0,    20,    19,    21,    19,    22,    22,    22,    22,    22,
+      22,    22,    22,    23,    24,    25,    27,    26,    28,    28,
+      30,    29,    31,    32,    31,    33,    33,    34,    35,    34,
+      36,    38,    37,    39,    40,    39,    41,    41,    43,    42,
+      44,    45,    44,    46,    46,    47,    49,    48,    50,    51,
+      50,    52,    52,    54,    53,    55,    56,    55,    57,    57,
+      59,    58,    60,    61,    60,    62,    62,    63,    63,    63,
+      64,    65,    66,    67
+};
+
+/* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
+static const short yyr2[] =
+{
+       0,     0,     4,     0,     2,     1,     1,     1,     1,     1,
+       1,     1,     1,     3,     1,     2,     0,     2,     1,     3,
+       0,     3,     1,     0,     4,     1,     3,     1,     0,     2,
+       2,     0,     2,     1,     0,     4,     1,     3,     0,     3,
+       1,     0,     4,     1,     3,     2,     0,     2,     1,     0,
+       4,     1,     3,     0,     3,     1,     0,     4,     1,     3,
+       0,     3,     1,     0,     4,     1,     3,     1,     1,     1,
+       2,     3,     4,     1
+};
+
+/* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
+   doesn't specify something else to do.  Zero means the default is an
+   error. */
+static const short yydefact[] =
+{
+       3,     0,     0,     0,     0,    16,    31,    46,    20,    38,
+      53,    60,    14,     4,     5,     6,     9,     7,    10,     8,
+      11,    12,     1,    13,    15,     0,    30,    34,    45,    49,
+      23,    41,    56,    63,     0,    73,     0,    17,    18,    32,
+       0,    33,    47,     0,    48,    21,     0,    22,    39,     0,
+      40,    54,     0,    55,    61,     0,    62,     2,     0,     0,
+      67,    68,    69,     0,    34,    16,    31,    46,     0,    70,
+      19,     0,    36,    51,     0,    27,     0,    25,     0,    43,
+       0,    58,     0,    65,     0,    71,    35,     0,    50,    34,
+      24,    16,    29,    42,    31,    57,    46,    64,     0,    72,
+      37,    52,    26,    44,    59,    66,     0,     0,     0
+};
+
+static const short yydefgoto[] =
+{
+     106,    34,     2,    13,     3,    14,    15,    75,    25,    37,
+      16,    30,    45,    46,    76,    77,    78,    17,    26,    27,
+      39,    40,    71,    18,    31,    48,    49,    80,    19,    28,
+      29,    42,    43,    74,    20,    32,    51,    52,    82,    21,
+      33,    54,    55,    84,    59,    60,    61,    62,    41
+};
+
+static const short yypact[] =
+{
+       3,    -1,     2,     9,     4,-32768,-32768,-32768,-32768,-32768,
+  -32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,
+  -32768,-32768,-32768,-32768,-32768,     1,-32768,     7,-32768,     7,
+       7,     7,     7,     7,     2,-32768,    17,-32768,-32768,-32768,
+      32,-32768,-32768,    33,-32768,-32768,    34,-32768,-32768,    35,
+  -32768,-32768,    36,-32768,-32768,    37,-32768,-32768,    39,    38,
+  -32768,-32768,-32768,    17,     7,    41,-32768,-32768,     2,    42,
+  -32768,    18,-32768,-32768,    21,-32768,    23,-32768,    17,-32768,
+      25,-32768,    27,-32768,    29,    43,-32768,    17,-32768,     7,
+  -32768,    41,-32768,-32768,-32768,-32768,-32768,-32768,     2,-32768,
+  -32768,-32768,-32768,-32768,-32768,-32768,    56,    57,-32768
+};
+
+static const short yypgoto[] =
+{
+  -32768,-32768,-32768,   -34,-32768,-32768,-32768,    53,-32768,-32768,
+  -32768,-32768,-32768,-32768,-32768,   -32,-32768,-32768,   -63,-32768,
+     -62,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,   -66,
+  -32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,-32768,
+  -32768,-32768,-32768,-32768,   -59,-32768,-32768,-32768,    -8
+};
+
+
+#define        YYLAST          64
+
+
+static const short yytable[] =
+{
+      57,    81,    73,    79,    72,     5,     6,     7,     8,     9,
+      10,    11,    35,     1,    36,     4,    23,    38,    35,    92,
+      12,    44,    47,    50,    53,    56,    22,   101,   100,    58,
+     104,   103,    86,    87,    83,    88,    89,    90,    91,    93,
+      94,    95,    96,    97,    98,    63,    64,    65,    66,    67,
+      68,    69,    70,   -28,    85,    99,   107,   108,    24,   102,
+       0,     0,     0,     0,   105
+};
+
+static const short yycheck[] =
+{
+      34,    67,    64,    66,    63,     3,     4,     5,     6,     7,
+       8,     9,    11,    10,    13,    16,    12,    25,    11,    78,
+      18,    29,    30,    31,    32,    33,    17,    89,    87,    12,
+      96,    94,    14,    15,    68,    14,    15,    14,    15,    14,
+      15,    14,    15,    14,    15,    13,    13,    13,    13,    13,
+      13,    12,    14,    12,    12,    12,     0,     0,     5,    91,
+      -1,    -1,    -1,    -1,    98
+};
+/* -*-C-*-  Note some compilers choke on comments on `#line' lines.  */
+#line 3 "/usr/share/bison/bison.simple"
+
+/* Skeleton output parser for bison,
+   Copyright (C) 1984, 1989, 1990, 2000, 2001 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
+
+/* As a special exception, when this file is copied by Bison into a
+   Bison output file, you may use that output file without restriction.
+   This special exception was added by the Free Software Foundation
+   in version 1.24 of Bison.  */
+
+/* This is the parser code that is written into each bison parser when
+   the %semantic_parser declaration is not specified in the grammar.
+   It was written by Richard Stallman by simplifying the hairy parser
+   used when %semantic_parser is specified.  */
+
+/* All symbols defined below should begin with yy or YY, to avoid
+   infringing on user name space.  This should be done even for local
+   variables, as they might otherwise be expanded by user macros.
+   There are some unavoidable exceptions within include files to
+   define necessary library symbols; they are noted "INFRINGES ON
+   USER NAME SPACE" below.  */
+
+#ifdef __cplusplus
+# define YYSTD(x) std::x
+#else
+# define YYSTD(x) x
+#endif
+
+#if ! defined (yyoverflow) || defined (YYERROR_VERBOSE)
+
+/* The parser invokes alloca or malloc; define the necessary symbols.  */
+
+# if YYSTACK_USE_ALLOCA
+#  define YYSTACK_ALLOC alloca
+#  define YYSIZE_T YYSTD (size_t)
+# else
+#  ifndef YYSTACK_USE_ALLOCA
+#   if defined (alloca) || defined (_ALLOCA_H)
+#    define YYSTACK_ALLOC alloca
+#    define YYSIZE_T YYSTD (size_t)
+#   else
+#    ifdef __GNUC__
+#     define YYSTACK_ALLOC __builtin_alloca
+#    endif
+#   endif
+#  endif
+# endif
+
+# ifdef YYSTACK_ALLOC
+   /* Pacify GCC's `empty if-body' warning. */
+#  define YYSTACK_FREE(Ptr) do { /* empty */; } while (0)
+# else
+#  ifdef __cplusplus
+#   include <cstdlib> /* INFRINGES ON USER NAME SPACE */
+#   define YYSIZE_T std::size_t
+#  else
+#   ifdef __STDC__
+#    include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
+#    define YYSIZE_T size_t
+#   endif
+#  endif
+#  define YYSTACK_ALLOC YYSTD (malloc)
+#  define YYSTACK_FREE YYSTD (free)
+# endif
+
+/* A type that is properly aligned for any stack member.  */
+union yyalloc
+{
+  short yyss;
+  YYSTYPE yyvs;
+# if YYLSP_NEEDED
+  YYLTYPE yyls;
+# endif
+};
+
+/* The size of the maximum gap between one aligned stack and the next.  */
+# define YYSTACK_GAP_MAX (sizeof (union yyalloc) - 1)
+
+/* The size of an array large to enough to hold all stacks, each with
+   N elements.  */
+# if YYLSP_NEEDED
+#  define YYSTACK_BYTES(N) \
+     ((N) * (sizeof (short) + sizeof (YYSTYPE) + sizeof (YYLTYPE))     \
+      + 2 * YYSTACK_GAP_MAX)
+# else
+#  define YYSTACK_BYTES(N) \
+     ((N) * (sizeof (short) + sizeof (YYSTYPE))                                \
+      + YYSTACK_GAP_MAX)
+# endif
+
+/* Relocate the TYPE STACK from its old location to the new one.  The
+   local variables YYSIZE and YYSTACKSIZE give the old and new number of
+   elements in the stack, and YYPTR gives the new location of the
+   stack.  Advance YYPTR to a properly aligned location for the next
+   stack.  */
+# define YYSTACK_RELOCATE(Type, Stack)                                 \
+    do                                                                 \
+      {                                                                        \
+       YYSIZE_T yynewbytes;                                            \
+       yymemcpy ((char *) yyptr, (char *) (Stack),                     \
+                 yysize * (YYSIZE_T) sizeof (Type));                   \
+       Stack = &yyptr->Stack;                                          \
+       yynewbytes = yystacksize * sizeof (Type) + YYSTACK_GAP_MAX;     \
+       yyptr += yynewbytes / sizeof (*yyptr);                          \
+      }                                                                        \
+    while (0)
+
+#endif /* ! defined (yyoverflow) || defined (YYERROR_VERBOSE) */
+
+
+#if ! defined (YYSIZE_T) && defined (__SIZE_TYPE__)
+# define YYSIZE_T __SIZE_TYPE__
+#endif
+#if ! defined (YYSIZE_T) && defined (size_t)
+# define YYSIZE_T size_t
+#endif
+#if ! defined (YYSIZE_T)
+# ifdef __cplusplus
+#  include <cstddef> /* INFRINGES ON USER NAME SPACE */
+#  define YYSIZE_T std::size_t
+# else
+#  ifdef __STDC__
+#   include <stddef.h> /* INFRINGES ON USER NAME SPACE */
+#   define YYSIZE_T size_t
+#  endif
+# endif
+#endif
+#if ! defined (YYSIZE_T)
+# define YYSIZE_T unsigned int
+#endif
+
+#define yyerrok                (yyerrstatus = 0)
+#define yyclearin      (yychar = YYEMPTY)
+#define YYEMPTY                -2
+#define YYEOF          0
+#define YYACCEPT       goto yyacceptlab
+#define YYABORT        goto yyabortlab
+#define YYERROR                goto yyerrlab1
+/* Like YYERROR except do call yyerror.  This remains here temporarily
+   to ease the transition to the new meaning of YYERROR, for GCC.
+   Once GCC version 2 has supplanted version 1, this can go.  */
+#define YYFAIL         goto yyerrlab
+#define YYRECOVERING()  (!!yyerrstatus)
+#define YYBACKUP(Token, Value)                                 \
+do                                                             \
+  if (yychar == YYEMPTY && yylen == 1)                         \
+    {                                                          \
+      yychar = (Token);                                                \
+      yylval = (Value);                                                \
+      yychar1 = YYTRANSLATE (yychar);                          \
+      YYPOPSTACK;                                              \
+      goto yybackup;                                           \
+    }                                                          \
+  else                                                         \
+    {                                                          \
+      yyerror ("syntax error: cannot back up");                        \
+      YYERROR;                                                 \
+    }                                                          \
+while (0)
+
+#define YYTERROR       1
+#define YYERRCODE      256
+
+
+/* YYLLOC_DEFAULT -- Compute the default location (before the actions
+   are run).
+
+   When YYLLOC_DEFAULT is run, CURRENT is set the location of the
+   first token.  By default, to implement support for ranges, extend
+   its range to the last symbol.  */
+
+#ifndef YYLLOC_DEFAULT
+# define YYLLOC_DEFAULT(Current, Rhs, N)               \
+   Current.last_line   = Rhs[N].last_line;     \
+   Current.last_column = Rhs[N].last_column;
+#endif
+
+
+/* YYLEX -- calling `yylex' with the right arguments.  */
+
+#if YYPURE
+# if YYLSP_NEEDED
+#  ifdef YYLEX_PARAM
+#   define YYLEX               yylex (&yylval, &yylloc, YYLEX_PARAM)
+#  else
+#   define YYLEX               yylex (&yylval, &yylloc)
+#  endif
+# else /* !YYLSP_NEEDED */
+#  ifdef YYLEX_PARAM
+#   define YYLEX               yylex (&yylval, YYLEX_PARAM)
+#  else
+#   define YYLEX               yylex (&yylval)
+#  endif
+# endif /* !YYLSP_NEEDED */
+#else /* !YYPURE */
+# define YYLEX                 yylex ()
+#endif /* !YYPURE */
+
+
+/* Enable debugging if requested.  */
+#if YYDEBUG
+
+# ifndef YYFPRINTF
+#  ifdef __cplusplus
+#   include <cstdio>  /* INFRINGES ON USER NAME SPACE */
+#  else
+#   include <stdio.h> /* INFRINGES ON USER NAME SPACE */
+#  endif
+#  define YYFPRINTF YYSTD (fprintf)
+# endif
+
+# define YYDPRINTF(Args)                       \
+do {                                           \
+  if (yydebug)                                 \
+    YYFPRINTF Args;                            \
+} while (0)
+/* Nonzero means print parse trace. [The following comment makes no
+   sense to me.  Could someone clarify it?  --akim] Since this is
+   uninitialized, it does not stop multiple parsers from coexisting.
+   */
+int yydebug;
+#else /* !YYDEBUG */
+# define YYDPRINTF(Args)
+#endif /* !YYDEBUG */
+
+/* YYINITDEPTH -- initial size of the parser's stacks.  */
+#ifndef        YYINITDEPTH
+# define YYINITDEPTH 200
+#endif
+
+/* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
+   if the built-in stack extension method is used).
+
+   Do not make this value too large; the results are undefined if
+   SIZE_MAX < YYSTACK_BYTES (YYMAXDEPTH)
+   evaluated with infinite-precision integer arithmetic.  */
+
+#if YYMAXDEPTH == 0
+# undef YYMAXDEPTH
+#endif
+
+#ifndef YYMAXDEPTH
+# define YYMAXDEPTH 10000
+#endif
+\f
+#if ! defined (yyoverflow) && ! defined (yymemcpy)
+# if __GNUC__ > 1              /* GNU C and GNU C++ define this.  */
+#  define yymemcpy __builtin_memcpy
+# else                         /* not GNU C or C++ */
+
+/* This is the most reliable way to avoid incompatibilities
+   in available built-in functions on various systems.  */
+static void
+#  if defined (__STDC__) || defined (__cplusplus)
+yymemcpy (char *yyto, const char *yyfrom, YYSIZE_T yycount)
+#  else
+yymemcpy (yyto, yyfrom, yycount)
+     char *yyto;
+     const char *yyfrom;
+     YYSIZE_T yycount;
+#  endif
+{
+  register const char *yyf = yyfrom;
+  register char *yyt = yyto;
+  register YYSIZE_T yyi = yycount;
+
+  while (yyi-- != 0)
+    *yyt++ = *yyf++;
+}
+# endif
+#endif
+
+#ifdef YYERROR_VERBOSE
+
+# ifndef yystrlen
+#  if defined (__GLIBC__) && defined (_STRING_H)
+#   define yystrlen strlen
+#  else
+/* Return the length of YYSTR.  */
+static YYSIZE_T
+#   if defined (__STDC__) || defined (__cplusplus)
+yystrlen (const char *yystr)
+#   else
+yystrlen (yystr)
+     const char *yystr;
+#   endif
+{
+  register const char *yys = yystr;
+
+  while (*yys++ != '\0')
+    continue;
+
+  return yys - yystr - 1;
+}
+#  endif
+# endif
+
+# ifndef yystpcpy
+#  if defined (__GLIBC__) && defined (_STRING_H) && defined (_GNU_SOURCE)
+#   define yystpcpy stpcpy
+#  else
+/* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in
+   YYDEST.  */
+static char *
+#   if defined (__STDC__) || defined (__cplusplus)
+yystpcpy (char *yydest, const char *yysrc)
+#   else
+yystpcpy (yydest, yysrc)
+     char *yydest;
+     const char *yysrc;
+#   endif
+{
+  register char *yyd = yydest;
+  register const char *yys = yysrc;
+
+  while ((*yyd++ = *yys++) != '\0')
+    continue;
+
+  return yyd - 1;
+}
+#  endif
+# endif
+#endif
+\f
+#line 341 "/usr/share/bison/bison.simple"
+
+
+/* The user can define YYPARSE_PARAM as the name of an argument to be passed
+   into yyparse.  The argument should have type void *.
+   It should actually point to an object.
+   Grammar actions can access the variable by casting it
+   to the proper pointer type.  */
+
+#ifdef YYPARSE_PARAM
+# ifdef __cplusplus
+#  define YYPARSE_PARAM_ARG void *YYPARSE_PARAM
+#  define YYPARSE_PARAM_DECL
+# else /* !__cplusplus */
+#  define YYPARSE_PARAM_ARG YYPARSE_PARAM
+#  define YYPARSE_PARAM_DECL void *YYPARSE_PARAM;
+# endif /* !__cplusplus */
+#else /* !YYPARSE_PARAM */
+# define YYPARSE_PARAM_ARG
+# define YYPARSE_PARAM_DECL
+#endif /* !YYPARSE_PARAM */
+
+/* Prevent warning if -Wstrict-prototypes.  */
+#ifdef __GNUC__
+# ifdef YYPARSE_PARAM
+int yyparse (void *);
+# else
+int yyparse (void);
+# endif
+#endif
+
+/* YY_DECL_VARIABLES -- depending whether we use a pure parser,
+   variables are global, or local to YYPARSE.  */
+
+#define YY_DECL_NON_LSP_VARIABLES                      \
+/* The lookahead symbol.  */                           \
+int yychar;                                            \
+                                                       \
+/* The semantic value of the lookahead symbol. */      \
+YYSTYPE yylval;                                                \
+                                                       \
+/* Number of parse errors so far.  */                  \
+int yynerrs;
+
+#if YYLSP_NEEDED
+# define YY_DECL_VARIABLES                     \
+YY_DECL_NON_LSP_VARIABLES                      \
+                                               \
+/* Location data for the lookahead symbol.  */ \
+YYLTYPE yylloc;
+#else
+# define YY_DECL_VARIABLES                     \
+YY_DECL_NON_LSP_VARIABLES
+#endif
+
+
+/* If nonreentrant, generate the variables here. */
+
+#if !YYPURE
+YY_DECL_VARIABLES
+#endif  /* !YYPURE */
+
+int
+yyparse (YYPARSE_PARAM_ARG)
+     YYPARSE_PARAM_DECL
+{
+  /* If reentrant, generate the variables here. */
+#if YYPURE
+  YY_DECL_VARIABLES
+#endif  /* !YYPURE */
+
+  register int yystate;
+  register int yyn;
+  int yyresult;
+  /* Number of tokens to shift before error messages enabled.  */
+  int yyerrstatus;
+  /* Lookahead token as an internal (translated) token number.  */
+  int yychar1 = 0;
+
+  /* Three stacks and their tools:
+     `yyss': related to states,
+     `yyvs': related to semantic values,
+     `yyls': related to locations.
+
+     Refer to the stacks thru separate pointers, to allow yyoverflow
+     to reallocate them elsewhere.  */
+
+  /* The state stack. */
+  short        yyssa[YYINITDEPTH];
+  short *yyss = yyssa;
+  register short *yyssp;
+
+  /* The semantic value stack.  */
+  YYSTYPE yyvsa[YYINITDEPTH];
+  YYSTYPE *yyvs = yyvsa;
+  register YYSTYPE *yyvsp;
+
+#if YYLSP_NEEDED
+  /* The location stack.  */
+  YYLTYPE yylsa[YYINITDEPTH];
+  YYLTYPE *yyls = yylsa;
+  YYLTYPE *yylsp;
+#endif
+
+#if YYLSP_NEEDED
+# define YYPOPSTACK   (yyvsp--, yyssp--, yylsp--)
+#else
+# define YYPOPSTACK   (yyvsp--, yyssp--)
+#endif
+
+  YYSIZE_T yystacksize = YYINITDEPTH;
+
+
+  /* The variables used to return semantic value and location from the
+     action routines.  */
+  YYSTYPE yyval;
+#if YYLSP_NEEDED
+  YYLTYPE yyloc;
+#endif
+
+  /* When reducing, the number of symbols on the RHS of the reduced
+     rule. */
+  int yylen;
+
+  YYDPRINTF ((stderr, "Starting parse\n"));
+
+  yystate = 0;
+  yyerrstatus = 0;
+  yynerrs = 0;
+  yychar = YYEMPTY;            /* Cause a token to be read.  */
+
+  /* Initialize stack pointers.
+     Waste one element of value and location stack
+     so that they stay on the same level as the state stack.
+     The wasted elements are never initialized.  */
+
+  yyssp = yyss;
+  yyvsp = yyvs;
+#if YYLSP_NEEDED
+  yylsp = yyls;
+#endif
+  goto yysetstate;
+
+/*------------------------------------------------------------.
+| yynewstate -- Push a new state, which is found in yystate.  |
+`------------------------------------------------------------*/
+ yynewstate:
+  /* In all cases, when you get here, the value and location stacks
+     have just been pushed. so pushing a state here evens the stacks.
+     */
+  yyssp++;
+
+ yysetstate:
+  *yyssp = yystate;
+
+  if (yyssp >= yyss + yystacksize - 1)
+    {
+      /* Get the current used size of the three stacks, in elements.  */
+      YYSIZE_T yysize = yyssp - yyss + 1;
+
+#ifdef yyoverflow
+      {
+       /* Give user a chance to reallocate the stack. Use copies of
+          these so that the &'s don't force the real ones into
+          memory.  */
+       YYSTYPE *yyvs1 = yyvs;
+       short *yyss1 = yyss;
+
+       /* Each stack pointer address is followed by the size of the
+          data in use in that stack, in bytes.  */
+# if YYLSP_NEEDED
+       YYLTYPE *yyls1 = yyls;
+       /* This used to be a conditional around just the two extra args,
+          but that might be undefined if yyoverflow is a macro.  */
+       yyoverflow ("parser stack overflow",
+                   &yyss1, yysize * sizeof (*yyssp),
+                   &yyvs1, yysize * sizeof (*yyvsp),
+                   &yyls1, yysize * sizeof (*yylsp),
+                   &yystacksize);
+       yyls = yyls1;
+# else
+       yyoverflow ("parser stack overflow",
+                   &yyss1, yysize * sizeof (*yyssp),
+                   &yyvs1, yysize * sizeof (*yyvsp),
+                   &yystacksize);
+# endif
+       yyss = yyss1;
+       yyvs = yyvs1;
+      }
+#else /* no yyoverflow */
+      /* Extend the stack our own way.  */
+      if (yystacksize >= YYMAXDEPTH)
+       goto yyoverflowlab;
+      yystacksize *= 2;
+      if (yystacksize > YYMAXDEPTH)
+       yystacksize = YYMAXDEPTH;
+
+      {
+       short *yyss1 = yyss;
+       union yyalloc *yyptr =
+         (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize));
+       if (! yyptr)
+         goto yyoverflowlab;
+       YYSTACK_RELOCATE (short, yyss);
+       YYSTACK_RELOCATE (YYSTYPE, yyvs);
+# if YYLSP_NEEDED
+       YYSTACK_RELOCATE (YYLTYPE, yyls);
+# endif
+# undef YYSTACK_RELOCATE
+       if (yyss1 != yyssa)
+         YYSTACK_FREE (yyss1);
+      }
+#endif /* no yyoverflow */
+
+      yyssp = yyss + yysize - 1;
+      yyvsp = yyvs + yysize - 1;
+#if YYLSP_NEEDED
+      yylsp = yyls + yysize - 1;
+#endif
+
+      YYDPRINTF ((stderr, "Stack size increased to %lu\n",
+                 (unsigned long int) yystacksize));
+
+      if (yyssp >= yyss + yystacksize - 1)
+       YYABORT;
+    }
+
+  YYDPRINTF ((stderr, "Entering state %d\n", yystate));
+
+  goto yybackup;
+
+
+/*-----------.
+| yybackup.  |
+`-----------*/
+yybackup:
+
+/* Do appropriate processing given the current state.  */
+/* Read a lookahead token if we need one and don't already have one.  */
+/* yyresume: */
+
+  /* First try to decide what to do without reference to lookahead token.  */
+
+  yyn = yypact[yystate];
+  if (yyn == YYFLAG)
+    goto yydefault;
+
+  /* Not known => get a lookahead token if don't already have one.  */
+
+  /* yychar is either YYEMPTY or YYEOF
+     or a valid token in external form.  */
+
+  if (yychar == YYEMPTY)
+    {
+      YYDPRINTF ((stderr, "Reading a token: "));
+      yychar = YYLEX;
+    }
+
+  /* Convert token to internal form (in yychar1) for indexing tables with */
+
+  if (yychar <= 0)             /* This means end of input. */
+    {
+      yychar1 = 0;
+      yychar = YYEOF;          /* Don't call YYLEX any more */
+
+      YYDPRINTF ((stderr, "Now at end of input.\n"));
+    }
+  else
+    {
+      yychar1 = YYTRANSLATE (yychar);
+
+#if YYDEBUG
+     /* We have to keep this `#if YYDEBUG', since we use variables
+       which are defined only if `YYDEBUG' is set.  */
+      if (yydebug)
+       {
+         YYFPRINTF (stderr, "Next token is %d (%s",
+                    yychar, yytname[yychar1]);
+         /* Give the individual parser a way to print the precise
+            meaning of a token, for further debugging info.  */
+# ifdef YYPRINT
+         YYPRINT (stderr, yychar, yylval);
+# endif
+         YYFPRINTF (stderr, ")\n");
+       }
+#endif
+    }
+
+  yyn += yychar1;
+  if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != yychar1)
+    goto yydefault;
+
+  yyn = yytable[yyn];
+
+  /* yyn is what to do for this token type in this state.
+     Negative => reduce, -yyn is rule number.
+     Positive => shift, yyn is new state.
+       New state is final state => don't bother to shift,
+       just return success.
+     0, or most negative number => error.  */
+
+  if (yyn < 0)
+    {
+      if (yyn == YYFLAG)
+       goto yyerrlab;
+      yyn = -yyn;
+      goto yyreduce;
+    }
+  else if (yyn == 0)
+    goto yyerrlab;
+
+  if (yyn == YYFINAL)
+    YYACCEPT;
+
+  /* Shift the lookahead token.  */
+  YYDPRINTF ((stderr, "Shifting token %d (%s), ",
+             yychar, yytname[yychar1]));
+
+  /* Discard the token being shifted unless it is eof.  */
+  if (yychar != YYEOF)
+    yychar = YYEMPTY;
+
+  *++yyvsp = yylval;
+#if YYLSP_NEEDED
+  *++yylsp = yylloc;
+#endif
+
+  /* Count tokens shifted since error; after three, turn off error
+     status.  */
+  if (yyerrstatus)
+    yyerrstatus--;
+
+  yystate = yyn;
+  goto yynewstate;
+
+
+/*-----------------------------------------------------------.
+| yydefault -- do the default action for the current state.  |
+`-----------------------------------------------------------*/
+yydefault:
+  yyn = yydefact[yystate];
+  if (yyn == 0)
+    goto yyerrlab;
+  goto yyreduce;
+
+
+/*-----------------------------.
+| yyreduce -- Do a reduction.  |
+`-----------------------------*/
+yyreduce:
+  /* yyn is the number of a rule to reduce with.  */
+  yylen = yyr2[yyn];
+
+  /* If YYLEN is nonzero, implement the default value of the action:
+     `$$ = $1'.
+
+     Otherwise, the following line sets YYVAL to the semantic value of
+     the lookahead token.  This behavior is undocumented and Bison
+     users should not rely upon it.  Assigning to YYVAL
+     unconditionally makes the parser a bit smaller, and it avoids a
+     GCC warning that YYVAL may be used uninitialized.  */
+  yyval = yyvsp[1-yylen];
+
+#if YYLSP_NEEDED
+  /* Similarly for the default location.  Let the user run additional
+     commands if for instance locations are ranges.  */
+  yyloc = yylsp[1-yylen];
+  YYLLOC_DEFAULT (yyloc, (yylsp - yylen), yylen);
+#endif
+
+#if YYDEBUG
+  /* We have to keep this `#if YYDEBUG', since we use variables which
+     are defined only if `YYDEBUG' is set.  */
+  if (yydebug)
+    {
+      int yyi;
+
+      YYFPRINTF (stderr, "Reducing via rule %d (line %d), ",
+                yyn, yyrline[yyn]);
+
+      /* Print the symbols being reduced, and their result.  */
+      for (yyi = yyprhs[yyn]; yyrhs[yyi] > 0; yyi++)
+       YYFPRINTF (stderr, "%s ", yytname[yyrhs[yyi]]);
+      YYFPRINTF (stderr, " -> %s\n", yytname[yyr1[yyn]]);
+    }
+#endif
+
+  switch (yyn) {
+
+case 1:
+#line 30 "wktparse.y"
+{ alloc_lwgeom(srid); ;
+    break;}
+case 3:
+#line 31 "wktparse.y"
+{ alloc_lwgeom(-1); ;
+    break;}
+case 13:
+#line 36 "wktparse.y"
+{ set_srid(yyvsp[0].value); ;
+    break;}
+case 14:
+#line 38 "wktparse.y"
+{alloc_wkb(yyvsp[0].wkb) ; ;
+    break;}
+case 16:
+#line 45 "wktparse.y"
+{ alloc_point(); ;
+    break;}
+case 17:
+#line 45 "wktparse.y"
+{ pop();;
+    break;}
+case 20:
+#line 51 "wktparse.y"
+{ alloc_multipoint(); ;
+    break;}
+case 21:
+#line 51 "wktparse.y"
+{ pop();;
+    break;}
+case 23:
+#line 53 "wktparse.y"
+{ alloc_counter(); ;
+    break;}
+case 24:
+#line 53 "wktparse.y"
+{pop();;
+    break;}
+case 28:
+#line 57 "wktparse.y"
+{ alloc_point(); ;
+    break;}
+case 29:
+#line 57 "wktparse.y"
+{ pop();;
+    break;}
+case 31:
+#line 64 "wktparse.y"
+{ alloc_linestring(); ;
+    break;}
+case 32:
+#line 64 "wktparse.y"
+{pop();;
+    break;}
+case 34:
+#line 66 "wktparse.y"
+{ alloc_counter(); ;
+    break;}
+case 35:
+#line 66 "wktparse.y"
+{popc(); ;
+    break;}
+case 38:
+#line 72 "wktparse.y"
+{ alloc_multilinestring(); ;
+    break;}
+case 39:
+#line 72 "wktparse.y"
+{ pop();;
+    break;}
+case 41:
+#line 74 "wktparse.y"
+{ alloc_counter(); ;
+    break;}
+case 42:
+#line 74 "wktparse.y"
+{ pop();;
+    break;}
+case 46:
+#line 83 "wktparse.y"
+{ alloc_polygon(); ;
+    break;}
+case 47:
+#line 83 "wktparse.y"
+{ pop();;
+    break;}
+case 49:
+#line 85 "wktparse.y"
+{ alloc_counter(); ;
+    break;}
+case 50:
+#line 85 "wktparse.y"
+{ pop();;
+    break;}
+case 53:
+#line 91 "wktparse.y"
+{ alloc_multipolygon(); ;
+    break;}
+case 54:
+#line 91 "wktparse.y"
+{ pop();;
+    break;}
+case 56:
+#line 93 "wktparse.y"
+{ alloc_counter(); ;
+    break;}
+case 57:
+#line 93 "wktparse.y"
+{ pop();;
+    break;}
+case 60:
+#line 101 "wktparse.y"
+{ alloc_geomertycollection(); ;
+    break;}
+case 61:
+#line 101 "wktparse.y"
+{ pop();;
+    break;}
+case 63:
+#line 103 "wktparse.y"
+{ alloc_counter(); ;
+    break;}
+case 64:
+#line 103 "wktparse.y"
+{ pop();;
+    break;}
+case 70:
+#line 110 "wktparse.y"
+{alloc_point_2d(yyvsp[-1].value,yyvsp[0].value) ;
+    break;}
+case 71:
+#line 112 "wktparse.y"
+{alloc_point_3d(yyvsp[-2].value,yyvsp[-1].value,yyvsp[0].value) ;
+    break;}
+case 72:
+#line 114 "wktparse.y"
+{alloc_point_4d(yyvsp[-3].value,yyvsp[-2].value,yyvsp[-1].value,yyvsp[0].value) ;
+    break;}
+case 73:
+#line 116 "wktparse.y"
+{alloc_empty() ;
+    break;}
+}
+
+#line 727 "/usr/share/bison/bison.simple"
+
+\f
+  yyvsp -= yylen;
+  yyssp -= yylen;
+#if YYLSP_NEEDED
+  yylsp -= yylen;
+#endif
+
+#if YYDEBUG
+  if (yydebug)
+    {
+      short *yyssp1 = yyss - 1;
+      YYFPRINTF (stderr, "state stack now");
+      while (yyssp1 != yyssp)
+       YYFPRINTF (stderr, " %d", *++yyssp1);
+      YYFPRINTF (stderr, "\n");
+    }
+#endif
+
+  *++yyvsp = yyval;
+#if YYLSP_NEEDED
+  *++yylsp = yyloc;
+#endif
+
+  /* Now `shift' the result of the reduction.  Determine what state
+     that goes to, based on the state we popped back to and the rule
+     number reduced by.  */
+
+  yyn = yyr1[yyn];
+
+  yystate = yypgoto[yyn - YYNTBASE] + *yyssp;
+  if (yystate >= 0 && yystate <= YYLAST && yycheck[yystate] == *yyssp)
+    yystate = yytable[yystate];
+  else
+    yystate = yydefgoto[yyn - YYNTBASE];
+
+  goto yynewstate;
+
+
+/*------------------------------------.
+| yyerrlab -- here on detecting error |
+`------------------------------------*/
+yyerrlab:
+  /* If not already recovering from an error, report this error.  */
+  if (!yyerrstatus)
+    {
+      ++yynerrs;
+
+#ifdef YYERROR_VERBOSE
+      yyn = yypact[yystate];
+
+      if (yyn > YYFLAG && yyn < YYLAST)
+       {
+         YYSIZE_T yysize = 0;
+         char *yymsg;
+         int yyx, yycount;
+
+         yycount = 0;
+         /* Start YYX at -YYN if negative to avoid negative indexes in
+            YYCHECK.  */
+         for (yyx = yyn < 0 ? -yyn : 0;
+              yyx < (int) (sizeof (yytname) / sizeof (char *)); yyx++)
+           if (yycheck[yyx + yyn] == yyx)
+             yysize += yystrlen (yytname[yyx]) + 15, yycount++;
+         yysize += yystrlen ("parse error, unexpected ") + 1;
+         yysize += yystrlen (yytname[YYTRANSLATE (yychar)]);
+         yymsg = (char *) YYSTACK_ALLOC (yysize);
+         if (yymsg != 0)
+           {
+             char *yyp = yystpcpy (yymsg, "parse error, unexpected ");
+             yyp = yystpcpy (yyp, yytname[YYTRANSLATE (yychar)]);
+
+             if (yycount < 5)
+               {
+                 yycount = 0;
+                 for (yyx = yyn < 0 ? -yyn : 0;
+                      yyx < (int) (sizeof (yytname) / sizeof (char *));
+                      yyx++)
+                   if (yycheck[yyx + yyn] == yyx)
+                     {
+                       const char *yyq = ! yycount ? ", expecting " : " or ";
+                       yyp = yystpcpy (yyp, yyq);
+                       yyp = yystpcpy (yyp, yytname[yyx]);
+                       yycount++;
+                     }
+               }
+             yyerror (yymsg);
+             YYSTACK_FREE (yymsg);
+           }
+         else
+           yyerror ("parse error; also virtual memory exhausted");
+       }
+      else
+#endif /* defined (YYERROR_VERBOSE) */
+       yyerror ("parse error");
+    }
+  goto yyerrlab1;
+
+
+/*--------------------------------------------------.
+| yyerrlab1 -- error raised explicitly by an action |
+`--------------------------------------------------*/
+yyerrlab1:
+  if (yyerrstatus == 3)
+    {
+      /* If just tried and failed to reuse lookahead token after an
+        error, discard it.  */
+
+      /* return failure if at end of input */
+      if (yychar == YYEOF)
+       YYABORT;
+      YYDPRINTF ((stderr, "Discarding token %d (%s).\n",
+                 yychar, yytname[yychar1]));
+      yychar = YYEMPTY;
+    }
+
+  /* Else will try to reuse lookahead token after shifting the error
+     token.  */
+
+  yyerrstatus = 3;             /* Each real token shifted decrements this */
+
+  goto yyerrhandle;
+
+
+/*-------------------------------------------------------------------.
+| yyerrdefault -- current state does not do anything special for the |
+| error token.                                                       |
+`-------------------------------------------------------------------*/
+yyerrdefault:
+#if 0
+  /* This is wrong; only states that explicitly want error tokens
+     should shift them.  */
+
+  /* If its default is to accept any token, ok.  Otherwise pop it.  */
+  yyn = yydefact[yystate];
+  if (yyn)
+    goto yydefault;
+#endif
+
+
+/*---------------------------------------------------------------.
+| yyerrpop -- pop the current state because it cannot handle the |
+| error token                                                    |
+`---------------------------------------------------------------*/
+yyerrpop:
+  if (yyssp == yyss)
+    YYABORT;
+  yyvsp--;
+  yystate = *--yyssp;
+#if YYLSP_NEEDED
+  yylsp--;
+#endif
+
+#if YYDEBUG
+  if (yydebug)
+    {
+      short *yyssp1 = yyss - 1;
+      YYFPRINTF (stderr, "Error: state stack now");
+      while (yyssp1 != yyssp)
+       YYFPRINTF (stderr, " %d", *++yyssp1);
+      YYFPRINTF (stderr, "\n");
+    }
+#endif
+
+/*--------------.
+| yyerrhandle.  |
+`--------------*/
+yyerrhandle:
+  yyn = yypact[yystate];
+  if (yyn == YYFLAG)
+    goto yyerrdefault;
+
+  yyn += YYTERROR;
+  if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != YYTERROR)
+    goto yyerrdefault;
+
+  yyn = yytable[yyn];
+  if (yyn < 0)
+    {
+      if (yyn == YYFLAG)
+       goto yyerrpop;
+      yyn = -yyn;
+      goto yyreduce;
+    }
+  else if (yyn == 0)
+    goto yyerrpop;
+
+  if (yyn == YYFINAL)
+    YYACCEPT;
+
+  YYDPRINTF ((stderr, "Shifting error token, "));
+
+  *++yyvsp = yylval;
+#if YYLSP_NEEDED
+  *++yylsp = yylloc;
+#endif
+
+  yystate = yyn;
+  goto yynewstate;
+
+
+/*-------------------------------------.
+| yyacceptlab -- YYACCEPT comes here.  |
+`-------------------------------------*/
+yyacceptlab:
+  yyresult = 0;
+  goto yyreturn;
+
+/*-----------------------------------.
+| yyabortlab -- YYABORT comes here.  |
+`-----------------------------------*/
+yyabortlab:
+  yyresult = 1;
+  goto yyreturn;
+
+/*---------------------------------------------.
+| yyoverflowab -- parser overflow comes here.  |
+`---------------------------------------------*/
+yyoverflowlab:
+  yyerror ("parser stack overflow");
+  yyresult = 2;
+  /* Fall through.  */
+
+yyreturn:
+#ifndef yyoverflow
+  if (yyss != yyssa)
+    YYSTACK_FREE (yyss);
+#endif
+  return yyresult;
+}
+#line 117 "wktparse.y"
+
+
+
+
+
diff --git a/lwgeom/wktparse.tab.h b/lwgeom/wktparse.tab.h
new file mode 100644 (file)
index 0000000..7b03cdb
--- /dev/null
@@ -0,0 +1,31 @@
+#ifndef BISON_WKTPARSE_TAB_H
+# define BISON_WKTPARSE_TAB_H
+
+#ifndef YYSTYPE
+typedef union {
+       double value;
+       const char* wkb;
+} yystype;
+# define YYSTYPE yystype
+#endif
+# define       POINT   257
+# define       LINESTRING      258
+# define       POLYGON 259
+# define       MULTIPOINT      260
+# define       MULTILINESTRING 261
+# define       MULTIPOLYGON    262
+# define       GEOMETRYCOLLECTION      263
+# define       SRID    264
+# define       EMPTY   265
+# define       VALUE   266
+# define       LPAREN  267
+# define       RPAREN  268
+# define       COMMA   269
+# define       EQUALS  270
+# define       SEMICOLON       271
+# define       WKB     272
+
+
+extern YYSTYPE lwg_parse_yylval;
+
+#endif /* not BISON_WKTPARSE_TAB_H */
diff --git a/lwgeom/wktunparse.c b/lwgeom/wktunparse.c
new file mode 100644 (file)
index 0000000..d8ef084
--- /dev/null
@@ -0,0 +1,376 @@
+/*
+ * Written by Ralph Mason ralph.mason<at>telogis.com
+ *
+ * Copyright Telogis 2004
+ * www.telogis.com
+ *
+ */
+
+#include "wktparse.h"
+#include <string.h>
+#include <math.h>
+#include <stdio.h>
+//TO get byte order
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <netinet/in_systm.h>
+#include <netinet/ip.h>
+
+typedef unsigned long int4;
+
+static int dims;
+static allocator local_malloc;
+static freeor local_free;
+static char*  out_start;
+static char*  out_pos;
+static int len;
+static int lwgi;
+
+void ensure(int chars){
+
+       int pos = out_pos - out_start;
+
+       if (  (pos + chars) >= len ){
+               char* newp =(char*)local_malloc(len*2);
+               memcpy(newp,out_start,len);
+               local_free(out_start);
+               out_start = newp;
+               out_pos = newp + pos;
+               len *=2;
+       }
+}
+
+void to_end(){
+       while(*out_pos){
+               out_pos++;
+       }
+}
+
+void write_str(const char* str){
+       ensure(32);
+       strcpy(out_pos,str);
+       to_end();
+}
+
+void write_double(double val){
+       ensure(32);
+       if (lwgi)
+               sprintf(out_pos,"%.8g",val);
+       else
+               sprintf(out_pos,"%.15g",val);
+       to_end();
+}
+
+void write_int(int i){
+       ensure(32);
+       sprintf(out_pos,"%i",i);
+       to_end();
+}
+
+int4 read_int(byte** geom){
+       int4 ret;
+#ifdef SHRINK_INTS
+       if ( BYTE_ORDER == LITTLE_ENDIAN ){
+               if( (**geom)& 0x01){
+                       ret = **geom >>1;
+                       (*geom)++;
+                       return ret;
+               }
+       }
+       else{
+               if( (**geom)& 0x80){
+                       ret = **geom & ~0x80;
+                       (*geom)++;
+                       return ret;
+               }
+       }
+#endif
+       memcpy(&ret,*geom,4);
+
+#ifdef SHRINK_INTS
+       if ( BYTE_ORDER == LITTLE_ENDIAN ){
+               ret >>= 1;
+       }
+#endif
+
+       (*geom)+=4;
+       return ret;
+}
+
+double round(double);
+
+double read_double(byte** geom){
+       if (lwgi){
+               double ret = *((int4*)*geom);
+               ret /= 0xb60b60;
+               (*geom)+=4;
+               return ret-180.0;
+       }
+       else{
+               double ret = *((double*)*geom);
+               (*geom)+=8;
+               return ret;
+       }
+}
+
+typedef byte* (*outfunc)(byte*,int);
+
+byte* output_point(byte* geom,int supress){
+       int i;
+
+       for( i = 0 ; i < dims ; i++ ){
+               write_double(read_double(&geom));
+               if (i +1 < dims )
+                       write_str(" ");
+       }
+       return geom;
+}
+
+byte* output_single(byte* geom,int supress){
+       write_str("(");
+       geom=output_point(geom,supress);
+       write_str(")");
+       return geom;
+}
+
+byte* output_collection(byte* geom,outfunc func,int supress){
+       int cnt = read_int(&geom);
+       if ( cnt == 0 ){
+               write_str(" EMPTY");
+       }
+       else{
+               write_str("(");
+               while(cnt--){
+                       geom=func(geom,supress);
+                       if ( cnt ){
+                               write_str(",");
+                       }
+               }
+               write_str(")");
+       }
+       return geom;
+}
+
+byte* output_collection_2(byte* geom,int suppress){
+       return output_collection(geom,output_point,suppress);
+}
+
+byte* output_wkt(byte* geom, int supress){
+
+       unsigned type=*geom++;
+       dims = ((type & 0x30) >> 4)+2;
+       
+       //Skip the bounding box if there is one
+       if ( type & 0x80 ){
+               geom+=16;
+       }
+
+       if ( type & 0x40 ){
+               write_str("SRID=");write_int(read_int(&geom));write_str(";");
+       }
+
+       switch(type & 0x0F){
+               case POINTTYPE:
+                       if ( ! supress) write_str("POINT");
+                       geom=output_single(geom,0);
+                       break;
+               case LINETYPE:
+                       if ( ! supress) write_str("LINESTRING");
+                       geom = output_collection(geom,output_point,0);
+                       break;
+               case POLYGONTYPE:
+                       if ( ! supress) write_str("POLYGON");
+                       geom = output_collection(geom,output_collection_2,0);
+                       break;
+               case MULTIPOINTTYPE:
+                       if ( ! supress) write_str("MULTIPOINT");
+                       geom = output_collection(geom,output_wkt,1);
+                       break;
+               case MULTILINETYPE:
+                       if ( ! supress) write_str("MULTILINESTRING");
+                       geom = output_collection(geom,output_wkt,1);
+                       break;
+               case MULTIPOLYGONTYPE:
+                       if ( ! supress) write_str("MULTIPOLYGON");
+                       geom = output_collection(geom,output_wkt,1);
+                       break;
+               case COLLECTIONTYPE:
+                       if ( ! supress) write_str("GEOMETRYCOLLECTION");
+                       geom = output_collection(geom,output_wkt,0);
+                       break;
+
+               case POINTTYPEI:
+                       if ( ! supress) write_str("POINT");
+                       lwgi++;
+                       geom=output_single(geom,0);
+                       lwgi--;
+                       break;
+               case LINETYPEI:
+                       if ( ! supress) write_str("LINESTRING");
+                       lwgi++;
+                       geom = output_collection(geom,output_point,0);
+                       lwgi--;
+                       break;
+               case POLYGONTYPEI:
+                       if ( ! supress) write_str("POLYGON");
+                       lwgi++;
+                       geom =output_collection(geom,output_collection_2,0);
+                       lwgi--;
+                       break;
+       }
+       return geom;
+}
+
+char* unparse_WKT(byte* lw_geom,allocator alloc,freeor free){
+
+       if (lw_geom==NULL)
+               return NULL;
+
+       local_malloc=alloc;
+       local_free=free;
+       len = 128;
+       out_start = out_pos = alloc(len);
+       lwgi=0;
+
+       output_wkt(lw_geom+4,0);
+
+       return out_start;
+}
+
+static char outchr[]={"0123456789ABCDEF" };
+
+void write_wkb_bytes(byte* ptr,int cnt){
+       ensure(cnt*2);
+
+       while(cnt--){
+               *out_pos++ = outchr[*ptr>>4];
+               *out_pos++ = outchr[*ptr&0x0F];
+               ptr ++;
+       }
+}
+
+byte* output_wkb_point(byte* geom){
+       if ( lwgi ){
+               write_wkb_bytes(geom,dims*4);
+               return geom + (4*dims);
+       }
+       else{
+               write_wkb_bytes(geom,dims*8);
+               return geom + (8*dims);
+       }
+}
+
+typedef byte* (*outwkbfunc)(byte*);
+
+void write_wkb_int(int i){
+       write_wkb_bytes((byte*)&i,4);
+}
+
+byte* output_wkb_collection(byte* geom,outwkbfunc func){
+       int cnt = read_int(&geom);
+       write_wkb_int(cnt);
+       while(cnt--) geom=func(geom);
+       return geom;
+}
+
+byte* output_wkb_collection_2(byte* geom){
+       return output_wkb_collection(geom,output_wkb_point);
+}
+
+
+byte* output_wkb(byte* geom){
+
+       int4 type=*geom++;
+
+       dims = ((type & 0x30) >> 4)+2;
+
+       //Skip the bounding box
+       if ( type & 0x80 ){
+               geom+=16;
+       }
+
+       if ( type & 0x40 ){
+               write_str("SRID=");write_int(read_int(&geom));write_str(";");
+       }
+
+       type &=0x0f;
+
+       if ( dims==3)
+                type |=0x80000000;
+       else if (dims==4)
+                type |=0x40000000;
+
+       if ( BYTE_ORDER != LITTLE_ENDIAN ){
+               byte endian=0;
+               write_wkb_bytes(&endian,1);
+       }
+       else{
+               byte endian=1;
+               write_wkb_bytes(&endian,1);
+       }
+
+       write_wkb_int(type);
+
+       switch(type & 0x0F){
+               case POINTTYPE:
+                       geom=output_wkb_point(geom);
+                       break;
+               case LINETYPE:
+                       geom = output_wkb_collection(geom,output_wkb_point);
+                       break;
+               case POLYGONTYPE:
+                       geom = output_wkb_collection(geom,output_wkb_collection_2);
+                       break;
+               case MULTIPOINTTYPE:
+               case MULTILINETYPE:
+               case MULTIPOLYGONTYPE:
+               case COLLECTIONTYPE:
+                       geom = output_wkb_collection(geom,output_wkb);
+                       break;
+
+               /*
+                       These don't output standard wkb at the moment
+                       the output and integer version.
+
+                       however you could run it through the wkt parser
+                       for a lwg and then output that.  There should
+                       also be a force_to_real_(lwgi)
+               */
+               case POINTTYPEI:
+                       lwgi++;
+                       geom=output_wkb_point(geom);
+                       lwgi--;
+                       break;
+               case LINETYPEI:
+                       lwgi++;
+                       geom = output_wkb_collection(geom,output_wkb_point);
+                       lwgi--;
+                       break;
+               case POLYGONTYPEI:
+                       lwgi++;
+                       geom = output_wkb_collection(geom,output_wkb_collection_2);
+                       lwgi--;
+                       break;
+       }
+       return geom;
+}
+
+char* unparse_WKB(byte* lw_geom,allocator alloc,freeor free){
+
+       if (lw_geom==NULL)
+               return NULL;
+
+       local_malloc=alloc;
+       local_free=free;
+       len = 128;
+       out_start = out_pos = alloc(len);
+       lwgi=0;
+
+       output_wkb(lw_geom+4);
+       ensure(1);
+       *out_pos=0;
+
+       return out_start;
+}
+
+