PostGIS  2.4.9dev-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 #include "../postgis_config.h"
31 #include "lwgeom_geos.h"
32 #include "liblwgeom.h"
33 #include "lwgeom_pg.h"
34 
35 #include <string.h>
36 #include <assert.h>
37 
38 /* #define POSTGIS_DEBUG_LEVEL 4 */
39 
40 Datum ST_MakeValid(PG_FUNCTION_ARGS);
42 Datum ST_MakeValid(PG_FUNCTION_ARGS)
43 {
44  GSERIALIZED *in, *out;
45  LWGEOM *lwgeom_in, *lwgeom_out;
46 
47  in = PG_GETARG_GSERIALIZED_P(0);
48  lwgeom_in = lwgeom_from_gserialized(in);
49 
50  switch ( lwgeom_in->type )
51  {
52  case POINTTYPE:
53  case MULTIPOINTTYPE:
54  case LINETYPE:
55  case POLYGONTYPE:
56  case MULTILINETYPE:
57  case MULTIPOLYGONTYPE:
58  case COLLECTIONTYPE:
59  break;
60 
61  default:
62  lwpgerror("ST_MakeValid: unsupported geometry type %s",
63  lwtype_name(lwgeom_in->type));
64  PG_RETURN_NULL();
65  break;
66  }
67 
68  lwgeom_out = lwgeom_make_valid(lwgeom_in);
69  if ( ! lwgeom_out )
70  {
71  PG_FREE_IF_COPY(in, 0);
72  PG_RETURN_NULL();
73  }
74 
75  out = geometry_serialize(lwgeom_out);
76 
77  PG_RETURN_POINTER(out);
78 }
79 
80 /* Uses GEOS internally */
81 static LWGEOM* lwgeom_clean(LWGEOM* lwgeom_in);
82 static LWGEOM*
83 lwgeom_clean(LWGEOM* lwgeom_in)
84 {
85  LWGEOM* lwgeom_out;
86 
87  lwgeom_out = lwgeom_make_valid(lwgeom_in);
88  if ( ! lwgeom_out )
89  {
90  return NULL;
91  }
92 
93  /* Check dimensionality is the same as input */
94  if ( lwgeom_dimensionality(lwgeom_in) != lwgeom_dimensionality(lwgeom_out) )
95  {
96  lwpgnotice("lwgeom_clean: dimensional collapse (%d to %d)",
97  lwgeom_dimensionality(lwgeom_in), lwgeom_dimensionality(lwgeom_out));
98 
99  return NULL;
100  }
101 
102  /* Check that the output is not a collection if the input wasn't */
103  if ( lwgeom_out->type == COLLECTIONTYPE &&
104  lwgeom_in->type != COLLECTIONTYPE )
105  {
106  lwpgnotice("lwgeom_clean: mixed-type output (%s) "
107  "from single-type input (%s)",
108  lwtype_name(lwgeom_out->type),
109  lwtype_name(lwgeom_in->type));
110  return NULL;
111  }
112 
113  /* Force right-hand-rule (will only affect polygons) */
114  /* gout := ST_ForceRHR(gout); */
115 
116  /* Remove repeated duplicated points ? */
117  /* gout = ST_RemoveRepeatedPoints(gout); */
118 
119  return lwgeom_out;
120 }
121 
122 
123 Datum ST_CleanGeometry(PG_FUNCTION_ARGS);
125 Datum ST_CleanGeometry(PG_FUNCTION_ARGS)
126 {
127  GSERIALIZED *in, *out;
128  LWGEOM *lwgeom_in, *lwgeom_out;
129 
130  in = PG_GETARG_GSERIALIZED_P(0);
131  lwgeom_in = lwgeom_from_gserialized(in);
132 
133  /* Short-circuit: empty geometry are the cleanest ! */
134 #if 0
135  if ( lwgeom_is_empty(lwgeom_in) )
136  {
137  out = geometry_serialize(lwgeom_in);
138  PG_FREE_IF_COPY(in, 0);
139  PG_RETURN_POINTER(out);
140  }
141 #endif
142 
143  lwgeom_out = lwgeom_clean(lwgeom_in);
144  if ( ! lwgeom_out )
145  {
146  PG_FREE_IF_COPY(in, 0);
147  PG_RETURN_NULL();
148  }
149 
150  out = geometry_serialize(lwgeom_out);
151  PG_RETURN_POINTER(out);
152 }
153 
#define LINETYPE
Definition: liblwgeom.h:86
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
#define POLYGONTYPE
Definition: liblwgeom.h:87
Datum ST_CleanGeometry(PG_FUNCTION_ARGS)
#define MULTIPOINTTYPE
Definition: liblwgeom.h:88
int lwgeom_dimensionality(LWGEOM *geom)
Return the dimensionality (relating to point/line/poly) of an lwgeom.
Definition: lwgeom.c:1411
PG_FUNCTION_INFO_V1(ST_MakeValid)
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:218
LWGEOM * lwgeom_make_valid(LWGEOM *geom)
Attempts to make an invalid geometries valid w/out losing points.
Datum ST_MakeValid(PG_FUNCTION_ARGS)
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:90
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:85
uint8_t type
Definition: liblwgeom.h:396
static LWGEOM * lwgeom_clean(LWGEOM *lwgeom_in)
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1346
#define MULTILINETYPE
Definition: liblwgeom.h:89
#define COLLECTIONTYPE
Definition: liblwgeom.h:91
This library is the generic geometry handling section of PostGIS.