PostGIS  2.2.7dev-r@@SVN_REVISION@@
g_util.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * Copyright 2009 Paul Ramsey <pramsey@cleverelephant.ca>
5  *
6  * This is free software; you can redistribute and/or modify it under
7  * the terms of the GNU General Public Licence. See the COPYING file.
8  *
9  **********************************************************************/
10 
11 #include <ctype.h>
12 
13 #include "liblwgeom_internal.h"
14 
15 /* Structure for the type array */
17 {
18  char *typename;
19  int type;
20  int z;
21  int m;
22 };
23 
24 /* Type array. Note that the order of this array is important in
25  that any typename in the list must *NOT* occur within an entry
26  before it. Otherwise if we search for "POINT" at the top of the
27  list we would also match MULTIPOINT, for example. */
28 
30 {
31  { "GEOMETRYCOLLECTIONZM", COLLECTIONTYPE, 1, 1 },
32  { "GEOMETRYCOLLECTIONZ", COLLECTIONTYPE, 1, 0 },
33  { "GEOMETRYCOLLECTIONM", COLLECTIONTYPE, 0, 1 },
34  { "GEOMETRYCOLLECTION", COLLECTIONTYPE, 0, 0 },
35 
36  { "GEOMETRYZM", 0, 1, 1 },
37  { "GEOMETRYZ", 0, 1, 0 },
38  { "GEOMETRYM", 0, 0, 1 },
39  { "GEOMETRY", 0, 0, 0 },
40 
41  { "POLYHEDRALSURFACEZM", POLYHEDRALSURFACETYPE, 1, 1 },
42  { "POLYHEDRALSURFACEZ", POLYHEDRALSURFACETYPE, 1, 0 },
43  { "POLYHEDRALSURFACEM", POLYHEDRALSURFACETYPE, 0, 1 },
44  { "POLYHEDRALSURFACE", POLYHEDRALSURFACETYPE, 0, 0 },
45 
46  { "TINZM", TINTYPE, 1, 1 },
47  { "TINZ", TINTYPE, 1, 0 },
48  { "TINM", TINTYPE, 0, 1 },
49  { "TIN", TINTYPE, 0, 0 },
50 
51  { "CIRCULARSTRINGZM", CIRCSTRINGTYPE, 1, 1 },
52  { "CIRCULARSTRINGZ", CIRCSTRINGTYPE, 1, 0 },
53  { "CIRCULARSTRINGM", CIRCSTRINGTYPE, 0, 1 },
54  { "CIRCULARSTRING", CIRCSTRINGTYPE, 0, 0 },
55 
56  { "COMPOUNDCURVEZM", COMPOUNDTYPE, 1, 1 },
57  { "COMPOUNDCURVEZ", COMPOUNDTYPE, 1, 0 },
58  { "COMPOUNDCURVEM", COMPOUNDTYPE, 0, 1 },
59  { "COMPOUNDCURVE", COMPOUNDTYPE, 0, 0 },
60 
61  { "CURVEPOLYGONZM", CURVEPOLYTYPE, 1, 1 },
62  { "CURVEPOLYGONZ", CURVEPOLYTYPE, 1, 0 },
63  { "CURVEPOLYGONM", CURVEPOLYTYPE, 0, 1 },
64  { "CURVEPOLYGON", CURVEPOLYTYPE, 0, 0 },
65 
66  { "MULTICURVEZM", MULTICURVETYPE, 1, 1 },
67  { "MULTICURVEZ", MULTICURVETYPE, 1, 0 },
68  { "MULTICURVEM", MULTICURVETYPE, 0, 1 },
69  { "MULTICURVE", MULTICURVETYPE, 0, 0 },
70 
71  { "MULTISURFACEZM", MULTISURFACETYPE, 1, 1 },
72  { "MULTISURFACEZ", MULTISURFACETYPE, 1, 0 },
73  { "MULTISURFACEM", MULTISURFACETYPE, 0, 1 },
74  { "MULTISURFACE", MULTISURFACETYPE, 0, 0 },
75 
76  { "MULTILINESTRINGZM", MULTILINETYPE, 1, 1 },
77  { "MULTILINESTRINGZ", MULTILINETYPE, 1, 0 },
78  { "MULTILINESTRINGM", MULTILINETYPE, 0, 1 },
79  { "MULTILINESTRING", MULTILINETYPE, 0, 0 },
80 
81  { "MULTIPOLYGONZM", MULTIPOLYGONTYPE, 1, 1 },
82  { "MULTIPOLYGONZ", MULTIPOLYGONTYPE, 1, 0 },
83  { "MULTIPOLYGONM", MULTIPOLYGONTYPE, 0, 1 },
84  { "MULTIPOLYGON", MULTIPOLYGONTYPE, 0, 0 },
85 
86  { "MULTIPOINTZM", MULTIPOINTTYPE, 1, 1 },
87  { "MULTIPOINTZ", MULTIPOINTTYPE, 1, 0 },
88  { "MULTIPOINTM", MULTIPOINTTYPE, 0, 1 },
89  { "MULTIPOINT", MULTIPOINTTYPE, 0, 0 },
90 
91  { "LINESTRINGZM", LINETYPE, 1, 1 },
92  { "LINESTRINGZ", LINETYPE, 1, 0 },
93  { "LINESTRINGM", LINETYPE, 0, 1 },
94  { "LINESTRING", LINETYPE, 0, 0 },
95 
96  { "TRIANGLEZM", TRIANGLETYPE, 1, 1 },
97  { "TRIANGLEZ", TRIANGLETYPE, 1, 0 },
98  { "TRIANGLEM", TRIANGLETYPE, 0, 1 },
99  { "TRIANGLE", TRIANGLETYPE, 0, 0 },
100 
101  { "POLYGONZM", POLYGONTYPE, 1, 1 },
102  { "POLYGONZ", POLYGONTYPE, 1, 0 },
103  { "POLYGONM", POLYGONTYPE, 0, 1 },
104  { "POLYGON", POLYGONTYPE, 0, 0 },
105 
106  { "POINTZM", POINTTYPE, 1, 1 },
107  { "POINTZ", POINTTYPE, 1, 0 },
108  { "POINTM", POINTTYPE, 0, 1 },
109  { "POINT", POINTTYPE, 0, 0 }
110 
111 };
112 #define GEOMTYPE_STRUCT_ARRAY_LEN (sizeof geomtype_struct_array/sizeof(struct geomtype_struct))
113 
114 /*
115 * We use a very simple upper case mapper here, because the system toupper() function
116 * is locale dependent and may have trouble mapping lower case strings to the upper
117 * case ones we expect (see, the "Turkisk I", http://www.i18nguy.com/unicode/turkish-i18n.html)
118 * We could also count on PgSQL sending us *lower* case inputs, as it seems to do that
119 * regardless of the case the user provides for the type arguments.
120 */
121 const char dumb_upper_map[128] = "................................................0123456789.......ABCDEFGHIJKLMNOPQRSTUVWXYZ......ABCDEFGHIJKLMNOPQRSTUVWXYZ.....";
122 
123 static char dump_toupper(int in)
124 {
125  if ( in < 0 || in > 127 )
126  return '.';
127  return dumb_upper_map[in];
128 }
129 
130 uint8_t gflags(int hasz, int hasm, int geodetic)
131 {
132  uint8_t flags = 0;
133  if ( hasz )
134  FLAGS_SET_Z(flags, 1);
135  if ( hasm )
136  FLAGS_SET_M(flags, 1);
137  if ( geodetic )
138  FLAGS_SET_GEODETIC(flags, 1);
139  return flags;
140 }
141 
148 int geometry_type_from_string(const char *str, uint8_t *type, int *z, int *m)
149 {
150  char *tmpstr;
151  int tmpstartpos, tmpendpos;
152  int i;
153 
154  assert(str);
155  assert(type);
156  assert(z);
157  assert(m);
158 
159  /* Initialize. */
160  *type = 0;
161  *z = 0;
162  *m = 0;
163 
164  /* Locate any leading/trailing spaces */
165  tmpstartpos = 0;
166  for (i = 0; i < strlen(str); i++)
167  {
168  if (str[i] != ' ')
169  {
170  tmpstartpos = i;
171  break;
172  }
173  }
174 
175  tmpendpos = strlen(str) - 1;
176  for (i = strlen(str) - 1; i >= 0; i--)
177  {
178  if (str[i] != ' ')
179  {
180  tmpendpos = i;
181  break;
182  }
183  }
184 
185  /* Copy and convert to upper case for comparison */
186  tmpstr = lwalloc(tmpendpos - tmpstartpos + 2);
187  for (i = tmpstartpos; i <= tmpendpos; i++)
188  tmpstr[i - tmpstartpos] = dump_toupper(str[i]);
189 
190  /* Add NULL to terminate */
191  tmpstr[i - tmpstartpos] = '\0';
192 
193  /* Now check for the type */
194  for (i = 0; i < GEOMTYPE_STRUCT_ARRAY_LEN; i++)
195  {
196  if (!strcmp(tmpstr, geomtype_struct_array[i].typename))
197  {
198  *type = geomtype_struct_array[i].type;
199  *z = geomtype_struct_array[i].z;
200  *m = geomtype_struct_array[i].m;
201 
202  lwfree(tmpstr);
203 
204  return LW_SUCCESS;
205  }
206 
207  }
208 
209  lwfree(tmpstr);
210 
211  return LW_FAILURE;
212 }
213 
214 
215 
216 
#define LINETYPE
Definition: liblwgeom.h:71
#define MULTICURVETYPE
Definition: liblwgeom.h:80
void lwfree(void *mem)
Definition: lwutil.c:214
#define POLYGONTYPE
Definition: liblwgeom.h:72
#define CURVEPOLYTYPE
Definition: liblwgeom.h:79
#define COMPOUNDTYPE
Definition: liblwgeom.h:78
#define MULTIPOINTTYPE
Definition: liblwgeom.h:73
int geometry_type_from_string(const char *str, uint8_t *type, int *z, int *m)
Calculate type integer and dimensional flags from string input.
Definition: g_util.c:148
#define LW_SUCCESS
Definition: liblwgeom.h:65
#define TRIANGLETYPE
Definition: liblwgeom.h:83
#define POLYHEDRALSURFACETYPE
Definition: liblwgeom.h:82
#define FLAGS_SET_GEODETIC(flags, value)
Definition: liblwgeom.h:133
#define LW_FAILURE
Definition: liblwgeom.h:64
#define FLAGS_SET_Z(flags, value)
Definition: liblwgeom.h:130
#define TINTYPE
Definition: liblwgeom.h:84
const char dumb_upper_map[128]
Definition: g_util.c:121
struct geomtype_struct geomtype_struct_array[]
Definition: g_util.c:29
uint8_t gflags(int hasz, int hasm, int geodetic)
Construct a new flags char.
Definition: g_util.c:130
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:75
#define MULTISURFACETYPE
Definition: liblwgeom.h:81
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:70
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:77
void * lwalloc(size_t size)
Definition: lwutil.c:199
static char dump_toupper(int in)
Definition: g_util.c:123
#define MULTILINETYPE
Definition: liblwgeom.h:74
#define GEOMTYPE_STRUCT_ARRAY_LEN
Definition: g_util.c:112
#define COLLECTIONTYPE
Definition: liblwgeom.h:76
#define FLAGS_SET_M(flags, value)
Definition: liblwgeom.h:131