+PostGIS 1.1.1CVS
+
+ - Source code cleanups
+
PostGIS 1.1.0
2005/12/21
# 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
* (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)))
/*
* 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);
/*------------------------------------------------------
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,
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);
#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)
{
return serialized;
}
-// Force Right-hand-rule on LWGEOM polygons
+/* Force Right-hand-rule on LWGEOM polygons */
void
lwgeom_forceRHR(LWGEOM *lwgeom)
{
}
}
-// Reverse vertex order of LWGEOM
+/* Reverse vertex order of LWGEOM */
void
lwgeom_reverse(LWGEOM *lwgeom)
{
return 0;
}
-//dont forget to lwfree() result
-
+/*
+ * dont forget to lwfree() result
+ */
BOX2DFLOAT4 *
lwgeom_compute_box2d(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++)
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)
{
}
}
-// 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)
{
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)
{
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:
// 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);
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))
{
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);
// 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);
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);
{
int t;
POINT4D pt;
- uchar *mflag;
+ char *mflag;
if ( TYPE_HASM(pa->dims) ) mflag = "M";
else mflag = "";
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)
{
return 1;
}
-const uchar *
+const char *
lwgeom_typeflags(uchar type)
{
static char flags[5];
// -> 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 *
#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);
#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)
*/
#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);
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)
{
char *str2,*str3;
long datum;
- //elog(NOTICE, "lwhistogram2d parser called");
+ /*elog(NOTICE, "lwhistogram2d parser called");*/
int t;
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;
-//text version
+/* text version */
PG_FUNCTION_INFO_V1(lwhistogram2d_out);
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++)
{
}
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;
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)
{
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;
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();
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)
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) {
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;
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();
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)
{
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 )
{
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");
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)
{
}
-//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)
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++)
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;
#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)
{
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);
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)
{
#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);
PG_RETURN_FLOAT8(rows_returned / total_tuples);
}
-#endif // REALLY_DO_JOINSEL
+#endif /* REALLY_DO_JOINSEL */
/**************************** FROM POSTGIS ****************/
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)
{
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();
}
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 )
{
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) ;
}
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);
}
PG_RETURN_POINTER(box);
}
-#else // USE_VERSION >= 80
+#else /* USE_VERSION >= 80 */
/*
* This function returns an estimate of the selectivity
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;
#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;
#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)
#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;
}
#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)
#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;
}
#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;
* 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
*/
#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);
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
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
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);
histobox.ymax = newhistobox->ymax;
-#else // ! USE_STANDARD_DEVIATION
+#else /* ! USE_STANDARD_DEVIATION */
/*
* Set histogram extent box
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
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;
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();
}
}
- // 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++;
return;
}
- // what about null features (TODO) ?
+ /* what about null features (TODO) ? */
geomstats->avgFeatureCells = (float4)total_boxes_cells/examinedsamples;
#if DEBUG_GEOMETRY_STATS
* 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;
nsp[VARSIZE(txnsp)-VARHDRSZ]='\0';
querysize += VARSIZE(txnsp);
} else {
- querysize += 32; // current_schema()
+ querysize += 32; /* current_schema() */
}
tbl = palloc(VARSIZE(txtbl)+1);
}
-#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
*
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);
*/
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 ) {
return oline;
}
-// TODO
LWPOLY *
simplify2d_lwpoly(const LWPOLY *ipoly, double dist)
{
LWPOINT *point;
POINTARRAY *ipa, *opa;
POINT4D pt;
- char *srl;
+ uchar *srl;
int nsegs, i;
double length, slength, tlength;
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);
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);
/* 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);
/* 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;
opa = ptarray_grid(point->point, grid);
- // TODO: grid bounding box ?
+ /* TODO: grid bounding box ? */
opoint = lwpoint_construct(point->SRID, NULL, opa);
#if VERBOSE
#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);
-// 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
#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;
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);
}
- 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);
}
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;
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);
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);
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;
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);
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;
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);
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;
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);
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;
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);
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;
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);
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;
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);
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;
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);
}
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;
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);
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;
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);
}
-// These functions are taken from the postgis_gist_72.c file
+/* These functions are taken from the postgis_gist_72.c file */
/*
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
#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);
}
#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,
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;
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));
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)) ||
#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",
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) &&
((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);
}
-// 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)
{
{
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;
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)
{
{
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);
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));
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);
}
-// 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();
}
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) ))
+/**********************************************************************
+ * $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)
{
PG_RETURN_INT32(srid);
}
-//setSRID(lwgeom, int4) :: lwgeom
+/* setSRID(lwgeom, int4) :: lwgeom */
PG_FUNCTION_INFO_V1(LWGEOM_setSRID);
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)
{
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);
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;
int32 npoints;
int type;
LWLINE *line=NULL;
- char *subgeom;
+ uchar *subgeom;
line = lwgeom_getline_inspected(inspected, i);
if (line != NULL)
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);
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)
{
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 )
PG_RETURN_NULL();
}
-// 1-based offset
+/* 1-based offset */
PG_FUNCTION_INFO_V1(LWGEOM_geometryn_collection);
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));
subgeom = coll->geoms[idx];
subgeom->SRID = coll->SRID;
- //COMPUTE_BBOX==TAINTING
+ /* COMPUTE_BBOX==TAINTING */
if ( coll->bbox ) lwgeom_addBBOX(subgeom);
result = pglwgeom_serialize(subgeom);
}
-//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;
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;
dims = lwgeom_dimension_recursive(subgeom);
}
- if ( dims == 2 ) { // nothing can be higher
+ if ( dims == 2 ) { /* nothing can be higher */
pfree_inspected(inspected);
return 2;
}
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)
{
}
-// 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)
{
}
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);
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)
{
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);
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)
{
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));
}
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);
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);
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)
{
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));
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);
}
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);
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)
{
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)
{
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);
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)
{
}
- // 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);
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)
{
size = VARSIZE(wkttext)-VARHDRSZ;
- //lwnotice("size: %d", size);
+ /*lwnotice("size: %d", size); */
if ( size < 10 )
{
}
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)");
memcpy(wkt, VARDATA(wkttext), size);
wkt[size]='\0';
- //lwnotice("wkt: [%s]", wkt);
+ /* lwnotice("wkt: [%s]", wkt); */
geom = (PG_LWGEOM *)parse_lwgeom_wkt(wkt);
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);
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)
{
}
- // read user-requested SRID if any
+ /* read user-requested SRID if any */
if ( PG_NARGS() > 1 )
{
SRID = PG_GETARG_INT32(1);
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)
{
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;
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)
{
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)
{
#include "liblwgeom.h"
#include "lwgeom_pg.h"
-//#undef PGIS_DEBUG
+/* #undef PGIS_DEBUG */
+
+#define PARANOIA_LEVEL 1
void *
pg_alloc(size_t size)
#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;
}
/* 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);
}
getbox2d_p(ser, &box);
}
- size+=4; // size header
+ size+=4; /* size header */
result = lwalloc(size);
result->size = size;
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);
+}
+
+
+
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);
#include "wktparse.h"
/*
-//To get byte order
+ * To get byte order
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
};
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);
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);
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)
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;
{
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);
}
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;
}
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;
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);
lwg_parse_yyerror(char* s)
{
error("parse error - invalid geometry");
- //error_func("parse error - invalid geometry");
+ /* error_func("parse error - invalid geometry"); */
return 1;
}
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){
}
uchar
-read_wkb_byte(const uchar** in)
+read_wkb_byte(const char** in)
{
uchar ret = strhex_readbyte(*in);
(*in)+=2;
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);
}
int4
-read_wkb_int(const uchar** in)
+read_wkb_int(const char** in)
{
int4 ret=0;
read_wkb_bytes(in,(uchar*)&ret,4);
}
double
-read_wkb_double(const uchar** in,int convert_from_int)
+read_wkb_double(const char** in, int convert_from_int)
{
double ret=0;
}
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;
}
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();
}
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);
type = read_wkb_int(b);
- //quick exit on error
+ /* quick exit on error */
if ( ferror_occured ) return;
the_geom.ndims=2;
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 )
{
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;
void
-alloc_wkb(const uchar* parser)
+alloc_wkb(const char *parser)
{
parse_wkb(&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;
}
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);
-// 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)
{
}
-// 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)
{
int ndims, hasz, hasm;
uint32 npoints;
uchar type;
- char *loc;
+ uchar *loc;
int t;
if (serialized_form == NULL)
result = (LWPOLY*) lwalloc(sizeof(LWPOLY));
- type = (unsigned char) serialized_form[0];
+ type = serialized_form[0];
result->type = type;
ndims = TYPE_NDIMS(type);
if ( lwgeom_hasSRID(type))
{
result->SRID = get_int32(loc);
- loc +=4; // type + SRID
+ loc +=4; /* type + SRID */
}
else
{
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;
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)
{
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;
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),
if (poly->bbox)
{
memcpy(loc, poly->bbox, sizeof(BOX2DFLOAT4));
- size += sizeof(BOX2DFLOAT4); // bvol
+ size += sizeof(BOX2DFLOAT4); /* bvol */
loc += sizeof(BOX2DFLOAT4);
}
{
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++)
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;
}
-// 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;
#ifdef PGIS_DEBUG
lwnotice("lwgeom_size_poly: has srid");
#endif
- loc +=4; // type + SRID
+ loc +=4; /* type + SRID */
result += 4;
}
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;
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
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);
}
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)
{
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)
{
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;
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)
{
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
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);
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 */