Hi Paul et al,

Well, clearly there's a bug to be fixed.

And attached is a patch. This updates BOX2DFLOAT4_to_LWGEOM and BOX3D_to_LWGEOM to:

* Return a point when xmin==xmax && ymin==ymax

* Return a line string when xmin==xmax || ymin==ymax

* Otherwise return a polygon

Note both methods are exactly the same, except the first line - which does seem pretty silly. I'd probably change BOX3D_to_LWGEOM to create a box2dfloat and then call BOX2DFLOAT4_to_LWGEOM. But I left it as it was. I also cleaned up the code a bit to use pointArray_construct - which for some reason it wasn't.

Thanks,

Charlie

Index: lwgeom/lwgeom_box2dfloat4.c
===================================================================
--- lwgeom/lwgeom_box2dfloat4.c (revision 2708)
+++ lwgeom/lwgeom_box2dfloat4.c (working copy)
@@ -456,32 +456,57 @@
 Datum BOX2DFLOAT4_to_LWGEOM(PG_FUNCTION_ARGS)
 {
        BOX2DFLOAT4 *box = (BOX2DFLOAT4 *)PG_GETARG_POINTER(0);
-       POINT2D *pts = palloc(sizeof(POINT2D)*5);
-       POINTARRAY *pa[1];
-       LWPOLY *poly;
+       POINTARRAY *pa;
        int wantbbox = 0;
        PG_LWGEOM *result;
        uchar *ser;
 
-       /* Assign coordinates to POINT2D array */
-       pts[0].x = box->xmin; pts[0].y = box->ymin;
-       pts[1].x = box->xmin; pts[1].y = box->ymax;
-       pts[2].x = box->xmax; pts[2].y = box->ymax;
-       pts[3].x = box->xmax; pts[3].y = box->ymin;
-       pts[4].x = box->xmin; pts[4].y = box->ymin;
+       if (box->xmin == box->xmax &&
+           box->ymin == box->ymax)
+        {
+         /* Construct and serialize point */
+          LWPOINT *point = make_lwpoint2d(-1, box->xmin, box->ymin);
+         ser = lwpoint_serialize(point);
+        }
+       else if (box->xmin == box->xmax ||
+                box->ymin == box->ymax)
+        {
+         LWLINE *line;
+         POINT2D *pts = palloc(sizeof(POINT2D)*2);
+         
+         /* Assign coordinates to POINT2D array */
+         pts[0].x = box->xmin; pts[0].y = box->ymin;
+         pts[1].x = box->xmax; pts[1].y = box->ymax;
+         
+         /* Construct point array */
+          pa = pointArray_construct(pts, 0, 0, 2);       
 
-       /* Construct point array */
-       pa[0] = palloc(sizeof(POINTARRAY));
-       pa[0]->serialized_pointlist = (uchar *)pts;
-       TYPE_SETZM(pa[0]->dims, 0, 0);
-       pa[0]->npoints = 5;
+         /* Construct and serialize linestring */
+         line = lwline_construct(-1, NULL, pa);
+         ser = lwline_serialize(line);
+        }
+        else
+        {
+         LWPOLY *poly;
+         POINT2D *pts = palloc(sizeof(POINT2D)*5);
 
-       /* Construct polygon */
-       poly = lwpoly_construct(-1, NULL, 1, pa);
+         /* Assign coordinates to POINT2D array */
+         pts[0].x = box->xmin; pts[0].y = box->ymin;
+         pts[1].x = box->xmin; pts[1].y = box->ymax;
+         pts[2].x = box->xmax; pts[2].y = box->ymax;
+         pts[3].x = box->xmax; pts[3].y = box->ymin;
+         pts[4].x = box->xmin; pts[4].y = box->ymin;
 
-       /* Serialize polygon */
-       ser = lwpoly_serialize(poly);
+         /* Construct point array */
+          pa = pointArray_construct(pts, 0, 0, 5);       
 
+         /* Construct polygon */
+         poly = lwpoly_construct(-1, NULL, 1, &pa);
+
+         /* Serialize polygon */
+         ser = lwpoly_serialize(poly);
+        }
+        
        /* Construct PG_LWGEOM  */
        result = PG_LWGEOM_construct(ser, -1, wantbbox);
        
Index: lwgeom/lwgeom_box3d.c
===================================================================
--- lwgeom/lwgeom_box3d.c       (revision 2708)
+++ lwgeom/lwgeom_box3d.c       (working copy)
@@ -162,32 +162,57 @@
 Datum BOX3D_to_LWGEOM(PG_FUNCTION_ARGS)
 {
        BOX3D *box = (BOX3D *)PG_GETARG_POINTER(0);
-       POINT2D *pts = palloc(sizeof(POINT2D)*5);
-       POINTARRAY *pa[1];
-       LWPOLY *poly;
+       POINTARRAY *pa;
        int wantbbox = 0;
        PG_LWGEOM *result;
        uchar *ser;
 
-       /* Assign coordinates to POINT2D array */
-       pts[0].x = box->xmin; pts[0].y = box->ymin;
-       pts[1].x = box->xmin; pts[1].y = box->ymax;
-       pts[2].x = box->xmax; pts[2].y = box->ymax;
-       pts[3].x = box->xmax; pts[3].y = box->ymin;
-       pts[4].x = box->xmin; pts[4].y = box->ymin;
+       if (box->xmin == box->xmax &&
+           box->ymin == box->ymax)
+        {
+         /* Construct and serialize point */
+          LWPOINT *point = make_lwpoint2d(-1, box->xmin, box->ymin);
+         ser = lwpoint_serialize(point);
+        }
+       else if (box->xmin == box->xmax ||
+                box->ymin == box->ymax)
+        {
+         LWLINE *line;
+         POINT2D *pts = palloc(sizeof(POINT2D)*2);
+         
+         /* Assign coordinates to POINT2D array */
+         pts[0].x = box->xmin; pts[0].y = box->ymin;
+         pts[1].x = box->xmax; pts[1].y = box->ymax;
+         
+         /* Construct point array */
+          pa = pointArray_construct(pts, 0, 0, 2);       
 
-       /* Construct point array */
-       pa[0] = palloc(sizeof(POINTARRAY));
-       pa[0]->serialized_pointlist = (uchar *)pts;
-       TYPE_SETZM(pa[0]->dims, 0, 0);
-       pa[0]->npoints = 5;
+         /* Construct and serialize linestring */
+         line = lwline_construct(-1, NULL, pa);
+         ser = lwline_serialize(line);
+        }
+        else
+        {
+         LWPOLY *poly;
+         POINT2D *pts = palloc(sizeof(POINT2D)*5);
 
-       /* Construct polygon */
-       poly = lwpoly_construct(-1, NULL, 1, pa);
+         /* Assign coordinates to POINT2D array */
+         pts[0].x = box->xmin; pts[0].y = box->ymin;
+         pts[1].x = box->xmin; pts[1].y = box->ymax;
+         pts[2].x = box->xmax; pts[2].y = box->ymax;
+         pts[3].x = box->xmax; pts[3].y = box->ymin;
+         pts[4].x = box->xmin; pts[4].y = box->ymin;
 
-       /* Serialize polygon */
-       ser = lwpoly_serialize(poly);
+         /* Construct point array */
+          pa = pointArray_construct(pts, 0, 0, 5);       
 
+         /* Construct polygon */
+         poly = lwpoly_construct(-1, NULL, 1, &pa);
+
+         /* Serialize polygon */
+         ser = lwpoly_serialize(poly);
+        }
+        
        /* Construct PG_LWGEOM  */
        result = PG_LWGEOM_construct(ser, -1, wantbbox);
        

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

_______________________________________________
postgis-users mailing list
[email protected]
http://postgis.refractions.net/mailman/listinfo/postgis-users

Reply via email to