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