PostGIS  2.1.10dev-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  * Copyright (C) 2004 Refractions Research Inc.
7  *
8  * This is free software; you can redistribute and/or modify it under
9  * the terms of the GNU General Public Licence. See the COPYING file.
10  *
11  **********************************************************************/
12 
13 #include "lwgeom_log.h"
14 #include "liblwgeom.h"
15 
16 #include <stdio.h>
17 #include <string.h>
18 
19 /* Place to hold the ZM string used in other summaries */
20 static char tflags[6];
21 
22 static char *
24 {
25  int flagno = 0;
26  if ( FLAGS_GET_Z(lwg->flags) ) tflags[flagno++] = 'Z';
27  if ( FLAGS_GET_M(lwg->flags) ) tflags[flagno++] = 'M';
28  if ( FLAGS_GET_BBOX(lwg->flags) ) tflags[flagno++] = 'B';
29  if ( FLAGS_GET_GEODETIC(lwg->flags) ) tflags[flagno++] = 'G';
30  if ( lwg->srid != SRID_UNKNOWN ) tflags[flagno++] = 'S';
31  tflags[flagno] = '\0';
32 
33  LWDEBUGF(4, "Flags: %s - returning %p", flags, tflags);
34 
35  return tflags;
36 }
37 
38 /*
39  * Returns an alloced string containing summary for the LWGEOM object
40  */
41 static char *
42 lwpoint_summary(LWPOINT *point, int offset)
43 {
44  char *result;
45  char *pad="";
46  char *zmflags = lwgeom_flagchars((LWGEOM*)point);
47 
48  result = (char *)lwalloc(128+offset);
49 
50  sprintf(result, "%*.s%s[%s]",
51  offset, pad, lwtype_name(point->type),
52  zmflags);
53  return result;
54 }
55 
56 static char *
57 lwline_summary(LWLINE *line, int offset)
58 {
59  char *result;
60  char *pad="";
61  char *zmflags = lwgeom_flagchars((LWGEOM*)line);
62 
63  result = (char *)lwalloc(128+offset);
64 
65  sprintf(result, "%*.s%s[%s] with %d points",
66  offset, pad, lwtype_name(line->type),
67  zmflags,
68  line->points->npoints);
69  return result;
70 }
71 
72 
73 static char *
75 {
76  size_t size = 128;
77  char *result;
78  char *tmp;
79  int i;
80  static char *nl = "\n";
81  char *pad="";
82  char *zmflags = lwgeom_flagchars((LWGEOM*)col);
83 
84  LWDEBUG(2, "lwcollection_summary called");
85 
86  result = (char *)lwalloc(size);
87 
88  sprintf(result, "%*.s%s[%s] with %d elements\n",
89  offset, pad, lwtype_name(col->type),
90  zmflags,
91  col->ngeoms);
92 
93  for (i=0; i<col->ngeoms; i++)
94  {
95  tmp = lwgeom_summary(col->geoms[i], offset+2);
96  size += strlen(tmp)+1;
97  result = lwrealloc(result, size);
98 
99  LWDEBUGF(4, "Reallocated %d bytes for result", size);
100  if ( i > 0 ) strcat(result,nl);
101 
102  strcat(result, tmp);
103  lwfree(tmp);
104  }
105 
106  LWDEBUG(3, "lwcollection_summary returning");
107 
108  return result;
109 }
110 
111 static char *
112 lwpoly_summary(LWPOLY *poly, int offset)
113 {
114  char tmp[256];
115  size_t size = 64*(poly->nrings+1)+128;
116  char *result;
117  int i;
118  char *pad="";
119  static char *nl = "\n";
120  char *zmflags = lwgeom_flagchars((LWGEOM*)poly);
121 
122  LWDEBUG(2, "lwpoly_summary called");
123 
124  result = (char *)lwalloc(size);
125 
126  sprintf(result, "%*.s%s[%s] with %i rings\n",
127  offset, pad, lwtype_name(poly->type),
128  zmflags,
129  poly->nrings);
130 
131  for (i=0; i<poly->nrings; i++)
132  {
133  sprintf(tmp,"%s ring %i has %i points",
134  pad, i, poly->rings[i]->npoints);
135  if ( i > 0 ) strcat(result,nl);
136  strcat(result,tmp);
137  }
138 
139  LWDEBUG(3, "lwpoly_summary returning");
140 
141  return result;
142 }
143 
144 char *
145 lwgeom_summary(const LWGEOM *lwgeom, int offset)
146 {
147  char *result;
148 
149  switch (lwgeom->type)
150  {
151  case POINTTYPE:
152  return lwpoint_summary((LWPOINT *)lwgeom, offset);
153 
154  case LINETYPE:
155  return lwline_summary((LWLINE *)lwgeom, offset);
156 
157  case POLYGONTYPE:
158  return lwpoly_summary((LWPOLY *)lwgeom, offset);
159 
160  case MULTIPOINTTYPE:
161  case MULTILINETYPE:
162  case MULTIPOLYGONTYPE:
163  case COLLECTIONTYPE:
164  return lwcollection_summary((LWCOLLECTION *)lwgeom, offset);
165  default:
166  result = (char *)lwalloc(256);
167  sprintf(result, "Object is of unknown type: %d",
168  lwgeom->type);
169  return result;
170  }
171 
172  return NULL;
173 }
#define LINETYPE
Definition: liblwgeom.h:61
uint8_t type
Definition: liblwgeom.h:374
static char * lwline_summary(LWLINE *line, int offset)
Definition: lwgeom_debug.c:57
uint8_t type
Definition: liblwgeom.h:363
void lwfree(void *mem)
Definition: lwutil.c:190
int npoints
Definition: liblwgeom.h:327
uint8_t type
Definition: liblwgeom.h:459
#define FLAGS_GET_GEODETIC(flags)
Definition: liblwgeom.h:109
#define POLYGONTYPE
Definition: liblwgeom.h:62
uint8_t flags
Definition: liblwgeom.h:353
char * lwgeom_summary(const LWGEOM *lwgeom, int offset)
Definition: lwgeom_debug.c:145
#define MULTIPOINTTYPE
Definition: liblwgeom.h:63
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:50
static char * lwpoint_summary(LWPOINT *point, int offset)
Definition: lwgeom_debug.c:42
static char tflags[6]
Definition: lwgeom_debug.c:20
char ** result
Definition: liblwgeom.h:218
int32_t srid
Definition: liblwgeom.h:355
static char * lwgeom_flagchars(LWGEOM *lwg)
Definition: lwgeom_debug.c:23
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:164
#define FLAGS_GET_BBOX(flags)
Definition: liblwgeom.h:108
uint8_t type
Definition: liblwgeom.h:407
LWGEOM ** geoms
Definition: liblwgeom.h:465
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:154
POINTARRAY ** rings
Definition: liblwgeom.h:413
static char * lwpoly_summary(LWPOLY *poly, int offset)
Definition: lwgeom_debug.c:112
int nrings
Definition: liblwgeom.h:411
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:106
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:65
static char * lwcollection_summary(LWCOLLECTION *col, int offset)
Definition: lwgeom_debug.c:74
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:183
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:60
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:107
uint8_t type
Definition: liblwgeom.h:352
void * lwalloc(size_t size)
Definition: lwutil.c:175
#define MULTILINETYPE
Definition: liblwgeom.h:64
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55
#define COLLECTIONTYPE
Definition: liblwgeom.h:66
This library is the generic geometry handling section of PostGIS.
POINTARRAY * points
Definition: liblwgeom.h:378