forked from griddb/griddb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gis_geomfromtext.h
181 lines (164 loc) · 4.14 KB
/
gis_geomfromtext.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
Copyright (c) 2017 TOSHIBA Digital Solutions Corporation
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
@file
@brief GeomFromText Functor
*/
#ifndef GIS_GEOMFROMTEXT_H_
#define GIS_GEOMFROMTEXT_H_
#include "function_map.h"
#include "gis_geometry.h"
#include "gis_linestring.h"
#include "gis_multipolygon.h"
#include "gis_pointgeom.h"
#include "gis_polygon.h"
#include "gis_polyhedralsurface.h"
#include "gis_quadraticsurface.h"
#include "qp_def.h"
#include "lexer.h"
#include "tql_token.h"
#include "wkt.h"
#include "wkt_token.h"
//#define WKT_TRACE
/*!
* @brief GeomFromText Functor
*
*/
class FunctorGeomFromText : public TqlFunc {
public:
using TqlFunc::operator();
Expr *operator()(
ExprList &args, TransactionContext &txn, ObjectManager &objectManager) {
if (args.size() != 1) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_COUNT,
"Invalid argument count");
}
FunctionMap *wktFunctionMap = FunctionMap::getInstanceForWkt();
if (args[0]->isNullValue()) {
return Expr::newNullValue(txn);
}
if (!args[0]->isString()) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_TYPE,
"Argument 1 is not a string.");
}
lemon_wktParser::wktParser *p =
QP_NEW_BY_TXN(txn) lemon_wktParser::wktParser();
if (p == NULL) {
GS_THROW_USER_ERROR(
GS_ERROR_CM_NO_MEMORY, "Cannot allocate WKT parser");
}
lemon_wktParser::wktParserArg arg;
int ret;
const char *c_str = args[0]->getValueAsString(txn);
Token t;
Expr *e = NULL;
arg.ev = &e;
arg.txn = &txn;
arg.objectManager = &objectManager;
arg.fmap = wktFunctionMap;
arg.err = 0;
#ifdef WKT_TRACE
p->wktParserSetTrace(&std::cerr, "wkt_trace:");
#endif
do {
ret = getToken(txn, c_str, t);
if (ret == 0 || ret == -1) {
break;
}
if (t.id == TK_ILLEGAL) {
break;
}
if (ret != -2) {
p->Execute(t.id, t, &arg);
}
if (arg.err != 0) {
QP_SAFE_DELETE(p);
std::string str2 =
"Argument 1 is not a WKT string: ";
str2 += c_str;
GS_THROW_USER_ERROR(
GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_TYPE, str2.c_str());
}
c_str += t.n;
} while (1);
p->Execute(0, t, &arg);
QP_SAFE_DELETE(p);
if (*arg.ev == NULL) {
std::string str2 = "Argument 1 is not a WKT string: ";
str2 += c_str;
GS_THROW_USER_ERROR(
GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_TYPE, str2.c_str());
}
return *arg.ev;
}
private:
/*!
* Get token from the main TQL lexer.
* (We do not use special lexer for maintainability.)
*
* @param str WKT string to parse
* @param t Token reference
*
* @return 1 if succeeded or 0 when failed
*/
int getToken(TransactionContext &txn, const char *str, Token &t) {
int ret = Lexer::gsGetToken(str, t);
#define CASE_MAP_TOKEN(x) \
case TK_##x: \
t.id = TK_WKT_##x; \
break
switch (t.id) {
CASE_MAP_TOKEN(GISFUNC);
CASE_MAP_TOKEN(LP);
CASE_MAP_TOKEN(RP);
CASE_MAP_TOKEN(SEMICOLON);
CASE_MAP_TOKEN(INTEGER);
CASE_MAP_TOKEN(COMMA);
CASE_MAP_TOKEN(MINUS);
CASE_MAP_TOKEN(FLOAT);
CASE_MAP_TOKEN(PLUS);
case TK_ID: {
char buf[256], *p;
if (t.n < 256) {
p = buf;
}
else {
p = reinterpret_cast<char *>(QP_ALLOCATOR.allocate(t.n + 1));
}
memcpy(p, t.z, t.n);
buf[t.n] = '\0';
if (util::stricmp(p, "EMPTY") == 0) {
t.id = TK_WKT_EMPTY;
}
else {
t.id = TK_WKT_GISFUNC;
}
} break;
case TK_SPACE:
ret = -2;
break;
case TK_NAN:
case TK_INF:
case TK_ILLEGAL:
ret = -1;
break;
default:
t.id = TK_WKT_GISFUNC;
break;
}
#undef CASE_MAP_TOKEN
return ret;
}
};
#endif