PostGIS  2.5.0dev-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  default:
108  {
110  }
111  }
112  }
113  return;
114 }
115 
116 static LWGEOM*
118 {
119  int i;
120  int ntypes = 0;
121  int type = 0;
122  LWGEOM *outgeom = NULL;
123 
125 
126  /* Sort all the parts into a buffer */
127  init_homogenizebuffer(&buffer);
128  lwcollection_build_buffer(col, &buffer);
129 
130  /* Check for homogeneity */
131  for ( i = 0; i < NUMTYPES; i++ )
132  {
133  if ( buffer.cnt[i] > 0 )
134  {
135  ntypes++;
136  type = i;
137  }
138  }
139 
140  /* No types? Huh. Return empty. */
141  if ( ntypes == 0 )
142  {
143  LWCOLLECTION *outcol;
145  outgeom = lwcollection_as_lwgeom(outcol);
146  }
147  /* One type, return homogeneous collection */
148  else if ( ntypes == 1 )
149  {
150  LWCOLLECTION *outcol;
151  outcol = buffer.buf[type];
152  if ( outcol->ngeoms == 1 )
153  {
154  outgeom = outcol->geoms[0];
155  outcol->ngeoms=0; lwcollection_free(outcol);
156  }
157  else
158  {
159  outgeom = lwcollection_as_lwgeom(outcol);
160  }
161  outgeom->srid = col->srid;
162  }
163  /* Bah, more than out type, return anonymous collection */
164  else if ( ntypes > 1 )
165  {
166  int j;
167  LWCOLLECTION *outcol;
169  for ( j = 0; j < NUMTYPES; j++ )
170  {
171  if ( buffer.buf[j] )
172  {
173  LWCOLLECTION *bcol = buffer.buf[j];
174  if ( bcol->ngeoms == 1 )
175  {
176  lwcollection_add_lwgeom(outcol, bcol->geoms[0]);
177  bcol->ngeoms=0; lwcollection_free(bcol);
178  }
179  else
180  {
182  }
183  }
184  }
185  outgeom = lwcollection_as_lwgeom(outcol);
186  }
187 
188  return outgeom;
189 }
190 
191 
192 
193 
194 
195 /*
196 ** Given a generic geometry, return the "simplest" form.
197 **
198 ** eg:
199 ** LINESTRING() => LINESTRING()
200 **
201 ** MULTILINESTRING(with a single line) => LINESTRING()
202 **
203 ** GEOMETRYCOLLECTION(MULTILINESTRING()) => MULTILINESTRING()
204 **
205 ** GEOMETRYCOLLECTION(MULTILINESTRING(), MULTILINESTRING(), POINT())
206 ** => GEOMETRYCOLLECTION(MULTILINESTRING(), POINT())
207 */
208 LWGEOM *
210 {
211  LWGEOM *hgeom;
212 
213  /* EMPTY Geometry */
214  if (lwgeom_is_empty(geom))
215  {
216  if( lwgeom_is_collection(geom) )
217  {
219  }
220 
221  return lwgeom_clone(geom);
222  }
223 
224  switch (geom->type)
225  {
226 
227  /* Return simple geometries untouched */
228  case POINTTYPE:
229  case LINETYPE:
230  case CIRCSTRINGTYPE:
231  case COMPOUNDTYPE:
232  case TRIANGLETYPE:
233  case CURVEPOLYTYPE:
234  case POLYGONTYPE:
235  return lwgeom_clone(geom);
236 
237  /* Process homogeneous geometries lightly */
238  case MULTIPOINTTYPE:
239  case MULTILINETYPE:
240  case MULTIPOLYGONTYPE:
241  case MULTICURVETYPE:
242  case MULTISURFACETYPE:
244  case TINTYPE:
245  {
246  LWCOLLECTION *col = (LWCOLLECTION*)geom;
247 
248  /* Strip single-entry multi-geometries down to singletons */
249  if ( col->ngeoms == 1 )
250  {
251  hgeom = lwgeom_clone((LWGEOM*)(col->geoms[0]));
252  hgeom->srid = geom->srid;
253  if (geom->bbox)
254  hgeom->bbox = gbox_copy(geom->bbox);
255  return hgeom;
256  }
257 
258  /* Return proper multigeometry untouched */
259  return lwgeom_clone(geom);
260  }
261 
262  /* Work on anonymous collections separately */
263  case COLLECTIONTYPE:
264  return lwcollection_homogenize((LWCOLLECTION *) geom);
265  }
266 
267  /* Unknown type */
268  lwerror("lwgeom_homogenize: Geometry Type not supported (%i)",
269  lwtype_name(geom->type));
270 
271  return NULL; /* Never get here! */
272 }
#define LINETYPE
Definition: liblwgeom.h:85
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
Definition: g_box.c:433
GBOX * bbox
Definition: liblwgeom.h:397
#define MULTICURVETYPE
Definition: liblwgeom.h:94
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1078
uint8_t type
Definition: liblwgeom.h:502
#define POLYGONTYPE
Definition: liblwgeom.h:86
#define CURVEPOLYTYPE
Definition: liblwgeom.h:93
#define COMPOUNDTYPE
Definition: liblwgeom.h:92
#define MULTIPOINTTYPE
Definition: liblwgeom.h:87
#define TRIANGLETYPE
Definition: liblwgeom.h:97
#define POLYHEDRALSURFACETYPE
Definition: liblwgeom.h:96
int cnt[NUMTYPES]
Definition: lwhomogenize.c:32
uint32_t ngeoms
Definition: liblwgeom.h:506
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:923
int32_t srid
Definition: liblwgeom.h:398
unsigned int uint32_t
Definition: uthash.h:78
uint8_t flags
Definition: liblwgeom.h:503
static void init_homogenizebuffer(HomogenizeBuffer *buffer)
Definition: lwhomogenize.c:37
Datum buffer(PG_FUNCTION_ARGS)
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:218
static LWGEOM * lwcollection_homogenize(const LWCOLLECTION *col)
Definition: lwhomogenize.c:117
LWGEOM ** geoms
Definition: liblwgeom.h:508
LWGEOM * lwgeom_homogenize(const LWGEOM *geom)
Definition: lwhomogenize.c:209
#define TINTYPE
Definition: liblwgeom.h:98
LWCOLLECTION * buf[NUMTYPES]
Definition: lwhomogenize.c:33
#define NUMTYPES
Definition: liblwgeom.h:100
int32_t srid
Definition: liblwgeom.h:505
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:139
LWGEOM * lwgeom_clone(const LWGEOM *lwgeom)
Clone LWGEOM object.
Definition: lwgeom.c:475
uint32_t lwtype_get_collectiontype(uint8_t type)
Given an lwtype number, what homogeneous collection can hold it?
Definition: lwgeom.c:1113
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:89
#define MULTISURFACETYPE
Definition: liblwgeom.h:95
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
Definition: lwgeom.c:223
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:84
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:140
uint8_t type
Definition: liblwgeom.h:395
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:342
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:91
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:1386
static void lwcollection_build_buffer(const LWCOLLECTION *col, HomogenizeBuffer *buffer)
Definition: lwhomogenize.c:76
#define MULTILINETYPE
Definition: liblwgeom.h:88
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwcollection.c:94
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:930
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
Definition: lwcollection.c:187
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
#define COLLECTIONTYPE
Definition: liblwgeom.h:90
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:299