PostGIS  3.0.6dev-r@@SVN_REVISION@@
lwgeom_debug.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 (C) 2004 Refractions Research Inc.
22  *
23  **********************************************************************/
24 
25 
26 #include "lwgeom_log.h"
27 #include "liblwgeom.h"
28 
29 #include <stdio.h>
30 #include <string.h>
31 
32 /* Place to hold the ZM string used in other summaries */
33 static char tflags[6];
34 
35 static char *
37 {
38  int flagno = 0;
39  if ( FLAGS_GET_Z(lwg->flags) ) tflags[flagno++] = 'Z';
40  if ( FLAGS_GET_M(lwg->flags) ) tflags[flagno++] = 'M';
41  if ( FLAGS_GET_BBOX(lwg->flags) ) tflags[flagno++] = 'B';
42  if ( FLAGS_GET_GEODETIC(lwg->flags) ) tflags[flagno++] = 'G';
43  if ( lwg->srid != SRID_UNKNOWN ) tflags[flagno++] = 'S';
44  tflags[flagno] = '\0';
45 
46  LWDEBUGF(4, "Flags: %s - returning %p", lwg->flags, tflags);
47 
48  return tflags;
49 }
50 
51 /*
52  * Returns an alloced string containing summary for the LWGEOM object
53  */
54 static char *
55 lwpoint_summary(LWPOINT *point, int offset)
56 {
57  char *result;
58  char *pad="";
59  char *zmflags = lwgeom_flagchars((LWGEOM*)point);
60  size_t sz = 128+offset;
61 
62  result = (char *)lwalloc(sz);
63 
64  snprintf(result, sz, "%*.s%s[%s]",
65  offset, pad, lwtype_name(point->type),
66  zmflags);
67  return result;
68 }
69 
70 static char *
71 lwline_summary(LWLINE *line, int offset)
72 {
73  char *result;
74  char *pad="";
75  char *zmflags = lwgeom_flagchars((LWGEOM*)line);
76  size_t sz = 128+offset;
77 
78  result = (char *)lwalloc(sz);
79 
80  snprintf(result, sz, "%*.s%s[%s] with %d points",
81  offset, pad, lwtype_name(line->type),
82  zmflags,
83  line->points->npoints);
84  return result;
85 }
86 
87 
88 static char *
90 {
91  size_t size = 128;
92  char *result;
93  char *tmp;
94  uint32_t i;
95  static char *nl = "\n";
96  char *pad="";
97  char *zmflags = lwgeom_flagchars((LWGEOM*)col);
98 
99  LWDEBUG(2, "lwcollection_summary called");
100 
101  result = (char *)lwalloc(size);
102 
103  snprintf(result, size, "%*.s%s[%s] with %d element%s",
104  offset, pad, lwtype_name(col->type),
105  zmflags,
106  col->ngeoms,
107  col->ngeoms ?
108  ( col->ngeoms > 1 ? "s:\n" : ":\n")
109  : "s");
110 
111  for (i=0; i<col->ngeoms; i++)
112  {
113  tmp = lwgeom_summary(col->geoms[i], offset+2);
114  size += strlen(tmp)+1;
115  result = lwrealloc(result, size);
116 
117  LWDEBUGF(4, "Reallocated %d bytes for result", size);
118  if ( i > 0 ) strcat(result,nl);
119 
120  strcat(result, tmp);
121  lwfree(tmp);
122  }
123 
124  LWDEBUG(3, "lwcollection_summary returning");
125 
126  return result;
127 }
128 
129 static char *
130 lwpoly_summary(LWPOLY *poly, int offset)
131 {
132  char tmp[256];
133  size_t size = 64*(poly->nrings+1)+128;
134  char *result;
135  uint32_t i;
136  char *pad="";
137  static char *nl = "\n";
138  char *zmflags = lwgeom_flagchars((LWGEOM*)poly);
139 
140  LWDEBUG(2, "lwpoly_summary called");
141 
142  result = (char *)lwalloc(size);
143 
144  snprintf(result, size, "%*.s%s[%s] with %i ring%s",
145  offset, pad, lwtype_name(poly->type),
146  zmflags,
147  poly->nrings,
148  poly->nrings ?
149  ( poly->nrings > 1 ? "s:\n" : ":\n")
150  : "s");
151 
152  for (i=0; i<poly->nrings; i++)
153  {
154  snprintf(tmp, sizeof(tmp), "%s ring %i has %i points",
155  pad, i, poly->rings[i]->npoints);
156  if ( i > 0 ) strcat(result,nl);
157  strcat(result,tmp);
158  }
159 
160  LWDEBUG(3, "lwpoly_summary returning");
161 
162  return result;
163 }
164 
165 char *
166 lwgeom_summary(const LWGEOM *lwgeom, int offset)
167 {
168  char *result;
169 
170  switch (lwgeom->type)
171  {
172  case POINTTYPE:
173  return lwpoint_summary((LWPOINT *)lwgeom, offset);
174 
175  case CIRCSTRINGTYPE:
176  case TRIANGLETYPE:
177  case LINETYPE:
178  return lwline_summary((LWLINE *)lwgeom, offset);
179 
180  case POLYGONTYPE:
181  return lwpoly_summary((LWPOLY *)lwgeom, offset);
182 
183  case TINTYPE:
184  case MULTISURFACETYPE:
185  case MULTICURVETYPE:
186  case CURVEPOLYTYPE:
187  case COMPOUNDTYPE:
188  case MULTIPOINTTYPE:
189  case MULTILINETYPE:
190  case MULTIPOLYGONTYPE:
191  case COLLECTIONTYPE:
192  return lwcollection_summary((LWCOLLECTION *)lwgeom, offset);
193  default:
194  result = (char *)lwalloc(256);
195  snprintf(result, 256, "Object is of unknown type: %d",
196  lwgeom->type);
197  return result;
198  }
199 
200  return NULL;
201 }
#define COLLECTIONTYPE
Definition: liblwgeom.h:122
#define COMPOUNDTYPE
Definition: liblwgeom.h:124
#define CURVEPOLYTYPE
Definition: liblwgeom.h:125
#define MULTILINETYPE
Definition: liblwgeom.h:120
#define MULTISURFACETYPE
Definition: liblwgeom.h:127
#define LINETYPE
Definition: liblwgeom.h:117
#define FLAGS_GET_BBOX(flags)
Definition: liblwgeom.h:181
#define MULTIPOINTTYPE
Definition: liblwgeom.h:119
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:116
#define FLAGS_GET_Z(flags)
Definition: liblwgeom.h:179
#define TINTYPE
Definition: liblwgeom.h:130
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:121
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:235
void lwfree(void *mem)
Definition: lwutil.c:242
#define POLYGONTYPE
Definition: liblwgeom.h:118
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:123
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:180
#define MULTICURVETYPE
Definition: liblwgeom.h:126
#define TRIANGLETYPE
Definition: liblwgeom.h:129
void * lwalloc(size_t size)
Definition: lwutil.c:227
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:229
#define FLAGS_GET_GEODETIC(flags)
Definition: liblwgeom.h:182
This library is the generic geometry handling section of PostGIS.
static char tflags[6]
Definition: lwgeom_debug.c:33
static char * lwline_summary(LWLINE *line, int offset)
Definition: lwgeom_debug.c:71
static char * lwgeom_flagchars(LWGEOM *lwg)
Definition: lwgeom_debug.c:36
char * lwgeom_summary(const LWGEOM *lwgeom, int offset)
Definition: lwgeom_debug.c:166
static char * lwcollection_summary(LWCOLLECTION *col, int offset)
Definition: lwgeom_debug.c:89
static char * lwpoint_summary(LWPOINT *point, int offset)
Definition: lwgeom_debug.c:55
static char * lwpoly_summary(LWPOLY *poly, int offset)
Definition: lwgeom_debug.c:130
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
uint32_t ngeoms
Definition: liblwgeom.h:566
uint8_t type
Definition: liblwgeom.h:564
LWGEOM ** geoms
Definition: liblwgeom.h:561
uint8_t type
Definition: liblwgeom.h:448
int32_t srid
Definition: liblwgeom.h:446
lwflags_t flags
Definition: liblwgeom.h:447
POINTARRAY * points
Definition: liblwgeom.h:469
uint8_t type
Definition: liblwgeom.h:472
uint8_t type
Definition: liblwgeom.h:460
POINTARRAY ** rings
Definition: liblwgeom.h:505
uint8_t type
Definition: liblwgeom.h:508
uint32_t nrings
Definition: liblwgeom.h:510
uint32_t npoints
Definition: liblwgeom.h:413