-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathspatial_reference_system_impl.cc
More file actions
219 lines (179 loc) · 8.73 KB
/
spatial_reference_system_impl.cc
File metadata and controls
219 lines (179 loc) · 8.73 KB
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
/* Copyright (c) 2016, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
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 General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "sql/dd/impl/types/spatial_reference_system_impl.h"
#include <stdint.h>
#include <optional>
#include "my_rapidjson_size_t.h" // IWYU pragma: keep
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>
#include "m_string.h"
#include "sql/dd/impl/dictionary_impl.h" // Dictionary_impl
#include "sql/dd/impl/raw/raw_record.h" // Raw_record
#include "sql/dd/impl/sdi_impl.h" // sdi read/write functions
#include "sql/dd/impl/tables/spatial_reference_systems.h" // Spatial_reference_sy...
#include "sql/dd/impl/transaction_impl.h" // Open_dictionary_tables_ctx
#include "sql/gis/srs/srs.h" // gis::srs::parse_wkt
#include "string_with_len.h"
namespace dd {
class Sdi_rcontext;
class Sdi_wcontext;
} // namespace dd
using dd::tables::Spatial_reference_systems;
namespace dd {
///////////////////////////////////////////////////////////////////////////
// Spatial_reference_system_impl implementation.
///////////////////////////////////////////////////////////////////////////
bool Spatial_reference_system_impl::validate() const {
// The ID is an unsigned value, so we don't need to check the lower
// bound.
return id() > UINT32_MAX;
}
bool Spatial_reference_system_impl::is_lat_long() const {
return (is_geographic() && (m_parsed_definition->axis_direction(0) ==
gis::srs::Axis_direction::NORTH ||
m_parsed_definition->axis_direction(0) ==
gis::srs::Axis_direction::SOUTH));
}
///////////////////////////////////////////////////////////////////////////
bool Spatial_reference_system_impl::restore_attributes(const Raw_record &r) {
restore_id(r, Spatial_reference_systems::FIELD_ID);
restore_name(r, Spatial_reference_systems::FIELD_NAME);
m_last_altered = r.read_int(Spatial_reference_systems::FIELD_LAST_ALTERED);
m_created = r.read_int(Spatial_reference_systems::FIELD_CREATED);
if (!r.is_null(Spatial_reference_systems::FIELD_ORGANIZATION))
m_organization = r.read_str(Spatial_reference_systems::FIELD_ORGANIZATION);
if (!r.is_null(Spatial_reference_systems::FIELD_ORGANIZATION_COORDSYS_ID))
m_organization_coordsys_id =
r.read_int(Spatial_reference_systems::FIELD_ORGANIZATION_COORDSYS_ID);
m_definition = r.read_str(Spatial_reference_systems::FIELD_DEFINITION);
if (!r.is_null(Spatial_reference_systems::FIELD_DESCRIPTION))
m_description = r.read_str(Spatial_reference_systems::FIELD_DESCRIPTION);
return parse_definition();
}
///////////////////////////////////////////////////////////////////////////
bool Spatial_reference_system_impl::store_attributes(Raw_record *r) {
Object_id default_catalog_id =
Dictionary_impl::instance()->default_catalog_id();
return store_id(r, Spatial_reference_systems::FIELD_ID) ||
store_name(r, Spatial_reference_systems::FIELD_NAME) ||
r->store(Spatial_reference_systems::FIELD_CATALOG_ID,
default_catalog_id) ||
r->store(Spatial_reference_systems::FIELD_LAST_ALTERED,
m_last_altered) ||
r->store(Spatial_reference_systems::FIELD_CREATED, m_created) ||
r->store(Spatial_reference_systems::FIELD_ORGANIZATION,
m_organization.has_value() ? m_organization.value() : "",
!m_organization.has_value()) ||
r->store(Spatial_reference_systems::FIELD_ORGANIZATION_COORDSYS_ID,
m_organization_coordsys_id.has_value()
? m_organization_coordsys_id.value()
: 0,
!m_organization_coordsys_id.has_value()) ||
r->store(Spatial_reference_systems::FIELD_DEFINITION, m_definition) ||
r->store(Spatial_reference_systems::FIELD_DESCRIPTION,
m_description.has_value() ? m_description.value() : "",
!m_description.has_value());
}
///////////////////////////////////////////////////////////////////////////
static_assert(Spatial_reference_systems::NUMBER_OF_FIELDS == 10,
"Spatial_reference_systems definition has changed, check if "
"serialize() and deserialize() need to be updated!");
void Spatial_reference_system_impl::serialize(Sdi_wcontext *wctx,
Sdi_writer *w) const {
w->StartObject();
Entity_object_impl::serialize(wctx, w);
write(w, m_last_altered, STRING_WITH_LEN("last_altered"));
write(w, m_created, STRING_WITH_LEN("created"));
write(w, !m_organization.has_value(), STRING_WITH_LEN("organization_null"));
write(w, m_organization.has_value() ? m_organization.value() : "",
STRING_WITH_LEN("organization"));
write(w, !m_organization_coordsys_id.has_value(),
STRING_WITH_LEN("organization_coordsys_id_null"));
write(w,
m_organization_coordsys_id.has_value()
? m_organization_coordsys_id.value()
: 0,
STRING_WITH_LEN("organization_coordsys_id"));
write(w, m_definition, STRING_WITH_LEN("definition"));
write(w, !m_description.has_value(), STRING_WITH_LEN("description_null"));
write(w, m_description.has_value() ? m_description.value() : "",
STRING_WITH_LEN("description"));
w->EndObject();
}
///////////////////////////////////////////////////////////////////////////
bool Spatial_reference_system_impl::deserialize(Sdi_rcontext *rctx,
const RJ_Value &val) {
Entity_object_impl::deserialize(rctx, val);
read(&m_last_altered, val, "last_altered");
read(&m_created, val, "created");
bool is_null{false};
read(&is_null, val, "organization_null");
if (!is_null) {
String_type s;
read(&s, val, "organization");
m_organization = std::optional<String_type>(s);
}
read(&is_null, val, "organization_coordsys_id_null");
if (!is_null) {
gis::srid_t id = 0;
read(&id, val, "organization_coordsys_id");
m_organization_coordsys_id = std::optional<gis::srid_t>(id);
}
read(&m_definition, val, "definition");
read(&is_null, val, "description_null");
if (!is_null) {
String_type s;
read(&s, val, "description");
m_description = std::optional<String_type>(s);
}
return parse_definition();
}
///////////////////////////////////////////////////////////////////////////
bool Spatial_reference_system_impl::parse_definition() {
gis::srs::Spatial_reference_system *srs = nullptr;
// parse_wkt() will only allocate memory if successful.
if (!gis::srs::parse_wkt(id(), m_definition.data(),
m_definition.data() + m_definition.size(), &srs)) {
m_parsed_definition.reset(srs);
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////
bool Spatial_reference_system::update_id_key(Id_key *key, Object_id id) {
key->update(id);
return false;
}
///////////////////////////////////////////////////////////////////////////
bool Spatial_reference_system::update_name_key(Name_key *key,
const String_type &name) {
return Spatial_reference_systems::update_object_key(
key, Dictionary_impl::instance()->default_catalog_id(), name);
}
///////////////////////////////////////////////////////////////////////////
const Object_table &Spatial_reference_system_impl::object_table() const {
return DD_table::instance();
}
///////////////////////////////////////////////////////////////////////////
void Spatial_reference_system_impl::register_tables(
Open_dictionary_tables_ctx *otx) {
otx->add_table<Spatial_reference_systems>();
}
///////////////////////////////////////////////////////////////////////////
} // namespace dd