PostGIS  2.5.0dev-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 
61  result = (char *)lwalloc(128+offset);
62 
63  sprintf(result, "%*.s%s[%s]",
64  offset, pad, lwtype_name(point->type),
65  zmflags);
66  return result;
67 }
68 
69 static char *
70 lwline_summary(LWLINE *line, int offset)
71 {
72  char *result;
73  char *pad="";
74  char *zmflags = lwgeom_flagchars((LWGEOM*)line);
75 
76  result = (char *)lwalloc(128+offset);
77 
78  sprintf(result, "%*.s%s[%s] with %d points",
79  offset, pad, lwtype_name(line->type),
80  zmflags,
81  line->points->npoints);
82  return result;
83 }
84 
85 
86 static char *
88 {
89  size_t size = 128;
90  char *result;
91  char *tmp;
92  uint32_t i;
93  static char *nl = "\n";
94  char *pad="";
95  char *zmflags = lwgeom_flagchars((LWGEOM*)col);
96 
97  LWDEBUG(2, "lwcollection_summary called");
98 
99  result = (char *)lwalloc(size);
100 
101  sprintf(result, "%*.s%s[%s] with %d elements\n",
102  offset, pad, lwtype_name(col->type),
103  zmflags,
104  col->ngeoms);
105 
106  for (i=0; i<col->ngeoms; i++)
107  {
108  tmp = lwgeom_summary(col->geoms[i], offset+2);
109  size += strlen(tmp)+1;
110  result = lwrealloc(result, size);
111 
112  LWDEBUGF(4, "Reallocated %d bytes for result", size);
113  if ( i > 0 ) strcat(result,nl);
114 
115  strcat(result, tmp);
116  lwfree(tmp);
117  }
118 
119  LWDEBUG(3, "lwcollection_summary returning");
120 
121  return result;
122 }
123 
124 static char *
125 lwpoly_summary(LWPOLY *poly, int offset)
126 {
127  char tmp[256];
128  size_t size = 64*(poly->nrings+1)+128;
129  char *result;
130  uint32_t i;
131  char *pad="";
132  static char *nl = "\n";
133  char *zmflags = lwgeom_flagchars((LWGEOM*)poly);
134 
135  LWDEBUG(2, "lwpoly_summary called");
136 
137  result = (char *)lwalloc(size);
138 
139  sprintf(result, "%*.s%s[%s] with %i rings\n",
140  offset, pad, lwtype_name(poly->type),
141  zmflags,
142  poly->nrings);
143 
144  for (i=0; i<poly->nrings; i++)
145  {
146  sprintf(tmp,"%s ring %i has %i points",
147  pad, i, poly->rings[i]->npoints);
148  if ( i > 0 ) strcat(result,nl);
149  strcat(result,tmp);
150  }
151 
152  LWDEBUG(3, "lwpoly_summary returning");
153 
154  return result;
155 }
156 
157 char *
158 lwgeom_summary(const LWGEOM *lwgeom, int offset)
159 {
160  char *result;
161 
162  switch (lwgeom->type)
163  {
164  case POINTTYPE:
165  return lwpoint_summary((LWPOINT *)lwgeom, offset);
166 
167  case CIRCSTRINGTYPE:
168  case TRIANGLETYPE:
169  case LINETYPE:
170  return lwline_summary((LWLINE *)lwgeom, offset);
171 
172  case POLYGONTYPE:
173  return lwpoly_summary((LWPOLY *)lwgeom, offset);
174 
175  case TINTYPE:
176  case MULTISURFACETYPE:
177  case MULTICURVETYPE:
178  case CURVEPOLYTYPE:
179  case COMPOUNDTYPE:
180  case MULTIPOINTTYPE:
181  case MULTILINETYPE:
182  case MULTIPOLYGONTYPE:
183  case COLLECTIONTYPE:
184  return lwcollection_summary((LWCOLLECTION *)lwgeom, offset);
185  default:
186  result = (char *)lwalloc(256);
187  sprintf(result, "Object is of unknown type: %d",
188  lwgeom->type);
189  return result;
190  }
191 
192  return NULL;
193 }
#define LINETYPE
Definition: liblwgeom.h:85
static char * lwline_summary(LWLINE *line, int offset)
Definition: lwgeom_debug.c:70
#define MULTICURVETYPE
Definition: liblwgeom.h:94
void lwfree(void *mem)
Definition: lwutil.c:244
uint8_t type
Definition: liblwgeom.h:505
#define FLAGS_GET_GEODETIC(flags)
Definition: liblwgeom.h:142
#define POLYGONTYPE
Definition: liblwgeom.h:86
uint8_t flags
Definition: liblwgeom.h:399
char * lwgeom_summary(const LWGEOM *lwgeom, int offset)
Definition: lwgeom_debug.c:158
#define CURVEPOLYTYPE
Definition: liblwgeom.h:93
#define COMPOUNDTYPE
Definition: liblwgeom.h:92
#define MULTIPOINTTYPE
Definition: liblwgeom.h:87
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
#define TRIANGLETYPE
Definition: liblwgeom.h:97
static char * lwpoint_summary(LWPOINT *point, int offset)
Definition: lwgeom_debug.c:55
static char tflags[6]
Definition: lwgeom_debug.c:33
int32_t srid
Definition: liblwgeom.h:401
uint32_t nrings
Definition: liblwgeom.h:457
unsigned int uint32_t
Definition: uthash.h:78
static char * lwgeom_flagchars(LWGEOM *lwg)
Definition: lwgeom_debug.c:36
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_BBOX(flags)
Definition: liblwgeom.h:141
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:187
#define TINTYPE
Definition: liblwgeom.h:98
static char * lwpoly_summary(LWPOLY *poly, int offset)
Definition: lwgeom_debug.c:125
#define FLAGS_GET_Z(flags)
Macros for manipulating the &#39;flags&#39; byte.
Definition: liblwgeom.h:139
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:89
#define MULTISURFACETYPE
Definition: liblwgeom.h:95
static char * lwcollection_summary(LWCOLLECTION *col, int offset)
Definition: lwgeom_debug.c:87
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:237
#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:398
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:91
void * lwalloc(size_t size)
Definition: lwutil.c:229
#define MULTILINETYPE
Definition: liblwgeom.h:88
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
#define COLLECTIONTYPE
Definition: liblwgeom.h:90
This library is the generic geometry handling section of PostGIS.