forked from libgeos/geos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeoJSON.cpp
277 lines (235 loc) · 6.08 KB
/
GeoJSON.cpp
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**********************************************************************
*
* GEOS - Geometry Engine Open Source
* http://geos.osgeo.org
*
* Copyright (C) 2021 Jared Erickson
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU Lesser General Public Licence as published
* by the Free Software Foundation.
* See the COPYING file for more information.
*
**********************************************************************/
#include <geos/io/GeoJSON.h>
namespace geos {
namespace io { // geos.io
// GeoJSONValue
GeoJSONValue::GeoJSONValue(double value)
{
type = Type::NUMBER;
d = value;
}
GeoJSONValue::GeoJSONValue(const std::string& value)
{
type = Type::STRING;
new (&s) std::string{value};
}
GeoJSONValue::GeoJSONValue()
{
type = Type::NULLTYPE;
n = nullptr;
}
GeoJSONValue::GeoJSONValue(bool value)
{
type = Type::BOOLEAN;
b = value;
}
GeoJSONValue::GeoJSONValue(const std::map<std::string, GeoJSONValue>& value)
{
type = Type::OBJECT;
new (&o) std::map<std::string, GeoJSONValue> {value};
}
GeoJSONValue::GeoJSONValue(const std::vector<GeoJSONValue>& value)
{
type = Type::ARRAY;
new (&a) std::vector<GeoJSONValue> {};
a.reserve(value.size());
for (const auto& v : value) {
a.push_back(v);
}
}
void GeoJSONValue::cleanup()
{
using std::string;
using object = std::map<std::string, GeoJSONValue>;
using array = std::vector<GeoJSONValue>;
if (type == Type::STRING) {
s.~string();
}
else if (type == Type::OBJECT) {
o.~object();
}
else if (type == Type::ARRAY) {
a.~array();
}
}
GeoJSONValue::~GeoJSONValue()
{
cleanup();
}
GeoJSONValue::GeoJSONValue(const GeoJSONValue& v)
{
switch (v.type) {
case Type::NUMBER:
d = v.d;
break;
case Type::BOOLEAN:
b = v.b;
break;
case Type::STRING:
new (&s) std::string{v.s};
break;
case Type::NULLTYPE:
n = v.n;
break;
case Type::OBJECT:
new (&o) std::map<std::string, GeoJSONValue> {v.o};
break;
case Type::ARRAY:
new (&a) std::vector<GeoJSONValue> {};
a.reserve(v.a.size());
for (const auto& i : v.a) {
a.push_back(i);
}
break;
}
type = v.type;
}
GeoJSONValue& GeoJSONValue::operator=(const GeoJSONValue& v)
{
if (type == Type::STRING && v.type == Type::STRING) {
s = v.s;
return *this;
}
else if (type == Type::OBJECT && v.type == Type::OBJECT) {
o = v.o;
return *this;
}
else if (type == Type::ARRAY && v.type == Type::ARRAY) {
a = v.a;
return *this;
}
cleanup();
switch (v.type) {
case Type::NUMBER:
d = v.d;
break;
case Type::BOOLEAN:
b = v.b;
break;
case Type::STRING:
new (&s) std::string{v.s};
break;
case Type::NULLTYPE:
n = v.n;
break;
case Type::OBJECT:
new (&o) std::map<std::string, GeoJSONValue> {v.o};
break;
case Type::ARRAY:
new (&a) std::vector<GeoJSONValue> {};
a.reserve(v.a.size());
for (const auto& i : v.a) {
a.push_back(i);
}
break;
}
type = v.type;
return *this;
}
double GeoJSONValue::getNumber() const
{
if (type != Type::NUMBER) throw GeoJSONTypeError{};
return d;
}
const std::string& GeoJSONValue::getString() const
{
if (type != Type::STRING) throw GeoJSONTypeError{};
return s;
}
std::nullptr_t GeoJSONValue::getNull() const
{
if (type != Type::NULLTYPE) throw GeoJSONTypeError{};
return n;
}
bool GeoJSONValue::getBoolean() const
{
if (type != Type::BOOLEAN) throw GeoJSONTypeError{};
return b;
}
const std::map<std::string, GeoJSONValue>& GeoJSONValue::getObject() const
{
if (type != Type::OBJECT) throw GeoJSONTypeError{};
return o;
}
const std::vector<GeoJSONValue>& GeoJSONValue::getArray() const
{
if (type != Type::ARRAY) throw GeoJSONTypeError{};
return a;
}
bool GeoJSONValue::isNumber() const
{
return type == Type::NUMBER;
}
bool GeoJSONValue::isString() const
{
return type == Type::STRING;
}
bool GeoJSONValue::isNull() const
{
return type == Type::NULLTYPE;
}
bool GeoJSONValue::isBoolean() const
{
return type == Type::BOOLEAN;
}
bool GeoJSONValue::isObject() const
{
return type == Type::OBJECT;
}
bool GeoJSONValue::isArray() const
{
return type == Type::ARRAY;
}
// GeoJSONFeature
GeoJSONFeature::GeoJSONFeature(std::unique_ptr<geom::Geometry> g,
const std::map<std::string, GeoJSONValue>& p) : geometry(std::move(g)), properties(p) {}
GeoJSONFeature::GeoJSONFeature(std::unique_ptr<geom::Geometry> g,
std::map<std::string, GeoJSONValue>&& p) : geometry(std::move(g)), properties(std::move(p)) {}
GeoJSONFeature::GeoJSONFeature(GeoJSONFeature const& other) : geometry(other.geometry->clone()),
properties(other.properties) {}
GeoJSONFeature::GeoJSONFeature(GeoJSONFeature&& other) : geometry(std::move(other.geometry)),
properties(std::move(other.properties)) {}
GeoJSONFeature& GeoJSONFeature::operator=(const GeoJSONFeature& other)
{
if (this == &other) {
return *this;
}
geometry = other.geometry->clone();
properties = other.properties;
return *this;
}
GeoJSONFeature& GeoJSONFeature::operator=(GeoJSONFeature&& other)
{
geometry = std::move(other.geometry);
properties = std::move(other.properties);
return *this;
}
const geom::Geometry* GeoJSONFeature::getGeometry() const
{
return geometry.get();
}
const std::map<std::string, GeoJSONValue>& GeoJSONFeature::getProperties() const
{
return properties;
}
// GeoJSONFeatureCollection
GeoJSONFeatureCollection::GeoJSONFeatureCollection(const std::vector<GeoJSONFeature>& f) : features(f) {}
GeoJSONFeatureCollection::GeoJSONFeatureCollection(std::vector<GeoJSONFeature>&& f) : features(std::move(f)) {}
const std::vector<GeoJSONFeature>& GeoJSONFeatureCollection::getFeatures() const
{
return features;
}
} // namespace geos.io
} // namespace geos