From 4b0ade47ebd194f97ddabb54ecdfa0c0243a3107 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Fri, 2 Dec 2016 08:06:09 +0000 Subject: [PATCH] Refactor logic for ST_MakeEnvelope to be reusable in liblwgeom MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Patch by Björn Harrtell via https://git.osgeo.org/gogs/postgis/postgis/pulls/8 git-svn-id: http://svn.osgeo.org/postgis/trunk@15266 b70326c6-7e19-0410-871a-916f4a2858ee --- liblwgeom/liblwgeom.h.in | 5 +++-- liblwgeom/lwpoly.c | 22 ++++++++++++++++++++++ postgis/lwgeom_functions_basic.c | 31 +------------------------------ 3 files changed, 26 insertions(+), 32 deletions(-) diff --git a/liblwgeom/liblwgeom.h.in b/liblwgeom/liblwgeom.h.in index e96839e10..d15afa8b3 100644 --- a/liblwgeom/liblwgeom.h.in +++ b/liblwgeom/liblwgeom.h.in @@ -1356,8 +1356,9 @@ extern LWLINE *lwline_addpoint(LWLINE *line, LWPOINT *point, uint32_t where); extern LWLINE *lwline_removepoint(LWLINE *line, uint32_t which); extern void lwline_setPoint4d(LWLINE *line, uint32_t which, POINT4D *newpoint); extern LWPOLY *lwpoly_from_lwlines(const LWLINE *shell, uint32_t nholes, const LWLINE **holes); -extern LWPOLY* lwpoly_construct_rectangle(char hasz, char hasm, POINT4D *p1, POINT4D *p2, POINT4D *p3, POINT4D *p4); -extern LWPOLY* lwpoly_construct_circle(int srid, double x, double y, double radius, uint32_t segments_per_quarter, char exterior); +extern LWPOLY *lwpoly_construct_rectangle(char hasz, char hasm, POINT4D *p1, POINT4D *p2, POINT4D *p3, POINT4D *p4); +extern LWPOLY *lwpoly_construct_envelope(int srid, double x1, double y1, double x2, double y2); +extern LWPOLY *lwpoly_construct_circle(int srid, double x, double y, double radius, uint32_t segments_per_quarter, char exterior); extern LWTRIANGLE *lwtriangle_from_lwline(const LWLINE *shell); extern LWMPOINT *lwmpoint_from_lwgeom(const LWGEOM *g); /* Extract the coordinates of an LWGEOM into an LWMPOINT */ diff --git a/liblwgeom/lwpoly.c b/liblwgeom/lwpoly.c index 293133060..4d2b8a450 100644 --- a/liblwgeom/lwpoly.c +++ b/liblwgeom/lwpoly.c @@ -94,6 +94,28 @@ lwpoly_construct_rectangle(char hasz, char hasm, POINT4D *p1, POINT4D *p2, return lwpoly; } +LWPOLY * +lwpoly_construct_envelope(int srid, double x1, double y1, double x2, double y2) +{ + POINT4D p1, p2, p3, p4; + LWPOLY *poly; + + p1.x = x1; + p1.y = y1; + p2.x = x1; + p2.y = y2; + p3.x = x2; + p3.y = y2; + p4.x = x2; + p4.y = y1; + + poly = lwpoly_construct_rectangle(0, 0, &p1, &p2, &p3, &p4); + lwgeom_set_srid(lwpoly_as_lwgeom(poly), srid); + lwgeom_add_bbox(lwpoly_as_lwgeom(poly)); + + return poly; +} + LWPOLY* lwpoly_construct_circle(int srid, double x, double y, double radius, uint32_t segments_per_quarter, char exterior) { diff --git a/postgis/lwgeom_functions_basic.c b/postgis/lwgeom_functions_basic.c index 1b78e5ade..ad0899dfd 100644 --- a/postgis/lwgeom_functions_basic.c +++ b/postgis/lwgeom_functions_basic.c @@ -2075,36 +2075,7 @@ Datum ST_MakeEnvelope(PG_FUNCTION_ARGS) srid = PG_GETARG_INT32(4); } - pa = (POINTARRAY**)palloc(sizeof(POINTARRAY**)); - pa[0] = ptarray_construct_empty(0, 0, 5); - - /* 1st point */ - p.x = x1; - p.y = y1; - ptarray_append_point(pa[0], &p, LW_TRUE); - - /* 2nd point */ - p.x = x1; - p.y = y2; - ptarray_append_point(pa[0], &p, LW_TRUE); - - /* 3rd point */ - p.x = x2; - p.y = y2; - ptarray_append_point(pa[0], &p, LW_TRUE); - - /* 4th point */ - p.x = x2; - p.y = y1; - ptarray_append_point(pa[0], &p, LW_TRUE); - - /* 5th point */ - p.x = x1; - p.y = y1; - ptarray_append_point(pa[0], &p, LW_TRUE); - - poly = lwpoly_construct(srid, NULL, 1, pa); - lwgeom_add_bbox(lwpoly_as_lwgeom(poly)); + poly = lwpoly_construct_envelope(srid, x1, y1, x2, y2); result = geometry_serialize(lwpoly_as_lwgeom(poly)); lwpoly_free(poly); -- 2.40.0