]> granicus.if.org Git - postgis/commitdiff
Moved PG_LWGEOM WKB I/O and SRID get/set funx
authorSandro Santilli <strk@keybit.net>
Fri, 30 Dec 2005 17:40:37 +0000 (17:40 +0000)
committerSandro Santilli <strk@keybit.net>
Fri, 30 Dec 2005 17:40:37 +0000 (17:40 +0000)
from lwgeom_api.c to lwgeom_pg.c.
Made lwgeom_from_ewkb directly invoke grammar parser rather then invoke
the PG_LWGEOM-specific function.
Cleaned up signedness-related and comments-related warnings for the files
being committed (more to do on other files)

git-svn-id: http://svn.osgeo.org/postgis/trunk@2243 b70326c6-7e19-0410-871a-916f4a2858ee

16 files changed:
CHANGES
Version.config
lwgeom/liblwgeom.h
lwgeom/lwgeom.c
lwgeom/lwgeom_api.c
lwgeom/lwgeom_chip.c
lwgeom/lwgeom_estimate.c
lwgeom/lwgeom_functions_analytic.c
lwgeom/lwgeom_gist.c
lwgeom/lwgeom_inout.c
lwgeom/lwgeom_ogc.c
lwgeom/lwgeom_pg.c
lwgeom/lwgeom_pg.h
lwgeom/lwgparse.c
lwgeom/lwpoly.c
lwgeom/wktparse.h

diff --git a/CHANGES b/CHANGES
index 6177c19d313d9cd63016705b6a7e4abd37900e2b..20c03ec5f43223d874886cb0ea627c6cd945403e 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,7 @@
+PostGIS 1.1.1CVS
+
+       - Source code cleanups
+
 PostGIS 1.1.0
 2005/12/21
 
index 4fc4da7392719c685c8283e51ae5786307e8d86e..62b462d947b5efc200b3c303cc9e1780b254a697 100644 (file)
@@ -4,7 +4,7 @@
 # shared library / release version
 SO_MAJOR_VERSION=1
 SO_MINOR_VERSION=1
-SO_MICRO_VERSION=0
+SO_MICRO_VERSION=1CVS
 
 # JDBC code version
 JDBC_MAJOR_VERSION=1
index 33ea68a9cce8032727cf7837e0b1da64c6fe7428..3ffc0ad80239ef1bbeb25900b334a747462c3398 100644 (file)
@@ -507,7 +507,7 @@ char is_worth_caching_lwgeom_bbox(const LWGEOM *);
  * (which is an PG_LWGEOM w/out int32 size casted to char *)
  */
 /*#define SERIALIZED_FORM(x) ((uchar *)(x))+4*/
-#define SERIALIZED_FORM(x) (VARDATA((x)))
+#define SERIALIZED_FORM(x) ((uchar *)VARDATA((x)))
 
 
 /*
@@ -796,9 +796,7 @@ size_t lwgeom_empty_length(int SRID);
  * get the SRID from the LWGEOM
  * none present => -1
  */
-extern int pglwgeom_getSRID(PG_LWGEOM *lwgeom);
 extern int lwgeom_getsrid(uchar *serialized);
-extern PG_LWGEOM *pglwgeom_setSRID(PG_LWGEOM *lwgeom, int32 newSRID);
 
 
 /*------------------------------------------------------
@@ -1010,7 +1008,7 @@ extern void lwline_setPoint4d(LWLINE *line, unsigned int which, POINT4D *newpoin
 extern LWPOLY *lwpoly_from_lwlines(const LWLINE *shell, unsigned int nholes, const LWLINE **holes);
 
 /* Return a char string with ASCII versionf of type flags */
-extern const uchar *lwgeom_typeflags(uchar type);
+extern const char *lwgeom_typeflags(uchar type);
 
 /* Construct an empty pointarray */
 extern POINTARRAY *ptarray_construct(char hasz, char hasm,
@@ -1047,15 +1045,13 @@ extern LWPOLY *lwpoly_segmentize2d(LWPOLY *line, double dist);
 extern LWCOLLECTION *lwcollection_segmentize2d(LWCOLLECTION *coll, double dist);
 
 extern uchar parse_hex(char *str);
-extern void deparse_hex(uchar str, uchar *result);
+extern void deparse_hex(uchar str, char *result);
 extern uchar *parse_lwgeom_wkt(char *wkt_input);
 
 extern char *lwgeom_to_ewkt(LWGEOM *lwgeom);
 extern char *lwgeom_to_hexwkb(LWGEOM *lwgeom, unsigned int byteorder);
 extern LWGEOM *lwgeom_from_ewkb(uchar *ewkb, size_t ewkblen);
 extern uchar *lwgeom_to_ewkb(LWGEOM *lwgeom, char byteorder, size_t *ewkblen);
-extern PG_LWGEOM *pglwgeom_from_ewkb(uchar *ewkb, size_t ewkblen);
-extern char *pglwgeom_to_ewkb(PG_LWGEOM *geom, char byteorder, size_t *ewkblen);
 
 extern void *lwalloc(size_t size);
 extern void *lwrealloc(void *mem, size_t size);
index 194c8d5798105ef49c50b8718a7929f6a9d5465d..124da0349ab6a55b447a26f6f1a8ba30bd633f89 100644 (file)
@@ -2,18 +2,20 @@
 #include <stdlib.h>
 #include <stdarg.h>
 
-//#include "lwgeom_pg.h"
+/*#include "lwgeom_pg.h"*/
 #include "liblwgeom.h"
 #include "wktparse.h"
 
-//#define PGIS_DEBUG_CALLS 1
+/*#define PGIS_DEBUG_CALLS 1*/
 
 LWGEOM *
 lwgeom_deserialize(uchar *srl)
 {
        int type = lwgeom_getType(srl[0]);
 
-       //lwnotice("lwgeom_deserialize got %s", lwgeom_typename(type));
+#ifdef PGIS_DEBUG_CALLS
+       lwnotice("lwgeom_deserialize got %s", lwgeom_typename(type));
+#endif
 
        switch (type)
        {
@@ -120,7 +122,7 @@ lwgeom_serialize(LWGEOM *lwgeom)
        return serialized;
 }
 
-// Force Right-hand-rule on LWGEOM polygons
+/* Force Right-hand-rule on LWGEOM polygons */
 void
 lwgeom_forceRHR(LWGEOM *lwgeom)
 {
@@ -142,7 +144,7 @@ lwgeom_forceRHR(LWGEOM *lwgeom)
        }
 }
 
-// Reverse vertex order of LWGEOM
+/* Reverse vertex order of LWGEOM */
 void
 lwgeom_reverse(LWGEOM *lwgeom)
 {
@@ -187,8 +189,9 @@ lwgeom_compute_box2d_p(LWGEOM *lwgeom, BOX2DFLOAT4 *buf)
        return 0;
 }
 
-//dont forget to lwfree() result
-
+/*
+ * dont forget to lwfree() result
+ */
 BOX2DFLOAT4 *
 lwgeom_compute_box2d(LWGEOM *lwgeom)
 {
@@ -275,10 +278,10 @@ lwgeom_release(LWGEOM *lwgeom)
                lwerror("lwgeom_release: someone called on 0x0");
 #endif
 
-       // Drop bounding box (always a copy)
+       /* Drop bounding box (always a copy) */
        if ( lwgeom->bbox ) lwfree(lwgeom->bbox);
 
-       // Collection
+       /* Collection */
        if ( (col=lwgeom_as_lwcollection(lwgeom)) )
        {
                for (i=0; i<col->ngeoms; i++)
@@ -288,12 +291,12 @@ lwgeom_release(LWGEOM *lwgeom)
                lwfree(lwgeom);
        }
 
-       // Single element
+       /* Single element */
        else lwfree(lwgeom);
 
 }
 
-// Clone an LWGEOM object. POINTARRAY are not copied.
+/* Clone an LWGEOM object. POINTARRAY are not copied. */
 LWGEOM *
 lwgeom_clone(const LWGEOM *lwgeom)
 {
@@ -315,11 +318,14 @@ lwgeom_clone(const LWGEOM *lwgeom)
        }
 }
 
-// Add 'what' to 'to' at position 'where'.
-// where=0 == prepend
-// where=-1 == append
-// Appended-to LWGEOM gets a new type based on new condition.
-// Mix of dimensions is not allowed (TODO: allow it?).
+/*
+ * Add 'what' to 'to' at position 'where'
+ *
+ * where=0 == prepend
+ * where=-1 == append
+ * Appended-to LWGEOM gets a new type based on new condition.
+ * Mix of dimensions is not allowed (TODO: allow it?).
+ */
 LWGEOM *
 lwgeom_add(const LWGEOM *to, uint32 where, const LWGEOM *what)
 {
@@ -403,26 +409,59 @@ uchar *
 lwgeom_to_ewkb(LWGEOM *lwgeom, char byteorder, size_t *outsize)
 {
        uchar *serialized = lwgeom_serialize(lwgeom);
-       char *hexwkb = unparse_WKB(serialized, lwalloc, lwfree,
+
+       /*
+        * We cast return to "unsigned" char as we are
+        * requesting a "binary" output, not HEX
+        * (last argument set to 0)
+        */
+       uchar *hexwkb = (uchar *)unparse_WKB(serialized, lwalloc, lwfree,
                byteorder, outsize, 0);
        lwfree(serialized);
        return hexwkb;
 }
 
+/*
+ * Make an LWGEOM object from a EWKB binary representation.
+ * Currently highly unoptimized as it:
+ *     - convert EWKB to HEXEWKB 
+ *     - construct PG_LWGEOM
+ *     - deserialize it
+ */
 LWGEOM *
 lwgeom_from_ewkb(uchar *ewkb, size_t size)
 {
-       uchar *pglwgeom = (uchar *)pglwgeom_from_ewkb(ewkb, size);
-       LWGEOM *ret = lwgeom_deserialize(pglwgeom+4);
+       size_t hexewkblen = size*2;
+       char *hexewkb;
+       long int i;
+       uchar *pglwgeom; /* This is a PG_LWGEOM */
+       LWGEOM *ret;
+
+       /* "HEXify" the EWKB */
+       hexewkb = lwalloc(hexewkblen+1);
+       for (i=0; i<size; ++i) deparse_hex(ewkb[i], &hexewkb[i*2]);
+       hexewkb[hexewkblen] = '\0';
+
+       /* Rely on grammar parser to construct a PG_LWGEOM */
+       pglwgeom = parse_lwgeom_wkt(hexewkb);
+
+       /* Free intermediate HEXified representation */
+       lwfree(hexewkb);
+
+       /* Deserialize */
+       ret = lwgeom_deserialize(pglwgeom+4);
+
        return ret;
 }
 
-// geom1 same as geom2
-//  iff
-//      + have same type                                                        //      + have same # objects
-//      + have same bvol
-//      + each object in geom1 has a corresponding object in geom2 (see above)
-//
+/*
+ * geom1 same as geom2
+ *  iff
+ *      + have same type 
+ *     + have same # objects
+ *      + have same bvol
+ *      + each object in geom1 has a corresponding object in geom2 (see above)
+ */
 char
 lwgeom_same(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2)
 {
@@ -432,14 +471,14 @@ lwgeom_same(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2)
        if ( TYPE_GETZM(lwgeom1->type) != TYPE_GETZM(lwgeom2->type) )
                return 0;
 
-       // Check boxes if both already computed 
+       /* Check boxes if both already computed  */
        if ( lwgeom1->bbox && lwgeom2->bbox )
        {
-               //lwnotice("bbox1:%p, bbox2:%p", lwgeom1->bbox, lwgeom2->bbox);
+               /*lwnotice("bbox1:%p, bbox2:%p", lwgeom1->bbox, lwgeom2->bbox);*/
                if ( ! box2d_same(lwgeom1->bbox, lwgeom2->bbox) ) return 0;
        }
 
-       // geoms have same type, invoke type-specific function
+       /* geoms have same type, invoke type-specific function */
        switch(TYPE_GETTYPE(lwgeom1->type))
        {
                case POINTTYPE:
index 1ba30ed6716e1bc7fef3bd3ddc727204c3ecbd72..134ca12a90283c90789491493b5fb8622f8f8d3e 100644 (file)
@@ -968,14 +968,14 @@ lwgeom_getpoint(uchar *serialized_form, int geom_number)
 // this is fine to call on a point (with geom_num=0), multipoint or geometrycollection
 LWPOINT *lwgeom_getpoint_inspected(LWGEOM_INSPECTED *inspected, int geom_number)
 {
-       char *sub_geom;
+       uchar *sub_geom;
        uchar type;
 
        sub_geom = lwgeom_getsubgeometry_inspected(inspected, geom_number);
 
        if (sub_geom == NULL) return NULL;
 
-       type = lwgeom_getType( (uchar) sub_geom[0]);
+       type = lwgeom_getType(sub_geom[0]);
        if (type != POINTTYPE) return NULL;
 
        return lwpoint_deserialize(sub_geom);
@@ -1037,7 +1037,7 @@ LWPOLY *
 lwgeom_getpoly(uchar *serialized_form, int geom_number)
 {
        uchar type = lwgeom_getType((uchar)serialized_form[0]);
-       char *sub_geom;
+       uchar *sub_geom;
 
        if ((type == POLYGONTYPE)  && (geom_number == 0))
        {
@@ -1051,7 +1051,7 @@ lwgeom_getpoly(uchar *serialized_form, int geom_number)
        sub_geom = lwgeom_getsubgeometry(serialized_form, geom_number);
        if (sub_geom == NULL) return NULL;
 
-       type = lwgeom_getType((uchar) sub_geom[0]);
+       type = lwgeom_getType(sub_geom[0]);
        if (type != POLYGONTYPE) return NULL;
 
        return lwpoly_deserialize(sub_geom);
@@ -1063,14 +1063,14 @@ lwgeom_getpoly(uchar *serialized_form, int geom_number)
 // this is fine to call on a polygon, multipolygon or geometrycollection
 LWPOLY *lwgeom_getpoly_inspected(LWGEOM_INSPECTED *inspected, int geom_number)
 {
-       char *sub_geom;
+       uchar *sub_geom;
        uchar type;
 
        sub_geom = lwgeom_getsubgeometry_inspected(inspected, geom_number);
 
        if (sub_geom == NULL) return NULL;
 
-       type = lwgeom_getType((uchar) sub_geom[0]);
+       type = lwgeom_getType(sub_geom[0]);
        if (type != POLYGONTYPE) return NULL;
 
        return lwpoly_deserialize(sub_geom);
@@ -1091,7 +1091,7 @@ uchar *
 lwgeom_getsubgeometry(const uchar *serialized_form, int geom_number)
 {
        //major cheat!!
-       char * result;
+       uchar *result;
        LWGEOM_INSPECTED *inspected = lwgeom_inspect(serialized_form);
 
        result = lwgeom_getsubgeometry_inspected(inspected, geom_number);
@@ -1631,7 +1631,7 @@ void printPA(POINTARRAY *pa)
 {
        int t;
        POINT4D pt;
-       uchar *mflag;
+       char *mflag;
 
        if ( TYPE_HASM(pa->dims) ) mflag = "M";
        else mflag = "";
@@ -1739,156 +1739,6 @@ lwgeom_getsrid(uchar *serialized)
        return get_int32(loc);
 }
 
-// get the SRID from the LWGEOM
-// none present => -1
-int
-pglwgeom_getSRID(PG_LWGEOM *lwgeom)
-{
-       uchar type = lwgeom->type;
-       uchar *loc = lwgeom->data;
-
-       if ( ! lwgeom_hasSRID(type)) return -1;
-
-       if (lwgeom_hasBBOX(type))
-       {
-               loc += sizeof(BOX2DFLOAT4);
-       }
-
-       return get_int32(loc);
-}
-
-/*
- * Make a PG_LWGEOM object from a WKB binary representation.
- * Currently unoptimized as it will convert WKB to HEXWKB first.
- */
-PG_LWGEOM *
-pglwgeom_from_ewkb(uchar *ewkb, size_t ewkblen)
-{
-       PG_LWGEOM *ret;
-       char *hexewkb;
-       size_t hexewkblen = ewkblen*2;
-       int i;
-
-       hexewkb = lwalloc(hexewkblen+1);
-       for (i=0; i<ewkblen; i++)
-       {
-               deparse_hex(ewkb[i], &hexewkb[i*2]);
-       }
-       hexewkb[hexewkblen] = '\0';
-
-       ret = (PG_LWGEOM *)parse_lwgeom_wkt(hexewkb);
-
-       lwfree(hexewkb);
-
-       return ret;
-}
-
-/*
- * Return the EWKB (binary) representation for a PG_LWGEOM.
- */
-char *
-pglwgeom_to_ewkb(PG_LWGEOM *geom, char byteorder, size_t *outsize)
-{
-       uchar *srl = &(geom->type);
-       return unparse_WKB(srl, lwalloc, lwfree,
-               byteorder, outsize, 0);
-}
-
-// Set the SRID of a PG_LWGEOM
-// Returns a newly allocated PG_LWGEOM object.
-// Allocation will be done using the lwalloc.
-PG_LWGEOM *
-pglwgeom_setSRID(PG_LWGEOM *lwgeom, int32 newSRID)
-{
-       uchar type = lwgeom->type;
-       int bbox_offset=0; //0=no bbox, otherwise sizeof(BOX2DFLOAT4)
-       int len,len_new,len_left;
-       PG_LWGEOM *result;
-       uchar *loc_new, *loc_old;
-
-       if (lwgeom_hasBBOX(type))
-               bbox_offset = sizeof(BOX2DFLOAT4);
-
-       len = lwgeom->size;
-
-       if (lwgeom_hasSRID(type))
-       {
-               if ( newSRID != -1 ) {
-                       //we create a new one and copy the SRID in
-                       result = lwalloc(len);
-                       memcpy(result, lwgeom, len);
-                       memcpy(result->data+bbox_offset, &newSRID,4);
-               } else {
-                       //we create a new one dropping the SRID
-                       result = lwalloc(len-4);
-                       result->size = len-4;
-                       result->type = lwgeom_makeType_full(
-                               TYPE_HASZ(type), TYPE_HASM(type),
-                               0, lwgeom_getType(type),
-                               lwgeom_hasBBOX(type));
-                       loc_new = result->data;
-                       loc_old = lwgeom->data;
-                       len_left = len-4-1;
-
-                       // handle bbox (if there)
-                       if (lwgeom_hasBBOX(type))
-                       {
-                               memcpy(loc_new, loc_old, sizeof(BOX2DFLOAT4));
-                               loc_new += sizeof(BOX2DFLOAT4);
-                               loc_old += sizeof(BOX2DFLOAT4);
-                               len_left -= sizeof(BOX2DFLOAT4);
-                       }
-
-                       // skip SRID, copy the remaining
-                       loc_old += 4;
-                       len_left -= 4;
-                       memcpy(loc_new, loc_old, len_left);
-
-               }
-
-       }
-       else 
-       {
-               // just copy input, already w/out a SRID
-               if ( newSRID == -1 ) {
-                       result = lwalloc(len);
-                       memcpy(result, lwgeom, len);
-               }
-               // need to add one
-               else {
-                       len_new = len + 4;//+4 for SRID
-                       result = lwalloc(len_new);
-                       memcpy(result, &len_new, 4); // size copy in
-                       result->type = lwgeom_makeType_full(
-                               TYPE_HASZ(type), TYPE_HASM(type),
-                               1, lwgeom_getType(type),lwgeom_hasBBOX(type));
-
-                       loc_new = result->data;
-                       loc_old = lwgeom->data;
-
-                       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);
-               }
-       }
-       return result;
-}
-
-
 char
 ptarray_isccw(const POINTARRAY *pa)
 {
@@ -1986,7 +1836,7 @@ box2d_union_p(BOX2DFLOAT4 *b1, BOX2DFLOAT4 *b2, BOX2DFLOAT4 *ubox)
        return 1;
 }
 
-const uchar *
+const char *
 lwgeom_typeflags(uchar type)
 {
        static char flags[5];
@@ -2122,117 +1972,18 @@ parse_hex(char *str)
 //             -> mystr[0] = 'F' and mystr[1] = 'F'
 // no error checking done
 void
-deparse_hex(uchar str, uchar *result)
+deparse_hex(uchar str, char *result)
 {
        int     input_high;
        int  input_low;
+       static char outchr[]={"0123456789ABCDEF" };
 
        input_high = (str>>4);
        input_low = (str & 0x0F);
 
-       switch (input_high)
-       {
-               case 0:
-                       result[0] = '0';
-                       break;
-               case 1:
-                       result[0] = '1';
-                       break;
-               case 2:
-                       result[0] = '2';
-                       break;
-               case 3:
-                       result[0] = '3';
-                       break;
-               case 4:
-                       result[0] = '4';
-                       break;
-               case 5:
-                       result[0] = '5';
-                       break;
-               case 6:
-                       result[0] = '6';
-                       break;
-               case 7:
-                       result[0] = '7';
-                       break;
-               case 8:
-                       result[0] = '8';
-                       break;
-               case 9:
-                       result[0] = '9';
-                       break;
-               case 10:
-                       result[0] = 'A';
-                       break;
-               case 11:
-                       result[0] = 'B';
-                       break;
-               case 12:
-                       result[0] = 'C';
-                       break;
-               case 13:
-                       result[0] = 'D';
-                       break;
-               case 14:
-                       result[0] = 'E';
-                       break;
-               case 15:
-                       result[0] = 'F';
-                       break;
-       }
+       result[0] = outchr[input_high];
+       result[1] = outchr[input_low];
 
-       switch (input_low)
-       {
-               case 0:
-                       result[1] = '0';
-                       break;
-               case 1:
-                       result[1] = '1';
-                       break;
-               case 2:
-                       result[1] = '2';
-                       break;
-               case 3:
-                       result[1] = '3';
-                       break;
-               case 4:
-                       result[1] = '4';
-                       break;
-               case 5:
-                       result[1] = '5';
-                       break;
-               case 6:
-                       result[1] = '6';
-                       break;
-               case 7:
-                       result[1] = '7';
-                       break;
-               case 8:
-                       result[1] = '8';
-                       break;
-               case 9:
-                       result[1] = '9';
-                       break;
-               case 10:
-                       result[1] = 'A';
-                       break;
-               case 11:
-                       result[1] = 'B';
-                       break;
-               case 12:
-                       result[1] = 'C';
-                       break;
-               case 13:
-                       result[1] = 'D';
-                       break;
-               case 14:
-                       result[1] = 'E';
-                       break;
-               case 15:
-                       result[1] = 'F';
-                       break;
-       }
 }
 
 uchar *
index 3f1baceaa410938fc7a54b2507d60ee6c019f8b6..d8eff064e9de56ad58ce61157f97d2d1a2f6d31f 100644 (file)
 #include "lwgeom_pg.h"
 #include "liblwgeom.h"
 
-// External functions (what's again the reason for using explicit hex form ?)
-extern void deparse_hex(uchar str, uchar *result);
-extern uchar parse_hex(char *str);
-
 // Internal funcs
 void swap_char(char *a,char *b);
 void flip_endian_double(char *d);
index ce33487d2de671af83a66a01ee34091ee65fc60d..d7f787ce82adea6b55a0212af8d6094585696b56 100644 (file)
@@ -25,7 +25,7 @@
 #include "liblwgeom.h"
 #include "lwgeom_pg.h"
 
-//#define DEBUG_GEOMETRY_STATS 1
+/*#define DEBUG_GEOMETRY_STATS 1*/
 
 
 #if USE_VERSION >= 80
 
 typedef struct GEOM_STATS_T
 {
-       // cols * rows = total boxes in grid
+       /* cols * rows = total boxes in grid */
        float4 cols;
        float4 rows;
        
-       // average bounding box area of not-null features 
+       /* average bounding box area of not-null features */
        float4 avgFeatureArea;
 
-       // average number of histogram cells
-       // covered by the sample not-null features
+       /*
+        * average number of histogram cells
+        * covered by the sample not-null features
+        */
        float4 avgFeatureCells;
 
-       // BOX of area
+       /* BOX of area */
        float4 xmin,ymin, xmax, ymax;
-       // variable length # of floats for histogram
+
+       /*
+        * variable length # of floats for histogram
+        */
        float4 value[1];
 } GEOM_STATS;
 
 static float8 estimate_selectivity(BOX2DFLOAT4 *box, GEOM_STATS *geomstats);
 
-#endif // USE_VERSION >= 80
+#endif /* USE_VERSION >= 80 */
 
 #define SHOW_DIGS_DOUBLE 15
 #define MAX_DIGS_DOUBLE (SHOW_DIGS_DOUBLE + 6 + 1 + 3 +1)
@@ -99,25 +104,26 @@ static float8 estimate_selectivity(BOX2DFLOAT4 *box, GEOM_STATS *geomstats);
  */
 #define REALLY_DO_JOINSEL 1
 
-// --------------------------------------------
-// lwhistogram2d type
-
-// 2d histogram is a bounding box with a bunch of cells in it.
-// The cells will have width (xmax-xmin)/boxesPerSide
-//                 and height(ymax-ymin)/boxesPerSide
-// The first box is the ll corner's box, the send is directly to the right
-//   (row-major).
-//
-//  Size of structure is:
-//        4 (size) + 32 (box) + 4 (boxesPerSide) +
-//                 boxesPerSide*boxesPerSide*4 (data)
+/* --------------------------------------------
+ * lwhistogram2d type
+ *
+ * 2d histogram is a bounding box with a bunch of cells in it.
+ * The cells will have width (xmax-xmin)/boxesPerSide
+ *                 and height(ymax-ymin)/boxesPerSide
+ * The first box is the ll corner's box, the send is directly to the right
+ *   (row-major).
+ *
+ *  Size of structure is:
+ *        4 (size) + 32 (box) + 4 (boxesPerSide) +
+ *                 boxesPerSide*boxesPerSide*4 (data)
+ */
 typedef struct histotag
 {
-       int32 size;       // postgres variable-length type requirement
-       int boxesPerSide; // boxesPerSide * boxesPerSide = total boxes in grid
-       double avgFeatureArea; // average bbox area of features in this histogram
-       double xmin,ymin, xmax, ymax; // BOX of area
-       unsigned int value[1]; // variable length # of ints for histogram
+       int32 size;       /* postgres variable-length type requirement */
+       int boxesPerSide; /* boxesPerSide * boxesPerSide = total boxes in grid */
+       double avgFeatureArea; /* average bbox area of features in this histogram */
+       double xmin,ymin, xmax, ymax; /* BOX of area */
+       unsigned int value[1]; /* variable length # of ints for histogram */
 } LWHISTOGRAM2D;
 
 Datum lwhistogram2d_in(PG_FUNCTION_ARGS);
@@ -133,10 +139,12 @@ Datum LWGEOM_estimated_extent(PG_FUNCTION_ARGS);
 Datum LWGEOM_analyze(PG_FUNCTION_ARGS);
 #endif
 
-//text form of LWHISTOGRAM2D is:
-// 'HISTOGRAM2D(xmin,ymin,xmax,ymax,boxesPerSide;value[0],value[1],...')
-//    note the ";" in the middle (for easy parsing)
-//  I dont expect anyone to actually create one by hand
+/* 
+ * text form of LWHISTOGRAM2D is:
+ * 'HISTOGRAM2D(xmin,ymin,xmax,ymax,boxesPerSide;value[0],value[1],...')
+ *    note the ";" in the middle (for easy parsing)
+ *  I dont expect anyone to actually create one by hand
+ */
 PG_FUNCTION_INFO_V1(lwhistogram2d_in);
 Datum lwhistogram2d_in(PG_FUNCTION_ARGS)
 {
@@ -149,7 +157,7 @@ Datum lwhistogram2d_in(PG_FUNCTION_ARGS)
        char *str2,*str3;
        long datum;
 
-       //elog(NOTICE, "lwhistogram2d parser called");
+       /*elog(NOTICE, "lwhistogram2d parser called");*/
 
        int t;
 
@@ -195,15 +203,15 @@ Datum lwhistogram2d_in(PG_FUNCTION_ARGS)
 
        for (t=0;t<boxesPerSide*boxesPerSide;t++)
        {
-               datum = strtol(str2,&str3,10); // str2=start of int, str3=end of int, base 10
-               // str3 points to "," or ")"
+               datum = strtol(str2,&str3,10); /* str2=start of int, str3=end of int, base 10 */
+               /* str3 points to "," or ")" */
                if (str3[0] ==0)
                {
                        elog(ERROR, "lwhistogram2d parser - histogram values prematurely ended!\n");
                        PG_RETURN_NULL() ;
                }
                histo->value[t] = (unsigned int) datum;
-               str2= str3+1; //move past the "," or ")"
+               str2= str3+1; /* move past the "," or ")" */
        }
        histo->xmin = xmin;
        histo->xmax = xmax;
@@ -217,7 +225,7 @@ Datum lwhistogram2d_in(PG_FUNCTION_ARGS)
 
 
 
-//text version
+/* text version */
 PG_FUNCTION_INFO_V1(lwhistogram2d_out);
 Datum lwhistogram2d_out(PG_FUNCTION_ARGS)
 {
@@ -230,13 +238,13 @@ Datum lwhistogram2d_out(PG_FUNCTION_ARGS)
        size = 26+6*MAX_DIGS_DOUBLE + histo->boxesPerSide*histo->boxesPerSide* (MAX_DIGS_DOUBLE+1);
        result = palloc(size);
 
-       //elog(NOTICE, "result@%x", result);
-
        sprintf(result,"HISTOGRAM2D(%.15g,%.15g,%.15g,%.15g,%i,%.15g;",
                histo->xmin,histo->ymin,histo->xmax,histo->ymax,histo->boxesPerSide,histo->avgFeatureArea );
 
-       //elog(NOTICE,"so far: %s",result);
-       //elog(NOTICE,"buffsize=%i, size=%i",size,histo->size);
+#if PGIS_DEBUG
+       elog(NOTICE,"so far: %s",result);
+       elog(NOTICE,"buffsize=%i, size=%i",size,histo->size);
+#endif
 
        for (t=0;t<histo->boxesPerSide*histo->boxesPerSide;t++)
        {
@@ -246,19 +254,21 @@ Datum lwhistogram2d_out(PG_FUNCTION_ARGS)
        }
 
        strcat(result,")");
-       //elog(NOTICE,"about to return string (len=%i): -%s-",strlen(result),result);
-       //elog(NOTICE, "result@%x", result);
+#if PGIS_DEBUG
+       elog(NOTICE,"about to return string (len=%i): -%s-",strlen(result),result);
+       elog(NOTICE, "result@%x", result);
+#endif
 
 
        PG_RETURN_CSTRING(result);
 }
 
-//create_lwhistogram2d(BOX2D, boxesPerSide)
-// returns a histgram with 0s in all the boxes.
+/*create_lwhistogram2d(BOX2D, boxesPerSide)*/
+/* returns a histgram with 0s in all the boxes.*/
 PG_FUNCTION_INFO_V1(create_lwhistogram2d);
 Datum create_lwhistogram2d(PG_FUNCTION_ARGS)
 {
-       //BOX3D  *bbox = (BOX3D *) PG_GETARG_POINTER(0);
+       /*BOX3D  *bbox = (BOX3D *) PG_GETARG_POINTER(0);*/
        BOX2DFLOAT4 *bbox = (BOX2DFLOAT4 *)PG_GETARG_DATUM(0);
        int32   boxesPerSide = PG_GETARG_INT32(1);
        LWHISTOGRAM2D *histo;
@@ -289,14 +299,16 @@ Datum create_lwhistogram2d(PG_FUNCTION_ARGS)
                histo->value[t] = 0;
        }
 
-       //elog(NOTICE,"create_lwhistogram2d returning");
+       /*elog(NOTICE,"create_lwhistogram2d returning");*/
 
        PG_RETURN_POINTER(histo);
 }
 
-//build_histogram2d (LWHISTOGRAM2D, tablename, columnname)
-// executes the SPI 'SELECT box3d(columnname) FROM tablename'
-// and sticks all the results in the histogram
+/* 
+ * build_histogram2d (LWHISTOGRAM2D, tablename, columnname)
+ * executes the SPI 'SELECT box3d(columnname) FROM tablename'
+ * and sticks all the results in the histogram
+ */
 PG_FUNCTION_INFO_V1(build_lwhistogram2d);
 Datum build_lwhistogram2d(PG_FUNCTION_ARGS)
 {
@@ -326,12 +338,12 @@ Datum build_lwhistogram2d(PG_FUNCTION_ARGS)
        int    sum_area_numb_new =0;
        int bump=0;
 
-       int tuplimit = 500000;  // No. of tuples returned on each cursor fetch
+       int tuplimit = 500000;  /* No. of tuples returned on each cursor fetch */
        bool moredata;
        void *SPIplan;
        void *SPIportal;
 
-       //elog(NOTICE,"build_lwhistogram2d called");
+       /*elog(NOTICE,"build_lwhistogram2d called");*/
 
        xmin = histo->xmin;
        ymin = histo->ymin;
@@ -367,8 +379,10 @@ Datum build_lwhistogram2d(PG_FUNCTION_ARGS)
        columnname = DatumGetCString(DirectFunctionCall1(textout,
                PointerGetDatum(PG_GETARG_DATUM(2))));
 
-       //elog(NOTICE,"Start build_histogram2d with %i items already existing", sum_area_numb);
-       //elog(NOTICE,"table=\"%s\", column = \"%s\"", tablename, columnname);
+#if DEBUG_GEOMETRY_STATS
+       elog(NOTICE,"Start build_histogram2d with %i items already existing", sum_area_numb);
+       elog(NOTICE,"table=\"%s\", column = \"%s\"", tablename, columnname);
+#endif
 
 
        SPIcode = SPI_connect();
@@ -381,7 +395,7 @@ Datum build_lwhistogram2d(PG_FUNCTION_ARGS)
 
 
        sprintf(sql,"SELECT box2d(\"%s\") FROM \"%s\"",columnname,tablename);
-       //elog(NOTICE,"executing %s",sql);
+       /*elog(NOTICE,"executing %s",sql);*/
 
        SPIplan = SPI_prepare(sql, 0, NULL);
        if (SPIplan  == NULL)
@@ -406,11 +420,16 @@ Datum build_lwhistogram2d(PG_FUNCTION_ARGS)
        while (moredata==TRUE)
        {
 
-               //elog(NOTICE,"about to fetch...");
+#if DEBUG_GEOMETRY_STATS
+               elog(NOTICE,"about to fetch...");
+#endif
                SPI_cursor_fetch(SPIportal, TRUE, tuplimit);
 
                ntuples = SPI_processed;
-               //elog(NOTICE,"processing %d records", ntuples);
+
+#if DEBUG_GEOMETRY_STATS
+               elog(NOTICE,"processing %d records", ntuples);
+#endif
 
                if (ntuples > 0) {
 
@@ -426,7 +445,6 @@ Datum build_lwhistogram2d(PG_FUNCTION_ARGS)
                                if (!(isnull))
                                {
                                        box = (BOX2DFLOAT4 *)DatumGetPointer(datum);
-                                       //box_area = (box->high.x-box->low.x)*(box->high.y-box->low.y);
                                        box_area = (box->xmax-box->xmin)*(box->ymax-box->ymin);
 
                                        sum_area_new += box_area;
@@ -435,78 +453,86 @@ Datum build_lwhistogram2d(PG_FUNCTION_ARGS)
                                        if (box_area > cell_area )
                                                box_area = cell_area;
                                        if (box_area<0)
-                                               box_area =0;  // for precision!
+                                               box_area =0;  /* for precision! */
 
-                                       //check to see which boxes this intersects
-                                       //x_idx_min = (box->low->x-xmin)/(xmax-xmin)*histo->boxesPerSide;
+                                       /* check to see which boxes this intersects */
                                        x_idx_min = (box->xmin-xmin)/(xmax-xmin)*histo->boxesPerSide;
                                        if (x_idx_min <0)
                                                x_idx_min = 0;
                                        if (x_idx_min >= histo->boxesPerSide)
                                                x_idx_min = histo->boxesPerSide-1;
-                                       //y_idx_min = (box->low.y-ymin)/(ymax-ymin)*histo->boxesPerSide;
                                        y_idx_min = (box->ymin-ymin)/(ymax-ymin)*histo->boxesPerSide;
                                        if (y_idx_min <0)
                                                y_idx_min = 0;
                                        if (y_idx_min >= histo->boxesPerSide)
                                                y_idx_min = histo->boxesPerSide-1;
 
-                                       //x_idx_max = (box->high.x-xmin)/(xmax-xmin)*histo->boxesPerSide;
                                        x_idx_max = (box->xmax-xmin)/(xmax-xmin)*histo->boxesPerSide;
                                        if (x_idx_max <0)
                                                x_idx_max = 0;
                                        if (x_idx_max >= histo->boxesPerSide)
                                                x_idx_max = histo->boxesPerSide-1;
-                                       //y_idx_max = (box->high.y-ymin)/(ymax-ymin)*histo->boxesPerSide ;
                                        y_idx_max = (box->ymax-ymin)/(ymax-ymin)*histo->boxesPerSide ;
                                        if (y_idx_max <0)
                                                y_idx_max = 0;
                                        if (y_idx_max >= histo->boxesPerSide)
                                                y_idx_max = histo->boxesPerSide-1;
 
-                                       //the {x,y}_idx_{min,max} define the grid squares that the box intersects
-                                       // if the area of the intersect between the box and the grid square > 5% of
+                                       /*
+                                        * the {x,y}_idx_{min,max} define the grid squares that the box intersects
+                                        * if the area of the intersect between the box and the grid square > 5% of
+                                        */
+
+#if DEBUG_GEOMETRY_STATS
+       elog(NOTICE,"box is : (%.15g,%.15g to %.15g,%.15g)",box->low.x,box->low.y, box->high.x, box->high.y);
+       elog(NOTICE,"        search is in x: %i to %i   y: %i to %i",x_idx_min, x_idx_max, y_idx_min,y_idx_max);
+#endif
 
-       //elog(NOTICE,"box is : (%.15g,%.15g to %.15g,%.15g)",box->low.x,box->low.y, box->high.x, box->high.y);
-//elog(NOTICE,"        search is in x: %i to %i   y: %i to %i",x_idx_min, x_idx_max, y_idx_min,y_idx_max);
                                        for (y= y_idx_min; y<=y_idx_max;y++)
                                        {
                                                for (x=x_idx_min;x<= x_idx_max;x++)
                                                {
-                                                       intersect_x = LW_MIN(box->xmax, xmin+ (x+1) * (xmax-xmin)/histo->boxesPerSide ) - LW_MAX(box->xmin, xmin+ x*(xmax-xmin)/histo->boxesPerSide ) ;
+                                                       intersect_x = LW_MIN(box->xmax, xmin+ (x+1) * (xmax-xmin)/histo->boxesPerSide ) - LW_MAX(box->xmin, xmin + x*(xmax-xmin)/histo->boxesPerSide ) ;
+
                                                        intersect_y = LW_MIN(box->ymax, ymin+ (y+1) * (ymax-ymin)/histo->boxesPerSide ) - LW_MAX(box->ymin, ymin+ y*(ymax-ymin)/histo->boxesPerSide ) ;
 
-                                                       // for a point, intersect_x=0, intersect_y=0, box_area =0
-//elog(NOTICE,"x=%i,y=%i, intersect_x= %.15g, intersect_y = %.15g",x,y,intersect_x,intersect_y);
+                                                       /* for a point, intersect_x=0, intersect_y=0, box_area =0*/
+#if DEBUG_GEOMETRY_STATS
+elog(NOTICE,"x=%i,y=%i, intersect_x= %.15g, intersect_y = %.15g",x,y,intersect_x,intersect_y);
+#endif
                                                        if ( (intersect_x>=0) && (intersect_y>=0) )
                                                        {
                                                                area_intersect = intersect_x*intersect_y;
                                                                if (area_intersect >= box_area*0.05)
                                                                {
-//elog(NOTICE,"bump");
+#if DEBUG_GEOMETRY_STATS
+elog(NOTICE,"bump");
+#endif
                                                                        bump++;
                                                                        result->value[x+y*histo->boxesPerSide]++;
                                                                }
                                                        }
                                                }
-                                       } // End of y
+                                       } /* End of y */
 
-                               } // End isnull
+                               } /* End isnull */
 
-                       } // End of for loop
+                       } /* End of for loop */
 
-                       // Free all the results after each fetch, otherwise all tuples stay
-                       // in memory until the end of the table...
+                       /*
+                        * Free all the results after each fetch, otherwise all tuples stay
+                        * in memory until the end of the table...
+                        */
                        SPI_freetuptable(tuptable);
 
                } else {
                                moredata = FALSE;
-               } // End of if ntuples > 0
+               } /* End of if ntuples > 0 */
 
-       } // End of while loop
+       } /* End of while loop */
 
 
-       // Close the cursor
+       /* Close the cursor */
        SPI_cursor_close(SPIportal);
 
        SPIcode =SPI_finish();
@@ -516,31 +542,38 @@ Datum build_lwhistogram2d(PG_FUNCTION_ARGS)
                PG_RETURN_NULL() ;
        }
 
-       //elog(NOTICE,"finishing up build_histogram2d ");
+#if DEBUG_GEOMETRY_STATS
+       elog(NOTICE,"finishing up build_histogram2d ");
+#endif
 
-       //pfree(tablename);
-       //pfree(columnname);
+       /*pfree(tablename);*/
+       /*pfree(columnname);*/
 
        total = 0;
        for(t=0;t<histo->boxesPerSide*histo->boxesPerSide;t++)
        {
                total+=result->value[t];
        }
-       //elog(NOTICE ,"histogram finishes with %i items in it - acutally added %i rows and %i bumps\n",total,sum_area_numb_new,bump);
-       //elog(NOTICE,"done build_histogram2d ");
+
+#if DEBUG_GEOMETRY_STATS
+       elog(NOTICE ,"histogram finishes with %i items in it - acutally added %i rows and %i bumps\n",total,sum_area_numb_new,bump);
+       elog(NOTICE,"done build_histogram2d ");
+#endif
 
 
-       //re-calculate statistics on avg bbox size
+       /* re-calculate statistics on avg bbox size */
        if (sum_area_numb_new >0)
                result->avgFeatureArea = (sum_area_new+sum_area)/((double)(sum_area_numb_new+sum_area_numb));
 
        PG_RETURN_POINTER(result) ;
 }
 
-//explode_lwhistogram2d(histogram2d, tablename::text)
-// executes CREATE TABLE tablename (the_geom geometry, id int, hits int, percent float)
-// then populates it
-//  DOES NOT UPDATE GEOMETRY_COLUMNS
+/*
+ * explode_lwhistogram2d(histogram2d, tablename::text)
+ * executes CREATE TABLE tablename (the_geom geometry, id int, hits int, percent float)
+ * then populates it
+ * DOES NOT UPDATE GEOMETRY_COLUMNS
+ */
 PG_FUNCTION_INFO_V1(explode_lwhistogram2d);
 Datum explode_lwhistogram2d(PG_FUNCTION_ARGS)
 {
@@ -578,7 +611,7 @@ Datum explode_lwhistogram2d(PG_FUNCTION_ARGS)
 
                sprintf(sql,"CREATE TABLE %s (the_geom geometry, id int, hits int, percent float)",tablename);
 
-               SPIcode = SPI_exec(sql, 2147483640 ); // max signed int32
+               SPIcode = SPI_exec(sql, 2147483640 ); /* max signed int32 */
 
                        if (SPIcode  != SPI_OK_UTILITY )
                        {
@@ -599,9 +632,8 @@ Datum explode_lwhistogram2d(PG_FUNCTION_ARGS)
                                                histo->xmin + x*cellx,     histo->ymin+y*celly
                                                );
                                                sprintf(sql,"INSERT INTO %s VALUES('%s'::geometry,%i,%i,%.15g)",tablename,geom,t,histo->value[t],histo->value[t]/((double)total)*100.0);
-//elog(NOTICE,"SQL:%s",sql);
                                                t++;
-                                               SPIcode = SPI_exec(sql, 2147483640 ); // max signed int32
+                                               SPIcode = SPI_exec(sql, 2147483640 ); /* max signed int32 */
                                                if (SPIcode  != SPI_OK_INSERT )
                                                {
                                                                elog(ERROR,"explode_histogram2d: couldnt insert into");
@@ -620,16 +652,17 @@ Datum explode_lwhistogram2d(PG_FUNCTION_ARGS)
        PG_RETURN_POINTER(histo) ;
 }
 
-//estimate_histogram2d(histogram2d, box2d)
-// returns a % estimate of the # of features that will be returned by that box query
-//
-//For each grid cell that intersects the query box
-//       Calculate area of intersection (AOI)
-//    IF AOI < avgFeatureArea THEN set AOI = avgFeatureArea
-//    SUM AOI/area-of-cell*value-of-cell
-//
-// change : instead of avgFeatureArea, use avgFeatureArea or 10% of a grid cell (whichever is smaller)
-
+/* 
+ * estimate_histogram2d(histogram2d, box2d)
+ * returns a % estimate of the # of features that will be returned by that box query
+ *
+ * For each grid cell that intersects the query box
+ *       Calculate area of intersection (AOI)
+ *    IF AOI < avgFeatureArea THEN set AOI = avgFeatureArea
+ *    SUM AOI/area-of-cell*value-of-cell
+ *
+ * change : instead of avgFeatureArea, use avgFeatureArea or 10% of a grid cell (whichever is smaller)
+ */
 PG_FUNCTION_INFO_V1(estimate_lwhistogram2d);
 Datum estimate_lwhistogram2d(PG_FUNCTION_ARGS)
 {
@@ -662,16 +695,18 @@ Datum estimate_lwhistogram2d(PG_FUNCTION_ARGS)
        }
 
 
-//elog(NOTICE,"start estimate_histogram2d: ");
-//elog(NOTICE,"box is : (%.15g,%.15g to %.15g,%.15g)",box->low.x,box->low.y, box->high.x, box->high.y);
+#if DEBUG_GEOMETRY_STATS
+elog(NOTICE,"start estimate_histogram2d: ");
+elog(NOTICE,"box is : (%.15g,%.15g to %.15g,%.15g)",box->low.x,box->low.y, box->high.x, box->high.y);
+#endif
 
-       //box_area = (box->high.x-box->low.x)*(box->high.y-box->low.y);
        box_area = (box->xmax-box->xmin)*(box->ymax-box->ymin);
 
-       if (box_area<0) box_area = 0;  // for precision!
+       if (box_area<0) box_area = 0;  /* for precision! */
 
-       //check to see which boxes this intersects
-       //x_idx_min = (box->low.x-xmin)/(xmax-xmin)*histo->boxesPerSide;
+       /*
+        * check to see which boxes this intersects
+        */
        x_idx_min = (box->xmin-xmin)/(xmax-xmin)*histo->boxesPerSide;
        if (x_idx_min <0) x_idx_min = 0;
        if (x_idx_min >= histo->boxesPerSide)
@@ -690,10 +725,12 @@ Datum estimate_lwhistogram2d(PG_FUNCTION_ARGS)
        if (y_idx_max >= histo->boxesPerSide)
                y_idx_max = histo->boxesPerSide-1;
 
-       //the {x,y}_idx_{min,max} define the grid squares that the box intersects
+       /* The {x,y}_idx_{min,max} define the grid squares that the box intersects */
 
+#if DEBUG_GEOMETRY_STATS
+elog(NOTICE," search is in x: %i to %i   y: %i to %i",x_idx_min, x_idx_max, y_idx_min,y_idx_max);
+#endif
 
-//elog(NOTICE," search is in x: %i to %i   y: %i to %i",x_idx_min, x_idx_max, y_idx_min,y_idx_max);
        for (y= y_idx_min; y<=y_idx_max;y++)
        {
                for (x=x_idx_min;x<= x_idx_max;x++)
@@ -701,8 +738,8 @@ Datum estimate_lwhistogram2d(PG_FUNCTION_ARGS)
                        intersect_x = LW_MIN(box->xmax, xmin+ (x+1) * (xmax-xmin)/histo->boxesPerSide ) - LW_MAX(box->xmin, xmin+ x*(xmax-xmin)/histo->boxesPerSide ) ;
                        intersect_y = LW_MIN(box->ymax, ymin+ (y+1) * (ymax-ymin)/histo->boxesPerSide ) - LW_MAX(box->ymin, ymin+ y*(ymax-ymin)/histo->boxesPerSide ) ;
 
-// for a point, intersect_x=0, intersect_y=0, box_area =0
-//elog(NOTICE,"x=%i,y=%i, intersect_x= %.15g, intersect_y = %.15g",x,y,intersect_x,intersect_y);
+/* for a point, intersect_x=0, intersect_y=0, box_area =0 */
+/* elog(NOTICE,"x=%i,y=%i, intersect_x= %.15g, intersect_y = %.15g",x,y,intersect_x,intersect_y); */
                        if ( (intersect_x>=0) && (intersect_y>=0) )
                        {
                                AOI = intersect_x*intersect_y;
@@ -728,8 +765,10 @@ Datum estimate_lwhistogram2d(PG_FUNCTION_ARGS)
 
 
 #if ! REALLY_DO_JOINSEL || USE_VERSION < 80
-// JOIN selectivity in the GiST && operator
-// for all PG versions
+/*
+ * JOIN selectivity in the GiST && operator
+ * for all PG versions
+ */
 PG_FUNCTION_INFO_V1(LWGEOM_gist_joinsel);
 Datum LWGEOM_gist_joinsel(PG_FUNCTION_ARGS)
 {
@@ -740,7 +779,7 @@ Datum LWGEOM_gist_joinsel(PG_FUNCTION_ARGS)
        PG_RETURN_FLOAT8(DEFAULT_GEOMETRY_JOINSEL);
 }
 
-#else // REALLY_DO_JOINSEL && USE_VERSION >= 80
+#else /* REALLY_DO_JOINSEL && USE_VERSION >= 80 */
 
 int calculate_column_intersection(BOX2DFLOAT4 *search_box, GEOM_STATS *geomstats1, GEOM_STATS *geomstats2);
 
@@ -770,8 +809,10 @@ calculate_column_intersection(BOX2DFLOAT4 *search_box, GEOM_STATS *geomstats1, G
        return -1;
 }
 
-// JOIN selectivity in the GiST && operator
-// for all PG versions
+/*
+ * JOIN selectivity in the GiST && operator
+ * for all PG versions
+ */
 PG_FUNCTION_INFO_V1(LWGEOM_gist_joinsel);
 Datum LWGEOM_gist_joinsel(PG_FUNCTION_ARGS)
 {
@@ -780,7 +821,7 @@ Datum LWGEOM_gist_joinsel(PG_FUNCTION_ARGS)
 #else
        PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
 #endif
-       //Oid operator = PG_GETARG_OID(1);
+       /* Oid operator = PG_GETARG_OID(1); */
        List *args = (List *) PG_GETARG_POINTER(2);
        JoinType jointype = (JoinType) PG_GETARG_INT16(3);
 
@@ -999,7 +1040,7 @@ Datum LWGEOM_gist_joinsel(PG_FUNCTION_ARGS)
        PG_RETURN_FLOAT8(rows_returned / total_tuples);
 }
 
-#endif // REALLY_DO_JOINSEL
+#endif /* REALLY_DO_JOINSEL */
 
 /**************************** FROM POSTGIS ****************/
 
@@ -1065,7 +1106,7 @@ get_restriction_var(List *args, int varRelid, Var **var,
        return true;
 }
 
-//restriction in the GiST && operator
+/* restriction in the GiST && operator */
 PG_FUNCTION_INFO_V1(LWGEOM_gist_sel);
 Datum LWGEOM_gist_sel(PG_FUNCTION_ARGS)
 {
@@ -1095,57 +1136,72 @@ Datum LWGEOM_gist_sel(PG_FUNCTION_ARGS)
        PG_RETURN_FLOAT8(DEFAULT_GEOMETRY_SEL);
 #endif
 
-       //elog(NOTICE,"LWGEOM_gist_sel was called");
+#if DEBUG_GEOMETRY_STATS
+       elog(NOTICE,"LWGEOM_gist_sel was called");
+#endif
 
        if (!get_restriction_var(args, varRelid, &var, &other, &varonleft))
        {
-               //elog(NOTICE,"get_restriction_var FAILED -returning early");
+#if DEBUG_GEOMETRY_STATS
+               elog(NOTICE,"get_restriction_var FAILED -returning early");
+#endif
                PG_RETURN_FLOAT8(DEFAULT_GEOMETRY_SEL);
        }
 
        relid = getrelid(var->varno, root->rtable);
        if (relid == InvalidOid)
        {
-               //elog(NOTICE,"getrelid FAILED (invalid oid) -returning early");
+#if DEBUG_GEOMETRY_STATS
+               elog(NOTICE,"getrelid FAILED (invalid oid) -returning early");
+#endif
                PG_RETURN_FLOAT8(DEFAULT_GEOMETRY_SEL);
        }
 
-       //elog(NOTICE,"operator's oid = %i (this should be GEOMETRY && GEOMETRY)",operator);
-       //elog(NOTICE,"relations' oid = %i (this should be the relation that the && is working on) ",relid);
-       //elog(NOTICE,"varatt oid = %i (basically relations column #) ",var->varattno);
+#if DEBUG_GEOMETRY_STATS
+       elog(NOTICE,"operator's oid = %i (this should be GEOMETRY && GEOMETRY)",operator);
+       elog(NOTICE,"relations' oid = %i (this should be the relation that the && is working on) ",relid);
+       elog(NOTICE,"varatt oid = %i (basically relations column #) ",var->varattno);
+#endif
 
 
        if (IsA(other, Const) &&((Const *) other)->constisnull)
        {
-               //elog(NOTICE,"other operand of && is NULL - returning early");
+#if DEBUG_GEOMETRY_STATS
+               elog(NOTICE,"other operand of && is NULL - returning early");
+#endif
                PG_RETURN_FLOAT8(DEFAULT_GEOMETRY_SEL);
        }
 
        if (IsA(other, Const))
        {
-               //elog(NOTICE,"The other side of the && is a constant with type (oid) = %i and length %i.  This should be GEOMETRY with length -1 (variable length)",((Const*)other)->consttype,((Const*)other)->constlen);
+#if DEBUG_GEOMETRY_STATS
+               elog(NOTICE,"The other side of the && is a constant with type (oid) = %i and length %i.  This should be GEOMETRY with length -1 (variable length)",((Const*)other)->consttype,((Const*)other)->constlen);
+#endif
 
        }
        else
        {
-               //elog(NOTICE,"the other side of && isnt a constant - returning early");
+#if DEBUG_GEOMETRY_STATS
+               elog(NOTICE,"the other side of && isnt a constant - returning early");
+#endif
                PG_RETURN_FLOAT8(DEFAULT_GEOMETRY_SEL);
        }
 
-       //get the BOX thats being searched in
+       /* get the BOX thats being searched in */
        in = (char *)PG_DETOAST_DATUM( ((Const*)other)->constvalue );
 
-       //search_box = convert_box3d_to_box(&in->bvol);
        if ( ! getbox2d_p(in+4, &search_box) )
        {
-               // empty geom
+               /* empty geom */
 #if DEBUG_GEOMETRY_STATS 
                elog(NOTICE, "search box is EMPTY");
 #endif
                PG_RETURN_FLOAT8(0.0);
        }
 
-       //elog(NOTICE,"requested search box is : (%.15g %.15g, %.15g %.15g)",search_box->xmin,search_box->ymin,search_box->xmax,search_box->ymax);
+#if DEBUG_GEOMETRY_STATS 
+       elog(NOTICE,"requested search box is : (%.15g %.15g, %.15g %.15g)",search_box->xmin,search_box->ymin,search_box->xmax,search_box->ymax);
+#endif
 
 
        SPIcode = SPI_connect();
@@ -1156,7 +1212,9 @@ Datum LWGEOM_gist_sel(PG_FUNCTION_ARGS)
        }
 
        sprintf(sql,"SELECT stats FROM GEOMETRY_COLUMNS WHERE attrelid=%u AND varattnum=%i",relid,var->varattno);
-       //elog(NOTICE,"sql:%s",sql);
+#if DEBUG_GEOMETRY_STATS 
+       elog(NOTICE,"sql:%s",sql);
+#endif
        SPIcode = SPI_exec(sql, 1 );
        if (SPIcode  != SPI_OK_SELECT )
        {
@@ -1168,7 +1226,9 @@ Datum LWGEOM_gist_sel(PG_FUNCTION_ARGS)
        if (SPI_processed !=1)
        {
                SPI_finish();
-               //elog(NOTICE,"LWGEOM_gist_sel: geometry_columns didnt return a unique value");
+#if DEBUG_GEOMETRY_STATS 
+               elog(NOTICE,"LWGEOM_gist_sel: geometry_columns didnt return a unique value");
+#endif
                PG_RETURN_FLOAT8(DEFAULT_GEOMETRY_SEL) ;
        }
 
@@ -1179,28 +1239,38 @@ Datum LWGEOM_gist_sel(PG_FUNCTION_ARGS)
        if (isnull)
        {
                SPI_finish();
-               //elog(NOTICE,"LWGEOM_gist_sel: geometry_columns returned a null histogram");
+#if DEBUG_GEOMETRY_STATS 
+               elog(NOTICE,"LWGEOM_gist_sel: geometry_columns returned a null histogram");
+#endif
                PG_RETURN_FLOAT8(DEFAULT_GEOMETRY_SEL) ;
        }
-//elog(NOTICE,"LWGEOM_gist_sel: checking against estimate_histogram2d");
+#if DEBUG_GEOMETRY_STATS 
+elog(NOTICE,"LWGEOM_gist_sel: checking against estimate_histogram2d");
+#endif
 
-       // now we have the histogram, and our search box - use the estimate_histogram2d(histo,box) to get the result!
+       /* now we have the histogram, and our search box - use the estimate_histogram2d(histo,box) to get the result! */
        myest = DatumGetFloat8( DirectFunctionCall2( estimate_lwhistogram2d, datum, PointerGetDatum(&search_box) ) );
 
-       if ( (myest<0) || (myest!=myest) ) // <0?  or NaN?
+       if ( (myest<0) || (myest!=myest) ) /* <0?  or NaN? */
        {
-               //elog(NOTICE,"LWGEOM_gist_sel: got something crazy back from estimate_histogram2d");
+#if DEBUG_GEOMETRY_STATS 
+               elog(NOTICE,"LWGEOM_gist_sel: got something crazy back from estimate_histogram2d");
+#endif
                PG_RETURN_FLOAT8(DEFAULT_GEOMETRY_SEL) ;
        }
 
        SPIcode =SPI_finish();
        if (SPIcode  != SPI_OK_FINISH )
        {
-               //elog(NOTICE,"LWGEOM_gist_sel: couldnt disconnect from SPI");
+#if DEBUG_GEOMETRY_STATS 
+               elog(NOTICE,"LWGEOM_gist_sel: couldnt disconnect from SPI");
+#endif
                PG_RETURN_FLOAT8(DEFAULT_GEOMETRY_SEL) ;
        }
 
-//elog(NOTICE,"LWGEOM_gist_sel: finished, returning with %lf",myest);
+#if DEBUG_GEOMETRY_STATS 
+elog(NOTICE,"LWGEOM_gist_sel: finished, returning with %lf",myest);
+#endif
         PG_RETURN_FLOAT8(myest);
 }
 
@@ -1355,7 +1425,7 @@ Datum LWGEOM_estimated_extent(PG_FUNCTION_ARGS)
        PG_RETURN_POINTER(box);
 }
 
-#else // USE_VERSION >= 80
+#else /* USE_VERSION >= 80 */
 
 /*
  * This function returns an estimate of the selectivity
@@ -1373,8 +1443,8 @@ estimate_selectivity(BOX2DFLOAT4 *box, GEOM_STATS *geomstats)
        int x_idx_min, x_idx_max, y_idx_min, y_idx_max;
        double intersect_x, intersect_y, AOI;
        double cell_area, box_area;
-       double geow, geoh; // width and height of histogram
-       int histocols, historows; // histogram grid size
+       double geow, geoh; /* width and height of histogram */
+       int histocols, historows; /* histogram grid size */
        double value;
        float overlapping_cells;
        float avg_feat_cells;
@@ -1422,7 +1492,6 @@ estimate_selectivity(BOX2DFLOAT4 *box, GEOM_STATS *geomstats)
 #endif
 
        cell_area = (geow*geoh) / (histocols*historows);
-       //box_area = (box->high.x-box->low.x)*(box->high.y-box->low.y);
        box_area = (box->xmax-box->xmin)*(box->ymax-box->ymin);
        value = 0;
 
@@ -1432,7 +1501,7 @@ estimate_selectivity(BOX2DFLOAT4 *box, GEOM_STATS *geomstats)
 #if DEBUG_GEOMETRY_STATS
                elog(NOTICE, " search_box overlaps %d columns on the left of histogram grid", -x_idx_min);
 #endif
-               // should increment the value somehow
+               /* should increment the value somehow */
                x_idx_min = 0;
        }
        if (x_idx_min >= histocols)
@@ -1440,7 +1509,7 @@ estimate_selectivity(BOX2DFLOAT4 *box, GEOM_STATS *geomstats)
 #if DEBUG_GEOMETRY_STATS
                elog(NOTICE, " search_box overlaps %d columns on the right of histogram grid", x_idx_min-histocols+1);
 #endif
-               // should increment the value somehow
+               /* should increment the value somehow */
                x_idx_min = histocols-1;
        }
 
@@ -1451,7 +1520,7 @@ estimate_selectivity(BOX2DFLOAT4 *box, GEOM_STATS *geomstats)
 #if DEBUG_GEOMETRY_STATS
                elog(NOTICE, " search_box overlaps %d columns on the bottom of histogram grid", -y_idx_min);
 #endif
-               // should increment the value somehow
+               /* should increment the value somehow */
                y_idx_min = 0;
        }
        if (y_idx_min >= historows)
@@ -1459,35 +1528,33 @@ estimate_selectivity(BOX2DFLOAT4 *box, GEOM_STATS *geomstats)
 #if DEBUG_GEOMETRY_STATS
                elog(NOTICE, " search_box overlaps %d columns on the top of histogram grid", y_idx_min-historows+1);
 #endif
-               // should increment the value somehow
+               /* should increment the value somehow */
                y_idx_min = historows-1;
        }
 
        /* Find last overlapping column */
-       //x_idx_max = (box->high.x-geomstats->xmin) / geow * histocols;
        x_idx_max = (box->xmax-geomstats->xmin) / geow * histocols;
        if (x_idx_max <0)
        {
-               // should increment the value somehow
+               /* should increment the value somehow */
                x_idx_max = 0;
        }
        if (x_idx_max >= histocols )
        {
-               // should increment the value somehow
+               /* should increment the value somehow */
                x_idx_max = histocols-1;
        }
 
        /* Find last overlapping row */
-       //y_idx_max = (box->high.y-geomstats->ymin) / geoh * historows;
        y_idx_max = (box->ymax-geomstats->ymin) / geoh * historows;
        if (y_idx_max <0)
        {
-               // should increment the value somehow
+               /* should increment the value somehow */
                y_idx_max = 0;
        }
        if (y_idx_max >= historows)
        {
-               // should increment the value somehow
+               /* should increment the value somehow */
                y_idx_max = historows-1;
        }
 
@@ -1624,16 +1691,16 @@ Datum LWGEOM_gist_sel(PG_FUNCTION_ARGS)
 #else
        PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
 #endif
-       //Oid operator = PG_GETARG_OID(1);
+       /* Oid operator = PG_GETARG_OID(1); */
        List *args = (List *) PG_GETARG_POINTER(2);
-       //int varRelid = PG_GETARG_INT32(3);
+       /* int varRelid = PG_GETARG_INT32(3); */
        Oid relid;
        HeapTuple stats_tuple;
        GEOM_STATS *geomstats;
        int geomstats_nvalues=0;
        Node *other;
        Var *self;
-       char *in;
+       uchar *in;
        BOX2DFLOAT4 search_box;
        float8 selectivity=0;
 
@@ -1691,7 +1758,7 @@ Datum LWGEOM_gist_sel(PG_FUNCTION_ARGS)
         * Convert the constant to a BOX
         */
 
-       in = (char *)PG_DETOAST_DATUM( ((Const*)other)->constvalue );
+       in = (uchar *)PG_DETOAST_DATUM( ((Const*)other)->constvalue );
        if ( ! getbox2d_p(in+4, &search_box) )
        {
 #if DEBUG_GEOMETRY_STATS 
@@ -1709,7 +1776,7 @@ Datum LWGEOM_gist_sel(PG_FUNCTION_ARGS)
         */
 
 #if USE_VERSION < 81
-//     relid = getrelid(varRelid, root->rtable);
+/*     relid = getrelid(varRelid, root->rtable); */
        relid = getrelid(self->varno, root->rtable);
 #else
        relid = getrelid(self->varno, root->parse->rtable);
@@ -1811,16 +1878,16 @@ compute_geometry_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc,
        double sdLOWx=0, sdLOWy=0, sdHIGx=0, sdHIGy=0;
        BOX2DFLOAT4 *newhistobox=NULL;
 #endif
-       double geow, geoh; // width and height of histogram
+       double geow, geoh; /* width and height of histogram */
        int histocells;
-       int cols, rows; // histogram grid size
+       int cols, rows; /* histogram grid size */
        BOX2DFLOAT4 histobox;
 
        /*
         * This is where geometry_analyze
         * should put its' custom parameters.
         */
-       //void *mystats = stats->extra_data;
+       /* void *mystats = stats->extra_data; */
        
        /*
         * We'll build an histogram having from 40 to 400 boxesPerSide
@@ -1870,7 +1937,7 @@ compute_geometry_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc,
 
                if ( ! getbox2d_p(SERIALIZED_FORM(geom), &box) )
                {
-                       // Skip empty geometry
+                       /* Skip empty geometry */
 #if DEBUG_GEOMETRY_STATS 
                        elog(NOTICE, " skipped empty geometry %d", i);
 #endif
@@ -1918,7 +1985,7 @@ compute_geometry_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc,
                                box.ymin);
                }
                
-               // TODO: ask if we need geom or bvol size for stawidth
+               /* TODO: ask if we need geom or bvol size for stawidth */
                total_width += geom->size;
                total_boxes_area += (box.xmax-box.xmin)*(box.ymax-box.ymin);
 
@@ -2054,7 +2121,7 @@ compute_geometry_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc,
                histobox.ymax = newhistobox->ymax;
 
 
-#else // ! USE_STANDARD_DEVIATION
+#else /* ! USE_STANDARD_DEVIATION */
 
        /*
         * Set histogram extent box
@@ -2063,7 +2130,7 @@ compute_geometry_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc,
        histobox.ymin = sample_extent->ymin;
        histobox.xmax = sample_extent->xmax;
        histobox.ymax = sample_extent->ymax;
-#endif // USE_STANDARD_DEVIATION
+#endif /* USE_STANDARD_DEVIATION */
 
 
 #if DEBUG_GEOMETRY_STATS 
@@ -2122,7 +2189,7 @@ compute_geometry_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc,
        geomstats->cols = cols;
        geomstats->rows = rows;
 
-       // Initialize all values to 0
+       /* Initialize all values to 0 */
        for (i=0;i<histocells; i++) geomstats->value[i] = 0;
 
        cell_width = geow/cols;
@@ -2153,7 +2220,7 @@ compute_geometry_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc,
                int numcells=0;
 
                box = (BOX2DFLOAT4 *)sampleboxes[i];
-               if ( ! box ) continue; // hard deviant..
+               if ( ! box ) continue; /* hard deviant.. */
 
                /* give backend a chance of interrupting us */
                vacuum_delay_point();
@@ -2201,9 +2268,11 @@ compute_geometry_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc,
                        }
                }
 
-               // before adding to the total cells
-               // we could decide if we really 
-               // want this feature to count
+               /*
+                * before adding to the total cells
+                * we could decide if we really 
+                * want this feature to count
+                */
                total_boxes_cells += numcells;
 
                examinedsamples++;
@@ -2221,7 +2290,7 @@ compute_geometry_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc,
                return;
        }
 
-       // what about null features (TODO) ?
+       /* what about null features (TODO) ? */
        geomstats->avgFeatureCells = (float4)total_boxes_cells/examinedsamples;
 
 #if DEBUG_GEOMETRY_STATS
@@ -2329,8 +2398,10 @@ Datum LWGEOM_analyze(PG_FUNCTION_ARGS)
         * There might be a reason not to analyze this column
         * (can we detect the absence of an index?)
         */
-       //elog(NOTICE, "compute_geometry_stats not implemented yet");
-       //PG_RETURN_BOOL(false);
+#if 0
+       elog(NOTICE, "compute_geometry_stats not implemented yet");
+       PG_RETURN_BOOL(false);
+#endif
 
        /* Setup the minimum rows and the algorithm function */
        stats->minrows = 300 * stats->attr->attstattarget;
@@ -2405,7 +2476,7 @@ Datum LWGEOM_estimated_extent(PG_FUNCTION_ARGS)
                nsp[VARSIZE(txnsp)-VARHDRSZ]='\0';
                querysize += VARSIZE(txnsp);
        } else {
-               querysize += 32; // current_schema()
+               querysize += 32; /* current_schema() */
        }
 
        tbl = palloc(VARSIZE(txtbl)+1);
@@ -2505,11 +2576,19 @@ Datum LWGEOM_estimated_extent(PG_FUNCTION_ARGS)
 }
        
 
-#endif // USE_VERSION >= 80
+#endif /* USE_VERSION >= 80 */
 
 
 /**********************************************************************
  * $Log$
+ * Revision 1.36  2005/12/30 17:40:37  strk
+ * Moved PG_LWGEOM WKB I/O and SRID get/set funx
+ * from lwgeom_api.c to lwgeom_pg.c.
+ * Made lwgeom_from_ewkb directly invoke grammar parser rather then invoke
+ * the PG_LWGEOM-specific function.
+ * Cleaned up signedness-related and comments-related warnings for the files
+ * being committed (more to do on other files)
+ *
  * Revision 1.35  2005/10/10 16:19:16  strk
  * Fixed null values fraction computation in geometry analyzer as suggested by Michael Fuhr
  *
index 4addcbd0e43d22fadff225ed7dd96a3908843b0d..3c8d2cdf6e172dc17bbae4395937ec154fd77b79 100644 (file)
@@ -121,11 +121,11 @@ DP_simplify2d(POINTARRAY *inpts, double epsilon)
        elog(NOTICE, "DP_simplify called input has %d pts and %d dims (ptsize: %d)", inpts->npoints, inpts->ndims, ptsize);
 #endif
 
-       // allocate space for output POINTARRAY
+       /* allocate space for output POINTARRAY */
        outpts = palloc(sizeof(POINTARRAY));
        outpts->dims = inpts->dims;
        outpts->npoints=1;
-       outpts->serialized_pointlist = (char *)palloc(ptsize*inpts->npoints);
+       outpts->serialized_pointlist = palloc(ptsize*inpts->npoints);
        memcpy(getPoint_internal(outpts, 0), getPoint_internal(inpts, 0),
                ptsize);
 
@@ -167,7 +167,7 @@ DP_simplify2d(POINTARRAY *inpts, double epsilon)
         */
        if ( outpts->npoints < inpts->npoints )
        {
-               outpts->serialized_pointlist = (char *)repalloc(
+               outpts->serialized_pointlist = repalloc(
                        outpts->serialized_pointlist,
                        ptsize*outpts->npoints);
                if ( outpts->serialized_pointlist == NULL ) {
@@ -196,7 +196,6 @@ simplify2d_lwline(const LWLINE *iline, double dist)
        return oline;
 }
 
-// TODO
 LWPOLY *
 simplify2d_lwpoly(const LWPOLY *ipoly, double dist)
 {
@@ -356,7 +355,7 @@ Datum LWGEOM_line_interpolate_point(PG_FUNCTION_ARGS)
        LWPOINT *point;
        POINTARRAY *ipa, *opa;
        POINT4D pt;
-       char *srl;
+       uchar *srl;
        int nsegs, i;
        double length, slength, tlength;
 
@@ -383,7 +382,7 @@ Datum LWGEOM_line_interpolate_point(PG_FUNCTION_ARGS)
                else
                        getPoint4d_p(ipa, ipa->npoints-1, &pt);
 
-               opa = pointArray_construct((char *)&pt,
+               opa = pointArray_construct((uchar *)&pt,
                        TYPE_HASZ(line->type),
                        TYPE_HASM(line->type),
                        1);
@@ -416,7 +415,7 @@ Datum LWGEOM_line_interpolate_point(PG_FUNCTION_ARGS)
                        pt.y = (p1.y) + ((p2.y - p1.y) * dseg);
                        pt.z = 0;
                        pt.m = 0;
-                       opa = pointArray_construct((char *)&pt,
+                       opa = pointArray_construct((uchar *)&pt,
                                TYPE_HASZ(line->type),
                                TYPE_HASM(line->type),
                                1);
@@ -431,7 +430,7 @@ Datum LWGEOM_line_interpolate_point(PG_FUNCTION_ARGS)
        /* Return the last point on the line. This shouldn't happen, but
         * could if there's some floating point rounding errors. */
        getPoint4d_p(ipa, ipa->npoints-1, &pt);
-       opa = pointArray_construct((char *)&pt,
+       opa = pointArray_construct((uchar *)&pt,
                TYPE_HASZ(line->type),
                TYPE_HASM(line->type),
                1);
@@ -612,7 +611,7 @@ lwline_grid(LWLINE *line, gridspec *grid)
        /* Skip line3d with less then 2 points */
        if ( opa->npoints < 2 ) return NULL;
 
-       // TODO: grid bounding box...
+       /* TODO: grid bounding box... */
        oline = lwline_construct(line->SRID, NULL, opa);
 
        return oline;
@@ -717,7 +716,7 @@ lwpoint_grid(LWPOINT *point, gridspec *grid)
 
        opa = ptarray_grid(point->point, grid);
 
-       // TODO: grid bounding box ?
+       /* TODO: grid bounding box ? */
        opoint = lwpoint_construct(point->SRID, NULL, opa);
 
 #if VERBOSE
index a9c86e013d21394ba085f6a2b73a2083911260c7..5125efc02e4ebe98e474f82d57dd64dcbbb56906 100644 (file)
 #include "lwgeom_pg.h"
 #include "stringBuffer.h"
 
-// implementation GiST support and basic LWGEOM operations (like &&)
+/*
+ * implementation GiST support and basic LWGEOM operations (like &&)
+ */
 
 
-//#define PGIS_DEBUG
-//#define PGIS_DEBUG_CALLS
-//#define PGIS_DEBUG_GIST
-//#define PGIS_DEBUG_GIST2
-//#define PGIS_DEBUG_GIST3
-//#define PGIS_DEBUG_GIST4
-//#define PGIS_DEBUG_GIST5
-//#define PGIS_DEBUG_GIST6
+/* #define PGIS_DEBUG */
+/* #define PGIS_DEBUG_CALLS */
+/* #define PGIS_DEBUG_GIST */
+/* #define PGIS_DEBUG_GIST2 */
+/* #define PGIS_DEBUG_GIST3 */
+/* #define PGIS_DEBUG_GIST4 */
+/* #define PGIS_DEBUG_GIST5 */
+/* #define PGIS_DEBUG_GIST6 */
 
 Datum LWGEOM_overlap(PG_FUNCTION_ARGS);
 Datum LWGEOM_overleft(PG_FUNCTION_ARGS);
@@ -57,12 +59,12 @@ static bool lwgeom_rtree_leaf_consistent(BOX2DFLOAT4 *key,BOX2DFLOAT4 *query,       St
 
 
 
-// for debugging
-int counter_leaf = 0;
-int counter_intern = 0;
+/* for debugging */
+static int counter_leaf = 0;
+static int counter_intern = 0;
 
 
-// GiST strategies (modified from src/include/access/rtree.h)
+/* GiST strategies (modified from src/include/access/rtree.h) */
 #define RTLeftStrategyNumber                   1
 #define RTOverLeftStrategyNumber               2
 #define RTOverlapStrategyNumber                        3
@@ -77,20 +79,22 @@ int counter_intern = 0;
 #define RTOverAboveStrategyNumber              12
 
 
-// all the lwgeom_<same,overlpa,overleft,left,right,overright,overbelow,below,above,overabove,contained,contain>
-//  work the same.
-//  1. get lwgeom1
-//  2. get lwgeom2
-//  3. get box2d for lwgeom1
-//  4. get box2d for lwgeom2
-//  7. call the appropriate BOX2DFLOAT4 function
-//  8. return result;
+/*
+ * all the lwgeom_<same,overlpa,overleft,left,right,overright,overbelow,below,above,overabove,contained,contain>
+ *  work the same.
+ *  1. get lwgeom1
+ *  2. get lwgeom2
+ *  3. get box2d for lwgeom1
+ *  4. get box2d for lwgeom2
+ *  7. call the appropriate BOX2DFLOAT4 function
+ *  8. return 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));
+       PG_LWGEOM *lwgeom1 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+       PG_LWGEOM *lwgeom2 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
        bool result;
        BOX2DFLOAT4 box1;
        BOX2DFLOAT4 box2;
@@ -99,7 +103,7 @@ Datum LWGEOM_overlap(PG_FUNCTION_ARGS)
        elog(NOTICE,"GIST: LWGEOM_overlap --entry");
 #endif
 
-       if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) )
+       if ( pglwgeom_getSRID(lwgeom1) != pglwgeom_getSRID(lwgeom2) )
        {
                PG_FREE_IF_COPY(lwgeom1, 0);
                PG_FREE_IF_COPY(lwgeom2, 1);
@@ -108,11 +112,11 @@ Datum LWGEOM_overlap(PG_FUNCTION_ARGS)
        }
 
 
-       if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) )
+       if ( ! (getbox2d_p(SERIALIZED_FORM(lwgeom1), &box1) && getbox2d_p(SERIALIZED_FORM(lwgeom2), &box2)) )
        {
                PG_FREE_IF_COPY(lwgeom1, 0);
                PG_FREE_IF_COPY(lwgeom2, 1);
-               // One or both are empty geoms
+               /* One or both are empty geoms */
                PG_RETURN_BOOL(FALSE);
        }
 
@@ -142,8 +146,8 @@ Datum LWGEOM_overlap(PG_FUNCTION_ARGS)
 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));
+       PG_LWGEOM *lwgeom1 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+       PG_LWGEOM *lwgeom2 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
        bool result;
        BOX2DFLOAT4 box1;
        BOX2DFLOAT4 box2;
@@ -152,7 +156,7 @@ Datum LWGEOM_overleft(PG_FUNCTION_ARGS)
        elog(NOTICE,"GIST: LWGEOM_overleft --entry");
 #endif
 
-       if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) )
+       if ( pglwgeom_getSRID(lwgeom1) != pglwgeom_getSRID(lwgeom2) )
        {
                PG_FREE_IF_COPY(lwgeom1, 0);
                PG_FREE_IF_COPY(lwgeom2, 1);
@@ -160,7 +164,7 @@ Datum LWGEOM_overleft(PG_FUNCTION_ARGS)
                PG_RETURN_NULL();
        }
 
-       if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) )
+       if ( ! (getbox2d_p(SERIALIZED_FORM(lwgeom1), &box1) && getbox2d_p(SERIALIZED_FORM(lwgeom2), &box2)) )
        {
                PG_FREE_IF_COPY(lwgeom1, 0);
                PG_FREE_IF_COPY(lwgeom2, 1);
@@ -180,8 +184,8 @@ Datum LWGEOM_overleft(PG_FUNCTION_ARGS)
 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));
+       PG_LWGEOM *lwgeom1 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+       PG_LWGEOM *lwgeom2 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
        bool result;
        BOX2DFLOAT4 box1;
        BOX2DFLOAT4 box2;
@@ -190,15 +194,9 @@ Datum LWGEOM_left(PG_FUNCTION_ARGS)
        elog(NOTICE,"GIST: LWGEOM_left --entry");
 #endif
 
-       if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) )
-       {
-               PG_FREE_IF_COPY(lwgeom1, 0);
-               PG_FREE_IF_COPY(lwgeom2, 1);
-               elog(ERROR, "Operation on two geometries with different SRIDs");
-               PG_RETURN_NULL();
-       }
+       errorIfSRIDMismatch(pglwgeom_getSRID(lwgeom1), pglwgeom_getSRID(lwgeom2));
 
-       if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) )
+       if ( ! (getbox2d_p(SERIALIZED_FORM(lwgeom1), &box1) && getbox2d_p(SERIALIZED_FORM(lwgeom2), &box2)) )
        {
                PG_FREE_IF_COPY(lwgeom1, 0);
                PG_FREE_IF_COPY(lwgeom2, 1);
@@ -218,8 +216,8 @@ Datum LWGEOM_left(PG_FUNCTION_ARGS)
 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));
+       PG_LWGEOM *lwgeom1 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+       PG_LWGEOM *lwgeom2 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
        bool result;
        BOX2DFLOAT4 box1;
        BOX2DFLOAT4 box2;
@@ -228,15 +226,9 @@ Datum LWGEOM_right(PG_FUNCTION_ARGS)
        elog(NOTICE,"GIST: LWGEOM_right --entry");
 #endif
 
-       if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) )
-       {
-               PG_FREE_IF_COPY(lwgeom1, 0);
-               PG_FREE_IF_COPY(lwgeom2, 1);
-               elog(ERROR, "Operation on two geometries with different SRIDs");
-               PG_RETURN_NULL();
-       }
+       errorIfSRIDMismatch(pglwgeom_getSRID(lwgeom1), pglwgeom_getSRID(lwgeom2));
 
-       if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) )
+       if ( ! (getbox2d_p(SERIALIZED_FORM(lwgeom1), &box1) && getbox2d_p(SERIALIZED_FORM(lwgeom2), &box2)) )
        {
                PG_FREE_IF_COPY(lwgeom1, 0);
                PG_FREE_IF_COPY(lwgeom2, 1);
@@ -256,8 +248,8 @@ Datum LWGEOM_right(PG_FUNCTION_ARGS)
 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));
+       PG_LWGEOM *lwgeom1 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+       PG_LWGEOM *lwgeom2 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
        bool result;
        BOX2DFLOAT4 box1;
        BOX2DFLOAT4 box2;
@@ -266,15 +258,9 @@ Datum LWGEOM_overright(PG_FUNCTION_ARGS)
        elog(NOTICE,"GIST: LWGEOM_overright --entry");
 #endif
 
-       if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) )
-       {
-               PG_FREE_IF_COPY(lwgeom1, 0);
-               PG_FREE_IF_COPY(lwgeom2, 1);
-               elog(ERROR, "Operation on two geometries with different SRIDs");
-               PG_RETURN_NULL();
-       }
+       errorIfSRIDMismatch(pglwgeom_getSRID(lwgeom1), pglwgeom_getSRID(lwgeom2));
 
-       if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) )
+       if ( ! (getbox2d_p(SERIALIZED_FORM(lwgeom1), &box1) && getbox2d_p(SERIALIZED_FORM(lwgeom2), &box2)) )
        {
                PG_FREE_IF_COPY(lwgeom1, 0);
                PG_FREE_IF_COPY(lwgeom2, 1);
@@ -294,8 +280,8 @@ Datum LWGEOM_overright(PG_FUNCTION_ARGS)
 PG_FUNCTION_INFO_V1(LWGEOM_overbelow);
 Datum LWGEOM_overbelow(PG_FUNCTION_ARGS)
 {
-       char *lwgeom1 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-       char *lwgeom2 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
+       PG_LWGEOM *lwgeom1 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+       PG_LWGEOM *lwgeom2 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
        bool result;
        BOX2DFLOAT4 box1;
        BOX2DFLOAT4 box2;
@@ -304,15 +290,9 @@ Datum LWGEOM_overbelow(PG_FUNCTION_ARGS)
        elog(NOTICE,"GIST: LWGEOM_overbelow --entry");
 #endif
 
-       if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) )
-       {
-               PG_FREE_IF_COPY(lwgeom1, 0);
-               PG_FREE_IF_COPY(lwgeom2, 1);
-               elog(ERROR, "Operation on two geometries with different SRIDs");
-               PG_RETURN_NULL();
-       }
+       errorIfSRIDMismatch(pglwgeom_getSRID(lwgeom1), pglwgeom_getSRID(lwgeom2));
 
-       if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) )
+       if ( ! (getbox2d_p(SERIALIZED_FORM(lwgeom1), &box1) && getbox2d_p(SERIALIZED_FORM(lwgeom2), &box2)) )
        {
                PG_FREE_IF_COPY(lwgeom1, 0);
                PG_FREE_IF_COPY(lwgeom2, 1);
@@ -332,8 +312,8 @@ Datum LWGEOM_overbelow(PG_FUNCTION_ARGS)
 PG_FUNCTION_INFO_V1(LWGEOM_below);
 Datum LWGEOM_below(PG_FUNCTION_ARGS)
 {
-       char *lwgeom1 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-       char *lwgeom2 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
+       PG_LWGEOM *lwgeom1 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+       PG_LWGEOM *lwgeom2 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
        bool result;
        BOX2DFLOAT4 box1;
        BOX2DFLOAT4 box2;
@@ -342,15 +322,9 @@ Datum LWGEOM_below(PG_FUNCTION_ARGS)
        elog(NOTICE,"GIST: LWGEOM_below --entry");
 #endif
 
-       if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) )
-       {
-               PG_FREE_IF_COPY(lwgeom1, 0);
-               PG_FREE_IF_COPY(lwgeom2, 1);
-               elog(ERROR, "Operation on two geometries with different SRIDs");
-               PG_RETURN_NULL();
-       }
+       errorIfSRIDMismatch(pglwgeom_getSRID(lwgeom1), pglwgeom_getSRID(lwgeom2));
 
-       if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) )
+       if ( ! (getbox2d_p(SERIALIZED_FORM(lwgeom1), &box1) && getbox2d_p(SERIALIZED_FORM(lwgeom2), &box2)) )
        {
                PG_FREE_IF_COPY(lwgeom1, 0);
                PG_FREE_IF_COPY(lwgeom2, 1);
@@ -370,8 +344,8 @@ Datum LWGEOM_below(PG_FUNCTION_ARGS)
 PG_FUNCTION_INFO_V1(LWGEOM_above);
 Datum LWGEOM_above(PG_FUNCTION_ARGS)
 {
-       char *lwgeom1 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-       char *lwgeom2 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
+       PG_LWGEOM *lwgeom1 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+       PG_LWGEOM *lwgeom2 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
        bool result;
        BOX2DFLOAT4 box1;
        BOX2DFLOAT4 box2;
@@ -380,15 +354,9 @@ Datum LWGEOM_above(PG_FUNCTION_ARGS)
        elog(NOTICE,"GIST: LWGEOM_above --entry");
 #endif
 
-       if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) )
-       {
-               PG_FREE_IF_COPY(lwgeom1, 0);
-               PG_FREE_IF_COPY(lwgeom2, 1);
-               elog(ERROR, "Operation on two geometries with different SRIDs");
-               PG_RETURN_NULL();
-       }
+       errorIfSRIDMismatch(pglwgeom_getSRID(lwgeom1), pglwgeom_getSRID(lwgeom2));
 
-       if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) )
+       if ( ! (getbox2d_p(SERIALIZED_FORM(lwgeom1), &box1) && getbox2d_p(SERIALIZED_FORM(lwgeom2), &box2)) )
        {
                PG_FREE_IF_COPY(lwgeom1, 0);
                PG_FREE_IF_COPY(lwgeom2, 1);
@@ -408,8 +376,8 @@ Datum LWGEOM_above(PG_FUNCTION_ARGS)
 PG_FUNCTION_INFO_V1(LWGEOM_overabove);
 Datum LWGEOM_overabove(PG_FUNCTION_ARGS)
 {
-       char *lwgeom1 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-       char *lwgeom2 = (char *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
+       PG_LWGEOM *lwgeom1 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+       PG_LWGEOM *lwgeom2 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
        bool result;
        BOX2DFLOAT4 box1;
        BOX2DFLOAT4 box2;
@@ -418,14 +386,12 @@ Datum LWGEOM_overabove(PG_FUNCTION_ARGS)
        elog(NOTICE,"GIST: LWGEOM_overabove --entry");
 #endif
 
-       if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) )
-       {
-               elog(ERROR, "Operation on two geometries with different SRIDs");
-               PG_RETURN_NULL();
-       }
+       errorIfSRIDMismatch(pglwgeom_getSRID(lwgeom1), pglwgeom_getSRID(lwgeom2));
 
-       if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) )
+       if ( ! (getbox2d_p(SERIALIZED_FORM(lwgeom1), &box1) && getbox2d_p(SERIALIZED_FORM(lwgeom2), &box2)) )
        {
+               PG_FREE_IF_COPY(lwgeom1, 0);
+               PG_FREE_IF_COPY(lwgeom2, 1);
                PG_RETURN_BOOL(FALSE);
        }
 
@@ -442,8 +408,8 @@ Datum LWGEOM_overabove(PG_FUNCTION_ARGS)
 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));
+       PG_LWGEOM *lwgeom1 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+       PG_LWGEOM *lwgeom2 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
        bool result;
        BOX2DFLOAT4 box1;
        BOX2DFLOAT4 box2;
@@ -452,15 +418,9 @@ Datum LWGEOM_contained(PG_FUNCTION_ARGS)
        elog(NOTICE,"GIST: LWGEOM_contained --entry");
 #endif
 
-       if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) )
-       {
-               PG_FREE_IF_COPY(lwgeom1, 0);
-               PG_FREE_IF_COPY(lwgeom2, 1);
-               elog(ERROR, "Operation on two geometries with different SRIDs");
-               PG_RETURN_NULL();
-       }
+       errorIfSRIDMismatch(pglwgeom_getSRID(lwgeom1), pglwgeom_getSRID(lwgeom2));
 
-       if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) )
+       if ( ! (getbox2d_p(SERIALIZED_FORM(lwgeom1), &box1) && getbox2d_p(SERIALIZED_FORM(lwgeom2), &box2)) )
        {
                PG_FREE_IF_COPY(lwgeom1, 0);
                PG_FREE_IF_COPY(lwgeom2, 1);
@@ -480,8 +440,8 @@ Datum LWGEOM_contained(PG_FUNCTION_ARGS)
 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));
+       PG_LWGEOM *lwgeom1 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+       PG_LWGEOM *lwgeom2 = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
        bool result;
        BOX2DFLOAT4 box1;
        BOX2DFLOAT4 box2;
@@ -490,15 +450,9 @@ Datum LWGEOM_contain(PG_FUNCTION_ARGS)
        elog(NOTICE,"GIST: LWGEOM_contain --entry");
 #endif
 
-       if ( lwgeom_getsrid(lwgeom1+4) != lwgeom_getsrid(lwgeom2+4) )
-       {
-               PG_FREE_IF_COPY(lwgeom1, 0);
-               PG_FREE_IF_COPY(lwgeom2, 1);
-               elog(ERROR, "Operation on two geometries with different SRIDs");
-               PG_RETURN_NULL();
-       }
+       errorIfSRIDMismatch(pglwgeom_getSRID(lwgeom1), pglwgeom_getSRID(lwgeom2));
 
-       if ( ! (getbox2d_p(lwgeom1+4, &box1) && getbox2d_p(lwgeom2+4, &box2)) )
+       if ( ! (getbox2d_p(SERIALIZED_FORM(lwgeom1), &box1) && getbox2d_p(SERIALIZED_FORM(lwgeom2), &box2)) )
        {
                PG_FREE_IF_COPY(lwgeom1, 0);
                PG_FREE_IF_COPY(lwgeom2, 1);
@@ -515,7 +469,7 @@ Datum LWGEOM_contain(PG_FUNCTION_ARGS)
 }
 
 
-// These functions are taken from the postgis_gist_72.c file
+/* These functions are taken from the postgis_gist_72.c file */
 
 
 /*
@@ -543,10 +497,10 @@ Datum LWGEOM_gist_compress(PG_FUNCTION_ARGS)
                elog(NOTICE,"GIST: LWGEOM_gist_compress got a non-NULL key");
 #endif
 
-                       PG_LWGEOM *in; // lwgeom serialized
+                       PG_LWGEOM *in; /* lwgeom serialized */
                        BOX2DFLOAT4 *rr;
 
-                       // lwgeom serialized form
+                       /* lwgeom serialized form */
                        in = (PG_LWGEOM*)PG_DETOAST_DATUM(entry->key);
 
 #ifdef PGIS_DEBUG_GIST4
@@ -572,7 +526,7 @@ Datum LWGEOM_gist_compress(PG_FUNCTION_ARGS)
 #endif
                                pfree(rr);
                                if (in!=(PG_LWGEOM*)DatumGetPointer(entry->key))
-                                       pfree(in);  // PG_FREE_IF_COPY
+                                       pfree(in);  /* PG_FREE_IF_COPY */
                                PG_RETURN_POINTER(entry);
                        }
 
@@ -586,7 +540,7 @@ Datum LWGEOM_gist_compress(PG_FUNCTION_ARGS)
 #endif
 
                        if (in != (PG_LWGEOM*)DatumGetPointer(entry->key))
-                               pfree(in);  // PG_FREE_IF_COPY
+                               pfree(in);  /* PG_FREE_IF_COPY */
 
                        gistentryinit(*retval, PointerGetDatum(rr),
                                entry->rel, entry->page,
@@ -620,7 +574,7 @@ PG_FUNCTION_INFO_V1(LWGEOM_gist_consistent);
 Datum LWGEOM_gist_consistent(PG_FUNCTION_ARGS)
 {
        GISTENTRY *entry = (GISTENTRY*) PG_GETARG_POINTER(0);
-       PG_LWGEOM *query ; // lwgeom serialized form
+       PG_LWGEOM *query ; /* lwgeom serialized form */
        StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
        bool result;
        BOX2DFLOAT4  box;
@@ -631,8 +585,8 @@ Datum LWGEOM_gist_consistent(PG_FUNCTION_ARGS)
 
        if ( ((Pointer *) PG_GETARG_DATUM(1)) == NULL )
        {
-               //elog(NOTICE,"LWGEOM_gist_consistent:: got null query!");
-               PG_RETURN_BOOL(false); // null query - this is screwy!
+               /*elog(NOTICE,"LWGEOM_gist_consistent:: got null query!"); */
+               PG_RETURN_BOOL(false); /* null query - this is screwy! */
        }
 
        query = (PG_LWGEOM*)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
@@ -680,7 +634,7 @@ lwgeom_rtree_internal_consistent(BOX2DFLOAT4 *key, BOX2DFLOAT4 *query,
                case RTOverLeftStrategyNumber:
                        retval = !DatumGetBool( DirectFunctionCall2( BOX2D_right, PointerGetDatum(key), PointerGetDatum(query) ) );
                        break;
-               case RTOverlapStrategyNumber:  //optimized for speed
+               case RTOverlapStrategyNumber:  /*optimized for speed */
 
         retval = (((key->xmax>= query->xmax) &&
                         (key->xmin <= query->xmax)) ||
@@ -695,7 +649,7 @@ lwgeom_rtree_internal_consistent(BOX2DFLOAT4 *key, BOX2DFLOAT4 *query,
 
 
 #ifdef PGIS_DEBUG_GIST5
-//keep track and report info about how many times this is called
+/*keep track and report info about how many times this is called */
                                        if (counter_intern == 0)
                                        {
                                                                         elog(NOTICE,"search bounding box is: <%.16g %.16g,%.16g %.16g> - size box2d= %i",
@@ -763,7 +717,7 @@ lwgeom_rtree_leaf_consistent(BOX2DFLOAT4 *key,
                case RTOverLeftStrategyNumber:
                        retval = DatumGetBool(DirectFunctionCall2(BOX2D_overleft, PointerGetDatum(key), PointerGetDatum(query)));
                        break;
-               case RTOverlapStrategyNumber://optimized for speed
+               case RTOverlapStrategyNumber: /*optimized for speed */
                                   retval = (((key->xmax>= query->xmax) &&
                                                 (key->xmin <= query->xmax)) ||
                                                ((query->xmax>= key->xmax) &&
@@ -774,7 +728,7 @@ lwgeom_rtree_leaf_consistent(BOX2DFLOAT4 *key,
                                         ((query->ymax>= key->ymax) &&
                                          (query->ymin<= key->ymax)));
 #ifdef PGIS_DEBUG_GIST5
-//keep track and report info about how many times this is called
+/*keep track and report info about how many times this is called */
 
                           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);
@@ -895,9 +849,11 @@ Datum LWGEOM_gist_union(PG_FUNCTION_ARGS)
 }
 
 
-// size of a box is width*height
-// we do this in double precision because width and height can be very very small
-// and it gives screwy results
+/*
+ * size of a box is width*height
+ * we do this in double precision because width and height can be very very small
+ * and it gives screwy results
+ */
 static float
 size_box2d(Datum box2d)
 {
@@ -920,7 +876,6 @@ size_box2d(Datum box2d)
                {
                        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;
 
@@ -930,10 +885,12 @@ size_box2d(Datum box2d)
        return result;
 }
 
-// size of a box is width*height
-// we do this in double precision because width and height can be very very small
-// and it gives screwy results
-// this version returns a double
+/*
+ * size of a box is width*height
+ * we do this in double precision because width and height can be very very small
+ * and it gives screwy results
+ * this version returns a double
+ */
 static double size_box2d_double(Datum box2d);
 static double size_box2d_double(Datum box2d)
 {
@@ -947,16 +904,19 @@ static double size_box2d_double(Datum box2d)
        {
                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));
+               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));
+               }
        }
        else
+       {
                result = (double) 0.0;
+       }
 
 #ifdef PGIS_DEBUG_GIST
        elog(NOTICE,"GIST: size_box2d_double called - returning %.15g",result);
@@ -985,7 +945,7 @@ Datum LWGEOM_gist_penalty(PG_FUNCTION_ARGS)
 
 
        ud = DirectFunctionCall2(BOX2D_union, origentry->key, newentry->key);
-       //ud = BOX2D_union(origentry->key, newentry->key);
+       /*ud = BOX2D_union(origentry->key, newentry->key); */
        tmp1 = size_box2d_double(ud);
        if (DatumGetPointer(ud) != NULL)
                pfree(DatumGetPointer(ud));
@@ -1258,7 +1218,7 @@ elog(NOTICE,"   unionB is: <%.16g %.16g,%.16g %.16g>", unionB->xmin, unionB->ymi
                        PointerGetDatum(unionB), PointerGetDatum(unionT));
                float sizeLR, sizeBT;
 
-//elog(NOTICE,"direction is abigeous");
+/*elog(NOTICE,"direction is abigeous"); */
 
                sizeLR = size_box2d(interLR);
                sizeBT = size_box2d(interBT);
@@ -1362,13 +1322,13 @@ elog(NOTICE,"   unionB is: <%.16g %.16g,%.16g %.16g>", unionB->xmin, unionB->ymi
 }
 
 
-// debug function
+/* debug function */
 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);
-       counter_intern =0;  //reset
+       counter_intern =0;  /*reset */
        counter_leaf = 0;
        PG_RETURN_NULL();
 }
index 866f8651397a4a35f71e8a8907fb03777eb9b678..6c133da3ca1a9d5ecb7d8d7a7863a2f3a506852e 100644 (file)
@@ -146,7 +146,7 @@ Datum LWGEOMFromWKB(PG_FUNCTION_ARGS)
 
        wkb_input = (WellKnownBinary *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
 
-       lwgeom2 = pglwgeom_from_ewkb(VARDATA(wkb_input),
+       lwgeom2 = pglwgeom_from_ewkb((uchar *)VARDATA(wkb_input),
                VARSIZE(wkb_input)-VARHDRSZ);
 
        if (  ( PG_NARGS()>1) && ( ! PG_ARGISNULL(1) ))
index 92be5646912490b5ed431c72a2f355f28f0cf99a..1d739c187281fd643e021ab6658e4f2909397e9b 100644 (file)
@@ -1,3 +1,15 @@
+/**********************************************************************
+ * $Id$
+ *
+ * PostGIS - Spatial Types for PostgreSQL
+ * http://postgis.refractions.net
+ * Copyright 2001-2005 Refractions Research Inc.
+ *
+ * This is free software; you can redistribute and/or modify it under
+ * the terms of the GNU General Public Licence. See the COPYING file.
+ * 
+ **********************************************************************/
+
 #include "postgres.h"
 
 #include <math.h>
 #include "lwgeom_pg.h"
 
 
-//#define PGIS_DEBUG
+/*#define PGIS_DEBUG */
 
 #include "wktparse.h"
 
-// ---- SRID(geometry)
+/* ---- SRID(geometry) */
 Datum LWGEOM_getSRID(PG_FUNCTION_ARGS);
-// ---- SetSRID(geometry, integer)
+/* ---- SetSRID(geometry, integer) */
 Datum LWGEOM_setSRID(PG_FUNCTION_ARGS);
-// ---- GeometryType(geometry)
+/* ---- GeometryType(geometry) */
 Datum LWGEOM_getTYPE(PG_FUNCTION_ARGS);
-// ---- NumPoints(geometry)
+/* ---- NumPoints(geometry) */
 Datum LWGEOM_numpoints_linestring(PG_FUNCTION_ARGS);
-// ---- NumGeometries(geometry)
+/* ---- NumGeometries(geometry) */
 Datum LWGEOM_numgeometries_collection(PG_FUNCTION_ARGS);
-// ---- GeometryN(geometry, integer)
+/* ---- GeometryN(geometry, integer) */
 Datum LWGEOM_geometryn_collection(PG_FUNCTION_ARGS);
-// ---- Dimension(geometry)
+/* ---- Dimension(geometry) */
 Datum LWGEOM_dimension(PG_FUNCTION_ARGS);
-// ---- ExteriorRing(geometry)
+/* ---- ExteriorRing(geometry) */
 Datum LWGEOM_exteriorring_polygon(PG_FUNCTION_ARGS);
-// ---- InteriorRingN(geometry, integer)
+/* ---- InteriorRingN(geometry, integer) */
 Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS);
-// ---- NumInteriorRings(geometry)
+/* ---- NumInteriorRings(geometry) */
 Datum LWGEOM_numinteriorrings_polygon(PG_FUNCTION_ARGS);
-// ---- PointN(geometry, integer)
+/* ---- PointN(geometry, integer) */
 Datum LWGEOM_pointn_linestring(PG_FUNCTION_ARGS);
-// ---- X(geometry)
+/* ---- X(geometry) */
 Datum LWGEOM_x_point(PG_FUNCTION_ARGS);
-// ---- Y(geometry)
+/* ---- Y(geometry) */
 Datum LWGEOM_y_point(PG_FUNCTION_ARGS);
-// ---- Z(geometry)
+/* ---- Z(geometry) */
 Datum LWGEOM_z_point(PG_FUNCTION_ARGS);
-// ---- M(geometry)
+/* ---- M(geometry) */
 Datum LWGEOM_m_point(PG_FUNCTION_ARGS);
-// ---- StartPoint(geometry)
+/* ---- StartPoint(geometry) */
 Datum LWGEOM_startpoint_linestring(PG_FUNCTION_ARGS);
-// ---- EndPoint(geometry)
+/* ---- EndPoint(geometry) */
 Datum LWGEOM_endpoint_linestring(PG_FUNCTION_ARGS);
-// ---- AsText(geometry)
+/* ---- AsText(geometry) */
 Datum LWGEOM_asText(PG_FUNCTION_ARGS);
-// ---- AsBinary(geometry, [XDR|NDR])
+/* ---- AsBinary(geometry, [XDR|NDR]) */
 Datum LWGEOM_asBinary(PG_FUNCTION_ARGS);
-// ---- GeometryFromText(text, integer)
+/* ---- GeometryFromText(text, integer) */
 Datum LWGEOM_from_text(PG_FUNCTION_ARGS);
-// ---- GeomFromWKB(bytea, integer)
+/* ---- GeomFromWKB(bytea, integer) */
 Datum LWGEOM_from_WKB(PG_FUNCTION_ARGS);
-// ---- IsClosed(geometry)
+/* ---- IsClosed(geometry) */
 Datum LWGEOM_isclosed_linestring(PG_FUNCTION_ARGS);
 
-// internal
-int32 lwgeom_numpoints_linestring_recursive(char *serialized);
-int32 lwgeom_dimension_recursive(char *serialized);
+/* internal */
+static int32 lwgeom_numpoints_linestring_recursive(const uchar *serialized);
+static int32 lwgeom_dimension_recursive(const uchar *serialized);
 char line_is_closed(LWLINE *line);
 
 /*------------------------------------------------------------------*/
 
-// getSRID(lwgeom) :: int4
+/* getSRID(lwgeom) :: int4 */
 PG_FUNCTION_INFO_V1(LWGEOM_getSRID);
 Datum LWGEOM_getSRID(PG_FUNCTION_ARGS)
 {
@@ -82,7 +94,7 @@ Datum LWGEOM_getSRID(PG_FUNCTION_ARGS)
        PG_RETURN_INT32(srid);
 }
 
-//setSRID(lwgeom, int4) :: lwgeom
+/* setSRID(lwgeom, int4) :: lwgeom */
 PG_FUNCTION_INFO_V1(LWGEOM_setSRID);
 Datum LWGEOM_setSRID(PG_FUNCTION_ARGS)
 {
@@ -98,7 +110,7 @@ Datum LWGEOM_setSRID(PG_FUNCTION_ARGS)
        PG_RETURN_POINTER(result);
 }
 
-//returns a string representation of this geometry's type
+/* returns a string representation of this geometry's type */
 PG_FUNCTION_INFO_V1(LWGEOM_getTYPE);
 Datum LWGEOM_getTYPE(PG_FUNCTION_ARGS)
 {
@@ -114,7 +126,7 @@ Datum LWGEOM_getTYPE(PG_FUNCTION_ARGS)
        text_ob = lwalloc(20+4);
        result = text_ob+4;
 
-       //type = lwgeom_getType(*(lwgeom+4));
+       /*type = lwgeom_getType(*(lwgeom+4)); */
        type = lwgeom_getType(lwgeom->type);
 
        memset(result, 0, 20);
@@ -141,18 +153,20 @@ Datum LWGEOM_getTYPE(PG_FUNCTION_ARGS)
 
        size = strlen(result) +4 ;
 
-       memcpy(text_ob, &size,4); // size of string
+       memcpy(text_ob, &size,4); /* size of string */
 
        PG_FREE_IF_COPY(lwgeom, 0);
 
        PG_RETURN_POINTER(text_ob);
 }
 
-// Find first linestring in serialized geometry and return
-// the number of points in it. If no linestrings are found
-// return -1.
-int32
-lwgeom_numpoints_linestring_recursive(char *serialized)
+/*
+ * Find first linestring in serialized geometry and return
+ * the number of points in it. If no linestrings are found
+ * return -1.
+ */
+static int32
+lwgeom_numpoints_linestring_recursive(const uchar *serialized)
 {
        LWGEOM_INSPECTED *inspected = lwgeom_inspect(serialized);
        int i;
@@ -162,7 +176,7 @@ lwgeom_numpoints_linestring_recursive(char *serialized)
                int32 npoints;
                int type;
                LWLINE *line=NULL;
-               char *subgeom;
+               uchar *subgeom;
 
                line = lwgeom_getline_inspected(inspected, i);
                if (line != NULL)
@@ -178,7 +192,7 @@ lwgeom_numpoints_linestring_recursive(char *serialized)
 
                type = lwgeom_getType(subgeom[0]);
 
-               // MULTILINESTRING && GEOMETRYCOLLECTION are worth checking
+               /* MULTILINESTRING && GEOMETRYCOLLECTION are worth checking */
                if ( type != 7 && type != 5 ) continue;
 
                npoints = lwgeom_numpoints_linestring_recursive(subgeom);
@@ -194,9 +208,11 @@ lwgeom_numpoints_linestring_recursive(char *serialized)
        return -1;
 }
 
-//numpoints(GEOMETRY) -- find the first linestring in GEOMETRY, return
-//the number of points in it.  Return NULL if there is no LINESTRING(..)
-//in GEOMETRY
+/* 
+ * numpoints(GEOMETRY) -- find the first linestring in GEOMETRY, return
+ * the number of points in it.  Return NULL if there is no LINESTRING(..)
+ * in GEOMETRY
+ */
 PG_FUNCTION_INFO_V1(LWGEOM_numpoints_linestring);
 Datum LWGEOM_numpoints_linestring(PG_FUNCTION_ARGS)
 {
@@ -218,7 +234,7 @@ Datum LWGEOM_numgeometries_collection(PG_FUNCTION_ARGS)
        PG_LWGEOM *geom = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
        int type;
        int32 ret;
-       char *serialized = SERIALIZED_FORM(geom);
+       uchar *serialized = SERIALIZED_FORM(geom);
 
        type = lwgeom_getType(geom->type);
        if ( type >= 4 )
@@ -231,7 +247,7 @@ Datum LWGEOM_numgeometries_collection(PG_FUNCTION_ARGS)
        PG_RETURN_NULL();
 }
 
-// 1-based offset
+/* 1-based offset */
 PG_FUNCTION_INFO_V1(LWGEOM_geometryn_collection);
 Datum LWGEOM_geometryn_collection(PG_FUNCTION_ARGS)
 {
@@ -242,17 +258,17 @@ Datum LWGEOM_geometryn_collection(PG_FUNCTION_ARGS)
        LWCOLLECTION *coll;
        LWGEOM *subgeom;
 
-       //elog(NOTICE, "GeometryN called");
+       /* elog(NOTICE, "GeometryN called"); */
 
-       // call is valid on multi* geoms only
+       /* call is valid on multi* geoms only */
        if ( type < 4 )
        {
-               //elog(NOTICE, "geometryn: geom is of type %d, requires >=4", type);
+               /* elog(NOTICE, "geometryn: geom is of type %d, requires >=4", type); */
                PG_RETURN_NULL();
        }
 
        idx = PG_GETARG_INT32(1);
-       idx -= 1; // index is 1-based
+       idx -= 1; /* index is 1-based */
 
        coll = (LWCOLLECTION *)lwgeom_deserialize(SERIALIZED_FORM(geom));
 
@@ -262,7 +278,7 @@ Datum LWGEOM_geometryn_collection(PG_FUNCTION_ARGS)
        subgeom = coll->geoms[idx];
        subgeom->SRID = coll->SRID;
 
-       //COMPUTE_BBOX==TAINTING
+       /* COMPUTE_BBOX==TAINTING */
        if ( coll->bbox ) lwgeom_addBBOX(subgeom);
 
        result = pglwgeom_serialize(subgeom);
@@ -274,12 +290,14 @@ Datum LWGEOM_geometryn_collection(PG_FUNCTION_ARGS)
 
 }
 
-//returns 0 for points, 1 for lines, 2 for polygons.
-//returns max dimension for a collection.
-//returns -1 for the empty geometry (TODO)
-//returns -2 on error
-int32
-lwgeom_dimension_recursive(char *serialized)
+/*
+ * returns 0 for points, 1 for lines, 2 for polygons.
+ * returns max dimension for a collection.
+ * returns -1 for the empty geometry (TODO)
+ * returns -2 on error
+ */
+static int32
+lwgeom_dimension_recursive(const uchar *serialized)
 {
        LWGEOM_INSPECTED *inspected;
        int ret = -1;
@@ -288,7 +306,7 @@ lwgeom_dimension_recursive(char *serialized)
        inspected = lwgeom_inspect(serialized);
        for (i=0; i<inspected->ngeometries; i++)
        {
-               char *subgeom;
+               uchar *subgeom;
                char typeflags = lwgeom_getsubtype_inspected(inspected, i);
                int type = lwgeom_getType(typeflags);
                int dims=-1;
@@ -310,7 +328,7 @@ lwgeom_dimension_recursive(char *serialized)
                        dims = lwgeom_dimension_recursive(subgeom);
                }
 
-               if ( dims == 2 ) { // nothing can be higher
+               if ( dims == 2 ) { /* nothing can be higher */
                                pfree_inspected(inspected);
                                return 2;
                }
@@ -322,9 +340,11 @@ lwgeom_dimension_recursive(char *serialized)
        return ret;
 }
 
-//returns 0 for points, 1 for lines, 2 for polygons.
-//returns max dimension for a collection.
-//returns -1 for the empty geometry (TODO)
+/*
+ * returns 0 for points, 1 for lines, 2 for polygons.
+ * returns max dimension for a collection.
+ * returns -1 for the empty geometry (TODO)
+ */
 PG_FUNCTION_INFO_V1(LWGEOM_dimension);
 Datum LWGEOM_dimension(PG_FUNCTION_ARGS)
 {
@@ -344,9 +364,11 @@ Datum LWGEOM_dimension(PG_FUNCTION_ARGS)
 }
 
 
-// exteriorRing(GEOMETRY) -- find the first polygon in GEOMETRY, return
-// its exterior ring (as a linestring).
-// Return NULL if there is no POLYGON(..) in (first level of) GEOMETRY.
+/*
+ * exteriorRing(GEOMETRY) -- find the first polygon in GEOMETRY, return
+ * its exterior ring (as a linestring).
+ * Return NULL if there is no POLYGON(..) in (first level of) GEOMETRY.
+ */
 PG_FUNCTION_INFO_V1(LWGEOM_exteriorring_polygon);
 Datum LWGEOM_exteriorring_polygon(PG_FUNCTION_ARGS)
 {
@@ -364,12 +386,14 @@ Datum LWGEOM_exteriorring_polygon(PG_FUNCTION_ARGS)
        }
        poly = lwpoly_deserialize(SERIALIZED_FORM(geom));
 
-       // Ok, now we have a polygon. Here is its exterior ring.
+       /* Ok, now we have a polygon. Here is its exterior ring. */
        extring = poly->rings[0];
 
-       // This is a LWLINE constructed by exterior ring POINTARRAY
-       // If the input geom has a bbox, use it for 
-       // the output geom, as exterior ring makes it up !
+       /*
+        * This is a LWLINE constructed by exterior ring POINTARRAY
+        * If the input geom has a bbox, use it for 
+        * the output geom, as exterior ring makes it up !
+        */
        if ( poly->bbox ) bbox=box2d_clone(poly->bbox);
        line = lwline_construct(poly->SRID, bbox, extring);
 
@@ -383,9 +407,11 @@ Datum LWGEOM_exteriorring_polygon(PG_FUNCTION_ARGS)
        PG_RETURN_POINTER(result);
 }
 
-// NumInteriorRings(GEOMETRY) -- find the first polygon in GEOMETRY, return
-// the number of its interior rings (holes).
-// Return NULL if there is no POLYGON(..) in (first level of) GEOMETRY.
+/*
+ * NumInteriorRings(GEOMETRY) -- find the first polygon in GEOMETRY, return
+ * the number of its interior rings (holes).
+ * Return NULL if there is no POLYGON(..) in (first level of) GEOMETRY.
+ */
 PG_FUNCTION_INFO_V1(LWGEOM_numinteriorrings_polygon);
 Datum LWGEOM_numinteriorrings_polygon(PG_FUNCTION_ARGS)
 {
@@ -407,7 +433,7 @@ Datum LWGEOM_numinteriorrings_polygon(PG_FUNCTION_ARGS)
                PG_RETURN_NULL();
        }
 
-       // Ok, now we have a polygon. Here is its number of holes
+       /* Ok, now we have a polygon. Here is its number of holes */
        result = poly->nrings-1;
 
        PG_FREE_IF_COPY(geom, 0);
@@ -417,10 +443,12 @@ Datum LWGEOM_numinteriorrings_polygon(PG_FUNCTION_ARGS)
        PG_RETURN_INT32(result);
 }
 
-// InteriorRingN(GEOMETRY) -- find the first polygon in GEOMETRY, return
-// its Nth interior ring (as a linestring).
-// Return NULL if there is no POLYGON(..) in (first level of) GEOMETRY.
-// Index is 1-based
+/*
+ * InteriorRingN(GEOMETRY) -- find the first polygon in GEOMETRY, return
+ * its Nth interior ring (as a linestring).
+ * Return NULL if there is no POLYGON(..) in (first level of) GEOMETRY.
+ * Index is 1-based
+ */
 PG_FUNCTION_INFO_V1(LWGEOM_interiorringn_polygon);
 Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS)
 {
@@ -435,8 +463,8 @@ Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS)
        wanted_index = PG_GETARG_INT32(1);
        if ( wanted_index < 1 )
        {
-               //elog(ERROR, "InteriorRingN: ring number is 1-based");
-               PG_RETURN_NULL(); // index out of range
+               /* elog(ERROR, "InteriorRingN: ring number is 1-based"); */
+               PG_RETURN_NULL(); /* index out of range */
        }
 
        geom = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
@@ -449,7 +477,7 @@ Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS)
        }
        poly = lwpoly_deserialize(SERIALIZED_FORM(geom));
 
-       // Ok, now we have a polygon. Let's see if it has enough holes
+       /* Ok, now we have a polygon. Let's see if it has enough holes */
        if ( wanted_index >= poly->nrings )
        {
                PG_FREE_IF_COPY(geom, 0);
@@ -459,13 +487,13 @@ Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS)
 
        ring = poly->rings[wanted_index];
 
-       // COMPUTE_BBOX==TAINTING
+       /* COMPUTE_BBOX==TAINTING */
        if ( poly->bbox ) bbox = ptarray_compute_box2d(ring);
 
-       // This is a LWLINE constructed by interior ring POINTARRAY
+       /* This is a LWLINE constructed by interior ring POINTARRAY */
        line = lwline_construct(poly->SRID, bbox, ring);
 
-       // Copy SRID from polygon
+       /* Copy SRID from polygon */
        line->SRID = poly->SRID;
 
        result = pglwgeom_serialize((LWGEOM *)line);
@@ -477,9 +505,11 @@ Datum LWGEOM_interiorringn_polygon(PG_FUNCTION_ARGS)
        PG_RETURN_POINTER(result);
 }
 
-// PointN(GEOMETRY,INTEGER) -- find the first linestring in GEOMETRY,
-// return the point at index INTEGER (1 is 1st point).  Return NULL if
-// there is no LINESTRING(..) in GEOMETRY or INTEGER is out of bounds.
+/*
+ * PointN(GEOMETRY,INTEGER) -- find the first linestring in GEOMETRY,
+ * return the point at index INTEGER (1 is 1st point).  Return NULL if
+ * there is no LINESTRING(..) in GEOMETRY or INTEGER is out of bounds.
+ */
 PG_FUNCTION_INFO_V1(LWGEOM_pointn_linestring);
 Datum LWGEOM_pointn_linestring(PG_FUNCTION_ARGS)
 {
@@ -489,13 +519,13 @@ Datum LWGEOM_pointn_linestring(PG_FUNCTION_ARGS)
        LWLINE *line = NULL;
        POINTARRAY *pts;
        LWPOINT *point;
-       char *serializedpoint;
+       uchar *serializedpoint;
        PG_LWGEOM *result;
        int i;
 
        wanted_index = PG_GETARG_INT32(1);
        if ( wanted_index < 1 )
-               PG_RETURN_NULL(); // index out of range
+               PG_RETURN_NULL(); /* index out of range */
 
        geom = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
        inspected = lwgeom_inspect(SERIALIZED_FORM(geom));
@@ -512,7 +542,7 @@ Datum LWGEOM_pointn_linestring(PG_FUNCTION_ARGS)
                PG_RETURN_NULL();
        }
 
-       // Ok, now we have a line. Let's see if it has enough points.
+       /* Ok, now we have a line. Let's see if it has enough points. */
        if ( wanted_index > line->points->npoints )
        {
                pfree_inspected(inspected);
@@ -522,20 +552,21 @@ Datum LWGEOM_pointn_linestring(PG_FUNCTION_ARGS)
        }
        pfree_inspected(inspected);
 
-       // Construct a point array
+       /* Construct a point array */
        pts = pointArray_construct(getPoint_internal(line->points,
                wanted_index-1),
                TYPE_HASZ(line->type), TYPE_HASM(line->type), 1);
 
-       // Construct an LWPOINT
+       /* Construct an LWPOINT */
        point = lwpoint_construct(pglwgeom_getSRID(geom),
                NULL, pts);
 
-       // Serialized the point
+       /* Serialized the point */
        serializedpoint = lwpoint_serialize(point);
 
-       // And we construct the line
-       // TODO: use serialize_buf above, instead ..
+       /* And we construct the line
+        * TODO: use serialize_buf above, instead ..
+        */
        result = PG_LWGEOM_construct(serializedpoint,
                pglwgeom_getSRID(geom), 0);
 
@@ -626,9 +657,10 @@ Datum LWGEOM_z_point(PG_FUNCTION_ARGS)
        PG_RETURN_FLOAT8(p.z);
 }
 
-// M(GEOMETRY) -- find the first POINT(..) in GEOMETRY, returns its M value.
-// Return NULL if there is no POINT(..) in GEOMETRY.
-// Return NULL if there is no M in this geometry.
+/* M(GEOMETRY) -- find the first POINT(..) in GEOMETRY, returns its M value.
+ * Return NULL if there is no POINT(..) in GEOMETRY.
+ * Return NULL if there is no M in this geometry.
+ */
 PG_FUNCTION_INFO_V1(LWGEOM_m_point);
 Datum LWGEOM_m_point(PG_FUNCTION_ARGS)
 {
@@ -653,9 +685,10 @@ Datum LWGEOM_m_point(PG_FUNCTION_ARGS)
        PG_RETURN_FLOAT8(p.m);
 }
 
-// StartPoint(GEOMETRY) -- find the first linestring in GEOMETRY,
-// return the first point.
-// Return NULL if there is no LINESTRING(..) in GEOMETRY 
+/* StartPoint(GEOMETRY) -- find the first linestring in GEOMETRY,
+ * return the first point.
+ * Return NULL if there is no LINESTRING(..) in GEOMETRY 
+ */
 PG_FUNCTION_INFO_V1(LWGEOM_startpoint_linestring);
 Datum LWGEOM_startpoint_linestring(PG_FUNCTION_ARGS)
 {
@@ -682,17 +715,17 @@ Datum LWGEOM_startpoint_linestring(PG_FUNCTION_ARGS)
                PG_RETURN_NULL();
        }
 
-       // Ok, now we have a line. 
+       /* Ok, now we have a line.  */
 
-       // Construct a point array
+       /* Construct a point array */
        pts = pointArray_construct(getPoint_internal(line->points, 0),
                TYPE_HASZ(line->type),
                TYPE_HASM(line->type), 1);
 
-       // Construct an LWPOINT
+       /* Construct an LWPOINT */
        point = lwpoint_construct(pglwgeom_getSRID(geom), NULL, pts);
 
-       // Construct a PG_LWGEOM
+       /* Construct a PG_LWGEOM */
        result = pglwgeom_serialize((LWGEOM *)point);
 
        lwgeom_release((LWGEOM *)line);
@@ -702,9 +735,10 @@ Datum LWGEOM_startpoint_linestring(PG_FUNCTION_ARGS)
        PG_RETURN_POINTER(result);
 }
 
-// EndPoint(GEOMETRY) -- find the first linestring in GEOMETRY,
-// return the last point.
-// Return NULL if there is no LINESTRING(..) in GEOMETRY 
+/* EndPoint(GEOMETRY) -- find the first linestring in GEOMETRY,
+ * return the last point.
+ * Return NULL if there is no LINESTRING(..) in GEOMETRY 
+ */
 PG_FUNCTION_INFO_V1(LWGEOM_endpoint_linestring);
 Datum LWGEOM_endpoint_linestring(PG_FUNCTION_ARGS)
 {
@@ -732,18 +766,18 @@ Datum LWGEOM_endpoint_linestring(PG_FUNCTION_ARGS)
        }
 
 
-       // Ok, now we have a line. 
+       /* Ok, now we have a line.  */
 
-       // Construct a point array
+       /* Construct a point array */
        pts = pointArray_construct(
                getPoint_internal(line->points, line->points->npoints-1),
                TYPE_HASZ(line->type),
                TYPE_HASM(line->type), 1);
 
-       // Construct an LWPOINT
+       /* Construct an LWPOINT */
        point = (LWGEOM *)lwpoint_construct(pglwgeom_getSRID(geom), NULL, pts);
 
-       // Serialize an PG_LWGEOM
+       /* Serialize an PG_LWGEOM */
        result = pglwgeom_serialize(point);
 
        lwgeom_release(point);
@@ -753,8 +787,13 @@ Datum LWGEOM_endpoint_linestring(PG_FUNCTION_ARGS)
        PG_RETURN_POINTER(result);
 }
 
-//given wkt and SRID, return a geometry
-//actually we cheat, postgres will convert the string to a geometry for us...
+/*
+ * Given an OGC WKT (and optionally a SRID) 
+ * return a geometry.
+ * Note that this is a a stricter version
+ * of geometry_in, where we refuse to
+ * accept (HEX)WKB or EWKT.
+ */
 PG_FUNCTION_INFO_V1(LWGEOM_from_text);
 Datum LWGEOM_from_text(PG_FUNCTION_ARGS)
 {
@@ -767,7 +806,7 @@ Datum LWGEOM_from_text(PG_FUNCTION_ARGS)
 
        size = VARSIZE(wkttext)-VARHDRSZ;
 
-       //lwnotice("size: %d", size);
+       /*lwnotice("size: %d", size); */
 
        if ( size < 10 )
        {
@@ -776,8 +815,13 @@ Datum LWGEOM_from_text(PG_FUNCTION_ARGS)
        }
 
        fc=*(VARDATA(wkttext));
-       // 'S' is accepted for backward compatibility, a WARNING
-       // is issued later.
+
+       /*
+        * 'S' is accepted for backward compatibility, a WARNING
+        * is issued later.
+        *
+        * TODO: reject 'SRID=x;..' form...
+        */
        if ( fc!='P' && fc!='L' && fc!='M' && fc!='G' && fc!='S' )
        {
                lwerror("Invalid OGC WKT (does not start with P,L,M or G)");
@@ -788,7 +832,7 @@ Datum LWGEOM_from_text(PG_FUNCTION_ARGS)
        memcpy(wkt, VARDATA(wkttext), size);
        wkt[size]='\0';
 
-       //lwnotice("wkt: [%s]", wkt);
+       /* lwnotice("wkt: [%s]", wkt); */
 
        geom = (PG_LWGEOM *)parse_lwgeom_wkt(wkt);
 
@@ -799,7 +843,7 @@ Datum LWGEOM_from_text(PG_FUNCTION_ARGS)
                elog(WARNING, "OGC WKT expected, EWKT provided - use GeomFromEWKT() for this");
        }
 
-       // read user-requested SRID if any
+       /* read user-requested SRID if any */
        if ( PG_NARGS() > 1 ) lwgeom->SRID = PG_GETARG_INT32(1);
 
        result = pglwgeom_serialize(lwgeom);
@@ -810,7 +854,14 @@ Datum LWGEOM_from_text(PG_FUNCTION_ARGS)
        PG_RETURN_POINTER(result);
 }
 
-//given wkb and SRID, return a geometry
+/*
+ * Given an OGC WKB (and optionally a SRID)
+ * return a geometry.
+ *
+ * Note that this is a wrapper around 
+ * LWGEOMFromWKB, where we refuse to
+ * accept EWKB.
+ */
 PG_FUNCTION_INFO_V1(LWGEOM_from_WKB);
 Datum LWGEOM_from_WKB(PG_FUNCTION_ARGS)
 {
@@ -827,7 +878,7 @@ Datum LWGEOM_from_WKB(PG_FUNCTION_ARGS)
        }
 
 
-       // read user-requested SRID if any
+       /* read user-requested SRID if any */
        if ( PG_NARGS() > 1 )
        {
                SRID = PG_GETARG_INT32(1);
@@ -843,7 +894,7 @@ Datum LWGEOM_from_WKB(PG_FUNCTION_ARGS)
        PG_RETURN_POINTER(result);
 }
 
-//convert LWGEOM to wkt (in TEXT format)
+/* convert LWGEOM to wkt (in TEXT format) */
 PG_FUNCTION_INFO_V1(LWGEOM_asText);
 Datum LWGEOM_asText(PG_FUNCTION_ARGS)
 {
@@ -866,7 +917,7 @@ Datum LWGEOM_asText(PG_FUNCTION_ARGS)
 
        semicolonLoc = strchr(result_cstring,';');
 
-       //loc points to start of wkt
+       /* loc points to start of wkt */
        if (semicolonLoc == NULL) loc_wkt = result_cstring;
        else loc_wkt = semicolonLoc +1;
 
@@ -883,7 +934,7 @@ Datum LWGEOM_asText(PG_FUNCTION_ARGS)
        PG_RETURN_POINTER(result);
 }
 
-//convert LWGEOM to wkb (in BINARY format)
+/* convert LWGEOM to wkb (in BINARY format) */
 PG_FUNCTION_INFO_V1(LWGEOM_asBinary);
 Datum LWGEOM_asBinary(PG_FUNCTION_ARGS)
 {
@@ -933,10 +984,12 @@ char line_is_closed(LWLINE *line)
        return 1;
 }
 
-// IsClosed(GEOMETRY) if geometry is a linestring then returns
-// startpoint == endpoint.  If its not a linestring then return NULL.
-// If it's a collection containing multiple linestrings,
-// return true only if all the linestrings have startpoint=endpoint.
+/*
+ * IsClosed(GEOMETRY) if geometry is a linestring then returns
+ * startpoint == endpoint.  If its not a linestring then return NULL.
+ * If it's a collection containing multiple linestrings,
+ * return true only if all the linestrings have startpoint=endpoint.
+ */
 PG_FUNCTION_INFO_V1(LWGEOM_isclosed_linestring);
 Datum LWGEOM_isclosed_linestring(PG_FUNCTION_ARGS)
 {
index 4150dc3e7587a66ea8f32a623c2141af9fc4008b..4cdab3511864c869fbf09c2c90a833e9e4908fa9 100644 (file)
@@ -6,7 +6,9 @@
 #include "liblwgeom.h"
 #include "lwgeom_pg.h"
 
-//#undef PGIS_DEBUG
+/* #undef PGIS_DEBUG */
+
+#define PARANOIA_LEVEL 1
 
 void *
 pg_alloc(size_t size)
@@ -109,15 +111,18 @@ pglwgeom_serialize(LWGEOM *in)
 #endif
 
        size = lwgeom_serialize_size(in) + VARHDRSZ;
-       //lwnotice("lwgeom_serialize_size returned %d", size-VARHDRSZ);
+       /* lwnotice("lwgeom_serialize_size returned %d", size-VARHDRSZ); */
        result = palloc(size);
        result->size = (size);
        lwgeom_serialize_buf(in, SERIALIZED_FORM(result), &size);
+
+#if PARANOIA_LEVEL > 0
        if ( size != result->size-VARHDRSZ )
        {
                lwerror("pglwgeom_serialize: serialized size:%d, computed size:%d", size, result->size-VARHDRSZ);
                return NULL;
        }
+#endif
 
        return result;
 }
@@ -172,22 +177,22 @@ PG_LWGEOM_construct(uchar *ser, int SRID, int wantbbox)
        /* COMPUTE_BBOX FOR_COMPLEX_GEOMS */
        if ( is_worth_caching_serialized_bbox(ser) ) 
        {
-               // if ( ! wantbbox ) elog(NOTICE, "PG_LWGEOM_construct forced wantbbox=1 due to rule FOR_COMPLEX_GEOMS");
+               /* if ( ! wantbbox ) elog(NOTICE, "PG_LWGEOM_construct forced wantbbox=1 due to rule FOR_COMPLEX_GEOMS"); */
                wantbbox=1;
        }
 
        size = lwgeom_size(ser);
-       eptr = ser+size; // end of subgeom
+       eptr = ser+size; /* end of subgeom */
 
-       iptr = ser+1; // skip type
+       iptr = ser+1; /* skip type */
        if ( lwgeom_hasSRID(ser[0]) )
        {
-               iptr += 4; // skip SRID
+               iptr += 4; /* skip SRID */
                size -= 4;
        }
        if ( lwgeom_hasBBOX(ser[0]) )
        {
-               iptr += sizeof(BOX2DFLOAT4); // skip BBOX
+               iptr += sizeof(BOX2DFLOAT4); /* skip BBOX */
                size -= sizeof(BOX2DFLOAT4);
        }
 
@@ -202,7 +207,7 @@ PG_LWGEOM_construct(uchar *ser, int SRID, int wantbbox)
                getbox2d_p(ser, &box);
        }
 
-       size+=4; // size header
+       size+=4; /* size header */
 
        result = lwalloc(size);
        result->size = size;
@@ -226,3 +231,158 @@ PG_LWGEOM_construct(uchar *ser, int SRID, int wantbbox)
        return result;
 }
 
+/*
+ * Make a PG_LWGEOM object from a WKB binary representation.
+ * Currently unoptimized as it will convert WKB to HEXWKB first.
+ */
+PG_LWGEOM *
+pglwgeom_from_ewkb(uchar *ewkb, size_t ewkblen)
+{
+       PG_LWGEOM *ret;
+       char *hexewkb;
+       size_t hexewkblen = ewkblen*2;
+       int i;
+
+       hexewkb = lwalloc(hexewkblen+1);
+       for (i=0; i<ewkblen; i++)
+       {
+               deparse_hex(ewkb[i], &hexewkb[i*2]);
+       }
+       hexewkb[hexewkblen] = '\0';
+
+       ret = (PG_LWGEOM *)parse_lwgeom_wkt(hexewkb);
+
+       lwfree(hexewkb);
+
+       return ret;
+}
+
+/*
+ * Return the EWKB (binary) representation for a PG_LWGEOM.
+ */
+char *
+pglwgeom_to_ewkb(PG_LWGEOM *geom, char byteorder, size_t *outsize)
+{
+       uchar *srl = &(geom->type);
+       return unparse_WKB(srl, lwalloc, lwfree,
+               byteorder, outsize, 0);
+}
+
+/*
+ * Set the SRID of a PG_LWGEOM
+ * Returns a newly allocated PG_LWGEOM object.
+ * Allocation will be done using the lwalloc.
+ */
+PG_LWGEOM *
+pglwgeom_setSRID(PG_LWGEOM *lwgeom, int32 newSRID)
+{
+       uchar type = lwgeom->type;
+       int bbox_offset=0; /* 0=no bbox, otherwise sizeof(BOX2DFLOAT4) */
+       int len,len_new,len_left;
+       PG_LWGEOM *result;
+       uchar *loc_new, *loc_old;
+
+       if (lwgeom_hasBBOX(type))
+               bbox_offset = sizeof(BOX2DFLOAT4);
+
+       len = lwgeom->size;
+
+       if (lwgeom_hasSRID(type))
+       {
+               if ( newSRID != -1 ) {
+                       /* we create a new one and copy the SRID in */
+                       result = lwalloc(len);
+                       memcpy(result, lwgeom, len);
+                       memcpy(result->data+bbox_offset, &newSRID,4);
+               } else {
+                       /* we create a new one dropping the SRID */
+                       result = lwalloc(len-4);
+                       result->size = len-4;
+                       result->type = lwgeom_makeType_full(
+                               TYPE_HASZ(type), TYPE_HASM(type),
+                               0, lwgeom_getType(type),
+                               lwgeom_hasBBOX(type));
+                       loc_new = result->data;
+                       loc_old = lwgeom->data;
+                       len_left = len-4-1;
+
+                       /* handle bbox (if there) */
+                       if (lwgeom_hasBBOX(type))
+                       {
+                               memcpy(loc_new, loc_old, sizeof(BOX2DFLOAT4));
+                               loc_new += sizeof(BOX2DFLOAT4);
+                               loc_old += sizeof(BOX2DFLOAT4);
+                               len_left -= sizeof(BOX2DFLOAT4);
+                       }
+
+                       /* skip SRID, copy the remaining */
+                       loc_old += 4;
+                       len_left -= 4;
+                       memcpy(loc_new, loc_old, len_left);
+
+               }
+
+       }
+       else 
+       {
+               /* just copy input, already w/out a SRID */
+               if ( newSRID == -1 ) {
+                       result = lwalloc(len);
+                       memcpy(result, lwgeom, len);
+               }
+               /* need to add one */
+               else {
+                       len_new = len + 4; /* +4 for SRID */
+                       result = lwalloc(len_new);
+                       memcpy(result, &len_new, 4); /* size copy in */
+                       result->type = lwgeom_makeType_full(
+                               TYPE_HASZ(type), TYPE_HASM(type),
+                               1, lwgeom_getType(type),lwgeom_hasBBOX(type));
+
+                       loc_new = result->data;
+                       loc_old = lwgeom->data;
+
+                       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);
+               }
+       }
+       return result;
+}
+
+/*
+ * get the SRID from the LWGEOM
+ * none present => -1
+ */
+int
+pglwgeom_getSRID(PG_LWGEOM *lwgeom)
+{
+       uchar type = lwgeom->type;
+       uchar *loc = lwgeom->data;
+
+       if ( ! lwgeom_hasSRID(type)) return -1;
+
+       if (lwgeom_hasBBOX(type))
+       {
+               loc += sizeof(BOX2DFLOAT4);
+       }
+
+       return get_int32(loc);
+}
+
+
+
index b8e40bf05c98220326195a1bd13cd14182dd09c6..06439a75df5d32db68e7c5ae64845128f40fb645 100644 (file)
@@ -22,8 +22,16 @@ void pg_error(const char *msg, ...);
 void pg_notice(const char *msg, ...);
 
 /* Serialize/deserialize a PG_LWGEOM (postgis datatype) */
-PG_LWGEOM *pglwgeom_serialize(LWGEOM *lwgeom);
-LWGEOM *pglwgeom_deserialize(PG_LWGEOM *pglwgeom);
+extern PG_LWGEOM *pglwgeom_serialize(LWGEOM *lwgeom);
+extern LWGEOM *pglwgeom_deserialize(PG_LWGEOM *pglwgeom);
+
+/* PG_LWGEOM WKB IO */
+extern PG_LWGEOM *pglwgeom_from_ewkb(uchar *ewkb, size_t ewkblen);
+extern char *pglwgeom_to_ewkb(PG_LWGEOM *geom, char byteorder, size_t *ewkblen);
+
+/* PG_LWGEOM SRID get/set */
+extern PG_LWGEOM *pglwgeom_setSRID(PG_LWGEOM *pglwgeom, int32 newSRID);
+extern int pglwgeom_getSRID(PG_LWGEOM *pglwgeom);
 
 extern Oid getGeometryOID(void);
 
index fbd30ea90446eee46155be7eb3346d89749673e8..88a26d7c26bb6e4fd2e6c4344842a100a8a193f2 100644 (file)
@@ -12,7 +12,7 @@
 #include "wktparse.h"
 
 /*
-//To get byte order
+ * To get byte order
 #include <sys/types.h>
 #include <netinet/in.h>
 #include <netinet/in_systm.h>
@@ -31,8 +31,8 @@ struct tag_outputstate{
 };
 
 typedef struct tag_outputstate output_state;
-typedef void (*output_func)(tuple* this,output_state* out);
-typedef void (*read_col_func)(const uchar**f);
+typedef void (*output_func)(tuple* this, output_state* out);
+typedef void (*read_col_func)(const char **f);
 
 
 
@@ -94,7 +94,7 @@ double *first_point=NULL;
 double *last_point=NULL;
 
 /* External functions */
-extern void init_parser(const uchar *);
+extern void init_parser(const char *);
 
 /* Prototypes */
 tuple* alloc_tuple(output_func of,size_t size);
@@ -131,19 +131,19 @@ void alloc_geomertycollection(void);
 void alloc_counter(void);
 void alloc_empty(void);
 uchar* make_lwgeom(void);
-uchar strhex_readbyte(const uchar* in);
-uchar read_wkb_byte(const uchar** in);
-void read_wkb_bytes(const uchar** in,uchar* out, int cnt);
-int4 read_wkb_int(const uchar** in);
-double read_wkb_double(const uchar** in,int convert_from_int);
-void read_wkb_point(const uchar** b);
-void read_collection(const uchar** b,read_col_func f);
-void read_collection2(const uchar** b);
-void parse_wkb(const uchar** b);
-void alloc_wkb(const uchar* parser);
-uchar* parse_it(const uchar* geometry,allocator allocfunc,report_error errfunc);
-uchar* parse_lwg(const uchar* geometry,allocator allocfunc,report_error errfunc);
-uchar* parse_lwgi(const uchar* geometry,allocator allocfunc,report_error errfunc);
+uchar strhex_readbyte(const char *in);
+uchar read_wkb_byte(const char **in);
+void read_wkb_bytes(const char **in, uchar* out, int cnt);
+int4 read_wkb_int(const char **in);
+double read_wkb_double(const char **in, int convert_from_int);
+void read_wkb_point(const char **b);
+void read_collection(const char **b, read_col_func f);
+void read_collection2(const char **b);
+void parse_wkb(const char **b);
+void alloc_wkb(const char *parser);
+uchar* parse_it(const char* geometry, allocator allocfunc, report_error errfunc);
+uchar* parse_lwg(const char* geometry, allocator allocfunc, report_error errfunc);
+uchar* parse_lwgi(const char* geometry, allocator allocfunc, report_error errfunc);
 
 void
 set_srid(double d_srid)
@@ -352,7 +352,7 @@ alloc_lwgeom(int srid)
        the_geom.hasZ=0;
        the_geom.hasM=0;
 
-       //Free if used already
+       /* Free if used already */
        if ( the_geom.first ){
                free_tuple(the_geom.first);
                the_geom.first=the_geom.last=NULL;
@@ -463,13 +463,13 @@ write_type(tuple* this,output_state* out)
 {
        uchar type=0;
 
-       //Empty handler - switch back
+       /* Empty handler - switch back */
        if ( this->uu.nn.type == 0xff )
                this->uu.nn.type = COLLECTIONTYPE;
 
        type |= this->uu.nn.type;
 
-       if (the_geom.ndims) //Support empty
+       if (the_geom.ndims) /* Support empty */
        {
                TYPE_SETZM(type, the_geom.hasZ, the_geom.hasM);
        }
@@ -482,7 +482,7 @@ write_type(tuple* this,output_state* out)
        out->pos++;
 
        if ( the_geom.srid != -1 ){
-               //Only the first geometry will have a srid attached
+               /* Only the first geometry will have a srid attached */
                WRITE_INT4(out,the_geom.srid);
                the_geom.srid = -1;
        }
@@ -572,20 +572,20 @@ void
 alloc_empty(void)
 {
        tuple* st = the_geom.stack;
-       //Find the last geometry
+       /* Find the last geometry */
        while(st->uu.nn.type == 0){
                st =st->uu.nn.stack_next;
        }
 
-       //Reclaim memory
+       /* Reclaim memory */
        free_tuple(st->next);
 
-       //Put an empty geometry collection on the top of the stack
+       /* Put an empty geometry collection on the top of the stack */
        st->next=NULL;
        the_geom.stack=st;
        the_geom.alloc_size=st->uu.nn.size_here;
 
-       //Mark as a empty stop
+       /* Mark as a empty stop */
        if (st->uu.nn.type != 0xFF){
                st->uu.nn.type=0xFF;
                st->of = write_type_count;
@@ -612,7 +612,7 @@ make_lwgeom(void)
                cur=cur->next;
        }
 
-       //if ints shrink then we need to rewrite the size smaller
+       /* if ints shrink then we need to rewrite the size smaller */
        out.pos = out_c;
        write_size(NULL,&out);
 
@@ -623,7 +623,7 @@ int
 lwg_parse_yyerror(char* s)
 {
        error("parse error - invalid geometry");
-       //error_func("parse error - invalid geometry");
+       /* error_func("parse error - invalid geometry"); */
        return 1;
 }
 
@@ -654,7 +654,7 @@ static const uchar to_hex[]  = {
        255,255,255,255,255,255,255,255};
 
 uchar
-strhex_readbyte(const uchar* in)
+strhex_readbyte(const char* in)
 {
        if ( *in == 0 ){
                if ( ! ferror_occured){
@@ -666,7 +666,7 @@ strhex_readbyte(const uchar* in)
 }
 
 uchar
-read_wkb_byte(const uchar** in)
+read_wkb_byte(const char** in)
 {
        uchar ret = strhex_readbyte(*in);
        (*in)+=2;
@@ -676,7 +676,7 @@ read_wkb_byte(const uchar** in)
 int swap_order;
 
 void
-read_wkb_bytes(const uchar** in,uchar* out, int cnt)
+read_wkb_bytes(const char** in, uchar* out, int cnt)
 {
        if ( ! swap_order ){
                while(cnt--) *out++ = read_wkb_byte(in);
@@ -688,7 +688,7 @@ read_wkb_bytes(const uchar** in,uchar* out, int cnt)
 }
 
 int4
-read_wkb_int(const uchar** in)
+read_wkb_int(const char** in)
 {
        int4 ret=0;
        read_wkb_bytes(in,(uchar*)&ret,4);
@@ -696,7 +696,7 @@ read_wkb_int(const uchar** in)
 }
 
 double
-read_wkb_double(const uchar** in,int convert_from_int)
+read_wkb_double(const char** in, int convert_from_int)
 {
        double ret=0;
 
@@ -711,15 +711,17 @@ read_wkb_double(const uchar** in,int convert_from_int)
 }
 
 void
-read_wkb_point(const uchar** b)
+read_wkb_point(const char **b)
 {
        int i;
        tuple* p = NULL;
 
 
        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.
+               /*
+                * 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;
@@ -749,7 +751,7 @@ read_wkb_point(const uchar** b)
 }
 
 void
-read_collection(const uchar** b,read_col_func f)
+read_collection(const char **b, read_col_func f)
 {
        int4 cnt=read_wkb_int(b);
        alloc_counter();
@@ -763,13 +765,13 @@ read_collection(const uchar** b,read_col_func f)
 }
 
 void
-read_collection2(const uchar** b)
+read_collection2(const char **b)
 {
-       return read_collection(b,read_wkb_point);
+       read_collection(b, read_wkb_point);
 }
 
 void
-parse_wkb(const uchar** b)
+parse_wkb(const char **b)
 {
        int4 type;
        uchar xdr = read_wkb_byte(b);
@@ -784,7 +786,7 @@ parse_wkb(const uchar** b)
 
        type = read_wkb_int(b);
 
-       //quick exit on error
+       /* quick exit on error */
        if ( ferror_occured ) return;
 
        the_geom.ndims=2;
@@ -803,7 +805,7 @@ parse_wkb(const uchar** b)
 
        if (type & WKBSRIDFLAG )
        {
-               // local (in-EWKB) srid spec overrides SRID=#; 
+               /* local (in-EWKB) srid spec overrides SRID=#; */
                localsrid = read_wkb_int(b);
                if ( localsrid != -1 )
                {
@@ -822,7 +824,7 @@ parse_wkb(const uchar** b)
                        alloc_stack_tuple(type,write_type,1);
        }
        else{
-               //If we are writing lwg and are reading wbki
+               /* If we are writing lwg and are reading wbki */
                int4 towrite=type;
                if (towrite > COLLECTIONTYPE ){
                        towrite-=9;
@@ -876,7 +878,7 @@ parse_wkb(const uchar** b)
 
 
 void
-alloc_wkb(const uchar* parser)
+alloc_wkb(const char *parser)
 {
        parse_wkb(&parser);
 }
@@ -885,7 +887,7 @@ alloc_wkb(const uchar* parser)
        Parse a string and return a LW_GEOM
 */
 uchar *
-parse_it(const uchar* geometry,allocator allocfunc,report_error errfunc)
+parse_it(const char *geometry, allocator allocfunc, report_error errfunc)
 {
 
        local_malloc = allocfunc;
@@ -904,14 +906,14 @@ parse_it(const uchar* geometry,allocator allocfunc,report_error errfunc)
 }
 
 uchar *
-parse_lwg(const uchar* geometry,allocator allocfunc,report_error errfunc)
+parse_lwg(const char* geometry,allocator allocfunc,report_error errfunc)
 {
        the_geom.lwgi=0;
        return parse_it(geometry,allocfunc,errfunc);
 }
 
 uchar *
-parse_lwgi(const uchar* geometry,allocator allocfunc,report_error errfunc)
+parse_lwgi(const char* geometry,allocator allocfunc,report_error errfunc)
 {
        the_geom.lwgi=1;
        return parse_it(geometry,allocfunc,errfunc);
index 7b0ce73022d9181b3423c9aea4282dc30c5f12ac..d013a13cebf4c1f6a13b3fe3be3496e245ae8a20 100644 (file)
@@ -1,16 +1,17 @@
-// basic LWPOLY manipulation
+/* basic LWPOLY manipulation */
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "liblwgeom.h"
 
-//#define PGIS_DEBUG_CALLS 1
+/*#define PGIS_DEBUG_CALLS 1 */
 
 #define CHECK_POLY_RINGS_ZM 1
 
-// construct a new LWPOLY.  arrays (points/points per ring) will NOT be copied
-// use SRID=-1 for unknown SRID (will have 8bit type's S = 0)
+/* construct a new LWPOLY.  arrays (points/points per ring) will NOT be copied
+ * use SRID=-1 for unknown SRID (will have 8bit type's S = 0)
+ */
 LWPOLY *
 lwpoly_construct(int SRID, BOX2DFLOAT4 *bbox, unsigned int nrings, POINTARRAY **points)
 {
@@ -47,10 +48,12 @@ lwpoly_construct(int SRID, BOX2DFLOAT4 *bbox, unsigned int nrings, POINTARRAY **
 }
 
 
-// given the LWPOLY serialized form (or a pointer into a muli* one)
-// construct a proper LWPOLY.
-// serialized_form should point to the 8bit type format (with type = 3)
-// See serialized form doc
+/*
+ * given the LWPOLY serialized form (or a pointer into a muli* one)
+ * construct a proper LWPOLY.
+ * serialized_form should point to the 8bit type format (with type = 3)
+ * See serialized form doc
+ */
 LWPOLY *
 lwpoly_deserialize(uchar *serialized_form)
 {
@@ -60,7 +63,7 @@ lwpoly_deserialize(uchar *serialized_form)
        int ndims, hasz, hasm;
        uint32 npoints;
        uchar type;
-       char  *loc;
+       uchar *loc;
        int t;
 
        if (serialized_form == NULL)
@@ -71,7 +74,7 @@ lwpoly_deserialize(uchar *serialized_form)
 
        result = (LWPOLY*) lwalloc(sizeof(LWPOLY));
 
-       type = (unsigned  char) serialized_form[0];
+       type = serialized_form[0];
        result->type = type;
 
        ndims = TYPE_NDIMS(type);
@@ -103,7 +106,7 @@ lwpoly_deserialize(uchar *serialized_form)
        if ( lwgeom_hasSRID(type))
        {
                result->SRID = get_int32(loc);
-               loc +=4; // type + SRID
+               loc +=4; /* type + SRID */
        }
        else
        {
@@ -117,7 +120,7 @@ lwpoly_deserialize(uchar *serialized_form)
 
        for (t =0;t<nrings;t++)
        {
-               //read in a single ring and make a PA
+               /* read in a single ring and make a PA */
                npoints = get_uint32(loc);
                loc +=4;
 
@@ -128,9 +131,11 @@ lwpoly_deserialize(uchar *serialized_form)
        return result;
 }
 
-// create the serialized form of the polygon
-// result's first char will be the 8bit type.  See serialized form doc
-// points copied
+/*
+ * create the serialized form of the polygon
+ * result's first char will be the 8bit type.  See serialized form doc
+ * points copied
+ */
 uchar *
 lwpoly_serialize(LWPOLY *poly)
 {
@@ -149,15 +154,17 @@ lwpoly_serialize(LWPOLY *poly)
        return result;
 }
 
-// create the serialized form of the polygon writing it into the
-// given buffer, and returning number of bytes written into
-// the given int pointer.
-// result's first char will be the 8bit type.  See serialized form doc
-// points copied
+/*
+ * create the serialized form of the polygon writing it into the
+ * given buffer, and returning number of bytes written into
+ * the given int pointer.
+ * result's first char will be the 8bit type.  See serialized form doc
+ * points copied
+ */
 void
 lwpoly_serialize_buf(LWPOLY *poly, uchar *buf, size_t *retsize)
 {
-       size_t size=1;  // type byte
+       size_t size=1;  /* type byte */
        char hasSRID;
        int t;
        uchar *loc;
@@ -171,8 +178,8 @@ lwpoly_serialize_buf(LWPOLY *poly, uchar *buf, size_t *retsize)
 
        hasSRID = (poly->SRID != -1);
 
-       size += 4; // nrings
-       size += 4*poly->nrings; //npoints/ring
+       size += 4; /* nrings */
+       size += 4*poly->nrings; /* npoints/ring */
 
        buf[0] = (uchar) lwgeom_makeType_full(
                TYPE_HASZ(poly->type), TYPE_HASM(poly->type),
@@ -182,7 +189,7 @@ lwpoly_serialize_buf(LWPOLY *poly, uchar *buf, size_t *retsize)
        if (poly->bbox)
        {
                memcpy(loc, poly->bbox, sizeof(BOX2DFLOAT4));
-               size += sizeof(BOX2DFLOAT4); // bvol
+               size += sizeof(BOX2DFLOAT4); /* bvol */
                loc += sizeof(BOX2DFLOAT4);
        }
 
@@ -190,10 +197,10 @@ lwpoly_serialize_buf(LWPOLY *poly, uchar *buf, size_t *retsize)
        {
                memcpy(loc, &poly->SRID, sizeof(int32));
                loc += 4;
-               size +=4;  //4 byte SRID
+               size +=4;  /* 4 byte SRID */
        }
 
-       memcpy(loc, &poly->nrings, sizeof(int32));  // nrings
+       memcpy(loc, &poly->nrings, sizeof(int32));  /* nrings */
        loc+=4;
 
        for (t=0;t<poly->nrings;t++)
@@ -207,13 +214,13 @@ lwpoly_serialize_buf(LWPOLY *poly, uchar *buf, size_t *retsize)
 
                npoints = pa->npoints;
 
-               memcpy(loc, &npoints, sizeof(uint32)); //npoints this ring
+               memcpy(loc, &npoints, sizeof(uint32)); /* npoints this ring */
                loc+=4;
 
                pasize = npoints*ptsize;
                size += pasize;
 
-               // copy points
+               /* copy points */
                memcpy(loc, getPoint_internal(pa, 0), pasize);
                loc += pasize;
 
@@ -223,24 +230,24 @@ lwpoly_serialize_buf(LWPOLY *poly, uchar *buf, size_t *retsize)
 }
 
 
-// find bounding box (standard one)  zmin=zmax=0 if 2d (might change to NaN)
+/* find bounding box (standard one)  zmin=zmax=0 if 2d (might change to NaN) */
 BOX3D *
 lwpoly_compute_box3d(LWPOLY *poly)
 {
        BOX3D *result;
 
-       // just need to check outer ring -- interior rings are inside
+       /* just need to check outer ring -- interior rings are inside */
        POINTARRAY *pa = poly->rings[0];  
        result  = ptarray_compute_box3d(pa);
 
        return result;
 }
 
-//find length of this serialized polygon
+/* find length of this serialized polygon */
 size_t
 lwgeom_size_poly(const uchar *serialized_poly)
 {
-       uint32 result = 1; // char type
+       uint32 result = 1; /* char type */
        uint32 nrings;
        int ndims;
        int t;
@@ -276,7 +283,7 @@ lwgeom_size_poly(const uchar *serialized_poly)
 #ifdef PGIS_DEBUG
                lwnotice("lwgeom_size_poly: has srid");
 #endif
-               loc +=4; // type + SRID
+               loc +=4; /* type + SRID */
                result += 4;
        }
 
@@ -288,7 +295,7 @@ lwgeom_size_poly(const uchar *serialized_poly)
 
        for (t =0;t<nrings;t++)
        {
-               //read in a single ring and make a PA
+               /* read in a single ring and make a PA */
                npoints = get_uint32(loc);
                loc += 4;
                result += 4;
@@ -312,14 +319,14 @@ lwgeom_size_poly(const uchar *serialized_poly)
        return result;
 }
 
-// find length of this deserialized polygon
+/* find length of this deserialized polygon */
 size_t
 lwpoly_serialize_size(LWPOLY *poly)
 {
-       size_t size = 1; // type
+       size_t size = 1; /* type */
        uint32 i;
 
-       if ( poly->SRID != -1 ) size += 4; // SRID
+       if ( poly->SRID != -1 ) size += 4; /* SRID */
        if ( poly->bbox ) size += sizeof(BOX2DFLOAT4);
 
 #ifdef PGIS_DEBUG_CALLS
@@ -327,11 +334,11 @@ lwpoly_serialize_size(LWPOLY *poly)
                        poly, poly->nrings);
 #endif
 
-       size += 4; // nrings
+       size += 4; /* nrings */
 
        for (i=0; i<poly->nrings; i++)
        {
-               size += 4; // npoints
+               size += 4; /* npoints */
                size += poly->rings[i]->npoints*TYPE_NDIMS(poly->type)*sizeof(double);
        }
 
@@ -387,7 +394,7 @@ lwpoly_compute_box2d_p(LWPOLY *poly, BOX2DFLOAT4 *box)
        return 1;
 }
 
-// Clone LWLINE object. POINTARRAY are not copied, it's ring array is.
+/* Clone LWLINE object. POINTARRAY are not copied, it's ring array is. */
 LWPOLY *
 lwpoly_clone(const LWPOLY *g)
 {
@@ -399,10 +406,12 @@ lwpoly_clone(const LWPOLY *g)
        return ret;
 }
 
-// Add 'what' to this poly at position 'where'.
-// where=0 == prepend
-// where=-1 == append
-// Returns a MULTIPOLYGON or a GEOMETRYCOLLECTION
+/*
+ * Add 'what' to this poly at position 'where'.
+ * where=0 == prepend
+ * where=-1 == append
+ * Returns a MULTIPOLYGON or a GEOMETRYCOLLECTION
+ */
 LWGEOM *
 lwpoly_add(const LWPOLY *to, uint32 where, const LWGEOM *what)
 {
@@ -416,28 +425,29 @@ lwpoly_add(const LWPOLY *to, uint32 where, const LWGEOM *what)
                return NULL;
        }
 
-       // dimensions compatibility are checked by caller
+       /* dimensions compatibility are checked by caller */
 
-       // Construct geoms array
+       /* Construct geoms array */
        geoms = lwalloc(sizeof(LWGEOM *)*2);
-       if ( where == -1 ) // append
+       if ( where == -1 ) /* append */
        {
                geoms[0] = lwgeom_clone((LWGEOM *)to);
                geoms[1] = lwgeom_clone(what);
        }
-       else // prepend
+       else /* prepend */
        {
                geoms[0] = lwgeom_clone(what);
                geoms[1] = lwgeom_clone((LWGEOM *)to);
        }
-       // reset SRID and wantbbox flag from component types
+
+       /* reset SRID and wantbbox flag from component types */
        geoms[0]->SRID = geoms[1]->SRID = -1;
        TYPE_SETHASSRID(geoms[0]->type, 0);
        TYPE_SETHASSRID(geoms[1]->type, 0);
        TYPE_SETHASBBOX(geoms[0]->type, 0);
        TYPE_SETHASBBOX(geoms[1]->type, 0);
 
-       // Find appropriate geom type
+       /* Find appropriate geom type */
        if ( TYPE_GETTYPE(what->type) == POLYGONTYPE ) newtype = MULTIPOLYGONTYPE;
        else newtype = COLLECTIONTYPE;
 
@@ -492,8 +502,10 @@ lwpoly_segmentize2d(LWPOLY *poly, double dist)
                poly->nrings, newrings);
 }
 
-// check coordinate equality 
-// ring and coordinate order is considered
+/*
+ * check coordinate equality 
+ * ring and coordinate order is considered
+ */
 char
 lwpoly_same(const LWPOLY *p1, const LWPOLY *p2)
 {
index 471865518720b4390fefdaefe1a98aa7fcb01ac5..e110299fbb48f1e22bd54e36a98b4cc357a12f96 100644 (file)
@@ -18,14 +18,14 @@ typedef void* (*allocator)(size_t size);
 typedef void  (*freeor)(void* mem);
 typedef void  (*report_error)(const char* string, ...);
 
-//typedef unsigned long int4;
+/*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 SHRINK_INTS */
 
 #define        POINTTYPE       1
 #define        LINETYPE        2
@@ -42,6 +42,14 @@ typedef void  (*report_error)(const char* string, ...);
 
 extern int srid;
 
+/*
+
+   These functions are used by  the
+   generated parser and are not meant
+   for public use
+
+*/
+
 void set_srid(double srid);
 void alloc_lwgeom(int srid);
 
@@ -63,17 +71,18 @@ void alloc_counter(void);
 void pop(void);
 void popc(void);
 
-void alloc_wkb(const uchar* parser);
+void alloc_wkb(const char* parser);
+
 /*
        Use these functions to parse and unparse lwgeoms
        You are responsible for freeing the returned memory.
 */
 
-uchar* parse_lwg(const uchar* wkt,allocator allocfunc,report_error errfunc);
-uchar* parse_lwgi(const uchar* wkt,allocator allocfunc,report_error errfunc);
+uchar* parse_lwg(const char* wkt,allocator allocfunc,report_error errfunc);
+uchar* parse_lwgi(const char* wkt,allocator allocfunc,report_error errfunc);
 char* unparse_WKT(uchar* serialized, allocator alloc,freeor free);
 char* unparse_WKB(uchar* serialized, allocator alloc,freeor free, char endian, size_t *outsize, uchar hexform);
 int lwg_parse_yyparse(void);
 int lwg_parse_yyerror(char* s);
 
-#endif // _WKTPARSE_H
+#endif /* _WKTPARSE_H */