PostGIS  3.1.6dev-r@@SVN_REVISION@@
postgis/lwgeom_geos_clean.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * PostGIS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * PostGIS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  **********************************************************************
20  *
21  * Copyright 2009-2010 Sandro Santilli <strk@kbt.io>
22  *
23  **********************************************************************/
24 
25 
26 #include "postgres.h"
27 #include "fmgr.h"
28 #include "funcapi.h"
29 
30 
31 #include "../postgis_config.h"
32 /*#define POSTGIS_DEBUG_LEVEL 4*/
33 
34 #include "lwgeom_geos.h"
35 #include "liblwgeom.h"
36 #include "lwgeom_pg.h"
37 
38 #include <string.h>
39 #include <assert.h>
40 
41 Datum ST_MakeValid(PG_FUNCTION_ARGS);
43 Datum ST_MakeValid(PG_FUNCTION_ARGS)
44 {
45  GSERIALIZED *in, *out;
46  LWGEOM *lwgeom_in, *lwgeom_out;
47 
48  in = PG_GETARG_GSERIALIZED_P_COPY(0);
49  lwgeom_in = lwgeom_from_gserialized(in);
50 
51  POSTGIS_DEBUG(1, "ST_MakeValid enter");
52 
53  switch ( lwgeom_in->type )
54  {
55  case POINTTYPE:
56  case MULTIPOINTTYPE:
57  case LINETYPE:
58  case POLYGONTYPE:
59  case MULTILINETYPE:
60  case MULTIPOLYGONTYPE:
61  case COLLECTIONTYPE:
62  break;
63 
64  default:
65  lwpgerror("ST_MakeValid: unsupported geometry type %s",
66  lwtype_name(lwgeom_in->type));
67  PG_RETURN_NULL();
68  break;
69  }
70 
71  lwgeom_out = lwgeom_make_valid(lwgeom_in);
72  if ( ! lwgeom_out )
73  {
74  PG_FREE_IF_COPY(in, 0);
75  PG_RETURN_NULL();
76  }
77 
78  out = geometry_serialize(lwgeom_out);
79 
80  PG_RETURN_POINTER(out);
81 }
82 
83 /* Uses GEOS internally */
84 static LWGEOM* lwgeom_clean(LWGEOM* lwgeom_in);
85 static LWGEOM*
86 lwgeom_clean(LWGEOM* lwgeom_in)
87 {
88  LWGEOM* lwgeom_out;
89 
90  lwgeom_out = lwgeom_make_valid(lwgeom_in);
91  if ( ! lwgeom_out )
92  {
93  return NULL;
94  }
95 
96  /* Check dimensionality is the same as input */
97  if ( lwgeom_dimensionality(lwgeom_in) != lwgeom_dimensionality(lwgeom_out) )
98  {
99  lwpgnotice("lwgeom_clean: dimensional collapse (%d to %d)",
100  lwgeom_dimensionality(lwgeom_in), lwgeom_dimensionality(lwgeom_out));
101 
102  return NULL;
103  }
104 
105  /* Check that the output is not a collection if the input wasn't */
106  if ( lwgeom_out->type == COLLECTIONTYPE &&
107  lwgeom_in->type != COLLECTIONTYPE )
108  {
109  lwpgnotice("lwgeom_clean: mixed-type output (%s) "
110  "from single-type input (%s)",
111  lwtype_name(lwgeom_out->type),
112  lwtype_name(lwgeom_in->type));
113  return NULL;
114  }
115 
116  /* Force right-hand-rule (will only affect polygons) */
117  /* gout := ST_ForceRHR(gout); */
118 
119  /* Remove repeated duplicated points ? */
120  /* gout = ST_RemoveRepeatedPoints(gout); */
121 
122  return lwgeom_out;
123 }
124 
125 
126 Datum ST_CleanGeometry(PG_FUNCTION_ARGS);
128 Datum ST_CleanGeometry(PG_FUNCTION_ARGS)
129 {
130  GSERIALIZED *in, *out;
131  LWGEOM *lwgeom_in, *lwgeom_out;
132 
133  in = PG_GETARG_GSERIALIZED_P(0);
134  lwgeom_in = lwgeom_from_gserialized(in);
135 
136  /* Short-circuit: empty geometry are the cleanest ! */
137 #if 0
138  if ( lwgeom_is_empty(lwgeom_in) )
139  {
140  out = geometry_serialize(lwgeom_in);
141  PG_FREE_IF_COPY(in, 0);
142  PG_RETURN_POINTER(out);
143  }
144 #endif
145 
146  lwgeom_out = lwgeom_clean(lwgeom_in);
147  if ( ! lwgeom_out )
148  {
149  PG_FREE_IF_COPY(in, 0);
150  PG_RETURN_NULL();
151  }
152 
153  out = geometry_serialize(lwgeom_out);
154  PG_RETURN_POINTER(out);
155 }
156 
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
#define COLLECTIONTYPE
Definition: liblwgeom.h:122
#define MULTILINETYPE
Definition: liblwgeom.h:120
#define LINETYPE
Definition: liblwgeom.h:117
#define MULTIPOINTTYPE
Definition: liblwgeom.h:119
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:116
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:121
#define POLYGONTYPE
Definition: liblwgeom.h:118
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216
int lwgeom_dimensionality(const LWGEOM *geom)
Return the dimensionality (relating to point/line/poly) of an lwgeom.
Definition: lwgeom.c:1409
LWGEOM * lwgeom_make_valid(LWGEOM *geom)
Attempts to make an invalid geometries valid w/out losing points.
This library is the generic geometry handling section of PostGIS.
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:203
static LWGEOM * lwgeom_clean(LWGEOM *lwgeom_in)
Datum ST_CleanGeometry(PG_FUNCTION_ARGS)
Datum ST_MakeValid(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(ST_MakeValid)
uint8_t type
Definition: liblwgeom.h:476