MySQL 9.1.0
Source Code Documentation
spatial_reference_system_impl.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef DD__SPATIAL_REFERENCE_SYSTEM_IMPL_INCLUDED
25#define DD__SPATIAL_REFERENCE_SYSTEM_IMPL_INCLUDED
26
27#include <assert.h>
28#include <stdio.h>
29
30#include <cstddef> // std::nullptr_t
31#include <memory> // std::unique_ptr
32#include <new>
33#include <optional>
34
35#include "my_inttypes.h"
36#include "sql/dd/impl/types/entity_object_impl.h" // dd::Entity_object_impl
38#include "sql/dd/object_id.h"
39#include "sql/dd/sdi_fwd.h"
40#include "sql/dd/string_type.h"
41#include "sql/dd/types/spatial_reference_system.h" // dd:Spatial_reference_system
43#include "sql/gis/geometries.h" // gis::Coordinate_system
44#include "sql/gis/srid.h" // srid_t
45#include "sql/gis/srs/srs.h" // gis::srs::Spatial_reference_...
46#include "sql/sql_time.h" // gmt_time_to_local_time
47
48class THD;
49
50namespace dd {
51
52///////////////////////////////////////////////////////////////////////////
53
54class Open_dictionary_tables_ctx;
55class Raw_record;
56class Sdi_rcontext;
57class Sdi_wcontext;
58class Object_table;
59
60///////////////////////////////////////////////////////////////////////////
61
64 public:
66 : m_created(0),
72 m_description() {}
73
74 private:
76 : Weak_object(srs),
85
86 public:
87 const Object_table &object_table() const override;
88
89 bool validate() const override;
90
91 bool store_attributes(Raw_record *r) override;
92
93 bool restore_attributes(const Raw_record &r) override;
94
95 void serialize(Sdi_wcontext *wctx, Sdi_writer *w) const;
96
97 bool deserialize(Sdi_rcontext *rctx, const RJ_Value &val);
98
99 /// Parse the SRS definition string.
100 ///
101 /// Used internally. Made public to make it easier to write unit tests.
102 bool parse_definition();
103
104 public:
106
107 /////////////////////////////////////////////////////////////////////////
108 // created
109 /////////////////////////////////////////////////////////////////////////
110
111 ulonglong created(bool convert_time) const override {
112 return convert_time ? gmt_time_to_local_time(m_created) : m_created;
113 }
114
116
117 /////////////////////////////////////////////////////////////////////////
118 // last_altered
119 /////////////////////////////////////////////////////////////////////////
120
121 ulonglong last_altered(bool convert_time) const override {
122 return convert_time ? gmt_time_to_local_time(m_last_altered)
124 }
125
128 }
129
130 /////////////////////////////////////////////////////////////////////////
131 // organization
132 /////////////////////////////////////////////////////////////////////////
133
134 const std::optional<String_type> &organization() const override {
135 return m_organization;
136 }
137
139 m_organization = std::optional<String_type>(organization);
140 }
141
142 void set_organization(std::nullptr_t) override {
143 m_organization = std::optional<String_type>();
144 }
145
146 /////////////////////////////////////////////////////////////////////////
147 // organization_coordsys_id
148 /////////////////////////////////////////////////////////////////////////
149
150 const std::optional<gis::srid_t> &organization_coordsys_id() const override {
152 }
153
157 std::optional<gis::srid_t>(organization_coordsys_id);
158 }
159
160 void set_organization_coordsys_id(std::nullptr_t) override {
161 m_organization_coordsys_id = std::optional<gis::srid_t>();
162 }
163
164 /////////////////////////////////////////////////////////////////////////
165 // definition
166 /////////////////////////////////////////////////////////////////////////
167
168 const String_type &definition() const override { return m_definition; }
169
170 void set_definition(const String_type &definition) override {
172 }
173
175 // Work around bugs in Developer Studio 12.5 on Solaris by casting the enum
176 // to int. Otherwise the default case, and only the default case, is always
177 // executed. This happens regardless of SRS type value.
178 switch (static_cast<int>(m_parsed_definition->srs_type())) {
179 case static_cast<int>(gis::srs::Srs_type::PROJECTED):
181 case static_cast<int>(gis::srs::Srs_type::GEOGRAPHIC):
183 default:
184 /* purecov: begin deadcode */
185 assert(false);
187 /* purecov: end */
188 }
189 }
190
191 bool is_projected() const override {
193 }
194
195 bool is_geographic() const override {
197 }
198
199 bool is_cartesian() const override {
201 }
202
203 bool is_lat_long() const override;
204
205 double semi_major_axis() const override {
206 if (is_geographic()) {
207 return static_cast<gis::srs::Geographic_srs *>(m_parsed_definition.get())
208 ->semi_major_axis();
209 } else {
210 return 0.0;
211 }
212 }
213
214 double semi_minor_axis() const override {
215 if (is_geographic()) {
217 static_cast<gis::srs::Geographic_srs *>(m_parsed_definition.get());
218 if (srs->inverse_flattening() == 0.0)
219 return srs->semi_major_axis();
220 else
221 return srs->semi_major_axis() * (1 - 1 / srs->inverse_flattening());
222 } else {
223 return 0.0;
224 }
225 }
226
227 double linear_unit() const override {
228 return m_parsed_definition->linear_unit();
229 }
230
231 double angular_unit() const override {
232 return m_parsed_definition->angular_unit();
233 }
234
235 double prime_meridian() const override {
236 return m_parsed_definition->prime_meridian();
237 }
238
239 bool positive_east() const override {
240 if (is_lat_long()) {
241 return (m_parsed_definition->axis_direction(1) ==
243 } else {
244 return (m_parsed_definition->axis_direction(0) ==
246 }
247 }
248
249 bool positive_north() const override {
250 if (is_lat_long()) {
251 return (m_parsed_definition->axis_direction(0) ==
253 } else {
254 return (m_parsed_definition->axis_direction(1) ==
256 }
257 }
258
259 bool missing_towgs84() const override {
260 return (!m_parsed_definition->is_wgs84_based() &&
261 !m_parsed_definition->has_towgs84());
262 }
263
264 double to_radians(double d) const override {
265 assert(is_geographic());
266 assert(angular_unit() > 0.0);
267 return d * angular_unit();
268 }
269
270 double from_radians(double d) const override {
271 assert(is_geographic());
272 assert(angular_unit() > 0.0);
273 return d / angular_unit();
274 }
275
276 double to_normalized_latitude(double d) const override {
277 double latitude = to_radians(d);
278 if (!positive_north()) latitude *= -1.0;
279 return latitude;
280 }
281
282 double from_normalized_latitude(double d) const override {
283 double latitude = from_radians(d);
284 if (!positive_north()) latitude *= -1.0;
285 return latitude;
286 }
287
288 double to_normalized_longitude(double d) const override {
289 double longitude = d;
290 if (!positive_east()) longitude *= -1.0;
291 longitude += prime_meridian();
292 longitude *= angular_unit();
293 return longitude;
294 }
295
296 double from_normalized_longitude(double d) const override {
297 double longitude = d;
298 longitude /= angular_unit();
299 longitude -= prime_meridian();
300 if (!positive_east()) longitude *= -1.0;
301 return longitude;
302 }
303
304 bool can_be_modified_to(const Spatial_reference_system &srs) const override {
305 return m_parsed_definition->can_be_modified_to(
306 *static_cast<const Spatial_reference_system_impl &>(srs)
308 }
309
310 String_type proj4_parameters() const override {
311 return m_parsed_definition->proj4_parameters().c_str();
312 }
313
314 /////////////////////////////////////////////////////////////////////////
315 // description
316 /////////////////////////////////////////////////////////////////////////
317
318 const std::optional<String_type> &description() const override {
319 return m_description;
320 }
321
323 m_description = std::optional<String_type>(description);
324 }
325
326 void set_description(std::nullptr_t) override {
327 m_description = std::optional<String_type>();
328 }
329
330 // Fix "inherits ... via dominance" warnings
332 const Entity_object_impl *impl() const override {
334 }
335 Object_id id() const override { return Entity_object_impl::id(); }
336 bool is_persistent() const override {
338 }
339 const String_type &name() const override {
341 }
342 void set_name(const String_type &name) override {
344 }
345
346 public:
347 void debug_print(String_type &outb) const override {
348 char outbuf[1024];
349 sprintf(outbuf,
350 "SPATIAL REFERENCE SYSTEM OBJECT: id= {OID: %lld}, "
351 "name= %s, m_created= %llu, m_last_altered= %llu",
352 id(), name().c_str(), m_created, m_last_altered);
353 outb = String_type(outbuf);
354 }
355
356 private:
357 // Fields
360 std::optional<String_type> m_organization;
361 std::optional<gis::srid_t> m_organization_coordsys_id;
363 std::unique_ptr<gis::srs::Spatial_reference_system> m_parsed_definition;
364 std::optional<String_type> m_description;
365
366 Spatial_reference_system *clone() const override {
367 return new Spatial_reference_system_impl(*this);
368 }
369
371 /*
372 Even though we don't drop SRSes en masse we still create slimmed
373 down version for consistency sake.
374 */
375 Spatial_reference_system_impl *placeholder =
377 placeholder->set_id(id());
378 placeholder->set_name(name());
379 return placeholder;
380 }
381};
382
383///////////////////////////////////////////////////////////////////////////
384
385} // namespace dd
386
387#endif // DD__SPATIAL_REFERENCE_SYSTEM_IMPL_INCLUDED
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: entity_object_impl.h:44
void set_name(const String_type &name) override
Definition: entity_object_impl.h:62
void set_id(Object_id id)
Definition: entity_object_impl.h:51
Object_id id() const override
The unique dictionary object id.
Definition: entity_object_impl.h:49
const String_type & name() const override
Definition: entity_object_impl.h:60
Entity_object_impl * impl() override
Definition: entity_object_impl.h:68
bool is_persistent() const override
Is dictionary object persistent in dictionary tables ?
Definition: entity_object_impl.h:57
This class represents all data dictionary table like mysql.tables, mysql.columns and more.
Definition: object_table.h:72
Auxiliary class for opening dictionary tables.
Definition: transaction_impl.h:76
Definition: raw_record.h:46
Opaque context which keeps reusable resoureces needed during deserialization.
Definition: sdi.cc:231
Opaque context which keeps reusable resources needed during serialization.
Definition: sdi.cc:129
Definition: spatial_reference_system_impl.h:63
bool missing_towgs84() const override
Checks whether the SRS definition is missing a TOWGS84 clause.
Definition: spatial_reference_system_impl.h:259
std::optional< String_type > m_organization
Definition: spatial_reference_system_impl.h:360
Spatial_reference_system * clone_dropped_object_placeholder() const override
Allocate a new object which can serve as a placeholder for the original object in the Dictionary_clie...
Definition: spatial_reference_system_impl.h:370
std::unique_ptr< gis::srs::Spatial_reference_system > m_parsed_definition
Definition: spatial_reference_system_impl.h:363
bool deserialize(Sdi_rcontext *rctx, const RJ_Value &val)
Definition: spatial_reference_system_impl.cc:146
ulonglong m_created
Definition: spatial_reference_system_impl.h:358
void set_organization_coordsys_id(std::nullptr_t) override
Definition: spatial_reference_system_impl.h:160
double semi_minor_axis() const override
Definition: spatial_reference_system_impl.h:214
double linear_unit() const override
Definition: spatial_reference_system_impl.h:227
std::optional< gis::srid_t > m_organization_coordsys_id
Definition: spatial_reference_system_impl.h:361
Spatial_reference_system_impl(const Spatial_reference_system_impl &srs)
Definition: spatial_reference_system_impl.h:75
double angular_unit() const override
Definition: spatial_reference_system_impl.h:231
double to_radians(double d) const override
Converts a coordinate value from the SRS unit to radians.
Definition: spatial_reference_system_impl.h:264
const Object_table & object_table() const override
Definition: spatial_reference_system_impl.cc:206
const std::optional< gis::srid_t > & organization_coordsys_id() const override
Definition: spatial_reference_system_impl.h:150
void set_definition(const String_type &definition) override
Definition: spatial_reference_system_impl.h:170
bool restore_attributes(const Raw_record &r) override
Definition: spatial_reference_system_impl.cc:72
void set_organization(const String_type &organization) override
Definition: spatial_reference_system_impl.h:138
ulonglong created(bool convert_time) const override
Definition: spatial_reference_system_impl.h:111
void serialize(Sdi_wcontext *wctx, Sdi_writer *w) const
Definition: spatial_reference_system_impl.cc:121
Spatial_reference_system_impl()
Definition: spatial_reference_system_impl.h:65
void set_description(const String_type &description) override
Definition: spatial_reference_system_impl.h:322
bool validate() const override
Definition: spatial_reference_system_impl.cc:57
double prime_meridian() const override
Definition: spatial_reference_system_impl.h:235
bool is_lat_long() const override
Check whether an SRS has latitude-longitude axis ordering.
Definition: spatial_reference_system_impl.cc:63
void debug_print(String_type &outb) const override
Definition: spatial_reference_system_impl.h:347
ulonglong last_altered(bool convert_time) const override
Definition: spatial_reference_system_impl.h:121
const std::optional< String_type > & organization() const override
Definition: spatial_reference_system_impl.h:134
static void register_tables(Open_dictionary_tables_ctx *otx)
Definition: spatial_reference_system_impl.cc:212
gis::Coordinate_system cs_type() const override
Definition: spatial_reference_system_impl.h:174
bool is_projected() const override
Definition: spatial_reference_system_impl.h:191
void set_organization_coordsys_id(gis::srid_t organization_coordsys_id) override
Definition: spatial_reference_system_impl.h:154
double from_normalized_longitude(double d) const override
Converts a longitude value from the in-memory representation of longitude (radians,...
Definition: spatial_reference_system_impl.h:296
double to_normalized_latitude(double d) const override
Converts a latitude value from the SRS unit and direction to the in-memory representation of latitude...
Definition: spatial_reference_system_impl.h:276
String_type m_definition
Definition: spatial_reference_system_impl.h:362
double from_radians(double d) const override
Converts a coordinate value from radians to the SRS unit.
Definition: spatial_reference_system_impl.h:270
bool is_persistent() const override
Is dictionary object persistent in dictionary tables ?
Definition: spatial_reference_system_impl.h:336
bool positive_east() const override
Definition: spatial_reference_system_impl.h:239
Entity_object_impl * impl() override
Definition: spatial_reference_system_impl.h:331
std::optional< String_type > m_description
Definition: spatial_reference_system_impl.h:364
void set_description(std::nullptr_t) override
Definition: spatial_reference_system_impl.h:326
String_type proj4_parameters() const override
Gets the proj4 parameters for this SRS.
Definition: spatial_reference_system_impl.h:310
Object_id id() const override
The unique dictionary object id.
Definition: spatial_reference_system_impl.h:335
Spatial_reference_system * clone() const override
Allocate a new object and invoke the copy constructor.
Definition: spatial_reference_system_impl.h:366
bool is_cartesian() const override
Definition: spatial_reference_system_impl.h:199
double semi_major_axis() const override
Definition: spatial_reference_system_impl.h:205
void set_created(ulonglong created) override
Definition: spatial_reference_system_impl.h:115
const String_type & name() const override
Definition: spatial_reference_system_impl.h:339
bool parse_definition()
Parse the SRS definition string.
Definition: spatial_reference_system_impl.cc:177
void set_name(const String_type &name) override
Definition: spatial_reference_system_impl.h:342
double from_normalized_latitude(double d) const override
Converts a latitude value from the in-memory representation of latitude (radians, positive North) to ...
Definition: spatial_reference_system_impl.h:282
bool can_be_modified_to(const Spatial_reference_system &srs) const override
Checks if this SRS can be changed to another SRS definition without changing any computations.
Definition: spatial_reference_system_impl.h:304
const std::optional< String_type > & description() const override
Definition: spatial_reference_system_impl.h:318
ulonglong m_last_altered
Definition: spatial_reference_system_impl.h:359
bool store_attributes(Raw_record *r) override
Definition: spatial_reference_system_impl.cc:92
double to_normalized_longitude(double d) const override
Converts a longitude value from the SRS unit, direction and meridian to the in-memory representation ...
Definition: spatial_reference_system_impl.h:288
const Entity_object_impl * impl() const override
Definition: spatial_reference_system_impl.h:332
bool positive_north() const override
Definition: spatial_reference_system_impl.h:249
void set_last_altered(ulonglong last_altered) override
Definition: spatial_reference_system_impl.h:126
const String_type & definition() const override
Definition: spatial_reference_system_impl.h:168
void set_organization(std::nullptr_t) override
Definition: spatial_reference_system_impl.h:142
bool is_geographic() const override
Definition: spatial_reference_system_impl.h:195
Definition: spatial_reference_system.h:53
Base class for all data dictionary objects.
Definition: weak_object.h:42
A geographic (longitude-latitude) spatial reference system.
Definition: srs.h:210
double semi_major_axis() const
Definition: srs.h:271
double inverse_flattening() const
Definition: srs.h:273
This file declares the geometry class hierarchy used by the server as the internal representation of ...
ulonglong gmt_time_to_local_time(ulonglong gmt_time)
This function gets GMT time and adds value of time_zone to get the local time.
Definition: sql_time.cc:868
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:43
RJ_Writer Sdi_writer
Alias for the rapidjson Writer type to use in serialization.
Definition: sdi_fwd.h:64
unsigned long long Object_id
Definition: object_id.h:31
rapidjson::GenericValue< RJ_Encoding, RJ_Allocator > RJ_Value
Definition: sdi_fwd.h:49
Char_string_template< String_type_allocator > String_type
Definition: string_type.h:51
Coordinate_system
Types of coordinate systems.
Definition: geometries.h:69
@ kCartesian
A Cartesian plane with the same unit in both directions.
@ kGeographic
An ellipsoidal system with longitude and latitude coordinates, both in the same unit.
std::uint32_t srid_t
A spatial reference system ID (SRID).
Definition: srid.h:33
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86
This header provides Rapidjson Type Aliases.
Interface for server time utilities.