PostGIS  2.5.7dev-r@@SVN_REVISION@@
lwhomogenize.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 2010 Olivier Courtin <olivier.courtin@oslandia.com>
22  *
23  **********************************************************************/
24 
25 
26 #include <stdlib.h>
27 #include "liblwgeom_internal.h"
28 #include "lwgeom_log.h"
29 
30 
31 typedef struct {
32  int cnt[NUMTYPES];
35 
36 static void
38 {
39  int i;
40  for ( i = 0; i < NUMTYPES; i++ )
41  {
42  buffer->cnt[i] = 0;
43  buffer->buf[i] = NULL;
44  }
45 }
46 
47 /*
48 static void
49 free_homogenizebuffer(HomogenizeBuffer *buffer)
50 {
51  int i;
52  for ( i = 0; i < NUMTYPES; i++ )
53  {
54  if ( buffer->buf[i] )
55  {
56  lwcollection_free(buffer->buf[i]);
57  }
58  }
59 }
60 */
61 
62 /*
63 ** Given a generic collection, return the "simplest" form.
64 **
65 ** eg: GEOMETRYCOLLECTION(MULTILINESTRING()) => MULTILINESTRING()
66 **
67 ** GEOMETRYCOLLECTION(MULTILINESTRING(), MULTILINESTRING(), POINT())
68 ** => GEOMETRYCOLLECTION(MULTILINESTRING(), POINT())
69 **
70 ** In general, if the subcomponents are homogeneous, return a properly
71 ** typed collection.
72 ** Otherwise, return a generic collection, with the subtypes in minimal
73 ** typed collections.
74 */
75 static void
77 {
78  uint32_t i;
79 
80  if ( ! col ) return;
81  if ( lwgeom_is_empty(lwcollection_as_lwgeom(col)) ) return;
82  for ( i = 0; i < col->ngeoms; i++ )
83  {
84  LWGEOM *geom = col->geoms[i];
85  switch(geom->type)
86  {
87  case POINTTYPE:
88  case LINETYPE:
89  case CIRCSTRINGTYPE:
90  case COMPOUNDTYPE:
91  case TRIANGLETYPE:
92  case CURVEPOLYTYPE:
93  case POLYGONTYPE:
94  {
95  /* Init if necessary */
96  if ( ! buffer->buf[geom->type] )
97  {
99  bufcol->type = lwtype_get_collectiontype(geom->type);
100  buffer->buf[geom->type] = bufcol;
101  }
102  /* Add sub-geom to buffer */
103  lwcollection_add_lwgeom(buffer->buf[geom->type], lwgeom_clone(geom));
104  /* Increment count for this singleton type */
105  buffer->cnt[geom->type] = buffer->cnt[geom->type] + 1;
106  }
107  /* FALLTHROUGH */
108  default:
109  {
111  break;
112  }
113  }
114  }
115  return;
116 }
117 
118 static LWGEOM*
120 {
121  int i;
122  int ntypes = 0;
123  int type = 0;
124  LWGEOM *outgeom = NULL;
125 
127 
128  /* Sort all the parts into a buffer */
131 
132  /* Check for homogeneity */
133  for ( i = 0; i < NUMTYPES; i++ )
134  {
135  if ( buffer.cnt[i] > 0 )
136  {
137  ntypes++;
138  type = i;
139  }
140  }
141 
142  /* No types? Huh. Return empty. */
143  if ( ntypes == 0 )
144  {
145  LWCOLLECTION *outcol;
147  outgeom = lwcollection_as_lwgeom(outcol);
148  }
149  /* One type, return homogeneous collection */
150  else if ( ntypes == 1 )
151  {
152  LWCOLLECTION *outcol;
153  outcol = buffer.buf[type];
154  if ( outcol->ngeoms == 1 )
155  {
156  outgeom = outcol->geoms[0];
157  outcol->ngeoms=0; lwcollection_free(outcol);
158  }
159  else
160  {
161  outgeom = lwcollection_as_lwgeom(outcol);
162  }
163  outgeom->srid = col->srid;
164  }
165  /* Bah, more than out type, return anonymous collection */
166  else if ( ntypes > 1 )
167  {
168  int j;
169  LWCOLLECTION *outcol;
171  for ( j = 0; j < NUMTYPES; j++ )
172  {
173  if ( buffer.buf[j] )
174  {
175  LWCOLLECTION *bcol = buffer.buf[j];
176  if ( bcol->ngeoms == 1 )
177  {
178  lwcollection_add_lwgeom(outcol, bcol->geoms[0]);
179  bcol->ngeoms=0; lwcollection_free(bcol);
180  }
181  else
182  {
184  }
185  }
186  }
187  outgeom = lwcollection_as_lwgeom(outcol);
188  }
189 
190  return outgeom;
191 }
192 
193 
194 
195 
196 
197 /*
198 ** Given a generic geometry, return the "simplest" form.
199 **
200 ** eg:
201 ** LINESTRING() => LINESTRING()
202 **
203 ** MULTILINESTRING(with a single line) => LINESTRING()
204 **
205 ** GEOMETRYCOLLECTION(MULTILINESTRING()) => MULTILINESTRING()
206 **
207 ** GEOMETRYCOLLECTION(MULTILINESTRING(), MULTILINESTRING(), POINT())
208 ** => GEOMETRYCOLLECTION(MULTILINESTRING(), POINT())
209 */
210 LWGEOM *
212 {
213  LWGEOM *hgeom;
214 
215  /* EMPTY Geometry */
216  if (lwgeom_is_empty(geom))
217  {
218  if( lwgeom_is_collection(geom) )
219  {
221  }
222 
223  return lwgeom_clone(geom);
224  }
225 
226  switch (geom->type)
227  {
228 
229  /* Return simple geometries untouched */
230  case POINTTYPE:
231  case LINETYPE:
232  case CIRCSTRINGTYPE:
233  case COMPOUNDTYPE:
234  case TRIANGLETYPE:
235  case CURVEPOLYTYPE:
236  case POLYGONTYPE:
237  return lwgeom_clone(geom);
238 
239  /* Process homogeneous geometries lightly */
240  case MULTIPOINTTYPE:
241  case MULTILINETYPE:
242  case MULTIPOLYGONTYPE:
243  case MULTICURVETYPE:
244  case MULTISURFACETYPE:
246  case TINTYPE:
247  {
248  LWCOLLECTION *col = (LWCOLLECTION*)geom;
249 
250  /* Strip single-entry multi-geometries down to singletons */
251  if ( col->ngeoms == 1 )
252  {
253  hgeom = lwgeom_clone((LWGEOM*)(col->geoms[0]));
254  hgeom->srid = geom->srid;
255  if (geom->bbox)
256  hgeom->bbox = gbox_copy(geom->bbox);
257  return hgeom;
258  }
259 
260  /* Return proper multigeometry untouched */
261  return lwgeom_clone(geom);
262  }
263 
264  /* Work on anonymous collections separately */
265  case COLLECTIONTYPE:
266  return lwcollection_homogenize((LWCOLLECTION *) geom);
267  }
268 
269  /* Unknown type */
270  lwerror("lwgeom_homogenize: Geometry Type not supported (%i)",
271  lwtype_name(geom->type));
272 
273  return NULL; /* Never get here! */
274 }
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
Definition: g_box.c:433
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:300
uint32_t lwtype_get_collectiontype(uint8_t type)
Given an lwtype number, what homogeneous collection can hold it?
Definition: lwgeom.c:1120
#define COLLECTIONTYPE
Definition: liblwgeom.h:91
#define COMPOUNDTYPE
Definition: liblwgeom.h:93
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwcollection.c:94
#define CURVEPOLYTYPE
Definition: liblwgeom.h:94
#define MULTILINETYPE
Definition: liblwgeom.h:89
#define MULTISURFACETYPE
Definition: liblwgeom.h:96
#define LINETYPE
Definition: liblwgeom.h:86
#define MULTIPOINTTYPE
Definition: liblwgeom.h:88
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:930
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:85
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:140
#define TINTYPE
Definition: liblwgeom.h:99
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:90
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1085
#define POLYGONTYPE
Definition: liblwgeom.h:87
#define POLYHEDRALSURFACETYPE
Definition: liblwgeom.h:97
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:92
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:218
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:141
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:356
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:1393
LWGEOM * lwgeom_clone(const LWGEOM *lwgeom)
Clone LWGEOM object.
Definition: lwgeom.c:482
#define MULTICURVETYPE
Definition: liblwgeom.h:95
#define TRIANGLETYPE
Definition: liblwgeom.h:98
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
Definition: lwcollection.c:187
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
Definition: lwgeom.c:224
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:937
#define NUMTYPES
Definition: liblwgeom.h:101
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
static void lwcollection_build_buffer(const LWCOLLECTION *col, HomogenizeBuffer *buffer)
Definition: lwhomogenize.c:76
LWGEOM * lwgeom_homogenize(const LWGEOM *geom)
Definition: lwhomogenize.c:211
static LWGEOM * lwcollection_homogenize(const LWCOLLECTION *col)
Definition: lwhomogenize.c:119
static void init_homogenizebuffer(HomogenizeBuffer *buffer)
Definition: lwhomogenize.c:37
type
Definition: ovdump.py:41
Datum buffer(PG_FUNCTION_ARGS)
uint32_t ngeoms
Definition: liblwgeom.h:510
uint8_t type
Definition: liblwgeom.h:506
uint8_t flags
Definition: liblwgeom.h:507
LWGEOM ** geoms
Definition: liblwgeom.h:512
int32_t srid
Definition: liblwgeom.h:509
uint8_t type
Definition: liblwgeom.h:399
GBOX * bbox
Definition: liblwgeom.h:401
int32_t srid
Definition: liblwgeom.h:402
unsigned int uint32_t
Definition: uthash.h:78